PreferencesDialog.cpp

Go to the documentation of this file.
00001 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
00002 
00003 /*
00004     Sonic Visualiser
00005     An audio file viewer and annotation editor.
00006     Centre for Digital Music, Queen Mary, University of London.
00007     This file copyright 2006 Chris Cannam.
00008     
00009     This program is free software; you can redistribute it and/or
00010     modify it under the terms of the GNU General Public License as
00011     published by the Free Software Foundation; either version 2 of the
00012     License, or (at your option) any later version.  See the file
00013     COPYING included with this distribution for more information.
00014 */
00015 
00016 #include "PreferencesDialog.h"
00017 
00018 #include <QGridLayout>
00019 #include <QComboBox>
00020 #include <QCheckBox>
00021 #include <QGroupBox>
00022 #include <QDoubleSpinBox>
00023 #include <QLabel>
00024 #include <QPushButton>
00025 #include <QHBoxLayout>
00026 #include <QString>
00027 #include <QDialogButtonBox>
00028 #include <QMessageBox>
00029 #include <QTabWidget>
00030 #include <QLineEdit>
00031 #include <QFileDialog>
00032 #include <QMessageBox>
00033 #include <QSpinBox>
00034 
00035 #include "widgets/WindowTypeSelector.h"
00036 #include "widgets/IconLoader.h"
00037 #include "base/Preferences.h"
00038 
00039 PreferencesDialog::PreferencesDialog(QWidget *parent, Qt::WFlags flags) :
00040     QDialog(parent, flags),
00041     m_changesOnRestart(false)
00042 {
00043     setWindowTitle(tr("Sonic Visualiser: Application Preferences"));
00044 
00045     Preferences *prefs = Preferences::getInstance();
00046 
00047     QGridLayout *grid = new QGridLayout;
00048     setLayout(grid);
00049 
00050     QTabWidget *tab = new QTabWidget;
00051     grid->addWidget(tab, 0, 0);
00052     
00053     tab->setTabPosition(QTabWidget::North);
00054 
00055     // Create this first, as slots that get called from the ctor will
00056     // refer to it
00057     m_applyButton = new QPushButton(tr("Apply"));
00058 
00059     // Create all the preference widgets first, then create the
00060     // individual tab widgets and place the preferences in their
00061     // appropriate places in one go afterwards
00062 
00063     int min, max, deflt, i;
00064 
00065     m_windowType = WindowType(prefs->getPropertyRangeAndValue
00066                               ("Window Type", &min, &max, &deflt));
00067     m_windowTypeSelector = new WindowTypeSelector(m_windowType);
00068 
00069     connect(m_windowTypeSelector, SIGNAL(windowTypeChanged(WindowType)),
00070             this, SLOT(windowTypeChanged(WindowType)));
00071 
00072     QComboBox *smoothing = new QComboBox;
00073     
00074     int sm = prefs->getPropertyRangeAndValue("Spectrogram Smoothing", &min, &max,
00075                                              &deflt);
00076     m_spectrogramSmoothing = sm;
00077 
00078     for (i = min; i <= max; ++i) {
00079         smoothing->addItem(prefs->getPropertyValueLabel("Spectrogram Smoothing", i));
00080     }
00081 
00082     smoothing->setCurrentIndex(sm);
00083 
00084     connect(smoothing, SIGNAL(currentIndexChanged(int)),
00085             this, SLOT(spectrogramSmoothingChanged(int)));
00086 
00087     QComboBox *propertyLayout = new QComboBox;
00088     int pl = prefs->getPropertyRangeAndValue("Property Box Layout", &min, &max,
00089                                          &deflt);
00090     m_propertyLayout = pl;
00091 
00092     for (i = min; i <= max; ++i) {
00093         propertyLayout->addItem(prefs->getPropertyValueLabel("Property Box Layout", i));
00094     }
00095 
00096     propertyLayout->setCurrentIndex(pl);
00097 
00098     connect(propertyLayout, SIGNAL(currentIndexChanged(int)),
00099             this, SLOT(propertyLayoutChanged(int)));
00100 
00101     m_tuningFrequency = prefs->getTuningFrequency();
00102 
00103     QDoubleSpinBox *frequency = new QDoubleSpinBox;
00104     frequency->setMinimum(100.0);
00105     frequency->setMaximum(5000.0);
00106     frequency->setSuffix(" Hz");
00107     frequency->setSingleStep(1);
00108     frequency->setValue(m_tuningFrequency);
00109     frequency->setDecimals(2);
00110 
00111     connect(frequency, SIGNAL(valueChanged(double)),
00112             this, SLOT(tuningFrequencyChanged(double)));
00113 
00114     QComboBox *resampleQuality = new QComboBox;
00115 
00116     int rsq = prefs->getPropertyRangeAndValue("Resample Quality", &min, &max,
00117                                               &deflt);
00118     m_resampleQuality = rsq;
00119 
00120     for (i = min; i <= max; ++i) {
00121         resampleQuality->addItem(prefs->getPropertyValueLabel("Resample Quality", i));
00122     }
00123 
00124     resampleQuality->setCurrentIndex(rsq);
00125 
00126     connect(resampleQuality, SIGNAL(currentIndexChanged(int)),
00127             this, SLOT(resampleQualityChanged(int)));
00128 
00129     QCheckBox *resampleOnLoad = new QCheckBox;
00130     m_resampleOnLoad = prefs->getResampleOnLoad();
00131     resampleOnLoad->setCheckState(m_resampleOnLoad ? Qt::Checked :
00132                                   Qt::Unchecked);
00133     connect(resampleOnLoad, SIGNAL(stateChanged(int)),
00134             this, SLOT(resampleOnLoadChanged(int)));
00135 
00136     m_tempDirRootEdit = new QLineEdit;
00137     QString dir = prefs->getTemporaryDirectoryRoot();
00138     m_tempDirRoot = dir;
00139     dir.replace("$HOME", tr("<home directory>"));
00140     m_tempDirRootEdit->setText(dir);
00141     m_tempDirRootEdit->setReadOnly(true);
00142     QPushButton *tempDirButton = new QPushButton;
00143     tempDirButton->setIcon(IconLoader().load("fileopen"));
00144     connect(tempDirButton, SIGNAL(clicked()),
00145             this, SLOT(tempDirButtonClicked()));
00146     tempDirButton->setFixedSize(QSize(24, 24));
00147 
00148     QCheckBox *showSplash = new QCheckBox;
00149     m_showSplash = prefs->getShowSplash();
00150     showSplash->setCheckState(m_showSplash ? Qt::Checked : Qt::Unchecked);
00151     connect(showSplash, SIGNAL(stateChanged(int)),
00152             this, SLOT(showSplashChanged(int)));
00153 
00154 #ifndef Q_WS_MAC
00155     QComboBox *bgMode = new QComboBox;
00156     int bg = prefs->getPropertyRangeAndValue("Background Mode", &min, &max,
00157                                              &deflt);
00158     m_backgroundMode = bg;
00159     for (i = min; i <= max; ++i) {
00160         bgMode->addItem(prefs->getPropertyValueLabel("Background Mode", i));
00161     }
00162     bgMode->setCurrentIndex(bg);
00163 
00164     connect(bgMode, SIGNAL(currentIndexChanged(int)),
00165             this, SLOT(backgroundModeChanged(int)));
00166 #endif
00167 
00168     QSpinBox *fontSize = new QSpinBox;
00169     int fs = prefs->getPropertyRangeAndValue("View Font Size", &min, &max,
00170                                              &deflt);
00171     m_viewFontSize = fs;
00172     fontSize->setMinimum(min);
00173     fontSize->setMaximum(max);
00174     fontSize->setSuffix(" pt");
00175     fontSize->setSingleStep(1);
00176     fontSize->setValue(fs);
00177 
00178     connect(fontSize, SIGNAL(valueChanged(int)),
00179             this, SLOT(viewFontSizeChanged(int)));
00180 
00181     // General tab
00182 
00183     QFrame *frame = new QFrame;
00184     
00185     QGridLayout *subgrid = new QGridLayout;
00186     frame->setLayout(subgrid);
00187 
00188     int row = 0;
00189 
00190     subgrid->addWidget(new QLabel(tr("%1:").arg(prefs->getPropertyLabel
00191                                                 ("Property Box Layout"))),
00192                        row, 0);
00193     subgrid->addWidget(propertyLayout, row++, 1, 1, 2);
00194 
00195 #ifndef Q_WS_MAC
00196     subgrid->addWidget(new QLabel(tr("%1:").arg(prefs->getPropertyLabel
00197                                                 ("Background Mode"))),
00198                        row, 0);
00199     subgrid->addWidget(bgMode, row++, 1, 1, 2);
00200 #endif
00201 
00202     subgrid->addWidget(new QLabel(tr("%1:").arg(prefs->getPropertyLabel
00203                                                 ("View Font Size"))),
00204                        row, 0);
00205     subgrid->addWidget(fontSize, row++, 1, 1, 2);
00206 
00207     subgrid->addWidget(new QLabel(tr("%1:").arg(prefs->getPropertyLabel
00208                                                 ("Show Splash Screen"))),
00209                        row, 0);
00210     subgrid->addWidget(showSplash, row++, 1, 1, 1);
00211 
00212     subgrid->addWidget(new QLabel(tr("%1:").arg(prefs->getPropertyLabel
00213                                                 ("Temporary Directory Root"))),
00214                        row, 0);
00215     subgrid->addWidget(m_tempDirRootEdit, row, 1, 1, 1);
00216     subgrid->addWidget(tempDirButton, row, 2, 1, 1);
00217     row++;
00218 
00219     subgrid->addWidget(new QLabel(tr("%1:").arg(prefs->getPropertyLabel
00220                                                 ("Resample On Load"))),
00221                        row, 0);
00222     subgrid->addWidget(resampleOnLoad, row++, 1, 1, 1);
00223 
00224     subgrid->addWidget(new QLabel(tr("%1:").arg(prefs->getPropertyLabel
00225                                                 ("Resample Quality"))),
00226                        row, 0);
00227     subgrid->addWidget(resampleQuality, row++, 1, 1, 2);
00228 
00229     subgrid->setRowStretch(row, 10);
00230     
00231     tab->addTab(frame, tr("&General"));
00232 
00233     // Analysis tab
00234 
00235     frame = new QFrame;
00236     subgrid = new QGridLayout;
00237     frame->setLayout(subgrid);
00238     row = 0;
00239 
00240     subgrid->addWidget(new QLabel(tr("%1:").arg(prefs->getPropertyLabel
00241                                                 ("Tuning Frequency"))),
00242                        row, 0);
00243     subgrid->addWidget(frequency, row++, 1, 1, 2);
00244 
00245     subgrid->addWidget(new QLabel(prefs->getPropertyLabel
00246                                   ("Spectrogram Smoothing")),
00247                        row, 0);
00248     subgrid->addWidget(smoothing, row++, 1, 1, 2);
00249 
00250     subgrid->addWidget(new QLabel(tr("%1:").arg(prefs->getPropertyLabel
00251                                                 ("Window Type"))),
00252                        row, 0);
00253     subgrid->addWidget(m_windowTypeSelector, row++, 1, 2, 2);
00254     subgrid->setRowStretch(row, 10);
00255     row++;
00256     
00257     subgrid->setRowStretch(row, 10);
00258     
00259     tab->addTab(frame, tr("&Analysis"));
00260 
00261     QDialogButtonBox *bb = new QDialogButtonBox(Qt::Horizontal);
00262     grid->addWidget(bb, 1, 0);
00263     
00264     QPushButton *ok = new QPushButton(tr("OK"));
00265     QPushButton *cancel = new QPushButton(tr("Cancel"));
00266     bb->addButton(ok, QDialogButtonBox::AcceptRole);
00267     bb->addButton(m_applyButton, QDialogButtonBox::ApplyRole);
00268     bb->addButton(cancel, QDialogButtonBox::RejectRole);
00269     connect(ok, SIGNAL(clicked()), this, SLOT(okClicked()));
00270     connect(m_applyButton, SIGNAL(clicked()), this, SLOT(applyClicked()));
00271     connect(cancel, SIGNAL(clicked()), this, SLOT(cancelClicked()));
00272 
00273     m_applyButton->setEnabled(false);
00274 }
00275 
00276 PreferencesDialog::~PreferencesDialog()
00277 {
00278     std::cerr << "PreferencesDialog::~PreferencesDialog()" << std::endl;
00279 }
00280 
00281 void
00282 PreferencesDialog::windowTypeChanged(WindowType type)
00283 {
00284     m_windowType = type;
00285     m_applyButton->setEnabled(true);
00286 }
00287 
00288 void
00289 PreferencesDialog::spectrogramSmoothingChanged(int smoothing)
00290 {
00291     m_spectrogramSmoothing = smoothing;
00292     m_applyButton->setEnabled(true);
00293 }
00294 
00295 void
00296 PreferencesDialog::propertyLayoutChanged(int layout)
00297 {
00298     m_propertyLayout = layout;
00299     m_applyButton->setEnabled(true);
00300 }
00301 
00302 void
00303 PreferencesDialog::tuningFrequencyChanged(double freq)
00304 {
00305     m_tuningFrequency = freq;
00306     m_applyButton->setEnabled(true);
00307 }
00308 
00309 void
00310 PreferencesDialog::resampleQualityChanged(int q)
00311 {
00312     m_resampleQuality = q;
00313     m_applyButton->setEnabled(true);
00314 }
00315 
00316 void
00317 PreferencesDialog::resampleOnLoadChanged(int state)
00318 {
00319     m_resampleOnLoad = (state == Qt::Checked);
00320     m_applyButton->setEnabled(true);
00321     m_changesOnRestart = true;
00322 }
00323 
00324 void
00325 PreferencesDialog::showSplashChanged(int state)
00326 {
00327     m_showSplash = (state == Qt::Checked);
00328     m_applyButton->setEnabled(true);
00329     m_changesOnRestart = true;
00330 }
00331 
00332 void
00333 PreferencesDialog::tempDirRootChanged(QString r)
00334 {
00335     m_tempDirRoot = r;
00336     m_applyButton->setEnabled(true);
00337 }
00338 
00339 void
00340 PreferencesDialog::tempDirButtonClicked()
00341 {
00342     QString dir = QFileDialog::getExistingDirectory
00343         (this, tr("Select a directory to create cache subdirectory in"),
00344          m_tempDirRoot);
00345     if (dir == "") return;
00346     m_tempDirRootEdit->setText(dir);
00347     tempDirRootChanged(dir);
00348     m_changesOnRestart = true;
00349 }
00350 
00351 void
00352 PreferencesDialog::backgroundModeChanged(int mode)
00353 {
00354     m_backgroundMode = mode;
00355     m_applyButton->setEnabled(true);
00356     m_changesOnRestart = true;
00357 }
00358 
00359 void
00360 PreferencesDialog::viewFontSizeChanged(int sz)
00361 {
00362     m_viewFontSize = sz;
00363     m_applyButton->setEnabled(true);
00364 }
00365 
00366 void
00367 PreferencesDialog::okClicked()
00368 {
00369     applyClicked();
00370     accept();
00371 }
00372 
00373 void
00374 PreferencesDialog::applyClicked()
00375 {
00376     Preferences *prefs = Preferences::getInstance();
00377     prefs->setWindowType(WindowType(m_windowType));
00378     prefs->setSpectrogramSmoothing(Preferences::SpectrogramSmoothing
00379                                    (m_spectrogramSmoothing));
00380     prefs->setPropertyBoxLayout(Preferences::PropertyBoxLayout
00381                                 (m_propertyLayout));
00382     prefs->setTuningFrequency(m_tuningFrequency);
00383     prefs->setResampleQuality(m_resampleQuality);
00384     prefs->setResampleOnLoad(m_resampleOnLoad);
00385     prefs->setShowSplash(m_showSplash);
00386     prefs->setTemporaryDirectoryRoot(m_tempDirRoot);
00387     prefs->setBackgroundMode(Preferences::BackgroundMode(m_backgroundMode));
00388     prefs->setViewFontSize(m_viewFontSize);
00389 
00390     m_applyButton->setEnabled(false);
00391 
00392     if (m_changesOnRestart) {
00393         QMessageBox::information(this, tr("Preferences"),
00394                                  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."));
00395         m_changesOnRestart = false;
00396     }
00397 }    
00398 
00399 void
00400 PreferencesDialog::cancelClicked()
00401 {
00402     reject();
00403 }
00404 
00405 void
00406 PreferencesDialog::applicationClosing(bool quickly)
00407 {
00408     if (quickly) {
00409         reject();
00410         return;
00411     }
00412 
00413     if (m_applyButton->isEnabled()) {
00414         int rv = QMessageBox::warning
00415             (this, tr("Preferences Changed"),
00416              tr("Some preferences have been changed but not applied.\n"
00417                 "Apply them before closing?"),
00418              QMessageBox::Apply | QMessageBox::Discard,
00419              QMessageBox::Discard);
00420         if (rv == QMessageBox::Apply) {
00421             applyClicked();
00422             accept();
00423         } else {
00424             reject();
00425         }
00426     } else {
00427         accept();
00428     }
00429 }
00430 

Generated on Wed Feb 20 15:45:27 2008 for SonicVisualiser by  doxygen 1.5.1