summaryrefslogtreecommitdiffstats
path: root/src/preferences/dialog/dlgprefwaveform.cpp
blob: 1e5fa917d6b9125b97e1afe2df469cbb68d08bcb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#include "preferences/dialog/dlgprefwaveform.h"

#include "mixxx.h"
#include "library/library.h"
#include "library/dao/analysisdao.h"
#include "preferences/waveformsettings.h"
#include "waveform/waveformwidgetfactory.h"
#include "waveform/renderers/waveformwidgetrenderer.h"
#include "util/db/dbconnectionpooled.h"

DlgPrefWaveform::DlgPrefWaveform(QWidget* pParent, MixxxMainWindow* pMixxx,
                                 UserSettingsPointer pConfig, Library* pLibrary)
        : DlgPreferencePage(pParent),
          m_pConfig(pConfig),
          m_pLibrary(pLibrary),
          m_pMixxx(pMixxx) {
    setupUi(this);

    // Waveform overview init
    waveformOverviewComboBox->addItem(tr("Filtered")); // "0"
    waveformOverviewComboBox->addItem(tr("HSV")); // "1"
    waveformOverviewComboBox->addItem(tr("RGB")); // "2"

    // Populate waveform options.
    WaveformWidgetFactory* factory = WaveformWidgetFactory::instance();
    QVector<WaveformWidgetAbstractHandle> handles = factory->getAvailableTypes();
    for (int i = 0; i < handles.size(); ++i) {
        waveformTypeComboBox->addItem(handles[i].getDisplayName(),
                                      handles[i].getType());
    }

    // Populate zoom options.
    for (int i = WaveformWidgetRenderer::s_waveformMinZoom;
         i <= WaveformWidgetRenderer::s_waveformMaxZoom; i++) {
        defaultZoomComboBox->addItem(QString::number(100/double(i), 'f', 1) + " %");
    }

    // The GUI is not fully setup so connecting signals before calling
    // slotUpdate can generate rebootMixxxView calls.
    // TODO(XXX): Improve this awkwardness.
    slotUpdate();
    connect(frameRateSpinBox,
            SIGNAL(valueChanged(int)),
            this,
            SLOT(slotSetFrameRate(int)));
    connect(endOfTrackWarningTimeSpinBox,
            SIGNAL(valueChanged(int)),
            this,
            SLOT(slotSetWaveformEndRender(int)));
    connect(beatGridAlphaSpinBox,
            SIGNAL(valueChanged(int)),
            this,
            SLOT(slotSetBeatGridAlpha(int)));
    connect(frameRateSlider,
            SIGNAL(valueChanged(int)),
            frameRateSpinBox,
            SLOT(setValue(int)));
    connect(frameRateSpinBox,
            SIGNAL(valueChanged(int)),
            frameRateSlider,
            SLOT(setValue(int)));
    connect(endOfTrackWarningTimeSlider,
            SIGNAL(valueChanged(int)),
            endOfTrackWarningTimeSpinBox,
            SLOT(setValue(int)));
    connect(endOfTrackWarningTimeSpinBox,
            SIGNAL(valueChanged(int)),
            endOfTrackWarningTimeSlider,
            SLOT(setValue(int)));
    connect(beatGridAlphaSlider,
            SIGNAL(valueChanged(int)),
            beatGridAlphaSpinBox,
            SLOT(setValue(int)));
    connect(beatGridAlphaSpinBox,
            SIGNAL(valueChanged(int)),
            beatGridAlphaSlider,
            SLOT(setValue(int)));

    connect(waveformTypeComboBox,
            SIGNAL(currentIndexChanged(int)),
            this,
            SLOT(slotSetWaveformType(int)));
    connect(defaultZoomComboBox,
            SIGNAL(currentIndexChanged(int)),
            this,
            SLOT(slotSetDefaultZoom(int)));
    connect(synchronizeZoomCheckBox,
            SIGNAL(clicked(bool)),
            this,
            SLOT(slotSetZoomSynchronization(bool)));
    connect(allVisualGain,
            SIGNAL(valueChanged(double)),
            this,
            SLOT(slotSetVisualGainAll(double)));
    connect(lowVisualGain,
            SIGNAL(valueChanged(double)),
            this,
            SLOT(slotSetVisualGainLow(double)));
    connect(midVisualGain,
            SIGNAL(valueChanged(double)),
            this,
            SLOT(slotSetVisualGainMid(double)));
    connect(highVisualGain,
            SIGNAL(valueChanged(double)),
            this,
            SLOT(slotSetVisualGainHigh(double)));
    connect(normalizeOverviewCheckBox,
            SIGNAL(toggled(bool)),
            this,
            SLOT(slotSetNormalizeOverview(bool)));
    connect(factory,
            SIGNAL(waveformMeasured(float, int)),
            this,
            SLOT(slotWaveformMeasured(float, int)));
    connect(waveformOverviewComboBox,
            SIGNAL(currentIndexChanged(int)),
            this,
            SLOT(slotSetWaveformOverviewType(int)));
    connect(clearCachedWaveforms,
            SIGNAL(clicked()),
            this,
            SLOT(slotClearCachedWaveforms()));
    connect(playMarkerPositionSlider,
            SIGNAL(valueChanged(int)),
            this,
            SLOT(slotSetPlayMarkerPosition(int)));
}

DlgPrefWaveform::~DlgPrefWaveform() {
}

