PluginPathConfigurator.cpp
Go to the documentation of this file.
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2 
3 /*
4  Sonic Visualiser
5  An audio file viewer and annotation editor.
6  Centre for Digital Music, Queen Mary, University of London.
7 
8  This program is free software; you can redistribute it and/or
9  modify it under the terms of the GNU General Public License as
10  published by the Free Software Foundation; either version 2 of the
11  License, or (at your option) any later version. See the file
12  COPYING included with this distribution for more information.
13 */
14 
15 #include "PluginPathConfigurator.h"
16 #include "PluginReviewDialog.h"
17 
18 #include <QPushButton>
19 #include <QListWidget>
20 #include <QGridLayout>
21 #include <QComboBox>
22 #include <QLabel>
23 #include <QCheckBox>
24 #include <QFileDialog>
25 
26 #include "IconLoader.h"
27 #include "WidgetScale.h"
28 
29 #include "plugin/PluginPathSetter.h"
30 
32  QFrame(parent)
33 {
34  m_layout = new QGridLayout;
35  setLayout(m_layout);
36 
37  QHBoxLayout *buttons = new QHBoxLayout;
38 
39  m_down = new QPushButton;
40  m_down->setIcon(IconLoader().load("down"));
41  m_down->setToolTip(tr("Move the selected location later in the list"));
42  connect(m_down, SIGNAL(clicked()), this, SLOT(downClicked()));
43  buttons->addWidget(m_down);
44 
45  m_up = new QPushButton;
46  m_up->setIcon(IconLoader().load("up"));
47  m_up->setToolTip(tr("Move the selected location earlier in the list"));
48  connect(m_up, SIGNAL(clicked()), this, SLOT(upClicked()));
49  buttons->addWidget(m_up);
50 
51  m_add = new QPushButton;
52  m_add->setIcon(IconLoader().load("plus"));
53  m_add->setToolTip(tr("Add a new location to the list"));
54  connect(m_add, SIGNAL(clicked()), this, SLOT(addClicked()));
55  buttons->addWidget(m_add);
56 
57  m_delete = new QPushButton;
58  m_delete->setIcon(IconLoader().load("datadelete"));
59  m_delete->setToolTip(tr("Remove the selected location from the list"));
60  connect(m_delete, SIGNAL(clicked()), this, SLOT(deleteClicked()));
61  buttons->addWidget(m_delete);
62 
63  m_reset = new QPushButton;
64  m_reset->setText(tr("Reset to Default"));
65  m_reset->setToolTip(tr("Reset the list for this plugin type to its default"));
66  connect(m_reset, SIGNAL(clicked()), this, SLOT(resetClicked()));
67  buttons->addWidget(m_reset);
68 
69  buttons->addStretch(50);
70 
71  m_seePlugins = new QPushButton;
72  m_seePlugins->setText(tr("Review plugins..."));
73  connect(m_seePlugins, SIGNAL(clicked()), this, SLOT(seePluginsClicked()));
74  buttons->addWidget(m_seePlugins);
75 
76  int row = 0;
77 
78  m_header = new QLabel;
79  m_header->setText(tr("Plugin locations for plugin type:"));
80  m_layout->addWidget(m_header, row, 0);
81 
82  m_pluginTypeSelector = new QComboBox;
83  m_layout->addWidget(m_pluginTypeSelector, row, 1, Qt::AlignLeft);
84  connect(m_pluginTypeSelector, SIGNAL(currentTextChanged(QString)),
85  this, SLOT(currentTypeChanged(QString)));
86 
87  m_layout->setColumnStretch(1, 10);
88  ++row;
89 
90  m_list = new QListWidget;
91  m_layout->addWidget(m_list, row, 0, 1, 3);
92  m_layout->setRowStretch(row, 20);
93  connect(m_list, SIGNAL(currentRowChanged(int)),
94  this, SLOT(currentLocationChanged(int)));
95  ++row;
96 
97  m_layout->addLayout(buttons, row, 0, 1, 3);
98 
99  ++row;
100 
101  m_envOverride = new QCheckBox;
102  connect(m_envOverride, SIGNAL(stateChanged(int)),
103  this, SLOT(envOverrideChanged(int)));
104  m_layout->addWidget(m_envOverride, row, 0, 1, 3);
105  ++row;
106 }
107 
109 {
110 }
111 
112 QString
113 PluginPathConfigurator::getLabelFor(PluginPathSetter::TypeKey key)
114 {
115  if (key.second == KnownPlugins::FormatNative) {
116  switch (key.first) {
117  case KnownPlugins::VampPlugin:
118  return tr("Vamp");
119  case KnownPlugins::LADSPAPlugin:
120  return tr("LADSPA");
121  case KnownPlugins::DSSIPlugin:
122  return tr("DSSI");
123  }
124  } else if (key.second == KnownPlugins::FormatNonNative32Bit) {
125  switch (key.first) {
126  case KnownPlugins::VampPlugin:
127  return tr("Vamp (32-bit)");
128  case KnownPlugins::LADSPAPlugin:
129  return tr("LADSPA (32-bit)");
130  case KnownPlugins::DSSIPlugin:
131  return tr("DSSI (32-bit)");
132  }
133  }
134  SVCERR << "PluginPathConfigurator::getLabelFor: WARNING: "
135  << "Unknown format value " << key.second << endl;
136  return "<unknown>";
137 }
138 
139 PluginPathSetter::TypeKey
141 {
142  for (const auto &p: m_paths) {
143  auto key = p.first;
144  if (getLabelFor(key) == label) {
145  return key;
146  }
147  }
148  SVCERR << "PluginPathConfigurator::getKeyForLabel: WARNING: "
149  << "Unrecognised label \"" << label << "\"" << endl;
150  return { KnownPlugins::VampPlugin, KnownPlugins::FormatNative };
151 }
152 
153 void
154 PluginPathConfigurator::setPaths(PluginPathSetter::Paths paths)
155 {
156  m_paths = paths;
157 
158  m_defaultPaths = PluginPathSetter::getDefaultPaths();
159 
160  m_pluginTypeSelector->clear();
161  for (const auto &p: paths) {
162  m_pluginTypeSelector->addItem(getLabelFor(p.first));
163  }
164 
165  populate();
166 }
167 
168 void
170 {
171  m_list->clear();
172 
173  if (m_paths.empty()) return;
174 
175  populateFor(m_paths.begin()->first, -1);
176 }
177 
178 void
179 PluginPathConfigurator::populateFor(PluginPathSetter::TypeKey key,
180  int makeCurrent)
181 {
182  QString envVariable = m_paths.at(key).envVariable;
183  bool useEnvVariable = m_paths.at(key).useEnvVariable;
184  QString envVarValue =
185  PluginPathSetter::getOriginalEnvironmentValue(envVariable);
186  QString currentValueRubric;
187  if (envVarValue == QString()) {
188  currentValueRubric = tr("(Variable is currently unset)");
189  } else {
190  if (envVarValue.length() > 100) {
191  QString envVarStart = envVarValue.left(95);
192  currentValueRubric = tr("(Current value begins: \"%1 ...\")")
193  .arg(envVarStart);
194  } else {
195  currentValueRubric = tr("(Currently set to: \"%1\")")
196  .arg(envVarValue);
197  }
198  }
199  m_envOverride->setText
200  (tr("Allow the %1 environment variable to take priority over this\n%2")
201  .arg(envVariable)
202  .arg(currentValueRubric));
203  m_envOverride->setCheckState(useEnvVariable ? Qt::Checked : Qt::Unchecked);
204 
205  m_list->clear();
206 
207  for (int i = 0; i < m_pluginTypeSelector->count(); ++i) {
208  if (getLabelFor(key) == m_pluginTypeSelector->itemText(i)) {
209  m_pluginTypeSelector->blockSignals(true);
210  m_pluginTypeSelector->setCurrentIndex(i);
211  m_pluginTypeSelector->blockSignals(false);
212  }
213  }
214 
215  QStringList path = m_paths.at(key).directories;
216 
217  for (int i = 0; i < path.size(); ++i) {
218  m_list->addItem(path[i]);
219  }
220 
221  if (makeCurrent < path.size()) {
222  m_list->setCurrentRow(makeCurrent);
223  currentLocationChanged(makeCurrent);
224  }
225 }
226 
227 void
229 {
230  QString label = m_pluginTypeSelector->currentText();
231  PluginPathSetter::TypeKey key = getKeyForLabel(label);
232  QStringList path = m_paths.at(key).directories;
233  m_up->setEnabled(i > 0);
234  m_down->setEnabled(i >= 0 && i + 1 < path.size());
235  m_delete->setEnabled(i >= 0 && i < path.size());
236  m_reset->setEnabled(path != m_defaultPaths.at(key).directories);
237 }
238 
239 void
241 {
242  populateFor(getKeyForLabel(label), -1);
243 }
244 
245 void
247 {
248  bool useEnvVariable = (state == Qt::Checked);
249 
250  QString label = m_pluginTypeSelector->currentText();
251  PluginPathSetter::TypeKey key = getKeyForLabel(label);
252 
253  auto newEntry = m_paths.at(key);
254  newEntry.useEnvVariable = useEnvVariable;
255  m_paths[key] = newEntry;
256 
257  emit pathsChanged();
258 }
259 
260 void
262 {
263  QString label = m_pluginTypeSelector->currentText();
264  PluginPathSetter::TypeKey key = getKeyForLabel(label);
265  QStringList path = m_paths.at(key).directories;
266 
267  int current = m_list->currentRow();
268  if (current <= 0) return;
269 
270  QStringList newPath;
271  for (int i = 0; i < path.size(); ++i) {
272  if (i + 1 == current) {
273  newPath.push_back(path[i+1]);
274  newPath.push_back(path[i]);
275  ++i;
276  } else {
277  newPath.push_back(path[i]);
278  }
279  }
280 
281  auto newEntry = m_paths.at(key);
282  newEntry.directories = newPath;
283  m_paths[key] = newEntry;
284 
285  populateFor(key, current - 1);
286 
287  emit pathsChanged();
288 }
289 
290 void
292 {
293  QString label = m_pluginTypeSelector->currentText();
294  PluginPathSetter::TypeKey key = getKeyForLabel(label);
295  QStringList path = m_paths.at(key).directories;
296 
297  int current = m_list->currentRow();
298  if (current < 0 || current + 1 >= path.size()) return;
299 
300  QStringList newPath;
301  for (int i = 0; i < path.size(); ++i) {
302  if (i == current) {
303  newPath.push_back(path[i+1]);
304  newPath.push_back(path[i]);
305  ++i;
306  } else {
307  newPath.push_back(path[i]);
308  }
309  }
310 
311  auto newEntry = m_paths.at(key);
312  newEntry.directories = newPath;
313  m_paths[key] = newEntry;
314 
315  populateFor(key, current + 1);
316 
317  emit pathsChanged();
318 }
319 
320 void
322 {
323  QString label = m_pluginTypeSelector->currentText();
324  PluginPathSetter::TypeKey key = getKeyForLabel(label);
325 
326  QString newDir = QFileDialog::getExistingDirectory
327  (this, tr("Choose directory to add"));
328 
329  if (newDir == QString()) return;
330 
331  auto newEntry = m_paths.at(key);
332  newEntry.directories.push_back(newDir);
333  m_paths[key] = newEntry;
334 
335  populateFor(key, newEntry.directories.size() - 1);
336 
337  emit pathsChanged();
338 }
339 
340 void
342 {
343  QString label = m_pluginTypeSelector->currentText();
344  PluginPathSetter::TypeKey key = getKeyForLabel(label);
345  QStringList path = m_paths.at(key).directories;
346 
347  int current = m_list->currentRow();
348  if (current < 0) return;
349 
350  QStringList newPath;
351  for (int i = 0; i < path.size(); ++i) {
352  if (i != current) {
353  newPath.push_back(path[i]);
354  }
355  }
356 
357  auto newEntry = m_paths.at(key);
358  newEntry.directories = newPath;
359  m_paths[key] = newEntry;
360 
361  populateFor(key, current < newPath.size() ? current : current-1);
362 
363  emit pathsChanged();
364 }
365 
366 void
368 {
369  QString label = m_pluginTypeSelector->currentText();
370  PluginPathSetter::TypeKey key = getKeyForLabel(label);
371  m_paths[key] = m_defaultPaths[key];
372  populateFor(key, -1);
373 
374  emit pathsChanged();
375 }
376 
377 void
379 {
380  PluginReviewDialog dialog(this);
381  dialog.populate();
382  dialog.exec();
383 }
384 
PluginPathConfigurator(QWidget *parent=0)
void setPaths(PluginPathSetter::Paths paths)
void populateFor(PluginPathSetter::TypeKey, int makeCurrent)
QString getLabelFor(PluginPathSetter::TypeKey)
PluginPathSetter::Paths m_defaultPaths
PluginPathSetter::Paths m_paths
PluginPathSetter::TypeKey getKeyForLabel(QString label)