annotate main/PreferencesDialog.cpp @ 248:842745edded8

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