annotate main/PreferencesDialog.cpp @ 228:b99c6449c2ed

* Fix #1844539 confusing waveform navigator with secondary files
author Chris Cannam
date Wed, 12 Dec 2007 17:42:23 +0000
parents d7ded015af32
children e3ff897a60bf
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@225 163 fontSize->setMinimum(min);
Chris@225 164 fontSize->setMaximum(max);
Chris@225 165 fontSize->setSuffix(" pt");
Chris@225 166 fontSize->setSingleStep(1);
Chris@225 167 fontSize->setValue(fs);
Chris@225 168
Chris@225 169 connect(fontSize, SIGNAL(valueChanged(int)),
Chris@225 170 this, SLOT(viewFontSizeChanged(int)));
Chris@225 171
Chris@180 172 // General tab
Chris@180 173
Chris@180 174 QFrame *frame = new QFrame;
Chris@180 175
Chris@180 176 QGridLayout *subgrid = new QGridLayout;
Chris@180 177 frame->setLayout(subgrid);
Chris@180 178
Chris@0 179 int row = 0;
Chris@0 180
Chris@0 181 subgrid->addWidget(new QLabel(tr("%1:").arg(prefs->getPropertyLabel
Chris@0 182 ("Property Box Layout"))),
Chris@0 183 row, 0);
Chris@0 184 subgrid->addWidget(propertyLayout, row++, 1, 1, 2);
Chris@0 185
Chris@0 186 subgrid->addWidget(new QLabel(tr("%1:").arg(prefs->getPropertyLabel
Chris@180 187 ("Background Mode"))),
Chris@0 188 row, 0);
Chris@180 189 subgrid->addWidget(bgMode, row++, 1, 1, 2);
Chris@180 190
Chris@180 191 subgrid->addWidget(new QLabel(tr("%1:").arg(prefs->getPropertyLabel
Chris@225 192 ("View Font Size"))),
Chris@225 193 row, 0);
Chris@225 194 subgrid->addWidget(fontSize, row++, 1, 1, 2);
Chris@225 195
Chris@225 196 subgrid->addWidget(new QLabel(tr("%1:").arg(prefs->getPropertyLabel
Chris@180 197 ("Resample On Load"))),
Chris@180 198 row, 0);
Chris@180 199 subgrid->addWidget(resampleOnLoad, row++, 1, 1, 1);
Chris@0 200
Chris@32 201 subgrid->addWidget(new QLabel(tr("%1:").arg(prefs->getPropertyLabel
Chris@32 202 ("Resample Quality"))),
Chris@32 203 row, 0);
Chris@32 204 subgrid->addWidget(resampleQuality, row++, 1, 1, 2);
Chris@32 205
Chris@180 206 subgrid->addWidget(new QLabel(tr("%1:").arg(prefs->getPropertyLabel
Chris@180 207 ("Temporary Directory Root"))),
Chris@180 208 row, 0);
Chris@180 209 subgrid->addWidget(m_tempDirRootEdit, row, 1, 1, 1);
Chris@180 210 subgrid->addWidget(tempDirButton, row, 2, 1, 1);
Chris@180 211 row++;
Chris@180 212
Chris@180 213 subgrid->setRowStretch(row, 10);
Chris@180 214
Chris@180 215 tab->addTab(frame, tr("&General"));
Chris@180 216
Chris@180 217 // Analysis tab
Chris@180 218
Chris@180 219 frame = new QFrame;
Chris@180 220 subgrid = new QGridLayout;
Chris@180 221 frame->setLayout(subgrid);
Chris@180 222 row = 0;
Chris@180 223
Chris@180 224 subgrid->addWidget(new QLabel(tr("%1:").arg(prefs->getPropertyLabel
Chris@180 225 ("Tuning Frequency"))),
Chris@180 226 row, 0);
Chris@180 227 subgrid->addWidget(frequency, row++, 1, 1, 2);
Chris@180 228
Chris@0 229 subgrid->addWidget(new QLabel(prefs->getPropertyLabel
Chris@115 230 ("Spectrogram Smoothing")),
Chris@115 231 row, 0);
Chris@115 232 subgrid->addWidget(smoothing, row++, 1, 1, 2);
Chris@0 233
Chris@0 234 subgrid->addWidget(new QLabel(tr("%1:").arg(prefs->getPropertyLabel
Chris@0 235 ("Window Type"))),
Chris@0 236 row, 0);
Chris@9 237 subgrid->addWidget(m_windowTypeSelector, row++, 1, 2, 2);
Chris@9 238 subgrid->setRowStretch(row, 10);
Chris@9 239 row++;
Chris@0 240
Chris@180 241 subgrid->setRowStretch(row, 10);
Chris@180 242
Chris@180 243 tab->addTab(frame, tr("&Analysis"));
Chris@180 244
Chris@163 245 QDialogButtonBox *bb = new QDialogButtonBox(Qt::Horizontal);
Chris@163 246 grid->addWidget(bb, 1, 0);
Chris@0 247
Chris@0 248 QPushButton *ok = new QPushButton(tr("OK"));
Chris@0 249 QPushButton *cancel = new QPushButton(tr("Cancel"));
Chris@163 250 bb->addButton(ok, QDialogButtonBox::AcceptRole);
Chris@163 251 bb->addButton(m_applyButton, QDialogButtonBox::ApplyRole);
Chris@163 252 bb->addButton(cancel, QDialogButtonBox::RejectRole);
Chris@0 253 connect(ok, SIGNAL(clicked()), this, SLOT(okClicked()));
Chris@0 254 connect(m_applyButton, SIGNAL(clicked()), this, SLOT(applyClicked()));
Chris@0 255 connect(cancel, SIGNAL(clicked()), this, SLOT(cancelClicked()));
Chris@0 256
Chris@0 257 m_applyButton->setEnabled(false);
Chris@0 258 }
Chris@0 259
Chris@0 260 PreferencesDialog::~PreferencesDialog()
Chris@0 261 {
Chris@0 262 std::cerr << "PreferencesDialog::~PreferencesDialog()" << std::endl;
Chris@0 263 }
Chris@0 264
Chris@0 265 void
Chris@9 266 PreferencesDialog::windowTypeChanged(WindowType type)
Chris@0 267 {
Chris@0 268 m_windowType = type;
Chris@0 269 m_applyButton->setEnabled(true);
Chris@0 270 }
Chris@0 271
Chris@0 272 void
Chris@115 273 PreferencesDialog::spectrogramSmoothingChanged(int smoothing)
Chris@0 274 {
Chris@115 275 m_spectrogramSmoothing = smoothing;
Chris@0 276 m_applyButton->setEnabled(true);
Chris@0 277 }
Chris@0 278
Chris@0 279 void
Chris@0 280 PreferencesDialog::propertyLayoutChanged(int layout)
Chris@0 281 {
Chris@0 282 m_propertyLayout = layout;
Chris@0 283 m_applyButton->setEnabled(true);
Chris@0 284 }
Chris@0 285
Chris@0 286 void
Chris@0 287 PreferencesDialog::tuningFrequencyChanged(double freq)
Chris@0 288 {
Chris@0 289 m_tuningFrequency = freq;
Chris@0 290 m_applyButton->setEnabled(true);
Chris@0 291 }
Chris@0 292
Chris@0 293 void
Chris@32 294 PreferencesDialog::resampleQualityChanged(int q)
Chris@32 295 {
Chris@32 296 m_resampleQuality = q;
Chris@32 297 m_applyButton->setEnabled(true);
Chris@32 298 }
Chris@32 299
Chris@32 300 void
Chris@180 301 PreferencesDialog::resampleOnLoadChanged(int state)
Chris@180 302 {
Chris@180 303 m_resampleOnLoad = (state == Qt::Checked);
Chris@180 304 m_applyButton->setEnabled(true);
Chris@180 305 m_changesOnRestart = true;
Chris@180 306 }
Chris@180 307
Chris@180 308 void
Chris@180 309 PreferencesDialog::tempDirRootChanged(QString r)
Chris@180 310 {
Chris@180 311 m_tempDirRoot = r;
Chris@180 312 m_applyButton->setEnabled(true);
Chris@180 313 }
Chris@180 314
Chris@180 315 void
Chris@180 316 PreferencesDialog::tempDirButtonClicked()
Chris@180 317 {
Chris@180 318 QString dir = QFileDialog::getExistingDirectory
Chris@180 319 (this, tr("Select a directory to create cache subdirectory in"),
Chris@180 320 m_tempDirRoot);
Chris@180 321 if (dir == "") return;
Chris@180 322 m_tempDirRootEdit->setText(dir);
Chris@180 323 tempDirRootChanged(dir);
Chris@180 324 m_changesOnRestart = true;
Chris@180 325 }
Chris@180 326
Chris@180 327 void
Chris@180 328 PreferencesDialog::backgroundModeChanged(int mode)
Chris@180 329 {
Chris@180 330 m_backgroundMode = mode;
Chris@180 331 m_applyButton->setEnabled(true);
Chris@180 332 m_changesOnRestart = true;
Chris@180 333 }
Chris@180 334
Chris@180 335 void
Chris@225 336 PreferencesDialog::viewFontSizeChanged(int sz)
Chris@225 337 {
Chris@225 338 m_viewFontSize = sz;
Chris@225 339 m_applyButton->setEnabled(true);
Chris@225 340 }
Chris@225 341
Chris@225 342 void
Chris@0 343 PreferencesDialog::okClicked()
Chris@0 344 {
Chris@0 345 applyClicked();
Chris@0 346 accept();
Chris@0 347 }
Chris@0 348
Chris@0 349 void
Chris@0 350 PreferencesDialog::applyClicked()
Chris@0 351 {
Chris@0 352 Preferences *prefs = Preferences::getInstance();
Chris@0 353 prefs->setWindowType(WindowType(m_windowType));
Chris@115 354 prefs->setSpectrogramSmoothing(Preferences::SpectrogramSmoothing
Chris@115 355 (m_spectrogramSmoothing));
Chris@0 356 prefs->setPropertyBoxLayout(Preferences::PropertyBoxLayout
Chris@0 357 (m_propertyLayout));
Chris@0 358 prefs->setTuningFrequency(m_tuningFrequency);
Chris@32 359 prefs->setResampleQuality(m_resampleQuality);
Chris@180 360 prefs->setResampleOnLoad(m_resampleOnLoad);
Chris@180 361 prefs->setTemporaryDirectoryRoot(m_tempDirRoot);
Chris@180 362 prefs->setBackgroundMode(Preferences::BackgroundMode(m_backgroundMode));
Chris@225 363 prefs->setViewFontSize(m_viewFontSize);
Chris@180 364
Chris@0 365 m_applyButton->setEnabled(false);
Chris@180 366
Chris@180 367 if (m_changesOnRestart) {
Chris@180 368 QMessageBox::information(this, tr("Preferences"),
Chris@180 369 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 370 m_changesOnRestart = false;
Chris@180 371 }
Chris@0 372 }
Chris@0 373
Chris@0 374 void
Chris@0 375 PreferencesDialog::cancelClicked()
Chris@0 376 {
Chris@0 377 reject();
Chris@0 378 }
Chris@0 379
Chris@163 380 void
Chris@163 381 PreferencesDialog::applicationClosing(bool quickly)
Chris@163 382 {
Chris@163 383 if (quickly) {
Chris@163 384 reject();
Chris@163 385 return;
Chris@163 386 }
Chris@163 387
Chris@163 388 if (m_applyButton->isEnabled()) {
Chris@163 389 int rv = QMessageBox::warning
Chris@163 390 (this, tr("Preferences Changed"),
Chris@163 391 tr("Some preferences have been changed but not applied.\n"
Chris@163 392 "Apply them before closing?"),
Chris@163 393 QMessageBox::Apply | QMessageBox::Discard,
Chris@163 394 QMessageBox::Discard);
Chris@163 395 if (rv == QMessageBox::Apply) {
Chris@163 396 applyClicked();
Chris@163 397 accept();
Chris@163 398 } else {
Chris@163 399 reject();
Chris@163 400 }
Chris@163 401 } else {
Chris@163 402 accept();
Chris@163 403 }
Chris@163 404 }
Chris@163 405