To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Revision:

root / main / PreferencesDialog.cpp

History | View | Annotate | Download (11.6 KB)

1
/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
2

    
3
/*
4
    Sonic Visualiser
5
    An audio file viewer and annotation editor.
6
    Centre for Digital Music, Queen Mary, University of London.
7
    This file copyright 2006 Chris Cannam.
8
    
9
    This program is free software; you can redistribute it and/or
10
    modify it under the terms of the GNU General Public License as
11
    published by the Free Software Foundation; either version 2 of the
12
    License, or (at your option) any later version.  See the file
13
    COPYING included with this distribution for more information.
14
*/
15

    
16
#include "PreferencesDialog.h"
17

    
18
#include <QGridLayout>
19
#include <QComboBox>
20
#include <QCheckBox>
21
#include <QGroupBox>
22
#include <QDoubleSpinBox>
23
#include <QLabel>
24
#include <QPushButton>
25
#include <QHBoxLayout>
26
#include <QString>
27
#include <QDialogButtonBox>
28
#include <QMessageBox>
29
#include <QTabWidget>
30
#include <QLineEdit>
31
#include <QFileDialog>
32
#include <QMessageBox>
33

    
34
#include "widgets/WindowTypeSelector.h"
35
#include "widgets/IconLoader.h"
36
#include "base/Preferences.h"
37

    
38
PreferencesDialog::PreferencesDialog(QWidget *parent, Qt::WFlags flags) :
39
    QDialog(parent, flags),
40
    m_changesOnRestart(false)
