Chris@1285: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@1285: Chris@1285: /* Chris@1285: Sonic Visualiser Chris@1285: An audio file viewer and annotation editor. Chris@1285: Centre for Digital Music, Queen Mary, University of London. Chris@1285: Chris@1285: This program is free software; you can redistribute it and/or Chris@1285: modify it under the terms of the GNU General Public License as Chris@1285: published by the Free Software Foundation; either version 2 of the Chris@1285: License, or (at your option) any later version. See the file Chris@1285: COPYING included with this distribution for more information. Chris@1285: */ Chris@1285: Chris@1285: #include "PluginPathConfigurator.h" Chris@1285: Chris@1285: #include Chris@1287: #include Chris@1288: #include Chris@1289: #include Chris@1285: #include Chris@1290: #include Chris@1291: #include Chris@1285: Chris@1285: #include "IconLoader.h" Chris@1285: #include "WidgetScale.h" Chris@1285: Chris@1285: PluginPathConfigurator::PluginPathConfigurator(QWidget *parent) : Chris@1287: QFrame(parent) Chris@1285: { Chris@1285: m_layout = new QGridLayout; Chris@1285: setLayout(m_layout); Chris@1291: Chris@1291: QHBoxLayout *buttons = new QHBoxLayout; Chris@1291: Chris@1291: m_down = new QPushButton; Chris@1291: m_down->setIcon(IconLoader().load("down")); Chris@1291: m_down->setToolTip(tr("Move the selected location later in the list")); Chris@1291: connect(m_down, SIGNAL(clicked()), this, SLOT(downClicked())); Chris@1291: buttons->addWidget(m_down); Chris@1291: Chris@1291: m_up = new QPushButton; Chris@1291: m_up->setIcon(IconLoader().load("up")); Chris@1291: m_up->setToolTip(tr("Move the selected location earlier in the list")); Chris@1291: connect(m_up, SIGNAL(clicked()), this, SLOT(upClicked())); Chris@1291: buttons->addWidget(m_up); Chris@1291: Chris@1291: m_add = new QPushButton; Chris@1291: m_add->setIcon(IconLoader().load("plus")); Chris@1291: m_add->setToolTip(tr("Move the selected location earlier in the list")); Chris@1291: connect(m_add, SIGNAL(clicked()), this, SLOT(addClicked())); Chris@1291: buttons->addWidget(m_add); Chris@1291: Chris@1291: m_delete = new QPushButton; Chris@1291: m_delete->setIcon(IconLoader().load("datadelete")); Chris@1291: m_delete->setToolTip(tr("Remove the selected location from the list")); Chris@1291: connect(m_delete, SIGNAL(clicked()), this, SLOT(deleteClicked())); Chris@1291: buttons->addWidget(m_delete); Chris@1291: Chris@1291: m_reset = new QPushButton; Chris@1291: m_reset->setText(tr("Reset")); Chris@1291: m_reset->setToolTip(tr("Reset the list for this plugin type to its default")); Chris@1291: connect(m_reset, SIGNAL(clicked()), this, SLOT(resetClicked())); Chris@1291: buttons->addWidget(m_reset); Chris@1287: Chris@1287: int row = 0; Chris@1287: Chris@1289: m_header = new QLabel; Chris@1290: m_header->setText(tr("Plugin locations for plugin type:")); Chris@1289: m_layout->addWidget(m_header, row, 0); Chris@1289: Chris@1289: m_pluginTypeSelector = new QComboBox; Chris@1290: m_layout->addWidget(m_pluginTypeSelector, row, 1, Qt::AlignLeft); Chris@1289: connect(m_pluginTypeSelector, SIGNAL(currentTextChanged(QString)), Chris@1289: this, SLOT(currentTypeChanged(QString))); Chris@1289: Chris@1290: m_layout->setColumnStretch(1, 10); Chris@1287: ++row; Chris@1287: Chris@1287: m_list = new QListWidget; Chris@1290: m_layout->addWidget(m_list, row, 0, 1, 3); Chris@1290: m_layout->setRowStretch(row, 20); Chris@1288: connect(m_list, SIGNAL(currentRowChanged(int)), Chris@1288: this, SLOT(currentLocationChanged(int))); Chris@1287: ++row; Chris@1290: Chris@1290: m_layout->addLayout(buttons, row, 2); Chris@1287: ++row; Chris@1291: Chris@1290: m_envOverride = new QCheckBox; Chris@1290: connect(m_envOverride, SIGNAL(stateChanged(int)), Chris@1290: this, SLOT(envOverrideChanged(int))); Chris@1290: m_layout->addWidget(m_envOverride, row, 0, 1, 3); Chris@1291: ++row; Chris@1285: } Chris@1285: Chris@1285: PluginPathConfigurator::~PluginPathConfigurator() Chris@1285: { Chris@1285: } Chris@1285: Chris@1285: void Chris@1289: PluginPathConfigurator::setPaths(Paths paths) Chris@1285: { Chris@1289: m_paths = paths; Chris@1290: if (m_originalPaths.empty()) { Chris@1290: m_originalPaths = paths; Chris@1290: } Chris@1289: Chris@1289: m_pluginTypeSelector->clear(); Chris@1289: for (const auto &p: paths) { Chris@1289: m_pluginTypeSelector->addItem(p.first); Chris@1289: } Chris@1289: Chris@1285: populate(); Chris@1285: } Chris@1285: Chris@1285: void Chris@1289: PluginPathConfigurator::populate() Chris@1285: { Chris@1287: m_list->clear(); Chris@1286: Chris@1289: if (m_paths.empty()) return; Chris@1289: Chris@1290: populateFor(m_paths.begin()->first, -1); Chris@1289: } Chris@1289: Chris@1289: void Chris@1289: PluginPathConfigurator::populateFor(QString type, int makeCurrent) Chris@1289: { Chris@1290: QString envVariable = m_paths.at(type).envVariable; Chris@1290: bool useEnvVariable = m_paths.at(type).useEnvVariable; Chris@1290: m_envOverride->setText Chris@1291: (tr("Allow the %1 environment variable to take priority over this") Chris@1290: .arg(envVariable)); Chris@1290: m_envOverride->setCheckState(useEnvVariable ? Qt::Checked : Qt::Unchecked); Chris@1290: Chris@1289: m_list->clear(); Chris@1289: Chris@1289: for (int i = 0; i < m_pluginTypeSelector->count(); ++i) { Chris@1289: if (type == m_pluginTypeSelector->itemText(i)) { Chris@1289: m_pluginTypeSelector->setCurrentIndex(i); Chris@1289: } Chris@1289: } Chris@1289: Chris@1289: QStringList path = m_paths.at(type).directories; Chris@1289: Chris@1289: for (int i = 0; i < path.size(); ++i) { Chris@1289: m_list->addItem(path[i]); Chris@1287: } Chris@1285: Chris@1290: if (makeCurrent < path.size()) { Chris@1287: m_list->setCurrentRow(makeCurrent); Chris@1290: currentLocationChanged(makeCurrent); Chris@1286: } Chris@1285: } Chris@1285: Chris@1285: void Chris@1288: PluginPathConfigurator::currentLocationChanged(int i) Chris@1288: { Chris@1289: QString type = m_pluginTypeSelector->currentText(); Chris@1289: QStringList path = m_paths.at(type).directories; Chris@1288: m_up->setEnabled(i > 0); Chris@1290: m_down->setEnabled(i >= 0 && i + 1 < path.size()); Chris@1290: m_delete->setEnabled(i >= 0 && i < path.size()); Chris@1290: m_reset->setEnabled(path != m_originalPaths.at(type).directories); Chris@1289: } Chris@1289: Chris@1289: void Chris@1289: PluginPathConfigurator::currentTypeChanged(QString type) Chris@1289: { Chris@1290: populateFor(type, -1); Chris@1290: } Chris@1290: Chris@1290: void Chris@1290: PluginPathConfigurator::envOverrideChanged(int ) Chris@1290: { Chris@1290: //!!! Chris@1288: } Chris@1288: Chris@1288: void Chris@1285: PluginPathConfigurator::upClicked() Chris@1285: { Chris@1289: QString type = m_pluginTypeSelector->currentText(); Chris@1289: QStringList path = m_paths.at(type).directories; Chris@1289: Chris@1287: int current = m_list->currentRow(); Chris@1287: if (current <= 0) return; Chris@1287: Chris@1286: QStringList newPath; Chris@1289: for (int i = 0; i < path.size(); ++i) { Chris@1287: if (i + 1 == current) { Chris@1289: newPath.push_back(path[i+1]); Chris@1289: newPath.push_back(path[i]); Chris@1286: ++i; Chris@1286: } else { Chris@1289: newPath.push_back(path[i]); Chris@1286: } Chris@1286: } Chris@1290: Chris@1290: auto newEntry = m_paths.at(type); Chris@1290: newEntry.directories = newPath; Chris@1290: m_paths[type] = newEntry; Chris@1287: Chris@1289: populateFor(type, current - 1); Chris@1285: } Chris@1285: Chris@1285: void Chris@1285: PluginPathConfigurator::downClicked() Chris@1285: { Chris@1289: QString type = m_pluginTypeSelector->currentText(); Chris@1289: QStringList path = m_paths.at(type).directories; Chris@1289: Chris@1287: int current = m_list->currentRow(); Chris@1289: if (current < 0 || current + 1 >= path.size()) return; Chris@1289: Chris@1286: QStringList newPath; Chris@1289: for (int i = 0; i < path.size(); ++i) { Chris@1287: if (i == current) { Chris@1289: newPath.push_back(path[i+1]); Chris@1289: newPath.push_back(path[i]); Chris@1286: ++i; Chris@1286: } else { Chris@1289: newPath.push_back(path[i]); Chris@1286: } Chris@1286: } Chris@1290: Chris@1290: auto newEntry = m_paths.at(type); Chris@1290: newEntry.directories = newPath; Chris@1290: m_paths[type] = newEntry; Chris@1287: Chris@1289: populateFor(type, current + 1); Chris@1285: } Chris@1285: Chris@1285: void Chris@1291: PluginPathConfigurator::addClicked() Chris@1291: { Chris@1291: QString type = m_pluginTypeSelector->currentText(); Chris@1291: Chris@1291: QString newDir = QFileDialog::getExistingDirectory Chris@1291: (this, tr("Choose directory to add")); Chris@1291: Chris@1291: if (newDir == QString()) return; Chris@1291: Chris@1291: auto newEntry = m_paths.at(type); Chris@1291: newEntry.directories.push_back(newDir); Chris@1291: m_paths[type] = newEntry; Chris@1291: Chris@1291: populateFor(type, newEntry.directories.size() - 1); Chris@1291: } Chris@1291: Chris@1291: void Chris@1285: PluginPathConfigurator::deleteClicked() Chris@1285: { Chris@1289: QString type = m_pluginTypeSelector->currentText(); Chris@1289: QStringList path = m_paths.at(type).directories; Chris@1289: Chris@1287: int current = m_list->currentRow(); Chris@1290: if (current < 0) return; Chris@1289: Chris@1286: QStringList newPath; Chris@1289: for (int i = 0; i < path.size(); ++i) { Chris@1287: if (i != current) { Chris@1289: newPath.push_back(path[i]); Chris@1286: } Chris@1286: } Chris@1290: Chris@1290: auto newEntry = m_paths.at(type); Chris@1290: newEntry.directories = newPath; Chris@1290: m_paths[type] = newEntry; Chris@1287: Chris@1289: populateFor(type, current < newPath.size() ? current : current-1); Chris@1285: } Chris@1290: Chris@1290: void Chris@1290: PluginPathConfigurator::resetClicked() Chris@1290: { Chris@1290: QString type = m_pluginTypeSelector->currentText(); Chris@1290: m_paths[type] = m_originalPaths[type]; Chris@1290: populateFor(type, -1); Chris@1290: }