annotate main/PreferencesDialog.cpp @ 192:d3477f673fb4

* Add Reset Counters... function
author Chris Cannam
date Wed, 10 Oct 2007 10:22:34 +0000
parents 98ba77e0d897
children d7ded015af32 fc542303eda2
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@0 33
Chris@9 34 #include "widgets/WindowTypeSelector.h"
Chris@180 35 #include "widgets/IconLoader.h"
Chris@0 36 #include "base/Preferences.h"
Chris@0 37
Chris@0 38 PreferencesDialog::PreferencesDialog(QWidget *parent, Qt::WFlags flags) :
Chris@180 39 QDialog(parent, flags),
Chris@180 40 m_changesOnRestart(false)
Chris@0 41 {
Chris@163 42 setWindowTitle(tr("Sonic Visualiser: Application Preferences"));
Chris@0 43
Chris@0 44 Preferences *prefs = Preferences::getInstance();
Chris@0 45
Chris@0 46 QGridLayout *grid = new QGridLayout;
Chris@0 47 setLayout(grid);
Chris@180 48
Chris@180 49 QTabWidget *tab = new QTabWidget;
Chris@180 50 grid->addWidget(tab, 0, 0);
Chris@0 51
Chris@180 52 tab->setTabPosition(QTabWidget::North);
Chris@0 53
Chris@0 54 // Create this first, as slots that get called from the ctor will
Chris@0 55 // refer to it
Chris@0 56 m_applyButton = new QPushButton(tr("Apply"));
Chris@0 57
Chris@180 58 // Create all the preference widgets first, then create the
Chris@180 59 // individual tab widgets and place the preferences in their
Chris@180 60 // appropriate places in one go afterwards
Chris@180 61
Chris@114 62 int min, max, deflt, i;
Chris@0 63
Chris@9 64 m_windowType = WindowType(prefs->getPropertyRangeAndValue
Chris@114 65 ("Window Type", &min, &max, &deflt));
Chris@9 66 m_windowTypeSelector = new WindowTypeSelector(m_windowType);
Chris@0 67
Chris@9 68 connect(m_windowTypeSelector, SIGNAL(windowTypeChanged(WindowType)),
Chris@9 69 this, SLOT(windowTypeChanged(WindowType)));
Chris@0 70
Chris@115 71 QComboBox *smoothing = new QComboBox;
Chris@115 72
Chris@115 73 int sm = prefs->getPropertyRangeAndValue("Spectrogram Smoothing", &min, &max,
Chris@115 74 &deflt);
Chris@115 75 m_spectrogramSmoothing = sm;
Chris@0 76
Chris@115 77 for (i = min; i <= max; ++i) {
Chris@115 78 smoothing->addItem(prefs->getPropertyValueLabel("Spectrogram Smoothing", i));
Chris@115 79 }
Chris@115 80
Chris@115 81 smoothing->setCurrentIndex(sm);
Chris@115 82
Chris@115 83 connect(smoothing, SIGNAL(currentIndexChanged(int)),
Chris@115 84 this, SLOT(spectrogramSmoothingChanged(int)));
Chris@0 85
Chris@0 86 QComboBox *propertyLayout = new QComboBox;
Chris@114 87 int pl = prefs->getPropertyRangeAndValue("Property Box Layout", &min, &max,
Chris@115 88 &deflt);
Chris@0 89 m_propertyLayout = pl;
Chris@0 90
Chris@0 91 for (i = min; i <= max; ++i) {
Chris@0 92 propertyLayout->addItem(prefs->getPropertyValueLabel("Property Box Layout", i));
Chris@0 93 }
Chris@0 94
Chris@0 95 propertyLayout->setCurrentIndex(pl);
Chris@0 96
Chris@0 97 connect(propertyLayout, SIGNAL(currentIndexChanged(int)),
Chris@0 98 this, SLOT(propertyLayoutChanged(int)));
Chris@0 99
Chris@0 100 m_tuningFrequency = prefs->getTuningFrequency();
Chris@0 101
Chris@0 102 QDoubleSpinBox *frequency = new QDoubleSpinBox;
Chris@0 103 frequency->setMinimum(100.0);
Chris@0 104 frequency->setMaximum(5000.0);
Chris@0 105 frequency->setSuffix(" Hz");
Chris@0 106 frequency->setSingleStep(1);
Chris@0 107 frequency->setValue(m_tuningFrequency);
Chris@0 108 frequency->setDecimals(2);
Chris@0 109
Chris@0 110 connect(frequency, SIGNAL(valueChanged(double)),
Chris@0 111 this, SLOT(tuningFrequencyChanged(double)));
Chris@0 112
Chris@32 113 QComboBox *resampleQuality = new QComboBox;
Chris@32 114
Chris@114 115 int rsq = prefs->getPropertyRangeAndValue("Resample Quality", &min, &max,
Chris@114 116 &deflt);
Chris@32 117 m_resampleQuality = rsq;
Chris@32 118
Chris@32 119 for (i = min; i <= max; ++i) {
Chris@32 120 resampleQuality->addItem(prefs->getPropertyValueLabel("Resample Quality", i));
Chris@32 121 }
Chris@32 122
Chris@32 123 resampleQuality->setCurrentIndex(rsq);
Chris@32 124
Chris@32 125 connect(resampleQuality, SIGNAL(currentIndexChanged(int)),
Chris@32 126 this, SLOT(resampleQualityChanged(int)));
Chris@32 127
Chris@180 128 QCheckBox *resampleOnLoad = new QCheckBox;
Chris@180 129 m_resampleOnLoad = prefs->getResampleOnLoad();
Chris@180 130 resampleOnLoad->setCheckState(m_resampleOnLoad ? Qt::Checked :
Chris@180 131 Qt::Unchecked);
Chris@180 132 connect(resampleOnLoad, SIGNAL(stateChanged(int)),
Chris@180 133 this, SLOT(resampleOnLoadChanged(int)));
Chris@180 134
Chris@180 135 m_tempDirRootEdit = new QLineEdit;
Chris@180 136 QString dir = prefs->getTemporaryDirectoryRoot();
Chris@180 137 m_tempDirRoot = dir;
Chris@180 138 dir.replace("$HOME", tr("<home directory>"));
Chris@180 139 m_tempDirRootEdit->setText(dir);
Chris@180 140 m_tempDirRootEdit->setReadOnly(true);
Chris@180 141 QPushButton *tempDirButton = new QPushButton;
Chris@180 142 tempDirButton->setIcon(IconLoader().load("fileopen"));
Chris@180 143 connect(tempDirButton, SIGNAL(clicked()),
Chris@180 144 this, SLOT(tempDirButtonClicked()));
Chris@180 145 tempDirButton->setFixedSize(QSize(24, 24));
Chris@180 146
Chris@180 147 QComboBox *bgMode = new QComboBox;
Chris@180 148 int bg = prefs->getPropertyRangeAndValue("Background Mode", &min, &max,
Chris@180 149 &deflt);
Chris@180 150 m_backgroundMode = bg;
Chris@180 151 for (i = min; i <= max; ++i) {
Chris@180 152 bgMode->addItem(prefs->getPropertyValueLabel("Background Mode", i));
Chris@180 153 }
Chris@180 154 bgMode->setCurrentIndex(bg);
Chris@180 155
Chris@180 156 connect(bgMode, SIGNAL(currentIndexChanged(int)),
Chris@180 157 this, SLOT(backgroundModeChanged(int)));
Chris@180 158
Chris@180 159 // General tab
Chris@180 160
Chris@180 161 QFrame *frame = new QFrame;
Chris@180 162
Chris@180 163 QGridLayout *subgrid = new QGridLayout;
Chris@180 164 frame->setLayout(subgrid);
Chris@180 165
Chris@0 166 int row = 0;
Chris@0 167
Chris@0 168 subgrid->addWidget(new QLabel(tr("%1:").arg(prefs->getPropertyLabel
Chris@0 169 ("Property Box Layout"))),
Chris@0 170 row, 0);
Chris@0 171 subgrid->addWidget(propertyLayout, row++, 1, 1, 2);
Chris@0 172
Chris@0 173 subgrid->addWidget(new QLabel(tr("%1:").arg(prefs->getPropertyLabel
Chris@180 174 ("Background Mode"))),
Chris@0 175 row, 0);
Chris@180 176 subgrid->addWidget(bgMode, row++, 1, 1, 2);
Chris@180 177
Chris@180 178 subgrid->addWidget(new QLabel(tr("%1:").arg(prefs->getPropertyLabel
Chris@180 179 ("Resample On Load"))),
Chris@180 180 row, 0);
Chris@180 181 subgrid->addWidget(resampleOnLoad, row++, 1, 1, 1);
Chris@0 182
Chris@32 183 subgrid->addWidget(new QLabel(tr("%1:").arg(prefs->getPropertyLabel
Chris@32 184 ("Resample Quality"))),
Chris@32 185 row, 0);
Chris@32 186 subgrid->addWidget(resampleQuality, row++, 1, 1, 2);
Chris@32 187
Chris@180 188 subgrid->addWidget(new QLabel(tr("%1:").arg(prefs->getPropertyLabel
Chris@180 189 ("Temporary Directory Root"))),
Chris@180 190 row, 0);
Chris@180 191 subgrid->addWidget(m_tempDirRootEdit, row, 1, 1, 1);
Chris@180 192 subgrid->addWidget(tempDirButton, row, 2, 1, 1);
Chris@180 193 row++;
Chris@180 194
Chris@180 195 subgrid->setRowStretch(row, 10);
Chris@180 196
Chris@180 197 tab->addTab(frame, tr("&General"));
Chris@180 198
Chris@180 199 // Analysis tab
Chris@180 200
Chris@180 201 frame = new QFrame;
Chris@180 202 subgrid = new QGridLayout;
Chris@180 203 frame->setLayout(subgrid);
Chris@180 204 row = 0;
Chris@180 205
Chris@180 206 subgrid->addWidget(new QLabel(tr("%1:").arg(prefs->getPropertyLabel
Chris@180 207 ("Tuning Frequency"))),
Chris@180 208 row, 0);
Chris@180 209 subgrid->addWidget(frequency, row++, 1, 1, 2);
Chris@180 210
Chris@0 211 subgrid->addWidget(new QLabel(prefs->getPropertyLabel
Chris@115 212 ("Spectrogram Smoothing")),
Chris@115 213 row, 0);
Chris@115 214 subgrid->addWidget(smoothing, row++, 1, 1, 2);
Chris@0 215
Chris@0 216 subgrid->addWidget(new QLabel(tr("%1:").arg(prefs->getPropertyLabel
Chris@0 217 ("Window Type"))),
Chris@0 218 row, 0);
Chris@9 219 subgrid->addWidget(m_windowTypeSelector, row++, 1, 2, 2);
Chris@9 220 subgrid->setRowStretch(row, 10);
Chris@9 221 row++;
Chris@0 222
Chris@180 223 subgrid->setRowStretch(row, 10);
Chris@180 224
Chris@180 225 tab->addTab(frame, tr("&Analysis"));
Chris@180 226
Chris@163 227 QDialogButtonBox *bb = new QDialogButtonBox(Qt::Horizontal);
Chris@163 228 grid->addWidget(bb, 1, 0);
Chris@0 229
Chris@0 230 QPushButton *ok = new QPushButton(tr("OK"));
Chris@0 231 QPushButton *cancel = new QPushButton(tr("Cancel"));
Chris@163 232 bb->addButton(ok, QDialogButtonBox::AcceptRole);
Chris@163 233 bb->addButton(m_applyButton, QDialogButtonBox::ApplyRole);
Chris@163 234 bb->addButton(cancel, QDialogButtonBox::RejectRole);
Chris@0 235 connect(ok, SIGNAL(clicked()), this, SLOT(okClicked()));
Chris@0 236 connect(m_applyButton, SIGNAL(clicked()), this, SLOT(applyClicked()));
Chris@0 237 connect(cancel, SIGNAL(clicked()), this, SLOT(cancelClicked()));
Chris@0 238
Chris@0 239 m_applyButton->setEnabled(false);
Chris@0 240 }
Chris@0 241
Chris@0 242 PreferencesDialog::~PreferencesDialog()
Chris@0 243 {
Chris@0 244 std::cerr << "PreferencesDialog::~PreferencesDialog()" << std::endl;
Chris@0 245 }
Chris@0 246
Chris@0 247 void
Chris@9 248 PreferencesDialog::windowTypeChanged(WindowType type)
Chris@0 249 {
Chris@0 250 m_windowType = type;
Chris@0 251 m_applyButton->setEnabled(true);
Chris@0 252 }
Chris@0 253
Chris@0 254 void
Chris@115 255 PreferencesDialog::spectrogramSmoothingChanged(int smoothing)
Chris@0 256 {
Chris@115 257 m_spectrogramSmoothing = smoothing;
Chris@0 258 m_applyButton->setEnabled(true);
Chris@0 259 }
Chris@0 260
Chris@0 261 void
Chris@0 262 PreferencesDialog::propertyLayoutChanged(int layout)
Chris@0 263 {
Chris@0 264 m_propertyLayout = layout;
Chris@0 265 m_applyButton->setEnabled(true);
Chris@0 266 }
Chris@0 267
Chris@0 268 void
Chris@0 269 PreferencesDialog::tuningFrequencyChanged(double freq)
Chris@0 270 {
Chris@0 271 m_tuningFrequency = freq;
Chris@0 272 m_applyButton->setEnabled(true);
Chris@0 273 }
Chris@0 274
Chris@0 275 void
Chris@32 276 PreferencesDialog::resampleQualityChanged(int q)
Chris@32 277 {
Chris@32 278 m_resampleQuality = q;
Chris@32 279 m_applyButton->setEnabled(true);
Chris@32 280 }
Chris@32 281
Chris@32 282 void
Chris@180 283 PreferencesDialog::resampleOnLoadChanged(int state)
Chris@180 284 {
Chris@180 285 m_resampleOnLoad = (state == Qt::Checked);
Chris@180 286 m_applyButton->setEnabled(true);
Chris@180 287 m_changesOnRestart = true;
Chris@180 288 }
Chris@180 289
Chris@180 290 void
Chris@180 291 PreferencesDialog::tempDirRootChanged(QString r)
Chris@180 292 {
Chris@180 293 m_tempDirRoot = r;
Chris@180 294 m_applyButton->setEnabled(true);
Chris@180 295 }
Chris@180 296
Chris@180 297 void
Chris@180 298 PreferencesDialog::tempDirButtonClicked()
Chris@180 299 {
Chris@180 300 QString dir = QFileDialog::getExistingDirectory
Chris@180 301 (this, tr("Select a directory to create cache subdirectory in"),
Chris@180 302 m_tempDirRoot);
Chris@180 303 if (dir == "") return;
Chris@180 304 m_tempDirRootEdit->setText(dir);
Chris@180 305 tempDirRootChanged(dir);
Chris@180 306 m_changesOnRestart = true;
Chris@180 307 }
Chris@180 308
Chris@180 309 void
Chris@180 310 PreferencesDialog::backgroundModeChanged(int mode)
Chris@180 311 {
Chris@180 312 m_backgroundMode = mode;
Chris@180 313 m_applyButton->setEnabled(true);
Chris@180 314 m_changesOnRestart = true;
Chris@180 315 }
Chris@180 316
Chris@180 317 void
Chris@0 318 PreferencesDialog::okClicked()
Chris@0 319 {
Chris@0 320 applyClicked();
Chris@0 321 accept();
Chris@0 322 }
Chris@0 323
Chris@0 324 void
Chris@0 325 PreferencesDialog::applyClicked()
Chris@0 326 {
Chris@0 327 Preferences *prefs = Preferences::getInstance();
Chris@0 328 prefs->setWindowType(WindowType(m_windowType));
Chris@115 329 prefs->setSpectrogramSmoothing(Preferences::SpectrogramSmoothing
Chris@115 330 (m_spectrogramSmoothing));
Chris@0 331 prefs->setPropertyBoxLayout(Preferences::PropertyBoxLayout
Chris@0 332 (m_propertyLayout));
Chris@0 333 prefs->setTuningFrequency(m_tuningFrequency);
Chris@32 334 prefs->setResampleQuality(m_resampleQuality);
Chris@180 335 prefs->setResampleOnLoad(m_resampleOnLoad);
Chris@180 336 prefs->setTemporaryDirectoryRoot(m_tempDirRoot);
Chris@180 337 prefs->setBackgroundMode(Preferences::BackgroundMode(m_backgroundMode));
Chris@180 338
Chris@0 339 m_applyButton->setEnabled(false);
Chris@180 340
Chris@180 341 if (m_changesOnRestart) {
Chris@180 342 QMessageBox::information(this, tr("Preferences"),
Chris@180 343 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 344 m_changesOnRestart = false;
Chris@180 345 }
Chris@0 346 }
Chris@0 347
Chris@0 348 void
Chris@0 349 PreferencesDialog::cancelClicked()
Chris@0 350 {
Chris@0 351 reject();
Chris@0 352 }
Chris@0 353
Chris@163 354 void
Chris@163 355 PreferencesDialog::applicationClosing(bool quickly)
Chris@163 356 {
Chris@163 357 if (quickly) {
Chris@163 358 reject();
Chris@163 359 return;
Chris@163 360 }
Chris@163 361
Chris@163 362 if (m_applyButton->isEnabled()) {
Chris@163 363 int rv = QMessageBox::warning
Chris@163 364 (this, tr("Preferences Changed"),
Chris@163 365 tr("Some preferences have been changed but not applied.\n"
Chris@163 366 "Apply them before closing?"),
Chris@163 367 QMessageBox::Apply | QMessageBox::Discard,
Chris@163 368 QMessageBox::Discard);
Chris@163 369 if (rv == QMessageBox::Apply) {
Chris@163 370 applyClicked();
Chris@163 371 accept();
Chris@163 372 } else {
Chris@163 373 reject();
Chris@163 374 }
Chris@163 375 } else {
Chris@163 376 accept();
Chris@163 377 }
Chris@163 378 }
Chris@163 379