annotate widgets/PluginPathConfigurator.cpp @ 1292:41824255ddf2 plugin-path-config

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