annotate widgets/PluginPathConfigurator.cpp @ 1294:9f9a7edd7d89 plugin-path-config

Show value of environment variable; populate for last plugin type by default (as that happens to be Vamp)
author Chris Cannam
date Mon, 04 Jun 2018 15:44:57 +0100
parents 6dd15b5c14f9
children 11888b7e193d
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@1292 16 #include "PluginReviewDialog.h"
Chris@1285 17
Chris@1285 18 #include <QPushButton>
Chris@1287 19 #include <QListWidget>
Chris@1288 20 #include <QGridLayout>
Chris@1289 21 #include <QComboBox>
Chris@1285 22 #include <QLabel>
Chris@1290 23 #include <QCheckBox>
Chris@1291 24 #include <QFileDialog>
Chris@1285 25
Chris@1285 26 #include "IconLoader.h"
Chris@1285 27 #include "WidgetScale.h"
Chris@1285 28
Chris@1293 29 #include "plugin/PluginPathSetter.h"
Chris@1293 30
Chris@1285 31 PluginPathConfigurator::PluginPathConfigurator(QWidget *parent) :
Chris@1287 32 QFrame(parent)
Chris@1285 33 {
Chris@1285 34 m_layout = new QGridLayout;
Chris@1285 35 setLayout(m_layout);
Chris@1292 36
Chris@1291 37 QHBoxLayout *buttons = new QHBoxLayout;
Chris@1291 38
Chris@1291 39 m_down = new QPushButton;
Chris@1291 40 m_down->setIcon(IconLoader().load("down"));
Chris@1291 41 m_down->setToolTip(tr("Move the selected location later in the list"));
Chris@1291 42 connect(m_down, SIGNAL(clicked()), this, SLOT(downClicked()));
Chris@1291 43 buttons->addWidget(m_down);
Chris@1291 44
Chris@1291 45 m_up = new QPushButton;
Chris@1291 46 m_up->setIcon(IconLoader().load("up"));
Chris@1291 47 m_up->setToolTip(tr("Move the selected location earlier in the list"));
Chris@1291 48 connect(m_up, SIGNAL(clicked()), this, SLOT(upClicked()));
Chris@1291 49 buttons->addWidget(m_up);
Chris@1291 50
Chris@1291 51 m_add = new QPushButton;
Chris@1291 52 m_add->setIcon(IconLoader().load("plus"));
Chris@1291 53 m_add->setToolTip(tr("Move the selected location earlier in the list"));
Chris@1291 54 connect(m_add, SIGNAL(clicked()), this, SLOT(addClicked()));
Chris@1291 55 buttons->addWidget(m_add);
Chris@1291 56
Chris@1291 57 m_delete = new QPushButton;
Chris@1291 58 m_delete->setIcon(IconLoader().load("datadelete"));
Chris@1291 59 m_delete->setToolTip(tr("Remove the selected location from the list"));
Chris@1291 60 connect(m_delete, SIGNAL(clicked()), this, SLOT(deleteClicked()));
Chris@1291 61 buttons->addWidget(m_delete);
Chris@1291 62
Chris@1291 63 m_reset = new QPushButton;
Chris@1293 64 m_reset->setText(tr("Reset to Default"));
Chris@1291 65 m_reset->setToolTip(tr("Reset the list for this plugin type to its default"));
Chris@1291 66 connect(m_reset, SIGNAL(clicked()), this, SLOT(resetClicked()));
Chris@1291 67 buttons->addWidget(m_reset);
Chris@1287 68
Chris@1287 69 int row = 0;
Chris@1287 70
Chris@1289 71 m_header = new QLabel;
Chris@1290 72 m_header->setText(tr("Plugin locations for plugin type:"));
Chris@1289 73 m_layout->addWidget(m_header, row, 0);
Chris@1289 74
Chris@1289 75 m_pluginTypeSelector = new QComboBox;
Chris@1290 76 m_layout->addWidget(m_pluginTypeSelector, row, 1, Qt::AlignLeft);
Chris@1289 77 connect(m_pluginTypeSelector, SIGNAL(currentTextChanged(QString)),
Chris@1289 78 this, SLOT(currentTypeChanged(QString)));
Chris@1289 79
Chris@1290 80 m_layout->setColumnStretch(1, 10);
Chris@1287 81 ++row;
Chris@1287 82
Chris@1287 83 m_list = new QListWidget;
Chris@1290 84 m_layout->addWidget(m_list, row, 0, 1, 3);
Chris@1290 85 m_layout->setRowStretch(row, 20);
Chris@1288 86 connect(m_list, SIGNAL(currentRowChanged(int)),
Chris@1288 87 this, SLOT(currentLocationChanged(int)));
Chris@1287 88 ++row;
Chris@1290 89
Chris@1293 90 m_layout->addLayout(buttons, row, 0, 1, 2, Qt::AlignLeft);
Chris@1292 91
Chris@1292 92 m_seePlugins = new QPushButton;
Chris@1292 93 m_seePlugins->setText(tr("Review plugins..."));
Chris@1292 94 connect(m_seePlugins, SIGNAL(clicked()), this, SLOT(seePluginsClicked()));
Chris@1292 95 m_layout->addWidget(m_seePlugins, row, 2);
Chris@1292 96
Chris@1287 97 ++row;
Chris@1291 98
Chris@1290 99 m_envOverride = new QCheckBox;
Chris@1290 100 connect(m_envOverride, SIGNAL(stateChanged(int)),
Chris@1290 101 this, SLOT(envOverrideChanged(int)));
Chris@1290 102 m_layout->addWidget(m_envOverride, row, 0, 1, 3);
Chris@1291 103 ++row;
Chris@1285 104 }
Chris@1285 105
Chris@1285 106 PluginPathConfigurator::~PluginPathConfigurator()
Chris@1285 107 {
Chris@1285 108 }
Chris@1285 109
Chris@1285 110 void
Chris@1293 111 PluginPathConfigurator::setPaths(PluginPathSetter::Paths paths)
Chris@1285 112 {
Chris@1289 113 m_paths = paths;
Chris@1293 114
Chris@1293 115 m_defaultPaths = PluginPathSetter::getDefaultPaths();
Chris@1289 116
Chris@1289 117 m_pluginTypeSelector->clear();
Chris@1289 118 for (const auto &p: paths) {
Chris@1289 119 m_pluginTypeSelector->addItem(p.first);
Chris@1289 120 }
Chris@1289 121
Chris@1285 122 populate();
Chris@1285 123 }
Chris@1285 124
Chris@1285 125 void
Chris@1289 126 PluginPathConfigurator::populate()
Chris@1285 127 {
Chris@1287 128 m_list->clear();
Chris@1286 129
Chris@1289 130 if (m_paths.empty()) return;
Chris@1289 131
Chris@1294 132 populateFor(m_paths.rbegin()->first, -1);
Chris@1289 133 }
Chris@1289 134
Chris@1289 135 void
Chris@1289 136 PluginPathConfigurator::populateFor(QString type, int makeCurrent)
Chris@1289 137 {
Chris@1290 138 QString envVariable = m_paths.at(type).envVariable;
Chris@1290 139 bool useEnvVariable = m_paths.at(type).useEnvVariable;
Chris@1294 140 QString envVarValue =
Chris@1294 141 PluginPathSetter::getOriginalEnvironmentValue(envVariable);
Chris@1294 142 QString currentValueRubric;
Chris@1294 143 if (envVarValue == QString()) {
Chris@1294 144 currentValueRubric = tr("(Variable is currently unset)");
Chris@1294 145 } else {
Chris@1294 146 if (envVarValue.length() > 100) {
Chris@1294 147 QString envVarStart = envVarValue.left(95);
Chris@1294 148 currentValueRubric = tr("(Current value begins: \"%1 ...\")")
Chris@1294 149 .arg(envVarStart);
Chris@1294 150 } else {
Chris@1294 151 currentValueRubric = tr("(Currently set to: \"%1\")")
Chris@1294 152 .arg(envVarValue);
Chris@1294 153 }
Chris@1294 154 }
Chris@1290 155 m_envOverride->setText
Chris@1294 156 (tr("Allow the %1 environment variable to take priority over this\n%2")
Chris@1294 157 .arg(envVariable)
Chris@1294 158 .arg(currentValueRubric));
Chris@1290 159 m_envOverride->setCheckState(useEnvVariable ? Qt::Checked : Qt::Unchecked);
Chris@1294 160
Chris@1289 161 m_list->clear();
Chris@1289 162
Chris@1289 163 for (int i = 0; i < m_pluginTypeSelector->count(); ++i) {
Chris@1289 164 if (type == m_pluginTypeSelector->itemText(i)) {
Chris@1294 165 m_pluginTypeSelector->blockSignals(true);
Chris@1289 166 m_pluginTypeSelector->setCurrentIndex(i);
Chris@1294 167 m_pluginTypeSelector->blockSignals(false);
Chris@1289 168 }
Chris@1289 169 }
Chris@1289 170
Chris@1289 171 QStringList path = m_paths.at(type).directories;
Chris@1289 172
Chris@1289 173 for (int i = 0; i < path.size(); ++i) {
Chris@1289 174 m_list->addItem(path[i]);
Chris@1287 175 }
Chris@1285 176
Chris@1290 177 if (makeCurrent < path.size()) {
Chris@1287 178 m_list->setCurrentRow(makeCurrent);
Chris@1290 179 currentLocationChanged(makeCurrent);
Chris@1286 180 }
Chris@1285 181 }
Chris@1285 182
Chris@1285 183 void
Chris@1288 184 PluginPathConfigurator::currentLocationChanged(int i)
Chris@1288 185 {
Chris@1289 186 QString type = m_pluginTypeSelector->currentText();
Chris@1289 187 QStringList path = m_paths.at(type).directories;
Chris@1288 188 m_up->setEnabled(i > 0);
Chris@1290 189 m_down->setEnabled(i >= 0 && i + 1 < path.size());
Chris@1290 190 m_delete->setEnabled(i >= 0 && i < path.size());
Chris@1293 191 m_reset->setEnabled(path != m_defaultPaths.at(type).directories);
Chris@1289 192 }
Chris@1289 193
Chris@1289 194 void
Chris@1289 195 PluginPathConfigurator::currentTypeChanged(QString type)
Chris@1289 196 {
Chris@1290 197 populateFor(type, -1);
Chris@1290 198 }
Chris@1290 199
Chris@1290 200 void
Chris@1292 201 PluginPathConfigurator::envOverrideChanged(int state)
Chris@1290 202 {
Chris@1292 203 bool useEnvVariable = (state == Qt::Checked);
Chris@1292 204
Chris@1292 205 QString type = m_pluginTypeSelector->currentText();
Chris@1292 206
Chris@1292 207 auto newEntry = m_paths.at(type);
Chris@1292 208 newEntry.useEnvVariable = useEnvVariable;
Chris@1292 209 m_paths[type] = newEntry;
Chris@1292 210
Chris@1293 211 emit pathsChanged();
Chris@1288 212 }
Chris@1288 213
Chris@1288 214 void
Chris@1285 215 PluginPathConfigurator::upClicked()
Chris@1285 216 {
Chris@1289 217 QString type = m_pluginTypeSelector->currentText();
Chris@1289 218 QStringList path = m_paths.at(type).directories;
Chris@1289 219
Chris@1287 220 int current = m_list->currentRow();
Chris@1287 221 if (current <= 0) return;
Chris@1287 222
Chris@1286 223 QStringList newPath;
Chris@1289 224 for (int i = 0; i < path.size(); ++i) {
Chris@1287 225 if (i + 1 == current) {
Chris@1289 226 newPath.push_back(path[i+1]);
Chris@1289 227 newPath.push_back(path[i]);
Chris@1286 228 ++i;
Chris@1286 229 } else {
Chris@1289 230 newPath.push_back(path[i]);
Chris@1286 231 }
Chris@1286 232 }
Chris@1290 233
Chris@1290 234 auto newEntry = m_paths.at(type);
Chris@1290 235 newEntry.directories = newPath;
Chris@1290 236 m_paths[type] = newEntry;
Chris@1287 237
Chris@1289 238 populateFor(type, current - 1);
Chris@1292 239
Chris@1293 240 emit pathsChanged();
Chris@1285 241 }
Chris@1285 242
Chris@1285 243 void
Chris@1285 244 PluginPathConfigurator::downClicked()
Chris@1285 245 {
Chris@1289 246 QString type = m_pluginTypeSelector->currentText();
Chris@1289 247 QStringList path = m_paths.at(type).directories;
Chris@1289 248
Chris@1287 249 int current = m_list->currentRow();
Chris@1289 250 if (current < 0 || current + 1 >= path.size()) return;
Chris@1289 251
Chris@1286 252 QStringList newPath;
Chris@1289 253 for (int i = 0; i < path.size(); ++i) {
Chris@1287 254 if (i == current) {
Chris@1289 255 newPath.push_back(path[i+1]);
Chris@1289 256 newPath.push_back(path[i]);
Chris@1286 257 ++i;
Chris@1286 258 } else {
Chris@1289 259 newPath.push_back(path[i]);
Chris@1286 260 }
Chris@1286 261 }
Chris@1290 262
Chris@1290 263 auto newEntry = m_paths.at(type);
Chris@1290 264 newEntry.directories = newPath;
Chris@1290 265 m_paths[type] = newEntry;
Chris@1287 266
Chris@1289 267 populateFor(type, current + 1);
Chris@1292 268
Chris@1293 269 emit pathsChanged();
Chris@1285 270 }
Chris@1285 271
Chris@1285 272 void
Chris@1291 273 PluginPathConfigurator::addClicked()
Chris@1291 274 {
Chris@1291 275 QString type = m_pluginTypeSelector->currentText();
Chris@1291 276
Chris@1291 277 QString newDir = QFileDialog::getExistingDirectory
Chris@1291 278 (this, tr("Choose directory to add"));
Chris@1291 279
Chris@1291 280 if (newDir == QString()) return;
Chris@1291 281
Chris@1291 282 auto newEntry = m_paths.at(type);
Chris@1291 283 newEntry.directories.push_back(newDir);
Chris@1291 284 m_paths[type] = newEntry;
Chris@1291 285
Chris@1291 286 populateFor(type, newEntry.directories.size() - 1);
Chris@1292 287
Chris@1293 288 emit pathsChanged();
Chris@1291 289 }
Chris@1291 290
Chris@1291 291 void
Chris@1285 292 PluginPathConfigurator::deleteClicked()
Chris@1285 293 {
Chris@1289 294 QString type = m_pluginTypeSelector->currentText();
Chris@1289 295 QStringList path = m_paths.at(type).directories;
Chris@1289 296
Chris@1287 297 int current = m_list->currentRow();
Chris@1290 298 if (current < 0) return;
Chris@1289 299
Chris@1286 300 QStringList newPath;
Chris@1289 301 for (int i = 0; i < path.size(); ++i) {
Chris@1287 302 if (i != current) {
Chris@1289 303 newPath.push_back(path[i]);
Chris@1286 304 }
Chris@1286 305 }
Chris@1290 306
Chris@1290 307 auto newEntry = m_paths.at(type);
Chris@1290 308 newEntry.directories = newPath;
Chris@1290 309 m_paths[type] = newEntry;
Chris@1287 310
Chris@1289 311 populateFor(type, current < newPath.size() ? current : current-1);
Chris@1292 312
Chris@1293 313 emit pathsChanged();
Chris@1285 314 }
Chris@1290 315
Chris@1290 316 void
Chris@1290 317 PluginPathConfigurator::resetClicked()
Chris@1290 318 {
Chris@1290 319 QString type = m_pluginTypeSelector->currentText();
Chris@1293 320 m_paths[type] = m_defaultPaths[type];
Chris@1290 321 populateFor(type, -1);
Chris@1292 322
Chris@1293 323 emit pathsChanged();
Chris@1290 324 }
Chris@1292 325
Chris@1292 326 void
Chris@1292 327 PluginPathConfigurator::seePluginsClicked()
Chris@1292 328 {
Chris@1292 329 PluginReviewDialog dialog(this);
Chris@1292 330 dialog.populate();
Chris@1292 331 dialog.exec();
Chris@1292 332 }
Chris@1292 333