summaryrefslogtreecommitdiffstats
path: root/src/gui/dialogs/TempoDialog.cpp
blob: 6d2c716aa179e756f685a0e2bdc30af8b7661f2f (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
/* -*- 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   <glaurent@telegraph-road.org>,
        Chris Cannam        <cannam@all-day-breakfast.com>,
        Richard Bown        <richard.bown@ferventsoftware.com>
 
    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 "TempoDialog.h"
#include <tqlayout.h>

#include <klocale.h>
#include "misc/Debug.h"
#include "base/Composition.h"
#include "base/NotationTypes.h"
#include "base/RealTime.h"
#include "document/RosegardenGUIDoc.h"
#include "gui/editors/notation/NotePixmapFactory.h"
#include "gui/widgets/TimeWidget.h"
#include "gui/widgets/HSpinBox.h"
#include <kdialogbase.h>
#include <tqbuttongroup.h>
#include <tqcheckbox.h>
#include <tqframe.h>
#include <tqgroupbox.h>
#include <tqhbox.h>
#include <tqlabel.h>
#include <tqradiobutton.h>
#include <tqstring.h>
#include <tqvbox.h>
#include <tqwidget.h>


namespace Rosegarden
{

TempoDialog::TempoDialog(TQWidget *tqparent, RosegardenGUIDoc *doc,
                         bool timeEditable):
        KDialogBase(tqparent, 0, true, i18n("Insert Tempo Change"), Ok | Cancel | Help),
        m_doc(doc),
        m_tempoTime(0)
{
    setHelp("tempo");

    TQVBox *vbox = makeVBoxMainWidget();
    TQGroupBox *groupBox = new TQGroupBox(1, Qt::Horizontal, i18n("Tempo"), vbox);

    TQFrame *frame = new TQFrame(groupBox);
    TQGridLayout *tqlayout = new TQGridLayout(frame, 4, 3, 5, 5);

    // Set tempo
    tqlayout->addWidget(new TQLabel(i18n("New tempo:"), frame), 0, 1);
    m_tempoValueSpinBox = new HSpinBox(frame, 0, 100000, 0.0, 1000.0, 5);
    tqlayout->addWidget(m_tempoValueSpinBox, 0, 2);

    connect(m_tempoValueSpinBox, TQT_SIGNAL(valueChanged(const TQString &)),
            TQT_SLOT(slotTempoChanged(const TQString &)));

    m_tempoTap= new TQPushButton(i18n("Tap"), frame);
    tqlayout->addWidget(m_tempoTap, 0, 3);
    connect(m_tempoTap, TQT_SIGNAL(clicked()), TQT_SLOT(slotTapClicked()));


    m_tempoConstant = new TQRadioButton(i18n("Tempo is fixed until the following tempo change"), frame);
    m_tempoRampToNext = new TQRadioButton(i18n("Tempo ramps to the following tempo"), frame);
    m_tempoRampToTarget = new TQRadioButton(i18n("Tempo ramps to:"), frame);

    //    m_tempoTargetCheckBox = new TQCheckBox(i18n("Ramping to:"), frame);
    m_tempoTargetSpinBox = new HSpinBox(frame, 0, 100000, 0.0, 1000.0, 5);

    //    tqlayout->addMultiCellWidget(m_tempoTargetCheckBox, 1, 1, 0, 1, AlignRight);
    //    tqlayout->addWidget(m_tempoTargetSpinBox, 1, 2);

    tqlayout->addMultiCellWidget(m_tempoConstant, 1, 1, 1, 2);
    tqlayout->addMultiCellWidget(m_tempoRampToNext, 2, 2, 1, 2);
    tqlayout->addWidget(m_tempoRampToTarget, 3, 1);
    tqlayout->addWidget(m_tempoTargetSpinBox, 3, 2);

    //    connect(m_tempoTargetCheckBox, TQT_SIGNAL(clicked()),
    //            TQT_SLOT(slotTargetCheckBoxClicked()));
    connect(m_tempoConstant, TQT_SIGNAL(clicked()),
            TQT_SLOT(slotTempoConstantClicked()));
    connect(m_tempoRampToNext, TQT_SIGNAL(clicked()),
            TQT_SLOT(slotTempoRampToNextClicked()));
    connect(m_tempoRampToTarget, TQT_SIGNAL(clicked()),
            TQT_SLOT(slotTempoRampToTargetClicked()));
    connect(m_tempoTargetSpinBox, TQT_SIGNAL(valueChanged(const TQString &)),
            TQT_SLOT(slotTargetChanged(const TQString &)));

    m_tempoBeatLabel = new TQLabel(frame);
    tqlayout->addWidget(m_tempoBeatLabel, 0, 4);

    m_tempoBeat = new TQLabel(frame);
    tqlayout->addWidget(m_tempoBeat, 0, 5);

    m_tempoBeatsPerMinute = new TQLabel(frame);
    tqlayout->addWidget(m_tempoBeatsPerMinute, 0, 6);

    m_timeEditor = 0;

    if (timeEditable) {
        m_timeEditor = new TimeWidget
                       (i18n("Time of tempo change"),
                        vbox, &m_doc->getComposition(), 0, true);
        populateTempo();
        return ;
    }

    // Scope Box
    TQButtonGroup *scopeGroup = new TQButtonGroup(1, Qt::Horizontal,
                                                i18n("Scope"), vbox);

//    new TQLabel(scopeBox);

    TQVBox *scopeBox = new TQVBox(scopeGroup);

    scopeBox->setSpacing(5);
    scopeBox->setMargin(5);

    TQHBox *currentBox = new TQHBox(scopeBox);
    new TQLabel(i18n("The pointer is currently at "), currentBox);
    m_tempoTimeLabel = new TQLabel(currentBox);
    m_tempoBarLabel = new TQLabel(currentBox);
    TQLabel *spare = new TQLabel(currentBox);
    currentBox->setStretchFactor(spare, 20);

    m_tempoStatusLabel = new TQLabel(scopeBox);

//    new TQLabel(scopeBox);

    TQHBox *changeWhereBox = new TQHBox(scopeBox);
    spare = new TQLabel("      ", changeWhereBox);
    TQVBox *changeWhereVBox = new TQVBox(changeWhereBox);
    changeWhereBox->setStretchFactor(changeWhereVBox, 20);

    m_tempoChangeHere = new TQRadioButton
                        (i18n("Apply this tempo from here onwards"),
                         changeWhereVBox);

    m_tempoChangeBefore = new TQRadioButton
                          (i18n("Replace the last tempo change"),
                           changeWhereVBox);
    m_tempoChangeBeforeAt = new TQLabel(changeWhereVBox);
    m_tempoChangeBeforeAt->hide();

    m_tempoChangeStartOfBar = new TQRadioButton
                              (i18n("Apply this tempo from the start of this bar"), changeWhereVBox);

    m_tempoChangeGlobal = new TQRadioButton
                          (i18n("Apply this tempo to the whole composition"), changeWhereVBox);

    TQHBox *optionHBox = new TQHBox(changeWhereVBox);
    new TQLabel("         ", optionHBox);
    m_defaultBox = new TQCheckBox
                   (i18n("Also make this the default tempo"), optionHBox);
    spare = new TQLabel(optionHBox);
    optionHBox->setStretchFactor(spare, 20);

//    new TQLabel(scopeBox);

    connect(m_tempoChangeHere, TQT_SIGNAL(clicked()),
            TQT_SLOT(slotActionChanged()));
    connect(m_tempoChangeBefore, TQT_SIGNAL(clicked()),
            TQT_SLOT(slotActionChanged()));
    connect(m_tempoChangeStartOfBar, TQT_SIGNAL(clicked()),
            TQT_SLOT(slotActionChanged()));
    connect(m_tempoChangeGlobal, TQT_SIGNAL(clicked()),
            TQT_SLOT(slotActionChanged()));

    m_tempoChangeHere->setChecked(true);

    // disable initially
    m_defaultBox->setEnabled(false);

    populateTempo();
}

TempoDialog::~TempoDialog()
{}

void
TempoDialog::setTempoPosition(timeT time)
{
    m_tempoTime = time;
    populateTempo();
}

void
TempoDialog::populateTempo()
{
    Composition &comp = m_doc->getComposition();
    tempoT tempo = comp.getTempoAtTime(m_tempoTime);
    std::pair<bool, tempoT> ramping(false, tempo);

    int tempoChangeNo = comp.getTempoChangeNumberAt(m_tempoTime);
    if (tempoChangeNo >= 0) {
        tempo = comp.getTempoChange(tempoChangeNo).second;
        ramping = comp.getTempoRamping(tempoChangeNo, false);
    }

    m_tempoValueSpinBox->setValue(tempo);

    if (ramping.first) {
        if (ramping.second) {
            m_tempoTargetSpinBox->setEnabled(true);
            m_tempoTargetSpinBox->setValue(ramping.second);
            m_tempoConstant->setChecked(false);
            m_tempoRampToNext->setChecked(false);
            m_tempoRampToTarget->setChecked(true);
        } else {
            ramping = comp.getTempoRamping(tempoChangeNo, true);
            m_tempoTargetSpinBox->setEnabled(false);
            m_tempoTargetSpinBox->setValue(ramping.second);
            m_tempoConstant->setChecked(false);
            m_tempoRampToNext->setChecked(true);
            m_tempoRampToTarget->setChecked(false);
        }
    } else {
        m_tempoTargetSpinBox->setEnabled(false);
        m_tempoTargetSpinBox->setValue(tempo);
        m_tempoConstant->setChecked(true);
        m_tempoRampToNext->setChecked(false);
        m_tempoRampToTarget->setChecked(false);
    }

    //    m_tempoTargetCheckBox->setChecked(ramping.first);
    m_tempoTargetSpinBox->setEnabled(ramping.first);

    updateBeatLabels(comp.getTempoQpm(tempo));

    if (m_timeEditor) {
        m_timeEditor->slotSetTime(m_tempoTime);
        return ;
    }

    RealTime tempoTime = comp.getElapsedRealTime(m_tempoTime);
    TQString milliSeconds;
    milliSeconds.sprintf("%03d", tempoTime.msec());
    m_tempoTimeLabel->setText(i18n("%1.%2 s,").tqarg(tempoTime.sec)
                              .tqarg(milliSeconds));

    int barNo = comp.getBarNumber(m_tempoTime);
    if (comp.getBarStart(barNo) == m_tempoTime) {
        m_tempoBarLabel->setText
        (i18n("at the start of measure %1.").tqarg(barNo + 1));
        m_tempoChangeStartOfBar->setEnabled(false);
    } else {
        m_tempoBarLabel->setText(
            i18n("in the middle of measure %1.").tqarg(barNo + 1));
        m_tempoChangeStartOfBar->setEnabled(true);
    }

    m_tempoChangeBefore->setEnabled(false);
    m_tempoChangeBeforeAt->setEnabled(false);

    bool havePrecedingTempo = false;

    if (tempoChangeNo >= 0) {

        timeT lastTempoTime = comp.getTempoChange(tempoChangeNo).first;
        if (lastTempoTime < m_tempoTime) {

            RealTime lastRT = comp.getElapsedRealTime(lastTempoTime);
            TQString lastms;
            lastms.sprintf("%03d", lastRT.msec());
            int lastBar = comp.getBarNumber(lastTempoTime);
            m_tempoChangeBeforeAt->setText
            (i18n("        (at %1.%2 s, in measure %3)").tqarg(lastRT.sec)
             .tqarg(lastms).tqarg(lastBar + 1));
            m_tempoChangeBeforeAt->show();

            m_tempoChangeBefore->setEnabled(true);
            m_tempoChangeBeforeAt->setEnabled(true);

            havePrecedingTempo = true;
        }
    }

    if (comp.getTempoChangeCount() > 0) {

        if (havePrecedingTempo) {
            m_tempoStatusLabel->hide();
        } else {
            m_tempoStatusLabel->setText
            (i18n("There are no preceding tempo changes."));
        }

        m_tempoChangeGlobal->setEnabled(true);

    } else {

        m_tempoStatusLabel->setText
        (i18n("There are no other tempo changes."));

        m_tempoChangeGlobal->setEnabled(false);
    }

    m_defaultBox->setEnabled(false);
}

void
TempoDialog::updateBeatLabels(double qpm)
{
    Composition &comp = m_doc->getComposition();

    // If the time signature's beat is not a crotchet, need to show
    // bpm separately

    timeT beat = comp.getTimeSignatureAt(m_tempoTime).getBeatDuration();
    if (beat == Note(Note::Crotchet).getDuration()) {
        m_tempoBeatLabel->setText(i18n(" bpm"));
        m_tempoBeatLabel->show();
        m_tempoBeat->hide();
        m_tempoBeatsPerMinute->hide();
    } else {
        //	m_tempoBeatLabel->setText(" (");
        m_tempoBeatLabel->setText("  ");

        timeT error = 0;
        m_tempoBeat->setPixmap(NotePixmapFactory::toTQPixmap
                               (NotePixmapFactory::makeNoteMenuPixmap(beat, error)));
        m_tempoBeat->setMaximumWidth(25);
        if (error)
            m_tempoBeat->setPixmap(NotePixmapFactory::toTQPixmap
                                   (NotePixmapFactory::makeToolbarPixmap
                                    ("menu-no-note")));

        m_tempoBeatsPerMinute->setText
        //	    (TQString("= %1 )").arg
        (TQString("= %1 ").arg
         (int(qpm * Note(Note::Crotchet).getDuration() / beat)));
        m_tempoBeatLabel->show();
        m_tempoBeat->show();
        m_tempoBeatsPerMinute->show();
    }
}

void
TempoDialog::slotTempoChanged(const TQString &)
{
    updateBeatLabels(double(m_tempoValueSpinBox->valuef()));
}

void
TempoDialog::slotTargetChanged(const TQString &)
{
    //...
}

void
TempoDialog::slotTempoConstantClicked()
{
    m_tempoRampToNext->setChecked(false);
    m_tempoRampToTarget->setChecked(false);
    m_tempoTargetSpinBox->setEnabled(false);
}

void
TempoDialog::slotTempoRampToNextClicked()
{
    m_tempoConstant->setChecked(false);
    m_tempoRampToTarget->setChecked(false);
    m_tempoTargetSpinBox->setEnabled(false);
}

void
TempoDialog::slotTempoRampToTargetClicked()
{
    m_tempoConstant->setChecked(false);
    m_tempoRampToNext->setChecked(false);
    m_tempoTargetSpinBox->setEnabled(true);
}

void
TempoDialog::slotActionChanged()
{
    m_defaultBox->setEnabled(m_tempoChangeGlobal->isChecked());
}

void
TempoDialog::slotOk()
{
    tempoT tempo = m_tempoValueSpinBox->value();
    RG_DEBUG << "Tempo is " << tempo << endl;

    tempoT target = -1;
    if (m_tempoRampToNext->isChecked()) {
        target = 0;
    } else if (m_tempoRampToTarget->isChecked()) {
        target = m_tempoTargetSpinBox->value();
    }

    RG_DEBUG << "Target is " << target << endl;

    if (m_timeEditor) {

        emit changeTempo(m_timeEditor->getTime(),
                         tempo,
                         target,
                         AddTempo);

    } else {

        TempoDialogAction action = AddTempo;

        if (m_tempoChangeBefore->isChecked()) {
            action = ReplaceTempo;
        } else if (m_tempoChangeStartOfBar->isChecked()) {
            action = AddTempoAtBarStart;
        } else if (m_tempoChangeGlobal->isChecked()) {
            action = GlobalTempo;
            if (m_defaultBox->isChecked()) {
                action = GlobalTempoWithDefault;
            }
        }

        emit changeTempo(m_tempoTime,
                         tempo,
                         target,
                         action);
    }

    KDialogBase::slotOk();
}

void
TempoDialog::slotTapClicked()
{
    TQTime now = TQTime::currentTime();

    if (m_tapMinusOne != TQTime()) {

        int ms1 = m_tapMinusOne.msecsTo(now);

        if (ms1 < 10000) {

            int msec = ms1;

            if (m_tapMinusTwo != TQTime()) {
                int ms2 = m_tapMinusTwo.msecsTo(m_tapMinusOne);
                if (ms2 < 10000) {
                    msec = (ms1 + ms2) / 2;
                }
            }

            int bpm = 60000 / msec;
            m_tempoValueSpinBox->setValue(bpm * 100000);
        }
    }

    m_tapMinusTwo = m_tapMinusOne;
    m_tapMinusOne = now;
}


}

#include "TempoDialog.moc"