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: | Tag: | Revision:

root / main / PreferencesDialog.cpp

History | View | Annotate | Download (7.86 KB)

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

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

    
15
#include "PreferencesDialog.h"
16

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

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

    
38
PreferencesDialog::PreferencesDialog(QWidget *parent) :
39
    QDialog(parent),
40
    m_changesOnRestart(false)
41
{
42
    setWindowTitle(tr("Sonic Lineup: 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
    m_tuningFrequency = prefs->getTuningFrequency();
63

    
64
    QDoubleSpinBox *frequency = new QDoubleSpinBox;
65
    frequency->setMinimum(100.0);
66
    frequency->setMaximum(5000.0);
67
    frequency->setSuffix(" Hz");
68
    frequency->setSingleStep(1);
69
    frequency->setValue(m_tuningFrequency);
70
    frequency->setDecimals(2);
71

    
72
    connect(frequency, SIGNAL(valueChanged(double)),
73
            this, SLOT(tuningFrequencyChanged(double)));
74

    
75
    QSettings settings;
76
    settings.beginGroup("Preferences");
77
    m_useAlignmentProgram = settings.value("use-external-alignment", false).toBool();
78
    QString program = settings.value("external-alignment-program", "").toString();
79
    m_alignmentProgram = program;
80
    program.replace("$HOME", tr("<home directory>"));
81
    settings.endGroup();
82

    
83
    m_alignmentProgramToggle = new QCheckBox();
84
    connect(m_alignmentProgramToggle, SIGNAL(clicked()),
85
            this, SLOT(alignmentProgramToggleClicked()));
86
    m_alignmentProgramToggle->setChecked(m_useAlignmentProgram);
87
    m_alignmentProgramEdit = new QLineEdit;
88
    m_alignmentProgramEdit->setText(program);
89
    m_alignmentProgramEdit->setReadOnly(true);
90
    m_alignmentProgramEdit->setEnabled(m_useAlignmentProgram);
91
    m_alignmentProgramButton = new QPushButton();
92
    m_alignmentProgramButton->setIcon(IconLoader().load("fileopen"));
93
    connect(m_alignmentProgramButton, SIGNAL(clicked()),
94
            this, SLOT(alignmentProgramButtonClicked()));
95
    m_alignmentProgramButton->setFixedSize(QSize(24, 24));
96
    m_alignmentProgramButton->setEnabled(m_useAlignmentProgram);
97

    
98
    m_normaliseAudio = prefs->getNormaliseAudio();
99
    m_normaliseAudioToggle = new QCheckBox();
100
    m_normaliseAudioToggle->setChecked(m_normaliseAudio);
101
    connect(m_normaliseAudioToggle, SIGNAL(clicked()),
102
            this, SLOT(normaliseAudioToggleClicked()));
103
    
104
    QFrame *frame = new QFrame;
105
    
106
    QGridLayout *subgrid = new QGridLayout;
107
    frame->setLayout(subgrid);
108

    
109
    int row = 0;
110

    
111
    subgrid->addWidget(new QLabel(tr("%1:").arg(prefs->getPropertyLabel
112
                                                ("Tuning Frequency"))),
113
                       row, 0);
114
    subgrid->addWidget(frequency, row++, 2, 1, 1);
115

    
116
    subgrid->addWidget(new QLabel(tr("%1:").arg(tr("Use external alignment program"))),
117
                       row, 0);
118
    subgrid->addWidget(m_alignmentProgramToggle, row, 1, 1, 1);
119
    subgrid->addWidget(m_alignmentProgramEdit, row, 2, 1, 1);
120
    subgrid->addWidget(m_alignmentProgramButton, row, 3, 1, 1);
121
    row++;
122

    
123
    subgrid->addWidget(new QLabel(tr("%1:").arg(tr("Normalise audio"))), row, 0);
124
    subgrid->addWidget(m_normaliseAudioToggle, row, 1, 1, 1);
125
    row++;
126
    
127
    subgrid->setColumnStretch(2, 10);
128
    subgrid->setRowStretch(row, 10);
129
    
130
    tab->addTab(frame, tr("&General"));
131

    
132
    QDialogButtonBox *bb = new QDialogButtonBox(Qt::Horizontal);
133
    grid->addWidget(bb, 1, 0);
134
    
135
    QPushButton *ok = new QPushButton(tr("OK"));
136
    QPushButton *cancel = new QPushButton(tr("Cancel"));
137
    bb->addButton(ok, QDialogButtonBox::AcceptRole);
138
    bb->addButton(m_applyButton, QDialogButtonBox::ApplyRole);
139
    bb->addButton(cancel, QDialogButtonBox::RejectRole);
140
    connect(ok, SIGNAL(clicked()), this, SLOT(okClicked()));
141
    connect(m_applyButton, SIGNAL(clicked()), this, SLOT(applyClicked()));
142
    connect(cancel, SIGNAL(clicked()), this, SLOT(cancelClicked()));
143

    
144
    m_applyButton->setEnabled(false);
145
}
146

    
147
PreferencesDialog::~PreferencesDialog()
148
{
149
    std::cerr << "PreferencesDialog::~PreferencesDialog()" << std::endl;
150
}
151

    
152
void
153
PreferencesDialog::tuningFrequencyChanged(double freq)
154
{
155
    m_tuningFrequency = freq;
156
    m_applyButton->setEnabled(true);
157
}
158

    
159
void
160
PreferencesDialog::alignmentProgramToggleClicked()
161
{
162
    m_useAlignmentProgram = m_alignmentProgramToggle->isChecked();
163
    m_alignmentProgramEdit->setEnabled(m_useAlignmentProgram);
164
    m_alignmentProgramButton->setEnabled(m_useAlignmentProgram);
165
    m_applyButton->setEnabled(true);
166
    m_changesOnRestart = true;
167
}
168
    
169
void
170
PreferencesDialog::alignmentProgramButtonClicked()
171
{
172
    QString file = QFileDialog::getOpenFileName
173
        (this, tr("Select an external alignment program to run"),
174
         m_alignmentProgram);
175
    if (file == "") return;
176
    m_alignmentProgram = file;
177
    file.replace("$HOME", tr("<home directory>"));
178
    m_alignmentProgramEdit->setText(file);
179
    m_applyButton->setEnabled(true);
180
    m_changesOnRestart = true;
181
}
182

    
183
void
184
PreferencesDialog::normaliseAudioToggleClicked()
185
{
186
    m_normaliseAudio = m_normaliseAudioToggle->isChecked();
187
    m_applyButton->setEnabled(true);
188
    m_changesOnRestart = true;
189
}
190

    
191
void
192
PreferencesDialog::okClicked()
193
{
194
    applyClicked();
195
    accept();
196
}
197

    
198
void
199
PreferencesDialog::applyClicked()
200
{
201
    Preferences *prefs = Preferences::getInstance();
202
    prefs->setTuningFrequency(m_tuningFrequency);
203
    prefs->setNormaliseAudio(m_normaliseAudio);
204

    
205
    QSettings settings;
206
    settings.beginGroup("Preferences");
207
    settings.setValue("use-external-alignment", m_useAlignmentProgram);
208
    settings.setValue("external-alignment-program", m_alignmentProgram);
209
    settings.endGroup();
210
    
211
    m_applyButton->setEnabled(false);
212

    
213
    if (m_changesOnRestart) {
214
        QMessageBox::information(this, tr("Preferences"),
215
                                 tr("One or more of the application preferences you have changed may not take full effect until Sonic Lineup is restarted.\nPlease exit and restart the application now if you want these changes to take effect immediately."));
216
        m_changesOnRestart = false;
217
    }
218
}    
219

    
220
void
221
PreferencesDialog::cancelClicked()
222
{
223
    reject();
224
}
225

    
226
void
227
PreferencesDialog::applicationClosing(bool quickly)
228
{
229
    if (quickly) {
230
        reject();
231
        return;
232
    }
233

    
234
    if (m_applyButton->isEnabled()) {
235
        int rv = QMessageBox::warning
236
            (this, tr("Preferences Changed"),
237
             tr("Some preferences have been changed but not applied.\n"
238
                "Apply them before closing?"),
239
             QMessageBox::Apply | QMessageBox::Discard,
240
             QMessageBox::Discard);
241
        if (rv == QMessageBox::Apply) {
242
            applyClicked();
243
            accept();
244
        } else {
245
            reject();
246
        }
247
    } else {
248
        accept();
249
    }
250
}
251