summaryrefslogtreecommitdiffstats
path: root/src/gui/widgets/QuantizeParameters.cpp
blob: 1cb2a5849ae609f767c4874216a76c8a46a9539f (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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */

/*
    Rosegarden
    A MIDI and audio sequencer and musical notation editor.
 
    This program is Copyright 2000-2008
        Guillaume Laurent   <[email protected]>,
        Chris Cannam        <[email protected]>,
        Richard Bown        <[email protected]>
 
    The moral rights of Guillaume Laurent, Chris Cannam, and Richard
    Bown to claim authorship of this work have been asserted.
 
    Other copyrights also apply to some parts of this work.  Please
    see the AUTHORS file and individual file headers for details.
 
    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License as
    published by the Free Software Foundation; either version 2 of the
    License, or (at your option) any later version.  See the file
    COPYING included with this distribution for more information.
*/


#include "QuantizeParameters.h"
#include <tqlayout.h>
#include <kapplication.h>

#include <klocale.h>
#include "base/NotationTypes.h"
#include "base/Quantizer.h"
#include "base/BasicQuantizer.h"
#include "base/LegatoQuantizer.h"
#include "base/NotationQuantizer.h"
#include "gui/editors/notation/NotationStrings.h"
#include "gui/editors/notation/NotePixmapFactory.h"
#include <kcombobox.h>
#include <kconfig.h>
#include <tqcheckbox.h>
#include <tqframe.h>
#include <tqgroupbox.h>
#include <tqhbox.h>
#include <tqlabel.h>
#include <tqobject.h>
#include <tqpixmap.h>
#include <tqpushbutton.h>
#include <tqstring.h>
#include <tqwidget.h>


namespace Rosegarden
{

QuantizeParameters::QuantizeParameters(TQWidget *parent,
                                       QuantizerType defaultQuantizer,
                                       bool showNotationOption,
                                       bool showAdvancedButton,
                                       TQString configCategory,
                                       TQString preamble) :
        TQFrame(parent),
        m_configCategory(configCategory),
        m_standardQuantizations
        (BasicQuantizer::getStandardQuantizations())
{
    m_mainLayout = new TQGridLayout(this,
                                   preamble ? 3 : 4, 2,
                                   preamble ? 10 : 0,
                                   preamble ? 5 : 4);

    int zero = 0;
    if (preamble) {
        TQLabel *label = new TQLabel(preamble, this);
        label->setAlignment(Qt::WordBreak);
        m_mainLayout->addMultiCellWidget(label, 0, 0, 0, 1);
        zero = 1;
    }

    TQGroupBox *quantizerBox = new QGroupBox
                              (1, Horizontal, i18n("Quantizer"), this);

    m_mainLayout->addWidget(quantizerBox, zero, 0);
    TQFrame *typeFrame = new TQFrame(quantizerBox);

    TQGridLayout *layout = new TQGridLayout(typeFrame, 2, 2, 5, 3);
    layout->addWidget(new TQLabel(i18n("Quantizer type:"), typeFrame), 0, 0);
    m_typeCombo = new KComboBox(typeFrame);
    m_typeCombo->insertItem(i18n("Grid quantizer"));
    m_typeCombo->insertItem(i18n("Legato quantizer"));
    m_typeCombo->insertItem(i18n("Heuristic notation quantizer"));
    layout->addWidget(m_typeCombo, 0, 1);

    m_notationTarget = new QCheckBox
                       (i18n("Quantize for notation only (leave performance unchanged)"),
                        typeFrame);
    layout->addMultiCellWidget(m_notationTarget, 1, 1, 0, 1);
    if (!showNotationOption)
        m_notationTarget->hide();

    TQHBox *parameterBox = new TQHBox(this);
    m_mainLayout->addWidget(parameterBox, zero + 1, 0);

    m_notationBox = new QGroupBox
                    (1, Horizontal, i18n("Notation parameters"), parameterBox);
    TQFrame *notationFrame = new TQFrame(m_notationBox);

    layout = new TQGridLayout(notationFrame, 4, 2, 5, 3);

    layout->addWidget(new TQLabel(i18n("Base grid unit:"), notationFrame),
                      1, 0);
    m_notationUnitCombo = new KComboBox(notationFrame);
    layout->addWidget(m_notationUnitCombo, 1, 1);

    layout->addWidget(new TQLabel(i18n("Complexity:"),
                                 notationFrame), 0, 0);

    m_simplicityCombo = new KComboBox(notationFrame);
    m_simplicityCombo->insertItem(i18n("Very high"));
    m_simplicityCombo->insertItem(i18n("High"));
    m_simplicityCombo->insertItem(i18n("Normal"));
    m_simplicityCombo->insertItem(i18n("Low"));
    m_simplicityCombo->insertItem(i18n("Very low"));
    layout->addWidget(m_simplicityCombo, 0, 1);

    layout->addWidget(new TQLabel(i18n("Tuplet level:"),
                                 notationFrame), 2, 0);
    m_maxTuplet = new KComboBox(notationFrame);
    m_maxTuplet->insertItem(i18n("None"));
    m_maxTuplet->insertItem(i18n("2-in-the-time-of-3"));
    m_maxTuplet->insertItem(i18n("Triplet"));
    /*
        m_maxTuplet->insertItem(i18n("4-Tuplet"));
        m_maxTuplet->insertItem(i18n("5-Tuplet"));
        m_maxTuplet->insertItem(i18n("6-Tuplet"));
        m_maxTuplet->insertItem(i18n("7-Tuplet"));
        m_maxTuplet->insertItem(i18n("8-Tuplet"));
    */
    m_maxTuplet->insertItem(i18n("Any"));
    layout->addWidget(m_maxTuplet, 2, 1);

    m_counterpoint = new TQCheckBox(i18n("Permit counterpoint"), notationFrame);
    layout->addMultiCellWidget(m_counterpoint, 3, 3, 0, 1);

    m_gridBox = new QGroupBox
                (1, Horizontal, i18n("Grid parameters"), parameterBox);
    TQFrame *gridFrame = new TQFrame(m_gridBox);

    layout = new TQGridLayout(gridFrame, 4, 2, 5, 3);

    layout->addWidget(new TQLabel(i18n("Base grid unit:"), gridFrame), 0, 0);
    m_gridUnitCombo = new KComboBox(gridFrame);
    layout->addWidget(m_gridUnitCombo, 0, 1);

    m_swingLabel = new TQLabel(i18n("Swing:"), gridFrame);
    layout->addWidget(m_swingLabel, 1, 0);
    m_swingCombo = new KComboBox(gridFrame);
    layout->addWidget(m_swingCombo, 1, 1);

    m_iterativeLabel = new TQLabel(i18n("Iterative amount:"), gridFrame);
    layout->addWidget(m_iterativeLabel, 2, 0);
    m_iterativeCombo = new KComboBox(gridFrame);
    layout->addWidget(m_iterativeCombo, 2, 1);

    m_durationCheckBox = new QCheckBox
                         (i18n("Quantize durations as well as start times"), gridFrame);
    layout->addMultiCellWidget(m_durationCheckBox, 3, 3, 0, 1);

    m_postProcessingBox = new QGroupBox
                          (1, Horizontal, i18n("After quantization"), this);

    if (preamble) {
        m_mainLayout->addMultiCellWidget(m_postProcessingBox,
                                         zero, zero + 1,
                                         1, 1);
    } else {
        m_mainLayout->addWidget(m_postProcessingBox, zero + 3, 0);
    }

    bool advanced = true;
    m_advancedButton = 0;
    if (showAdvancedButton) {
        m_advancedButton =
            new TQPushButton(i18n("Show advanced options"), this);
        m_mainLayout->addWidget(m_advancedButton, zero + 2, 0, Qt::AlignLeft);
        TQObject::connect(m_advancedButton, TQT_SIGNAL(clicked()),
                         this, TQT_SLOT(slotAdvancedChanged()));
    }

    TQFrame *postFrame = new TQFrame(m_postProcessingBox);

    layout = new TQGridLayout(postFrame, 4, 1, 5, 3);
    m_rebeam = new TQCheckBox(i18n("Re-beam"), postFrame);
    m_articulate = new QCheckBox
                   (i18n("Add articulations (staccato, tenuto, slurs)"), postFrame);
    m_makeViable = new TQCheckBox(i18n("Tie notes at barlines etc"), postFrame);
    m_deCounterpoint = new TQCheckBox(i18n("Split-and-tie overlapping chords"), postFrame);

    layout->addWidget(m_rebeam, 0, 0);
    layout->addWidget(m_articulate, 1, 0);
    layout->addWidget(m_makeViable, 2, 0);
    layout->addWidget(m_deCounterpoint, 3, 0);

    TQPixmap noMap = NotePixmapFactory::toQPixmap
                    (NotePixmapFactory::makeToolbarPixmap("menu-no-note"));

    int defaultType = 0;
    timeT defaultUnit =
        Note(Note::Demisemiquaver).getDuration();

    if (!m_configCategory) {
        if (defaultQuantizer == Notation)
            m_configCategory = "Quantize Dialog Notation";
        else
            m_configCategory = "Quantize Dialog Grid";
    }

    int defaultSwing = 0;
    int defaultIterate = 100;

    if (m_configCategory) {
        KConfig *config = kapp->config();
        config->setGroup(m_configCategory);
        defaultType =
            config->readNumEntry("quantizetype",
                                 (defaultQuantizer == Notation) ? 2 :
                                 (defaultQuantizer == Legato) ? 1 :
                                 0);
        defaultUnit =
            config->readNumEntry("quantizeunit", defaultUnit);
        defaultSwing =
            config->readNumEntry("quantizeswing", defaultSwing);
        defaultIterate =
            config->readNumEntry("quantizeiterate", defaultIterate);
        m_notationTarget->setChecked
        (config->readBoolEntry("quantizenotationonly",
                               defaultQuantizer == Notation));
        m_durationCheckBox->setChecked
        (config->readBoolEntry("quantizedurations", false));
        m_simplicityCombo->setCurrentItem
        (config->readNumEntry("quantizesimplicity", 13) - 11);
        m_maxTuplet->setCurrentItem
        (config->readNumEntry("quantizemaxtuplet", 3) - 1);
        m_counterpoint->setChecked
        (config->readBoolEntry("quantizecounterpoint", false));
        m_rebeam->setChecked
        (config->readBoolEntry("quantizerebeam", true));
        m_makeViable->setChecked
        (config->readBoolEntry("quantizemakeviable", false));
        m_deCounterpoint->setChecked
        (config->readBoolEntry("quantizedecounterpoint", false));
        m_articulate->setChecked
        (config->readBoolEntry("quantizearticulate", true));
        advanced = config->readBoolEntry("quantizeshowadvanced", false);
    } else {
        defaultType =
            (defaultQuantizer == Notation) ? 2 :
            (defaultQuantizer == Legato) ? 1 : 0;
        m_notationTarget->setChecked(defaultQuantizer == Notation);
        m_durationCheckBox->setChecked(false);
        m_simplicityCombo->setCurrentItem(2);
        m_maxTuplet->setCurrentItem(2);
        m_counterpoint->setChecked(false);
        m_rebeam->setChecked(true);
        m_makeViable->setChecked(defaultQuantizer == Notation);
        m_deCounterpoint->setChecked(defaultQuantizer == Notation);
        m_articulate->setChecked(true);
        advanced = false;
    }

    if (preamble || advanced) {
        m_postProcessingBox->show();
    } else {
        m_postProcessingBox->hide();
    }

    for (unsigned int i = 0; i < m_standardQuantizations.size(); ++i) {

        timeT time = m_standardQuantizations[i];
        timeT error = 0;

        TQPixmap pmap = NotePixmapFactory::toQPixmap
                       (NotePixmapFactory::makeNoteMenuPixmap(time, error));
        TQString label = NotationStrings::makeNoteMenuLabel(time, false, error);

        if (error == 0) {
            m_gridUnitCombo->insertItem(pmap, label);
            m_notationUnitCombo->insertItem(pmap, label);
        } else {
            m_gridUnitCombo->insertItem(noMap, TQString("%1").arg(time));
            m_notationUnitCombo->insertItem(noMap, TQString("%1").arg(time));
        }

        if (m_standardQuantizations[i] == defaultUnit) {
            m_gridUnitCombo->setCurrentItem(m_gridUnitCombo->count() - 1);
            m_notationUnitCombo->setCurrentItem
            (m_notationUnitCombo->count() - 1);
        }
    }

    for (int i = -100; i <= 200; i += 10) {
        m_swingCombo->insertItem(i == 0 ? i18n("None") : TQString("%1%").arg(i));
        if (i == defaultSwing)
            m_swingCombo->setCurrentItem(m_swingCombo->count() - 1);
    }

    for (int i = 10; i <= 100; i += 10) {
        m_iterativeCombo->insertItem(i == 100 ? i18n("Full quantize") :
                                     TQString("%1%").arg(i));
        if (i == defaultIterate)
            m_iterativeCombo->setCurrentItem(m_iterativeCombo->count() - 1);
    }

    switch (defaultType) {
    case 0:  // grid
        m_gridBox->show();
        m_swingLabel->show();
        m_swingCombo->show();
        m_iterativeLabel->show();
        m_iterativeCombo->show();
        m_notationBox->hide();
        m_durationCheckBox->show();
        m_typeCombo->setCurrentItem(0);
        break;
    case 1:  // legato
        m_gridBox->show();
        m_swingLabel->hide();
        m_swingCombo->hide();
        m_iterativeLabel->hide();
        m_iterativeCombo->hide();
        m_notationBox->hide();
        m_durationCheckBox->hide();
        m_typeCombo->setCurrentItem(1);
    case 2:  // notation
        m_gridBox->hide();
        m_notationBox->show();
        m_typeCombo->setCurrentItem(2);
        break;
    }

    connect(m_typeCombo, TQT_SIGNAL(activated(int)), TQT_SLOT(slotTypeChanged(int)));
}

Quantizer *
QuantizeParameters::getQuantizer() const
{
    //!!! Excessive duplication with
    // EventQuantizeCommand::makeQuantizer in editcommands.cpp

    int type = m_typeCombo->currentItem();
    timeT unit = 0;

    if (type == 0 || type == 1) {
        unit = m_standardQuantizations[m_gridUnitCombo->currentItem()];
    } else {
        unit = m_standardQuantizations[m_notationUnitCombo->currentItem()];
    }

    Quantizer *quantizer = 0;

    int swing = m_swingCombo->currentItem();
    swing *= 10;
    swing -= 100;

    int iterate = m_iterativeCombo->currentItem();
    iterate *= 10;
    iterate += 10;

    if (type == 0) {

        if (m_notationTarget->isChecked()) {
            quantizer = new BasicQuantizer
                        (Quantizer::RawEventData,
                         Quantizer::NotationPrefix,
                         unit, m_durationCheckBox->isChecked(),
                         swing, iterate);
        } else {
            quantizer = new BasicQuantizer
                        (Quantizer::RawEventData,
                         Quantizer::RawEventData,
                         unit, m_durationCheckBox->isChecked(),
                         swing, iterate);
        }
    } else if (type == 1) {
        if (m_notationTarget->isChecked()) {
            quantizer = new LegatoQuantizer
                        (Quantizer::RawEventData,
                         Quantizer::NotationPrefix, unit);
        } else {
            quantizer = new LegatoQuantizer
                        (Quantizer::RawEventData,
                         Quantizer::RawEventData,
                         unit);
        }
    } else {

        NotationQuantizer *nq;

        if (m_notationTarget->isChecked()) {
            nq = new NotationQuantizer();
        } else {
            nq = new NotationQuantizer
                 (Quantizer::RawEventData,
                  Quantizer::RawEventData);
        }

        nq->setUnit(unit);
        nq->setSimplicityFactor(m_simplicityCombo->currentItem() + 11);
        nq->setMaxTuplet(m_maxTuplet->currentItem() + 1);
        nq->setContrapuntal(m_counterpoint->isChecked());
        nq->setArticulate(m_articulate->isChecked());

        quantizer = nq;
    }

    if (m_configCategory) {
        KConfig *config = kapp->config();
        config->setGroup(m_configCategory);
        config->writeEntry("quantizetype", type);
        config->writeEntry("quantizeunit", unit);
        config->writeEntry("quantizeswing", swing);
        config->writeEntry("quantizeiterate", iterate);
        config->writeEntry("quantizenotationonly",
                           m_notationTarget->isChecked());
        if (type == 0) {
            config->writeEntry("quantizedurations",
                               m_durationCheckBox->isChecked());
        } else {
            config->writeEntry("quantizesimplicity",
                               m_simplicityCombo->currentItem() + 11);
            config->writeEntry("quantizemaxtuplet",
                               m_maxTuplet->currentItem() + 1);
            config->writeEntry("quantizecounterpoint",
                               m_counterpoint->isChecked());
            config->writeEntry("quantizearticulate",
                               m_articulate->isChecked());
        }
        config->writeEntry("quantizerebeam", m_rebeam->isChecked());
        config->writeEntry("quantizemakeviable", m_makeViable->isChecked());
        config->writeEntry("quantizedecounterpoint", m_deCounterpoint->isChecked());
    }

    return quantizer;
}

void
QuantizeParameters::slotAdvancedChanged()
{
    if (m_postProcessingBox->isVisible()) {
        if (m_advancedButton)
            m_advancedButton->setText(i18n("Show Advanced Options"));
        m_postProcessingBox->hide();
    } else {
        if (m_advancedButton)
            m_advancedButton->setText(i18n("Hide Advanced Options"));
        m_postProcessingBox->show();
    }
    adjustSize();
}

void
QuantizeParameters::showAdvanced(bool show)
{
    if (show) {
        m_postProcessingBox->show();
    } else {
        m_postProcessingBox->hide();
    }
    adjustSize();
}

void
QuantizeParameters::slotTypeChanged(int index)
{
    if (index == 0) {
        m_gridBox->show();
        m_swingLabel->show();
        m_swingCombo->show();
        m_iterativeLabel->show();
        m_iterativeCombo->show();
        m_durationCheckBox->show();
        m_notationBox->hide();
    } else if (index == 1) {
        m_gridBox->show();
        m_swingLabel->hide();
        m_swingCombo->hide();
        m_iterativeLabel->hide();
        m_iterativeCombo->hide();
        m_durationCheckBox->hide();
        m_notationBox->hide();
    } else {
        m_gridBox->hide();
        m_notationBox->show();
    }
}

}
#include "QuantizeParameters.moc"