void DlgPrefWaveform::slotUpdate() {
    WaveformWidgetFactory* factory = WaveformWidgetFactory::instance();

    if (factory->isOpenGlAvailable() || factory->isOpenGlesAvailable()) {
        openGlStatusIcon->setText(factory->getOpenGLVersion());
    } else {
        openGlStatusIcon->setText(tr("OpenGL not available") + ": " + factory->getOpenGLVersion());
    }

    WaveformWidgetType::Type currentType = factory->getType();
    int currentIndex = waveformTypeComboBox->findData(currentType);
    if (currentIndex != -1 && waveformTypeComboBox->currentIndex() != currentIndex) {
        waveformTypeComboBox->setCurrentIndex(currentIndex);
    }

    frameRateSpinBox->setValue(factory->getFrameRate());
    frameRateSlider->setValue(factory->getFrameRate());
    endOfTrackWarningTimeSpinBox->setValue(factory->getEndOfTrackWarningTime());
    endOfTrackWarningTimeSlider->setValue(factory->getEndOfTrackWarningTime());
    synchronizeZoomCheckBox->setChecked(factory->isZoomSync());
    allVisualGain->setValue(factory->getVisualGain(WaveformWidgetFactory::All));
    lowVisualGain->setValue(factory->getVisualGain(WaveformWidgetFactory::Low));
    midVisualGain->setValue(factory->getVisualGain(WaveformWidgetFactory::Mid));
    highVisualGain->setValue(factory->getVisualGain(WaveformWidgetFactory::High));
    normalizeOverviewCheckBox->setChecked(factory->isOverviewNormalized());
    // Round zoom to int to get a default zoom index.
    defaultZoomComboBox->setCurrentIndex(static_cast<int>(factory->getDefaultZoom()) - 1);
    playMarkerPositionSlider->setValue(factory->getPlayMarkerPosition() * 100);
    beatGridAlphaSpinBox->setValue(factory->beatGridAlpha());
    beatGridAlphaSlider->setValue(factory->beatGridAlpha());

    // By default we set RGB woverview = "2"
    int overviewType = m_pConfig->getValue(
            ConfigKey("[Waveform]","WaveformOverviewType"), 2);
    if (overviewType != waveformOverviewComboBox->currentIndex()) {
        waveformOverviewComboBox->setCurrentIndex(overviewType);
    }

    WaveformSettings waveformSettings(m_pConfig);
    enableWaveformCaching->setChecked(waveformSettings.waveformCachingEnabled());
    enableWaveformGenerationWithAnalysis->setChecked(
        waveformSettings.waveformGenerationWithAnalysisEnabled());
    calculateCachedWaveformDiskUsage();
}

void DlgPrefWaveform::slotApply() {
    WaveformSettings waveformSettings(m_pConfig);
    waveformSettings.setWaveformCachingEnabled(enableWaveformCaching->isChecked());
    waveformSettings.setWaveformGenerationWithAnalysisEnabled(
        enableWaveformGenerationWithAnalysis->isChecked());
}

void DlgPrefWaveform::slotResetToDefaults() {
    WaveformWidgetFactory* factory = WaveformWidgetFactory::instance();

    // Get the default we ought to use based on whether the user has OpenGL or
    // not.
    WaveformWidgetType::Type defaultType = factory->autoChooseWidgetType();
    int defaultIndex = waveformTypeComboBox->findData(defaultType);
    if (defaultIndex != -1 && waveformTypeComboBox->currentIndex() != defaultIndex) {
        waveformTypeComboBox->setCurrentIndex(defaultIndex);
    }

    allVisualGain->setValue(1.0);
    lowVisualGain->setValue(1.0);
    midVisualGain->setValue(1.0);
    highVisualGain->setValue(1.0);

    // Default zoom level is 3 in WaveformWidgetFactory.
    defaultZoomComboBox->setCurrentIndex(3 + 1);

    // Don't synchronize zoom by default.
    synchronizeZoomCheckBox->setChecked(false);

    // RGB overview.
    waveformOverviewComboBox->setCurrentIndex(2);

    // Don't normalize overview.
    normalizeOverviewCheckBox->setChecked(false);

    // 30FPS is the default
    frameRateSlider->setValue(30);
    endOfTrackWarningTimeSlider->setValue(30);

    // Waveform caching enabled.
    enableWaveformCaching->setChecked(true);
    enableWaveformGenerationWithAnalysis->setChecked(false);

    // Beat grid alpha default is 90
    beatGridAlphaSlider->setValue(90);
    beatGridAlphaSpinBox->setValue(90);

    // 50 (center) is default
    playMarkerPositionSlider->setValue(50);
}

void DlgPrefWaveform::slotSetFrameRate(int frameRate) {
    WaveformWidgetFactory::instance()->setFrameRate(frameRate);
}

void DlgPrefWaveform::slotSetWaveformEndRender(int endTime) {
    WaveformWidgetFactory::instance()->setEndOfTrackWarningTime(endTime);
}

void DlgPrefWaveform::slotSetWaveformType(int index) {
    // Ignore sets for -1 since this happens when we clear the combobox.
    if (index < 0) {
        return;
    }
    WaveformWidgetFactory::instance()->setWidgetTypeFromHandle(index);
#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0) && defined __WINDOWS__
    // This regenerates the waveforms twice because of a bug found on Windows
    // where the first one fails.
    // The problem is that the window of the widget thinks that it is not exposed.
    // (https://doc.qt.io/qt-5/qwindow.html#exposeEvent )
    // TODO: Remove this when it has been fixed upstream.
    WaveformWidgetFactory::instance()->setWidgetTypeFromHandle(index, true);
#endif
}

void DlgPrefWaveform::slotSetWaveformOverviewType(int index) {
    m_pConfig->set(ConfigKey("[Waveform]","WaveformOverviewType"), ConfigValue(index));
    m_pMixxx->rebootMixxxView();
}

void DlgPrefWaveform::slotSetDefaultZoom(int index