summaryrefslogtreecommitdiffstats
path: root/src/gui/dialogs/IntervalDialog.cpp
blob: 16a6a1ecc8cee3423f91121dd1b04ca45bf00451 (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
/*
    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 "IntervalDialog.h"
#include <tqlayout.h>

#include <iostream>
#include <tdelocale.h>
#include "misc/Strings.h"
#include "base/MidiDevice.h"
#include "base/NotationRules.h"
#include <kcombobox.h>
#include <kdialogbase.h>
#include <tqframe.h>
#include <tqgroupbox.h>
#include <tqcheckbox.h>
#include <tqlabel.h>
#include <tqradiobutton.h>
#include <tqbuttongroup.h>
#include <tqsizepolicy.h>
#include <tqstring.h>
#include <tqvbox.h>
#include <tqwidget.h>


namespace Rosegarden
{

IntervalDialog::IntervalDialog(TQWidget *parent, bool askChangeKey, bool askTransposeSegmentBack) :
        KDialogBase(parent, 0, true, i18n("Specify Interval"), Ok | Cancel )
{
    TQVBox *vBox = makeVBoxMainWidget();

    TQHBox *hBox = new TQHBox( vBox );

    m_referencenote = new DiatonicPitchChooser( i18n("Reference note:"), hBox );
    m_targetnote = new DiatonicPitchChooser( i18n("Target note:"), hBox );

    intervalChromatic = 0;
    intervalDiatonic = 0;

    //m_intervalPitchLabel = new TQLabel( i18n("Pitch: %1").arg(intervalChromatic), hBox);
    //m_intervalOctavesLabel = new TQLabel( i18n("Octaves: %1").arg(intervalDiatonic / 7), hBox);
    //m_intervalStepsLabel = new TQLabel( i18n("Steps: %1").arg(intervalDiatonic % 7), hBox);

    m_intervalLabel = new TQLabel( i18n("a perfect unison"), vBox);
    m_intervalLabel->setAlignment(TQt::AlignCenter);
    TQFont font(m_intervalLabel->font());
    font.setItalic(true);
    m_intervalLabel->setFont(font);

    if (askChangeKey)
    {
        TQButtonGroup *affectKeyGroup = new TQButtonGroup(1, Qt::Horizontal, i18n("Effect on Key"), vBox);
        m_transposeWithinKey = new TQRadioButton(i18n("Transpose within key"), affectKeyGroup);
        m_transposeWithinKey->setChecked(true);
        m_transposeChangingKey = new TQRadioButton(i18n("Change key for selection"), affectKeyGroup);
    }
    else
    {
        m_transposeChangingKey = NULL;
        m_transposeWithinKey = NULL;
    }
    
    if (askTransposeSegmentBack)
    {
        m_transposeSegmentBack = new TQCheckBox( i18n("Adjust segment transposition in opposite direction (maintain audible pitch)"), vBox );
        m_transposeSegmentBack->setTristate(false);
        m_transposeSegmentBack->setChecked(false);
    }
    else
    {
        m_transposeSegmentBack = NULL;
    }

    connect(m_referencenote, TQT_SIGNAL(noteChanged(int,int,int)),
            this, TQT_SLOT(slotSetReferenceNote(int,int,int)));

    connect(m_targetnote, TQT_SIGNAL(noteChanged(int,int,int)),
            this, TQT_SLOT(slotSetTargetNote(int,int,int)));
}

// number of octaves the notes are apart
int
IntervalDialog::getOctaveDistance()
{
    return m_targetnote->getOctave() - m_referencenote->getOctave();
}

// chromatic distance between the steps, not taking account octaves or 
// accidentals
int
IntervalDialog::getStepDistanceChromatic()
{
    return scale_Cmajor[m_targetnote->getStep()] - scale_Cmajor[m_referencenote->getStep()];
    // - getChromaticStepValue(m_referencestep->currentItem());
    //return m_targetnote->getPitch() - m_referencenote->getPitch();
}

// correction due to accidentals
int
IntervalDialog::getAccidentalCorrectionChromatic()
{
    return m_targetnote->getAccidental() - m_referencenote->getAccidental();
}

int
IntervalDialog::getDiatonicDistance()
{
    return getOctaveDistance() * 7 + m_targetnote->getStep() - m_referencenote->getStep();
}

int
IntervalDialog::getChromaticDistance()
{
    return getOctaveDistance() * 12 + getStepDistanceChromatic() + getAccidentalCorrectionChromatic();
}

TQString
IntervalDialog::getIntervalName(int intervalDiatonic, int intervalChromatic)
{
    // displayInterval: an intervalDiatonic of -3 will yield a displayInterval of 3 and
    // set the boolean 'down' to true.
    int displayIntervalDiatonic = intervalDiatonic;
    int displayIntervalChromatic = intervalChromatic;
    bool down = (intervalDiatonic < 0 || 
                 (intervalDiatonic == 0 &&
                  intervalChromatic < 0));
    if (down)
    {
        displayIntervalDiatonic = -displayIntervalDiatonic;
        displayIntervalChromatic = -displayIntervalChromatic;
    }
    
    int octaves = displayIntervalDiatonic / 7;
    int deviation = displayIntervalChromatic % 12 - scale_Cmajor[displayIntervalDiatonic % 7];
    // Note (hjj):
    // "1 octave and a diminished octave" is better than
    // "2 octaves and a diminished unison"
    if (displayIntervalDiatonic % 7 == 0) {
        if (octaves > 0) {
            deviation = (deviation < 5 ? deviation : deviation - 12);
        } else if (octaves < 0) {
            deviation = (deviation < 5 ? -deviation : 12 - deviation);
        }
    } else if (down) {
	// Note (hjj):
	// an augmented prime down, NOT a diminished prime down
	deviation = -deviation;
    }
    
    // show the step for an unison only if the octave doesn't change, any other interval 
    //  always, and augmented/dimnished unisons (modulo octaves) always.
    bool showStep = displayIntervalDiatonic == 0 || 
        displayIntervalDiatonic % 7 != 0 || deviation != 0;
    
    TQString textInterval = "";
    TQString textIntervalDeviated = "";
    if (showStep)
    {
        switch (displayIntervalDiatonic % 7)
        {
        // First the diminished/perfect/augmented:
        case 0: // unison or octaves
        case 3: // fourth
        case 4: // fifth
           if (deviation == -1)
               textIntervalDeviated += i18n("a diminished");
           else if (deviation == 1)
               textIntervalDeviated += i18n("an augmented");
           else if (deviation == -2)
               textIntervalDeviated += i18n("a doubly diminished");
           else if (deviation == 2)
               textIntervalDeviated += i18n("a doubly augmented");
           else if (deviation == -3)
               textIntervalDeviated += i18n("a triply diminished");
           else if (deviation == 3)
               textIntervalDeviated += i18n("a triply augmented");
           else if (deviation == -4)
               textIntervalDeviated += i18n("a quadruply diminished");
           else if (deviation == 4)
               textIntervalDeviated += i18n("a quadruply augmented");
           else if (deviation == 0)
               textIntervalDeviated += i18n("a perfect");
           else
               textIntervalDeviated += i18n("an (unknown, %1)").arg(deviation);
           break;
        // Then the major/minor:
        case 1: // second
        case 2: // third
        case 5: // sixth
        case 6: // seventh
           if (deviation == -1)
               textIntervalDeviated += i18n("a minor");
           else if (deviation == 0)
               textIntervalDeviated += i18n("a major");
           else if (deviation == -2)
               textIntervalDeviated += i18n("a diminished");
           else if (deviation == 1)
               textIntervalDeviated += i18n("an augmented");
           else if (deviation == -3)
               textIntervalDeviated += i18n("a doubly diminished");
           else if (deviation == 2)
               textIntervalDeviated += i18n("a doubly augmented");
           else if (deviation == -4)
               textIntervalDeviated += i18n("a triply diminished");
           else if (deviation == 3)
               textIntervalDeviated += i18n("a triply augmented");
           else if (deviation == 4)
               textIntervalDeviated += i18n("a quadruply augmented");
           else if (deviation == 0)
               textIntervalDeviated += i18n("a perfect");
           else
               textIntervalDeviated += i18n("an (unknown, %1)").arg(deviation);
           break;
        default:
           textIntervalDeviated += i18n("an (unknown)");
        }
        switch (displayIntervalDiatonic % 7)
	{
	case 0:
	    // Note (hjj):
	    // "1 octave and a diminished octave" is better than
	    // "2 octaves and a diminished unison"
	    if (octaves > 0) {
	      textInterval += i18n("%1 octave").arg(textIntervalDeviated);
	      octaves--;
	    } else if (octaves < 0) {
	      textInterval += i18n("%1 octave").arg(textIntervalDeviated);
	      octaves++;
	    } else {
	      textInterval += i18n("%1 unison").arg(textIntervalDeviated);
	    }
	    break;
	case 1:
	    textInterval += i18n("%1 second").arg(textIntervalDeviated);
	    break;
	case 2:
	    textInterval += i18n("%1 third").arg(textIntervalDeviated);
	    break;
	case 3:
	    textInterval += i18n("%1 fourth").arg(textIntervalDeviated);
	    break;
	case 4:
	    textInterval += i18n("%1 fifth").arg(textIntervalDeviated);
	    break;
	case 5:
	    textInterval += i18n("%1 sixth").arg(textIntervalDeviated);
	    break;
	case 6:
	    textInterval += i18n("%1 seventh").arg(textIntervalDeviated);
	    break;
        default:
	    textInterval += i18n("%1").arg(textIntervalDeviated);
        }
    }
    
    if (displayIntervalChromatic != 0 || displayIntervalDiatonic != 0)
    {
        if (!down)
        {
	    if (octaves != 0) {
		if (showStep) {
		    return i18n("up 1 octave and %1",
		           "up %n octaves and %1",
			   octaves).arg(textInterval);
		} else {
		    return i18n("up 1 octave",
		           "up %n octaves",
			   octaves);
		}
	    } else {
		return i18n("up %1").arg(textInterval);
	    }
        }
        else
        {
	    if (octaves != 0) {
		if (showStep) {
		    return i18n("down 1 octave and %1",
		           "down %n octaves and %1",
			   octaves).arg(textInterval);
		} else {
		    return i18n("down 1 octave",
		           "down %n octaves",
			   octaves);
		}
	    } else {
		return i18n("down %1").arg(textInterval);
	    }
        }
    } else {
	return i18n("a perfect unison");
    }
}

void
IntervalDialog::slotSetTargetNote(int pitch, int octave, int step)
{
    intervalChromatic = pitch - m_referencenote->getPitch();
    intervalDiatonic = (octave * 7 + step) - (m_referencenote->getOctave() * 7 + m_referencenote->getStep());

    m_intervalLabel->setText( getIntervalName( intervalDiatonic, intervalChromatic ) );
}

void
IntervalDialog::slotSetReferenceNote(int pitch, int octave, int step)
{
    // recalculate target note based on reference note and current interval
    int pitch_new = pitch + intervalChromatic;
    int diatonic_new = (octave * 7 + step) + intervalDiatonic;
    int octave_new = diatonic_new / 7;
    int step_new = diatonic_new % 7;

    m_targetnote->slotSetNote( pitch_new, octave_new, step_new );
}

bool
IntervalDialog::getChangeKey()
{
    if (m_transposeChangingKey == NULL)
    {
        return false;
    }
    else
    {
        return m_transposeChangingKey->isChecked();
    }
}

bool
IntervalDialog::getTransposeSegmentBack()
{
    if (m_transposeSegmentBack == NULL)
    {
        return false;
    }
    else
    {
        return m_transposeSegmentBack->isChecked();	
    }
}

}
#include "IntervalDialog.moc"