annotate main/PreferencesDialog.cpp @ 235:45912448a9e8

* Add ability to invert a colour 3d plot in the vertical axis
author Chris Cannam
date Mon, 04 Feb 2008 13:35:43 +0000
parents e3ff897a60bf
children 99fa3387dfef
rev   line source
Chris@0 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@0 2
Chris@0 3 /*
Chris@0 4 Sonic Visualiser
Chris@0 5 An audio file viewer and annotation editor.
Chris@0 6 Centre for Digital Music, Queen Mary, University of London.
Chris@0 7 This file copyright 2006 Chris Cannam.
Chris@0 8
Chris@0 9 This program is free software; you can redistribute it and/or
Chris@0 10 modify it under the terms of the GNU General Public License as
Chris@0 11 published by the Free Software Foundation; either version 2 of the
Chris@0 12 License, or (at your option) any later version. See the file
Chris@0 13 COPYING included with this distribution for more information.
Chris@0 14 */
Chris@0 15
Chris@0 16 #include "PreferencesDialog.h"
Chris@0 17
Chris@0 18 #include <QGridLayout>
Chris@0 19 #include <QComboBox>
Chris@0 20 #include <QCheckBox>
Chris@0 21 #include <QGroupBox>
Chris@0 22 #include <QDoubleSpinBox>
Chris@0 23 #include <QLabel>
Chris@0 24 #include <QPushButton>
Chris@0 25 #include <QHBoxLayout>
Chris@0 26 #include <QString>
Chris@163 27 #include <QDialogButtonBox>
Chris@163 28 #include <QMessageBox>
Chris@180 29 #include <QTabWidget>
Chris@180 30 #include <QLineEdit>
Chris@180 31 #include <QFileDialog>
Chris@180 32 #include <QMessageBox>
Chris@225 33 #include <QSpinBox>
Chris@0 34
Chris@9 35 #include "widgets/WindowTypeSelector.h"
Chris@180 36 #include "widgets/IconLoader.h"
Chris@0 37 #include "base/Preferences.h"
Chris@0 38
Chris@0 39 PreferencesDialog::PreferencesDialog(QWidget *parent, Qt::WFlags flags) :
Chris@180 40 QDialog(parent, flags),
Chris@180 41 m_changesOnRestart(false)
Chris@0 42 {
Chris@163 43 setWindowTitle(tr("Sonic Visualiser: Application Preferences"));
Chris@0 44
Chris@0 45 Preferences *prefs = Preferences::getInstance();
Chris@0 46
Chris@0 47 QGridLayout *grid = new QGridLayout;
Chris@0 48 setLayout(grid);
Chris@180 49
Chris@180 50 QTabWidget *tab = new QTabWidget;
Chris@180 51 grid->addWidget(tab, 0, 0);
Chris@0 52
Chris@180 53 tab->setTabPosition(QTabWidget::North);
Chris@0 54
Chris@0 55 // Create this first, as slots that get called from the ctor will
Chris@0 56 // refer to it
Chris@0 57 m_applyButton = new QPushButton(tr("Apply"));
Chris@0 58
Chris@180 59 // Create all the preference widgets first, then create the
Chris@180 60 // individual tab widgets and place the preferences in their
Chris@180 61 // appropriate places in one go afterwards
Chris@180 62
Chris@114 63 int min, max, deflt, i;
Chris@0 64
Chris@9 65 m_windowType = WindowType(prefs->getPropertyRangeAndValue
Chris@114 66 ("Window Type", &min, &max, &deflt));
Chris@9 67 m_windowTypeSelector = new WindowTypeSelector(m_windowType);
Chris@0 68
Chris@9 69 connect(m_windowTypeSelector, SIGNAL(windowTypeChanged(WindowType)),
Chris@9 70 this, SLOT(windowTypeChanged(WindowType)));
Chris@0 71
Chris@115 72 QComboBox *smoothing = new QComboBox;
Chris@115 73
Chris@115 74 int sm = prefs->getPropertyRangeAndValue("Spectrogram Smoothing", &min, &max,
Chris@115 75 &deflt);
Chris@115 76 m_spectrogramSmoothing = sm;
Chris@0 77
Chris@115 78 for (i = min; i <= max; ++i) {
Chris@115 79 smoothing->addItem(prefs->getPropertyValueLabel("Spectrogram Smoothing", i));
Chris@115 80 }
Chris@115 81
Chris@115 82 smoothing->setCurrentIndex(sm);
Chris@115 83
Chris@115 84 connect(smoothing, SIGNAL(currentIndexChanged(int)),
Chris@115 85 this, SLOT(spectrogramSmoothingChanged(int)));
Chris@0 86
Chris@0 87 QComboBox *propertyLayout = new QComboBox;
Chris@114 88 int pl = prefs->getPropertyRangeAndValue("Property Box Layout", &min, &max,
Chris@115 89 &deflt);
Chris@0 90 m_propertyLayout = pl;
Chris@0 91
Chris@0 92 for (i = min; i <= max; ++i) {
Chris@0 93 propertyLayout->addItem(prefs->getPropertyValueLabel("Property Box Layout", i));
Chris@0 94 }
Chris@0 95
Chris@0 96 propertyLayout->setCurrentIndex(pl);
Chris@0 97
Chris@0 98 connect(propertyLayout, SIGNAL(currentIndexChanged(int)),
Chris@0 99 this, SLOT(propertyLayoutChanged(int)));
Chris@0 100
Chris@0 101 m_tuningFrequency = prefs->getTuningFrequency();
Chris@0 102
Chris@0 103 QDoubleSpinBox *frequency = new QDoubleSpinBox;
Chris@0 104 frequency->setMinimum(100.0);
Chris@0 105 frequency->setMaximum(5000.0);
Chris@0 106 frequency->setSuffix(" Hz");
Chris@0 107 frequency->setSingleStep(1);
Chris@0 108 frequency->setValue(m_tuningFrequency);
Chris@0 109 frequency->setDecimals(2);
Chris@0 110
Chris@0 111 connect(frequency, SIGNAL(valueChanged(double)),
Chris@0 112 this, SLOT(tuningFrequencyChanged(double)));
Chris@0 113
Chris@32 114 QComboBox *resampleQuality = new QComboBox;
Chris@32 115
Chris@114 116 int rsq = prefs->getPropertyRangeAndValue("Resample Quality", &min, &max,
Chris@114 117 &deflt);
Chris@32 118 m_resampleQuality = rsq;
Chris@32 119
Chris@32 120 for (i = min; i <= max; ++i) {
Chris@32 121 resampleQuality->addItem(prefs->getPropertyValueLabel("Resample Quality", i));
Chris@32 122 }
Chris@32 123
Chris@32 124 resampleQuality->setCurrentIndex(rsq);
Chris@32 125
Chris@32 126 connect(resampleQuality, SIGNAL(currentIndexChanged(int)),
Chris@32 127 this, SLOT(resampleQualityChanged(int)));
Chris@32 128
Chris@180 129 QCheckBox *resampleOnLoad = new QCheckBox;
Chris@180 130 m_resampleOnLoad = prefs->getResampleOnLoad();
Chris@180 131 resampleOnLoad->setCheckState(m_resampleOnLoad ? Qt::Checked :
Chris@180 132 Qt::Unchecked);
Chris@180 133 connect(resampleOnLoad, SIGNAL(stateChanged(int)),
Chris@180 134 this, SLOT(resampleOnLoadChanged(int)));
Chris@180 135
Chris@180 136 m_tempDirRootEdit = new QLineEdit;
Chris@180 137 QString dir = prefs->getTemporaryDirectoryRoot();
Chris@180 138 m_tempDirRoot = dir;
Chris@180 139 dir.replace("$HOME", tr("<home directory>"));
Chris@180 140 m_tempDirRootEdit->setText(dir);
Chris@180 141 m_tempDirRootEdit->setReadOnly(true);
Chris@180 142 QPushButton *tempDirButton = new QPushButton;
Chris@180 143 tempDirButton->setIcon(IconLoader().load("fileopen"));
Chris@180 144 connect(tempDirButton, SIGNAL(clicked()),
Chris@180 145 this, SLOT(tempDirButtonClicked()));
Chris@180 146 tempDirButton->setFixedSize(QSize(24, 24));
Chris@180 147
Chris@180 148 QComboBox *bgMode = new QComboBox;
Chris@180 149 int bg = prefs->getPropertyRangeAndValue("Background Mode", &min, &max,
Chris@180 150 &deflt);
Chris@180 151 m_backgroundMode = bg;
Chris@180 152 for (i = min; i <= max; ++i) {
Chris@180 153 bgMode->addItem(prefs->getPropertyValueLabel("Background Mode", i));
Chris@180 154 }
Chris@180 155 bgMode->setCurrentIndex(bg);
Chris@180 156
Chris@180 157 connect(bgMode, SIGNAL(currentIndexChanged(int)),
Chris@180 158 this, SLOT(backgroundModeChanged(int)));
Chris@180 159
Chris@225 160 QSpinBox *fontSize = new QSpinBox;
Chris@225 161 int fs = prefs->getPropertyRangeAndValue("View Font Size", &min, &max,
Chris@225 162 &deflt);
Chris@234 163 m_viewFontSize = fs;
Chris@225 164 fontSize->setMinimum(min);
Chris@225 165 fontSize->setMaximum(max);
Chris@225 166 fontSize->setSuffix(" pt");
Chris@225 167 fontSize->setSingleStep(1);
Chris@225 168 fontSize->setValue(fs);
Chris@225 169
Chris@225 170 connect(fontSize, SIGNAL(valueChanged(int)),
Chris@225 171 this, SLOT(viewFontSizeChanged(int)));
Chris@225 172
Chris@180 173 // General tab
Chris@180 174
Chris@180 175 QFrame *frame = new QFrame;
Chris@180 176
Chris@180 177 QGridLayout *subgrid = new QGridLayout;
Chris@180 178 frame->setLayout(subgrid);
Chris@180 179
Chris@0 180 int row = 0;
Chris@0 181
Chris@0 182 subgrid->addWidget(new QLabel(tr("%1:").arg(prefs->getPropertyLabel
Chris@0 183 ("Property Box Layout"))),
Chris@0 184 row, 0);
Chris@0 185 subgrid->addWidget(propertyLayout, row++, 1, 1, 2);
Chris@0 186
Chris@0 187 subgrid->addWidget(new QLabel(tr("%1:").arg(prefs->getPropertyLabel
Chris@180 188 ("Background Mode"))),
Chris@0 189 row, 0);
Chris@180 190 subgrid->addWidget(bgMode, row++, 1, 1, 2);
Chris@180 191
Chris@180 192 subgrid->addWidget(new QLabel(tr("%1:").arg(prefs->getPropertyLabel
Chris@225 193 ("View Font Size"))),
Chris@225 194 row, 0);
Chris@225 195 subgrid->addWidget(fontSize, row++, 1, 1, 2);
Chris@225 196
Chris@225 197 subgrid->addWidget(new QLabel(tr("%1:").arg(prefs->getPropertyLabel
Chris@180 198 ("Resample On Load"))),
Chris@180 199 row, 0);
Chris@180 200 subgrid->addWidget(resampleOnLoad, row++, 1, 1, 1);
Chris@0 201
Chris@32 202 subgrid->addWidget(new QLabel(tr("%1:").arg(prefs->getPropertyLabel
Chris@32 203 ("Resample Quality"))),
Chris@32 204 row, 0);
Chris@32 205 subgrid->addWidget(resampleQuality, row++, 1, 1, 2);
Chris@32 206
Chris@180 207 subgrid->addWidget(new QLabel(tr("%1:").arg(prefs->getPropertyLabel
Chris@180 208 ("Temporary Directory Root"))),
Chris@180 209 row, 0);
Chris@180 210 subgrid->addWidget(m_tempDirRootEdit, row, 1, 1, 1);
Chris@180 211 subgrid->addWidget(tempDirButton, row, 2, 1, 1);
Chris@180 212 row++;
Chris@180 213
Chris@180 214 subgrid->setRowStretch(row, 10);
Chris@180 215
Chris@180 216 tab->addTab(frame, tr("&General"));
Chris@180 217
Chris@180 218 // Analysis tab
Chris@180 219
Chris@180 220 frame = new QFrame;
Chris@180 221 subgrid = new QGridLayout;
Chris@180 222 frame->setLayout(subgrid);
Chris@180 223 row = 0;
Chris@180 224
Chris@180 225 subgrid->addWidget(new QLabel(tr("%1:").arg(prefs->getPropertyLabel
Chris@180 226 ("Tuning Frequency"))),
Chris@180 227 row, 0);
Chris@180 228 subgrid->addWidget(frequency, row++, 1, 1, 2);
Chris@180 229
Chris@0 230 subgrid->addWidget(new QLabel(prefs->getPropertyLabel
Chris@115 231 ("Spectrogram Smoothing")),
Chris@115 232 row, 0);
Chris@115 233 subgrid->addWidget(smoothing, row++, 1, 1, 2);
Chris@0 234
Chris@0 235 subgrid->addWidget(new QLabel(tr("%1:").arg(prefs->getPropertyLabel
Chris@0 236 ("Window Type"))),
Chris@0 237 row, 0);
Chris@9 238 subgrid->addWidget(m_windowTypeSelector, row++, 1, 2, 2);
Chris@9 239 subgrid->setRowStretch(row, 10);
Chris@9 240 row++;
Chris@0 241
Chris@180 242 subgrid->setRowStretch(row, 10);
Chris@180 243
Chris@180 244 tab->addTab(frame, tr("&Analysis"));
Chris@180 245
Chris@163 246 QDialogButtonBox *bb = new QDialogButtonBox(Qt::Horizontal);
Chris@163 247 grid->addWidget(bb, 1, 0);
Chris@0 248
Chris@0 249 QPushButton *ok = new QPushButton(tr("OK"));
Chris@0 250 QPushButton *cancel = new QPushButton(tr("Cancel"));
Chris@163 251 bb->addButton(ok, QDialogButtonBox::AcceptRole);
Chris@163 252 bb->addButton(m_applyButton, QDialogButtonBox::ApplyRole);
Chris@163 253 bb->addButton(cancel, QDialogButtonBox::RejectRole);
Chris@0 254 connect(ok, SIGNAL(clicked()), this, SLOT(okClicked()));
Chris@0 255 connect(m_applyButton, SIGNAL(clicked()), this, SLOT(applyClicked()));
Chris@0 256 connect(cancel, SIGNAL(clicked()), this, SLOT(cancelClicked()));
Chris@0 257
Chris@0 258 m_applyButton->setEnabled(false);
Chris@0 259 }
Chris@0 260
Chris@0 261 PreferencesDialog::~PreferencesDialog()
Chris@0 262 {
Chris@0 263 std::cerr << "PreferencesDialog::~PreferencesDialog()" << std::endl;
Chris@0 264 }
Chris@0 265
Chris@0 266 void
Chris@9 267 PreferencesDialog::windowTypeChanged(WindowType type)
Chris@0 268 {
Chris@0 269 m_windowType = type;
Chris@0 270 m_applyButton->setEnabled(true);
Chris@0 271 }
Chris@0 272
Chris@0 273 void
Chris@115 274 PreferencesDialog::spectrogramSmoothingChanged(int smoothing)
Chris@0 275 {
Chris@115 276 m_spectrogramSmoothing = smoothing;
Chris@0 277 m_applyButton->setEnabled(true);
Chris@0 278 }
Chris@0 279
Chris@0 280 void
Chris@0 281 PreferencesDialog::propertyLayoutChanged(int layout)
Chris@0 282 {
Chris@0 283 m_propertyLayout = layout;
Chris@0 284 m_applyButton->setEnabled(true);
Chris@0 285 }
Chris@0 286
Chris@0 287 void
Chris@0 288 PreferencesDialog::tuningFrequencyChanged(double freq)
Chris@0 289 {
Chris@0 290 m_tuningFrequency = freq;
Chris@0 291 m_applyButton->setEnabled(true);
Chris@0 292 }
Chris@0 293
Chris@0 294 void
Chris@32 295 PreferencesDialog::resampleQualityChanged(int q)
Chris@32 296 {
Chris@32 297 m_resampleQuality = q;
Chris@32 298 m_applyButton->setEnabled(true);
Chris@32 299 }
Chris@32 300
Chris@32 301 void
Chris@180 302 PreferencesDialog::resampleOnLoadChanged(int state)
Chris@180 303 {
Chris@180 304 m_resampleOnLoad = (state == Qt::Checked);
Chris@180 305 m_applyButton->setEnabled(true);
Chris@180 306 m_changesOnRestart = true;
Chris@180 307 }
Chris@180 308
Chris@180 309 void
Chris@180 310 PreferencesDialog::tempDirRootChanged(QString r)
Chris@180 311 {
Chris@180 312 m_tempDirRoot = r;
Chris@180 313 m_applyButton->setEnabled(true);
Chris@180 314 }
Chris@180 315
Chris@180 316 void
Chris@180 317 PreferencesDialog::tempDirButtonClicked()
Chris@180 318 {
Chris@180 319 QString dir = QFileDialog::getExistingDirectory
Chris@180 320 (this, tr("Select a directory to create cache subdirectory in"),
Chris@180 321 m_tempDirRoot);
Chris@180 322 if (dir == "") return;
Chris@180 323 m_tempDirRootEdit->setText(dir);
Chris@180 324 tempDirRootChanged(dir);
Chris@180 325 m_changesOnRestart = true;
Chris@180 326 }
Chris@180 327
Chris@180 328 void
Chris@180 329 PreferencesDialog::backgroundModeChanged(int mode)
Chris@180 330 {
Chris@180 331 m_backgroundMode = mode;
Chris@180 332 m_applyButton->setEnabled(true);
Chris@180 333 m_changesOnRestart = true;
Chris@180 334 }
Chris@180 335
Chris@180 336 void
Chris@225 337 PreferencesDialog::viewFontSizeChanged(int sz)
Chris@225 338 {
Chris@225 339 m_viewFontSize = sz;
Chris@225 340 m_applyButton->setEnabled(true);
Chris@225 341 }
Chris@225 342
Chris@225 343 void
Chris@0 344 PreferencesDialog::okClicked()
Chris@0 345 {
Chris@0 346 applyClicked();
Chris@0 347 accept();
Chris@0 348 }
Chris@0 349
Chris@0 350 void
Chris@0 351 PreferencesDialog::applyClicked()
Chris@0 352 {
Chris@0 353 Preferences *prefs = Preferences::getInstance();
Chris@0 354 prefs->setWindowType(WindowType(m_windowType));
Chris@115 355 prefs->setSpectrogramSmoothing(Preferences::SpectrogramSmoothing
Chris@115 356 (m_spectrogramSmoothing));
Chris@0 357 prefs->setPropertyBoxLayout(Preferences::PropertyBoxLayout
Chris@0 358 (m_propertyLayout));
Chris@0 359 prefs->setTuningFrequency(m_tuningFrequency);
Chris@32 360 prefs->setResampleQuality(m_resampleQuality);
Chris@180 361 prefs->setResampleOnLoad(m_resampleOnLoad);
Chris@180 362 prefs->setTemporaryDirectoryRoot(m_tempDirRoot);
Chris@180 363 prefs->setBackgroundMode(Preferences::BackgroundMode(m_backgroundMode));
Chris@225 364 prefs->setViewFontSize(m_viewFontSize);
Chris@180 365
Chris@0 366 m_applyButton->setEnabled(false);
Chris@180 367
Chris@180 368 if (m_changesOnRestart) {
Chris@180 369 QMessageBox::information(this, tr("Preferences"),
Chris@180 370 tr("One or more of the application preferences you have changed may not take full effect until Sonic Visualiser is restarted.\nPlease exit and restart the application now if you want these changes to take effect immediately."));
Chris@180 371 m_changesOnRestart = false;
Chris@180 372 }
Chris@0 373 }
Chris@0 374
Chris@0 375 void
Chris@0 376 PreferencesDialog::cancelClicked()
Chris@0 377 {
Chris@0 378 reject();
Chris@0 379 }
Chris@0 380
Chris@163 381 void
Chris@163 382 PreferencesDialog::applicationClosing(bool quickly)
Chris@163 383 {
Chris@163 384 if (quickly) {
Chris@163 385 reject();
Chris@163 386 return;
Chris@163 387 }
Chris@163 388
Chris@163 389 if (m_applyButton->isEnabled()) {
Chris@163 390 int rv = QMessageBox::warning
Chris@163 391 (this, tr("Preferences Changed"),
Chris@163 392 tr("Some preferences have been changed but not applied.\n"
Chris@163 393 "Apply them before closing?"),
Chris@163 394 QMessageBox::Apply | QMessageBox::Discard,
Chris@163 395 QMessageBox::Discard);
Chris@163 396 if (rv == QMessageBox::Apply) {
Chris@163 397 applyClicked();
Chris@163 398 accept();
Chris@163 399 } else {
Chris@163 400 reject();
Chris@163 401 }
Chris@163 402 } else {
Chris@163 403 accept();
Chris@163 404 }
Chris@163 405 }
Chris@163 406