summaryrefslogtreecommitdiffstats
path: root/src/gui/dialogs/TimeSignatureDialog.cpp
blob: f9de575b0416f02eeb8be4532f9dc0a9dc5978a7 (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
/* -*- 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 "TimeSignatureDialog.h"
#include <kapplication.h>

#include <klocale.h>
#include "document/ConfigGroups.h"
#include "base/Composition.h"
#include "base/NotationTypes.h"
#include "gui/widgets/TimeWidget.h"
#include "gui/widgets/BigArrowButton.h"
#include <kconfig.h>
#include <kdialogbase.h>
#include <tqbuttongroup.h>
#include <tqcheckbox.h>
#include <tqfont.h>
#include <tqgroupbox.h>
#include <tqhbox.h>
#include <tqlabel.h>
#include <tqobject.h>
#include <tqradiobutton.h>
#include <tqstring.h>
#include <tqvbox.h>
#include <tqwidget.h>


namespace Rosegarden
{

TimeSignatureDialog::TimeSignatureDialog(TQWidget *parent,
        Composition *composition,
        timeT insertionTime,
        TimeSignature sig,
        bool timeEditable,
        TQString explanatoryText) :
        KDialogBase(parent, 0, true, i18n("Time Signature"), Ok | Cancel | Help),
        m_composition(composition),
        m_timeSignature(sig),
        m_time(insertionTime),
        m_numLabel(0),
        m_denomLabel(0),
        m_explanatoryLabel(0),
        m_commonTimeButton(0),
        m_hideSignatureButton(0),
        m_normalizeRestsButton(0),
        m_asGivenButton(0),
        m_startOfBarButton(0),
        m_timeEditor(0)
{
    static TQFont *timeSigFont = 0;

    if (timeSigFont == 0) {
        timeSigFont = new TQFont("new century schoolbook", 8, TQFont::Bold);
        timeSigFont->setPixelSize(20);
    }

    TQVBox *vbox = makeVBoxMainWidget();
    TQGroupBox *groupBox = new TQGroupBox
                          (1, Qt::Horizontal, i18n("Time signature"), vbox);
    TQHBox *numBox = new TQHBox(groupBox);
    TQHBox *denomBox = new TQHBox(groupBox);

    TQLabel *explanatoryLabel = 0;
    if (!explanatoryText.isNull()) {
        explanatoryLabel = new TQLabel(explanatoryText, groupBox);
    }

    BigArrowButton *numDown = new BigArrowButton(numBox, Qt::LeftArrow);
    BigArrowButton *denomDown = new BigArrowButton(denomBox, Qt::LeftArrow);

    m_numLabel = new TQLabel
                 (TQString("%1").arg(m_timeSignature.getNumerator()), numBox);
    m_denomLabel = new TQLabel
                   (TQString("%1").arg(m_timeSignature.getDenominator()), denomBox);

    m_numLabel->setAlignment(AlignHCenter | AlignVCenter);
    m_denomLabel->setAlignment(AlignHCenter | AlignVCenter);

    m_numLabel->setFont(*timeSigFont);
    m_denomLabel->setFont(*timeSigFont);

    BigArrowButton *numUp = new BigArrowButton(numBox, Qt::RightArrow);
    BigArrowButton *denomUp = new BigArrowButton(denomBox, Qt::RightArrow);

    TQObject::connect(numDown, TQT_SIGNAL(clicked()), this, TQT_SLOT(slotNumDown()));
    TQObject::connect(numUp, TQT_SIGNAL(clicked()), this, TQT_SLOT(slotNumUp()));
    TQObject::connect(denomDown, TQT_SIGNAL(clicked()), this, TQT_SLOT(slotDenomDown()));
    TQObject::connect(denomUp, TQT_SIGNAL(clicked()), this, TQT_SLOT(slotDenomUp()));

    if (timeEditable) {

        m_timeEditor = new TimeWidget
                       (i18n("Time where signature takes effect"),
                        vbox,
                        composition,
                        m_time,
                        true);

        m_asGivenButton = 0;
        m_startOfBarButton = 0;

    } else {

        m_timeEditor = 0;

        groupBox = new TQButtonGroup(1, Qt::Horizontal, i18n("Scope"), vbox);

        int barNo = composition->getBarNumber(m_time);
        bool atStartOfBar = (m_time == composition->getBarStart(barNo));

        if (!atStartOfBar) {

            TQString scopeText;

            if (barNo != 0 || !atStartOfBar) {
                if (atStartOfBar) {
                    scopeText = TQString
                                (i18n("Insertion point is at start of measure %1."))
                                .arg(barNo + 1);
                } else {
                    scopeText = TQString
                                (i18n("Insertion point is in the middle of measure %1."))
                                .arg(barNo + 1);
                }
            } else {
                scopeText = TQString
                            (i18n("Insertion point is at start of composition."));
            }

            new TQLabel(scopeText, groupBox);
            m_asGivenButton = new TQRadioButton
                              (i18n("Start measure %1 here").arg(barNo + 2), groupBox);

            if (!atStartOfBar) {
                m_startOfBarButton = new TQRadioButton
                                     (i18n("Change time from start of measure %1")
                                      .arg(barNo + 1), groupBox);
                m_startOfBarButton->setChecked(true);
            } else {
                m_asGivenButton->setChecked(true);
            }
        } else {
            new TQLabel(i18n("Time change will take effect at the start of measure %1.")
                       .arg(barNo + 1), groupBox);
        }
    }

    groupBox = new TQGroupBox(1, Qt::Horizontal, i18n("Options"), vbox);
    KConfig *config = kapp->config();
    config->setGroup(GeneralOptionsConfigGroup);

    m_hideSignatureButton = new TQCheckBox
                            (i18n("Hide the time signature"), groupBox);
    m_hideSignatureButton->setChecked
    (config->readBoolEntry("timesigdialogmakehidden", false));

    m_hideBarsButton = new TQCheckBox
                       (i18n("Hide the affected bar lines"), groupBox);
    m_hideBarsButton->setChecked
    (config->readBoolEntry("timesigdialogmakehiddenbars", false));

    m_commonTimeButton = new TQCheckBox
                         (i18n("Show as common time"), groupBox);
    m_commonTimeButton->setChecked
    (config->readBoolEntry("timesigdialogshowcommon", true));

    m_normalizeRestsButton = new TQCheckBox
                             (i18n("Correct the durations of following measures"), groupBox);
    m_normalizeRestsButton->setChecked
    (config->readBoolEntry("timesigdialognormalize", true));

    TQObject::connect(m_hideSignatureButton, TQT_SIGNAL(clicked()), this,
                     TQT_SLOT(slotUpdateCommonTimeButton()));
    slotUpdateCommonTimeButton();
    m_explanatoryLabel = explanatoryLabel;

    setHelp("time-signature");
}

TimeSignature
TimeSignatureDialog::getTimeSignature() const
{
    KConfig *config = kapp->config();
    config->setGroup(GeneralOptionsConfigGroup);

    config->writeEntry("timesigdialogmakehidden", m_hideSignatureButton->isChecked());
    config->writeEntry("timesigdialogmakehiddenbars", m_hideBarsButton->isChecked());
    config->writeEntry("timesigdialogshowcommon", m_commonTimeButton->isChecked());
    config->writeEntry("timesigdialognormalize", m_normalizeRestsButton->isChecked());

    TimeSignature ts(m_timeSignature.getNumerator(),
                     m_timeSignature.getDenominator(),
                     (m_commonTimeButton &&
                      m_commonTimeButton->isEnabled() &&
                      m_commonTimeButton->isChecked()),
                     (m_hideSignatureButton &&
                      m_hideSignatureButton->isEnabled() &&
                      m_hideSignatureButton->isChecked()),
                     (m_hideBarsButton &&
                      m_hideBarsButton->isEnabled() &&
                      m_hideBarsButton->isChecked()));
    return ts;
}

void
TimeSignatureDialog::slotNumDown()
{
    int n = m_timeSignature.getNumerator();
    if (--n >= 1) {
        m_timeSignature = TimeSignature(n, m_timeSignature.getDenominator());
        m_numLabel->setText(TQString("%1").arg(n));
    }
    slotUpdateCommonTimeButton();
}

void
TimeSignatureDialog::slotNumUp()
{
    int n = m_timeSignature.getNumerator();
    if (++n <= 99) {
        m_timeSignature = TimeSignature(n, m_timeSignature.getDenominator());
        m_numLabel->setText(TQString("%1").arg(n));
    }
    slotUpdateCommonTimeButton();
}

void
TimeSignatureDialog::slotDenomDown()
{
    int n = m_timeSignature.getDenominator();
    if ((n /= 2) >= 1) {
        m_timeSignature = TimeSignature(m_timeSignature.getNumerator(), n);
        m_denomLabel->setText(TQString("%1").arg(n));
    }
    slotUpdateCommonTimeButton();
}

void
TimeSignatureDialog::slotDenomUp()
{
    int n = m_timeSignature.getDenominator();
    if ((n *= 2) <= 64) {
        m_timeSignature = TimeSignature(m_timeSignature.getNumerator(), n);
        m_denomLabel->setText(TQString("%1").arg(n));
    }
    slotUpdateCommonTimeButton();
}

void
TimeSignatureDialog::slotUpdateCommonTimeButton()
{
    if (m_explanatoryLabel)
        m_explanatoryLabel->hide();
    if (!m_hideSignatureButton || !m_hideSignatureButton->isChecked()) {
        if (m_timeSignature.getDenominator() == m_timeSignature.getNumerator()) {
            if (m_timeSignature.getNumerator() == 4) {
                m_commonTimeButton->setText(i18n("Display as common time"));
                m_commonTimeButton->setEnabled(true);
                return ;
            } else if (m_timeSignature.getNumerator() == 2) {
                m_commonTimeButton->setText(i18n("Display as cut common time"));
                m_commonTimeButton->setEnabled(true);
                return ;
            }
        }
    }
    m_commonTimeButton->setEnabled(false);
}

timeT
TimeSignatureDialog::getTime() const
{
    if (m_timeEditor) {
        return m_timeEditor->getTime();
    } else if (m_asGivenButton && m_asGivenButton->isChecked()) {
        return m_time;
    } else if (m_startOfBarButton && m_startOfBarButton->isChecked()) {
        int barNo = m_composition->getBarNumber(m_time);
        return m_composition->getBarStart(barNo);
    } else {
        return m_time;
    }
}

bool
TimeSignatureDialog::shouldNormalizeRests() const
{
    return (m_normalizeRestsButton && m_normalizeRestsButton->isEnabled() &&
            m_normalizeRestsButton->isChecked());
}

}
#include "TimeSignatureDialog.moc"