annotate widgets/PluginPathConfigurator.cpp @ 1291:b5c71304286e plugin-path-config

Add "Add"
author Chris Cannam
date Wed, 23 May 2018 16:24:35 +0100
parents 050eca637c19
children 41824255ddf2
rev   line source
Chris@1285 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@1285 2
Chris@1285 3 /*
Chris@1285 4 Sonic Visualiser
Chris@1285 5 An audio file viewer and annotation editor.
Chris@1285 6 Centre for Digital Music, Queen Mary, University of London.
Chris@1285 7
Chris@1285 8 This program is free software; you can redistribute it and/or
Chris@1285 9 modify it under the terms of the GNU General Public License as
Chris@1285 10 published by the Free Software Foundation; either version 2 of the
Chris@1285 11 License, or (at your option) any later version. See the file
Chris@1285 12 COPYING included with this distribution for more information.
Chris@1285 13 */
Chris@1285 14
Chris@1285 15 #include "PluginPathConfigurator.h"
Chris@1285 16
Chris@1285 17 #include <QPushButton>
Chris@1287 18 #include <QListWidget>
Chris@1288 19 #include <QGridLayout>
Chris@1289 20 #include <QComboBox>
Chris@1285 21 #include <QLabel>
Chris@1290 22 #include <QCheckBox>
Chris@1291 23 #include <QFileDialog>
Chris@1285 24
Chris@1285 25 #include "IconLoader.h"
Chris@1285 26 #include "WidgetScale.h"
Chris@1285 27
Chris@1285 28 PluginPathConfigurator::PluginPathConfigurator(QWidget *parent) :
Chris@1287 29 QFrame(parent)
Chris@1285 30 {
Chris@1285 31 m_layout = new QGridLayout;
Chris@1285 32 setLayout(m_layout);
Chris@1291 33
Chris@1291 34 QHBoxLayout *buttons = new QHBoxLayout;
Chris@1291 35
Chris@1291 36 m_down = new QPushButton;
Chris@1291 37 m_down->setIcon(IconLoader().load("down"));
Chris@1291 38 m_down->setToolTip(tr("Move the selected location later in the list"));
Chris@1291 39 connect(m_down, SIGNAL(clicked()), this, SLOT(downClicked()));
Chris@1291 40 buttons->addWidget(m_down);
Chris@1291 41
Chris@1291 42 m_up = new QPushButton;
Chris@1291 43 m_up->setIcon(IconLoader().load("up"));
Chris@1291 44 m_up->setToolTip(tr("Move the selected location earlier in the list"));
Chris@1291 45 connect(m_up, SIGNAL(clicked()), this, SLOT(upClicked()));
Chris@1291 46 buttons->addWidget(m_up);
Chris@1291 47
Chris@1291 48 m_add = new QPushButton;
Chris@1291 49 m_add->setIcon(IconLoader().load("plus"));
Chris@1291 50 m_add->setToolTip(tr("Move the selected location earlier in the list"));
Chris@1291 51 connect(m_add, SIGNAL(clicked()), this, SLOT(addClicked()));
Chris@1291 52 buttons->addWidget(m_add);
Chris@1291 53
Chris@1291 54 m_delete = new QPushButton;
Chris@1291 55 m_delete->setIcon(IconLoader().load("datadelete"));
Chris@1291 56 m_delete->setToolTip(tr("Remove the selected location from the list"));
Chris@1291 57 connect(m_delete, SIGNAL(clicked()), this, SLOT(deleteClicked()));
Chris@1291 58 buttons->addWidget(m_delete);
Chris@1291 59
Chris@1291 60 m_reset = new QPushButton;
Chris@1291 61 m_reset->setText(tr("Reset"));
Chris@1291 62 m_reset->setToolTip(tr("Reset the list for this plugin type to its default"));
Chris@1291 63 connect(m_reset, SIGNAL(clicked()), this, SLOT(resetClicked()));
Chris@1291 64 buttons->addWidget(m_reset);
Chris@1287 65
Chris@1287 66 int row = 0;
Chris@1287 67
Chris@1289 68 m_header = new QLabel;
Chris@1290 69 m_header->setText(tr("Plugin locations for plugin type:"));
Chris@1289 70 m_layout->addWidget(m_header, row, 0);
Chris@1289 71
Chris@1289 72 m_pluginTypeSelector = new QComboBox;
Chris@1290 73 m_layout->addWidget(m_pluginTypeSelector, row, 1, Qt::AlignLeft);
Chris@1289 74 connect(m_pluginTypeSelector, SIGNAL(currentTextChanged(QString)),
Chris@1289 75 this, SLOT(currentTypeChanged(QString)));
Chris@1289 76
Chris@1290 77 m_layout->setColumnStretch(1, 10);
Chris@1287 78 ++row;
Chris@1287 79
Chris@1287 80 m_list = new QListWidget;
Chris@1290 81 m_layout->addWidget(m_list, row, 0, 1, 3);
Chris@1290 82 m_layout->setRowStretch(row, 20);
Chris@1288 83 connect(m_list, SIGNAL(currentRowChanged(int)),
Chris@1288 84 this, SLOT(currentLocationChanged(int)));
Chris@1287 85 ++row;
Chris@1290 86
Chris@1290 87 m_layout->addLayout(buttons, row, 2);
Chris@1287 88 ++row;
Chris@1291 89
Chris@1290 90 m_envOverride = new QCheckBox;
Chris@1290 91 connect(m_envOverride, SIGNAL(stateChanged(int)),
Chris@1290 92 this, SLOT(envOverrideChanged(int)));
Chris@1290 93 m_layout->addWidget(m_envOverride, row, 0, 1, 3);
Chris@1291 94 ++row;
Chris@1285 95 }
Chris@1285 96
Chris@1285 97 PluginPathConfigurator::~PluginPathConfigurator()
Chris@1285 98 {
Chris@1285 99 }
Chris@1285 100
Chris@1285 101 void
Chris@1289 102 PluginPathConfigurator::setPaths(Paths paths)
Chris@1285 103 {
Chris@1289 104 m_paths = paths;
Chris@1290 105 if (m_originalPaths.empty()) {
Chris@1290 106 m_originalPaths = paths;
Chris@1290 107 }
Chris@1289 108
Chris@1289 109 m_pluginTypeSelector->clear();
Chris@1289 110 for (const auto &p: paths) {
Chris@1289 111 m_pluginTypeSelector->addItem(p.first);
Chris@1289 112 }
Chris@1289 113
Chris@1285 114 populate();
Chris@1285 115 }
Chris@1285 116
Chris@1285 117 void
Chris@1289 118 PluginPathConfigurator::populate()
Chris@1285 119 {
Chris@1287 120 m_list->clear();
Chris@1286 121
Chris@1289 122 if (m_paths.empty()) return;
Chris@1289 123
Chris@1290 124 populateFor(m_paths.begin()->first, -1);
Chris@1289 125 }
Chris@1289 126
Chris@1289 127 void
Chris@1289 128 PluginPathConfigurator::populateFor(QString type, int makeCurrent)
Chris@1289 129 {
Chris@1290 130 QString envVariable = m_paths.at(type).envVariable;
Chris@1290 131 bool useEnvVariable = m_paths.at(type).useEnvVariable;
Chris@1290 132 m_envOverride->setText
Chris@1291 133 (tr("Allow the %1 environment variable to take priority over this")
Chris@1290 134 .arg(envVariable));
Chris@1290 135 m_envOverride->setCheckState(useEnvVariable ? Qt::Checked : Qt::Unchecked);
Chris@1290 136
Chris@1289 137 m_list->clear();
Chris@1289 138
Chris@1289 139 for (int i = 0; i < m_pluginTypeSelector->count(); ++i) {
Chris@1289 140 if (type == m_pluginTypeSelector->itemText(i)) {
Chris@1289 141 m_pluginTypeSelector->setCurrentIndex(i);
Chris@1289 142 }
Chris@1289 143 }
Chris@1289 144
Chris@1289 145 QStringList path = m_paths.at(type).directories;
Chris@1289 146
Chris@1289 147 for (int i = 0; i < path.size(); ++i) {
Chris@1289 148 m_list->addItem(path[i]);
Chris@1287 149 }
Chris@1285 150
Chris@1290 151 if (makeCurrent < path.size()) {
Chris@1287 152 m_list->setCurrentRow(makeCurrent);
Chris@1290 153 currentLocationChanged(makeCurrent);
Chris@1286 154 }
Chris@1285 155 }
Chris@1285 156
Chris@1285 157 void
Chris@1288 158 PluginPathConfigurator::currentLocationChanged(int i)
Chris@1288 159 {
Chris@1289 160 QString type = m_pluginTypeSelector->currentText();
Chris@1289 161 QStringList path = m_paths.at(type).directories;
Chris@1288 162 m_up->setEnabled(i > 0);
Chris@1290 163 m_down->setEnabled(i >= 0 && i + 1 < path.size());
Chris@1290 164 m_delete->setEnabled(i >= 0 && i < path.size());
Chris@1290 165 m_reset->setEnabled(path != m_originalPaths.at(type).directories);
Chris@1289 166 }
Chris@1289 167
Chris@1289 168 void
Chris@1289 169 PluginPathConfigurator::currentTypeChanged(QString type)
Chris@1289 170 {
Chris@1290 171 populateFor(type, -1);
Chris@1290 172 }
Chris@1290 173
Chris@1290 174 void
Chris@1290 175 PluginPathConfigurator::envOverrideChanged(int )
Chris@1290 176 {
Chris@1290 177 //!!!
Chris@1288 178 }
Chris@1288 179
Chris@1288 180 void
Chris@1285 181 PluginPathConfigurator::upClicked()
Chris@1285 182 {
Chris@1289 183 QString type = m_pluginTypeSelector->currentText();
Chris@1289 184 QStringList path = m_paths.at(type).directories;
Chris@1289 185
Chris@1287 186 int current = m_list->currentRow();
Chris@1287 187 if (current <= 0) return;
Chris@1287 188
Chris@1286 189 QStringList newPath;
Chris@1289 190 for (int i = 0; i < path.size(); ++i) {
Chris@1287 191 if (i + 1 == current) {
Chris@1289 192 newPath.push_back(path[i+1]);
Chris@1289 193 newPath.push_back(path[i]);
Chris@1286 194 ++i;
Chris@1286 195 } else {
Chris@1289 196 newPath.push_back(path[i]);
Chris@1286 197 }
Chris@1286 198 }
Chris@1290 199
Chris@1290 200 auto newEntry = m_paths.at(type);
Chris@1290 201 newEntry.directories = newPath;
Chris@1290 202 m_paths[type] = newEntry;
Chris@1287 203
Chris@1289 204 populateFor(type, current - 1);
Chris@1285 205 }
Chris@1285 206
Chris@1285 207 void
Chris@1285 208 PluginPathConfigurator::downClicked()
Chris@1285 209 {
Chris@1289 210 QString type = m_pluginTypeSelector->currentText();
Chris@1289 211 QStringList path = m_paths.at(type).directories;
Chris@1289 212
Chris@1287 213 int current = m_list->currentRow();
Chris@1289 214 if (current < 0 || current + 1 >= path.size()) return;
Chris@1289 215
Chris@1286 216 QStringList newPath;
Chris@1289 217 for (int i = 0; i < path.size(); ++i) {
Chris@1287 218 if (i == current) {
Chris@1289 219 newPath.push_back(path[i+1]);
Chris@1289 220 newPath.push_back(path[i]);
Chris@1286 221 ++i;
Chris@1286 222 } else {
Chris@1289 223 newPath.push_back(path[i]);
Chris@1286 224 }
Chris@1286 225 }
Chris@1290 226
Chris@1290 227 auto newEntry = m_paths.at(type);
Chris@1290 228 newEntry.directories = newPath;
Chris@1290 229 m_paths[type] = newEntry;
Chris@1287 230
Chris@1289 231 populateFor(type, current + 1);
Chris@1285 232 }
Chris@1285 233
Chris@1285 234 void
Chris@1291 235 PluginPathConfigurator::addClicked()
Chris@1291 236 {
Chris@1291 237 QString type = m_pluginTypeSelector->currentText();
Chris@1291 238
Chris@1291 239 QString newDir = QFileDialog::getExistingDirectory
Chris@1291 240 (this, tr("Choose directory to add"));
Chris@1291 241
Chris@1291 242 if (newDir == QString()) return;
Chris@1291 243
Chris@1291 244 auto newEntry = m_paths.at(type);
Chris@1291 245 newEntry.directories.push_back(newDir);
Chris@1291 246 m_paths[type] = newEntry;
Chris@1291 247
Chris@1291 248 populateFor(type, newEntry.directories.size() - 1);
Chris@1291 249 }
Chris@1291 250
Chris@1291 251 void
Chris@1285 252 PluginPathConfigurator::deleteClicked()
Chris@1285 253 {
Chris@1289 254 QString type = m_pluginTypeSelector->currentText();
Chris@1289 255 QStringList path = m_paths.at(type).directories;
Chris@1289 256
Chris@1287 257 int current = m_list->currentRow();
Chris@1290 258 if (current < 0) return;
Chris@1289 259
Chris@1286 260 QStringList newPath;
Chris@1289 261 for (int i = 0; i < path.size(); ++i) {
Chris@1287 262 if (i != current) {
Chris@1289 263 newPath.push_back(path[i]);
Chris@1286 264 }
Chris@1286 265 }
Chris@1290 266
Chris@1290 267 auto newEntry = m_paths.at(type);
Chris@1290 268 newEntry.directories = newPath;
Chris@1290 269 m_paths[type] = newEntry;
Chris@1287 270
Chris@1289 271 populateFor(type, current < newPath.size() ? current : current-1);
Chris@1285 272 }
Chris@1290 273
Chris@1290 274 void
Chris@1290 275 PluginPathConfigurator::resetClicked()
Chris@1290 276 {
Chris@1290 277 QString type = m_pluginTypeSelector->currentText();
Chris@1290 278 m_paths[type] = m_originalPaths[type];
Chris@1290 279 populateFor(type, -1);
Chris@1290 280 }