41
{
42
    setWindowTitle(tr("Sonic Visualiser: Application Preferences"));
43

    
44
    Preferences *prefs = Preferences::getInstance();
45

    
46
    QGridLayout *grid = new QGridLayout;
47
    setLayout(grid);
48

    
49
    QTabWidget *tab = new QTabWidget;
50
    grid->addWidget(tab, 0, 0);
51
    
52
    tab->setTabPosition(QTabWidget::North);
53

    
54
    // Create this first, as slots that get called from the ctor will
55
    // refer to it
56
    m_applyButton = new QPushButton(tr("Apply"));
57

    
58
    // Create all the preference widgets first, then create the
59
    // individual tab widgets and place the preferences in their
60
    // appropriate places in one go afterwards
61

    
62
    int min, max, deflt, i;
63

    
64
    m_windowType = WindowType(prefs->getPropertyRangeAndValue
65
                              ("Window Type", &min, &max, &deflt));
66
    m_windowTypeSelector = new WindowTypeSelector(m_windowType);
67

    
68
    connect(m_windowTypeSelector, SIGNAL(windowTypeChanged(WindowType)),
69
            this, SLOT(windowTypeChanged(WindowType)));
70

    
71
    QComboBox *smoothing = new QComboBox;
72
    
73
    int sm = prefs->getPropertyRangeAndValue("Spectrogram Smoothing", &min, &max,
74
                                             &deflt);
75
    m_spectrogramSmoothing = sm;
76

    
77
    for (i = min; i <= max; ++i) {
78
        smoothing->addItem(prefs->getPropertyValueLabel("Spectrogram Smoothing", i));
79
    }
80

    
81
    smoothing->setCurrentIndex(sm);
82

    
83
    connect(smoothing, SIGNAL(currentIndexChanged(int)),
84
            this, SLOT(spectrogramSmoothingChanged(int)));
85

    
86
    QComboBox *propertyLayout = new QComboBox;
87
    int pl = prefs->getPropertyRangeAndValue("Property Box Layout", &min, &max,
88
                                         &deflt);
89
    m_propertyLayout = pl;
90

    
91
    for (i = min; i <= max; ++i) {
92
        propertyLayout->addItem(prefs->getPropertyValueLabel("Property Box Layout", i));
93
    }
94

    
95
    propertyLayout->setCurrentIndex(pl);
96

    
97
    connect(propertyLayout, SIGNAL(currentIndexChanged(int)),
98
            this, SLOT(propertyLayoutChanged(int)));
99

    
100
    m_tuningFrequency = prefs->getTuningFrequency();
101

    
102
    QDoubleSpinBox *frequency = new QDoubleSpinBox;
103
    frequency->setMinimum(100.0);
104
    frequency->setMaximum(5000.0);
105
    frequency->setSuffix(" Hz");
106
    frequency->setSingleStep(1);
107
    frequency->setValue(m_tuningFrequency);
108
    frequency->setDecimals(2);
109

    
110
    connect(frequency, SIGNAL(valueChanged(double)),
111
            this, SLOT(tuningFrequencyChanged(double)));
112

    
113
    QComboBox *resampleQuality = new QComboBox;
114

    
115
    int rsq = prefs->getPropertyRangeAndValue("Resample Quality", &min, &max,
116
                                              &deflt);
117
    m_resampleQuality = rsq;
118

    
119
    for (i = min; i <= max; ++i) {
120
        resampleQuality->addItem(prefs->getPropertyValueLabel("Resample Quality", i));
121
    }
122

    
123
    resampleQuality->setCurrentIndex(rsq);
124

    
125
    connect(resampleQuality, SIGNAL(currentIndexChanged(int)),
126
            this, SLOT(resampleQualityChanged(int)));
127

    
128
    QCheckBox *resampleOnLoad = new QCheckBox;
129
    m_resampleOnLoad = prefs->getResampleOnLoad();
130
    resampleOnLoad->setCheckState(m_resampleOnLoad ? Qt::Checked :
131
                                  Qt::Unchecked);
132
    connect(resampleOnLoad, SIGNAL(stateChanged(int)),
133
            this, SLOT(resampleOnLoadChanged(int)));
134

    
135
    m_tempDirRootEdit = new QLineEdit;
136
    QString dir = prefs->getTemporaryDirectoryRoot();
137
    m_tempDirRoot = dir;
138
    dir.replace("$HOME", tr("<home directory>"));
139
    m_tempDirRootEdit->setText(dir);
140
    m_tempDirRootEdit->setReadOnly(true);
141
    QPushButton *tempDirButton = new QPushButton;
142
    tempDirButton->setIcon(IconLoader().load("fileopen"));
143
    connect(tempDirButton, SIGNAL(clicked()),
144
            this, SLOT(tempDirButtonClicked()));
145
    tempDirButton->setFixedSize(QSize(24, 24));
146

    
147
    QComboBox *bgMode = new QComboBox;
148
    int bg = prefs->getPropertyRangeAndValue("Background Mode", &min, &max,
149
                                             &deflt);
150
    m_backgroundMode = bg;
151
    for (i = min; i <= max; ++i) {
152
        bgMode->addItem(prefs->getPropertyValueLabel("Background Mode", i));
153
    }
154
    bgMode->setCurrentIndex(bg);
155

    
156
    connect(bgMode, SIGNAL(currentIndexChanged(int)),
157
            this, SLOT(backgroundModeChanged(int)));
158

    
159
    // General tab
160

    
161
    QFrame *frame = new QFrame;
162
    
163
    QGridLayout *subgrid = new QGridLayout;
164
    frame->setLayout(subgrid);
165

    
166
    int row = 0;
167

    
168
    subgrid->addWidget(new QLabel(tr("%1:").arg(prefs->getPropertyLabel
169
                                                ("Property Box Layout"))),
170
                       row, 0);
171
    subgrid->addWidget(propertyLayout, row++, 1, 1, 2);
172

    
173
    subgrid->addWidget(new QLabel(tr("%1:").arg(prefs->getPropertyLabel
174
                                                ("Background Mode"))),
175
                       row, 0);
176
    subgrid->addWidget(bgMode, row++, 1, 1, 2);
177

    
178
    subgrid->addWidget(new QLabel(tr("%1:").arg(prefs->getPropertyLabel
179
                                                ("Resample On Load"))),
180
                       row, 0);
181
    subgrid->addWidget(resampleOnLoad, row++, 1, 1, 1);
182

    
183
    subgrid->addWidget(new QLabel(tr("%1:").arg(prefs->getPropertyLabel
184
                                                ("Resample Quality"))),
185
                       row, 0);
186
    subgrid->addWidget(resampleQuality, row++, 1, 1, 2);
187

    
188
    subgrid->addWidget(new QLabel(tr("%1:").arg(prefs->getPropertyLabel
189
                                                ("Temporary Directory Root"))),
190
                       row, 0);
191
    subgrid->addWidget(m_tempDirRootEdit, row, 1, 1, 1);
192
    subgrid->addWidget(tempDirButton, row, 2, 1, 1);
193
    row++;
194

    
195
    subgrid->setRowStretch(row, 10);
196
    
197
    tab->addTab(frame, tr("&General"));
198

    
199
    // Analysis tab
200

    
201
    frame = new QFrame;
202
    subgrid = new QGridLayout;
203
    frame->setLayout(subgrid);
204
    row = 0;
205

    
206
    subgrid->addWidget(new QLabel(tr("%1:").arg(prefs->getPropertyLabel
207
                                                ("Tuning Frequency"))),
208
                       row, 0);
209
    subgrid->addWidget(frequency, row++, 1, 1, 2);
210

    
211
    subgrid->addWidget(new QLabel(prefs->getPropertyLabel
212
                                  ("Spectrogram Smoothing")),
213
                       row, 0);
214
    subgrid->addWidget(smoothing, row++, 1, 1, 2);
215

    
216
    subgrid->addWidget(new QLabel(tr("%1:").arg(prefs->getPropertyLabel
217
                                                ("Window Type"))),
218
                       row, 0);
219
    subgrid->addWidget(m_windowTypeSelector, row++, 1, 2, 2);
220
    subgrid->setRowStretch(row, 10);
221
    row++;
222
    
223
    subgrid->setRowStretch(row, 10);
224
    
225
    tab->addTab(frame, tr("&Analysis"));
226

    
227
    QDialogButtonBox *bb = new QDialogButtonBox(Qt::Horizontal);
228
    grid->addWidget(bb, 1, 0);
229
    
230
    QPushButton *ok = new QPushButton(tr("OK"));
231
    QPushButton *cancel = new QPushButton(tr("Cancel"));
232
    bb->addButton(ok, QDialogButtonBox::AcceptRole);
233
    bb->addButton(m_applyButton, QDialogButtonBox::ApplyRole);
234
    bb->addButton(cancel, QDialogButtonBox::RejectRole);
235
    connect(ok, SIGNAL(clicked()), this, SLOT(okClicked()));
236
    connect(m_applyButton, SIGNAL(clicked()), this, SLOT(applyClicked()));
237
    connect(cancel, SIGNAL(clicked()), this, SLOT(cancelClicked()));
238

    
239
    m_applyButton->setEnabled(false);
240
}
241

    
242
PreferencesDialog::~PreferencesDialog()
243
{
244
    std::cerr << "PreferencesDialog::~PreferencesDialog()" << std::endl;
245
}
246

    
247
void
248
PreferencesDialog::windowTypeChanged(WindowType type)
249
{
250
    m_windowType = type;
251
    m_applyButton->setEnabled(true);
252
}
253

    
254
void
255
PreferencesDialog::spectrogramSmoothingChanged(int smoothing)
256
{
257
    m_spectrogramSmoothing = smoothing;
258
    m_applyButton->setEnabled(true);
259
}
260

    
261
void
262
PreferencesDialog::propertyLayoutChanged(int layout)
263
{
264
    m_propertyLayout = layout;
265
    m_applyButton->setEnabled(true);
266
}
267

    
268
void
269
PreferencesDialog::tuningFrequencyChanged(double freq)
270
{
271
    m_tuningFrequency = freq;
272
    m_applyButton->setEnabled(true);
273
}
274

    
275
void
276
PreferencesDialog::resampleQualityChanged(int q)
277
{
278
    m_resampleQuality = q;
279
    m_applyButton->setEnabled(true);
280
}
281

    
282
void
283
PreferencesDialog::resampleOnLoadChanged(int state)
284
{
285
    m_resampleOnLoad = (state == Qt::Checked);
286
    m_applyButton->setEnabled(true);
287
    m_changesOnRestart = true;
288
}
289

    
290
void
291
PreferencesDialog::tempDirRootChanged(QString r)
292
{
293
    m_tempDirRoot = r;
294
    m_applyButton->setEnabled(true);
295
}
296

    
297
void
298
PreferencesDialog::tempDirButtonClicked()
299
{
300
    QString dir = QFileDialog::getExistingDirectory
301
        (this, tr("Select a directory to create cache subdirectory in"),
302
         m_tempDirRoot);
303
    if (dir == "") return;
304
    m_tempDirRootEdit->setText(dir);
305
    tempDirRootChanged(dir);
306
    m_changesOnRestart = true;
307
}
308

    
309
void
310
PreferencesDialog::backgroundModeChanged(int mode)
311
{
312
    m_backgroundMode = mode;
313
    m_applyButton->setEnabled(true);
314
    m_changesOnRestart = true;
315
}
316

    
317
void
318
PreferencesDialog::okClicked()
319
{
320
    applyClicked();
321
    accept();
322
}
323

    
324
void
325
PreferencesDialog::applyClicked()
326
{
327
    Preferences *prefs = Preferences::getInstance();
328
    prefs->setWindowType(WindowType(m_windowType));
329
    prefs->setSpectrogramSmoothing(Preferences::SpectrogramSmoothing
330
                                   (m_spectrogramSmoothing));
331
    prefs->setPropertyBoxLayout(Preferences::PropertyBoxLayout
332
                                (m_propertyLayout));
333
    prefs->setTuningFrequency(m_tuningFrequency);
334
    prefs->setResampleQuality(m_resampleQuality);
335
    prefs->setResampleOnLoad(m_resampleOnLoad);
336
    prefs->setTemporaryDirectoryRoot(m_tempDirRoot);
337
    prefs->setBackgroundMode(Preferences::BackgroundMode(m_backgroundMode));
338

    
339
    m_applyButton->setEnabled(false);
340

    
341
    if (m_changesOnRestart) {
342
        QMessageBox::information(this, tr("Preferences"),
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."));
344
        m_changesOnRestart = false;
345
    }
346
}    
347

    
348
void
349
PreferencesDialog::cancelClicked()
350
{
351
    reject();
352
}
353

    
354
void
355
PreferencesDialog::applicationClosing(bool quickly)
356
{
357
    if (quickly) {
358
        reject();
359
        return;
360
    }
361

    
362
    if (m_applyButton->isEnabled()) {
363
        int rv = QMessageBox::warning
364
            (this, tr("Preferences Changed"),
365
             tr("Some preferences have been changed but not applied.\n"
366
                "Apply them before closing?"),
367
             QMessageBox::Apply | QMessageBox::Discard,
368
             QMessageBox::Discard);
369
        if (rv == QMessageBox::Apply) {
370
            applyClicked();
371
            accept();
372
        } else {
373
            reject();
374
        }
375
    } else {
376
        accept();
377
    }
378
}
379