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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
|
/* -*- 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 "ChordNameRuler.h"
#include "misc/Debug.h"
#include <klocale.h>
#include "misc/Strings.h"
#include "base/AnalysisTypes.h"
#include "base/Composition.h"
#include "base/CompositionTimeSliceAdapter.h"
#include "base/Instrument.h"
#include "base/NotationTypes.h"
#include "base/Profiler.h"
#include "base/PropertyName.h"
#include "base/NotationQuantizer.h"
#include "base/RefreshStatus.h"
#include "base/RulerScale.h"
#include "base/Segment.h"
#include "base/Selection.h"
#include "base/Studio.h"
#include "base/Track.h"
#include "document/RosegardenGUIDoc.h"
#include "document/MultiViewCommandHistory.h"
#include "gui/general/GUIPalette.h"
#include <tqfont.h>
#include <tqfontmetrics.h>
#include <tqobject.h>
#include <tqpainter.h>
#include <tqrect.h>
#include <tqsize.h>
#include <tqtooltip.h>
#include <tqwidget.h>
namespace Rosegarden
{
ChordNameRuler::ChordNameRuler(RulerScale *rulerScale,
RosegardenGUIDoc *doc,
double xorigin,
int height,
TQWidget *parent,
const char *name) :
TQWidget(parent, name),
m_xorigin(xorigin),
m_height(height),
m_currentXOffset(0),
m_width( -1),
m_ready(false),
m_rulerScale(rulerScale),
m_composition(&doc->getComposition()),
m_regetSegmentsOnChange(true),
m_currentSegment(0),
m_studio(0),
m_chordSegment(0),
m_fontMetrics(m_boldFont),
TEXT_FORMAL_X("TextFormalX"),
TEXT_ACTUAL_X("TextActualX")
{
m_font.setPointSize(11);
m_font.setPixelSize(12);
m_boldFont.setPointSize(11);
m_boldFont.setPixelSize(12);
m_boldFont.setBold(true);
m_fontMetrics = TQFontMetrics(m_boldFont);
setBackgroundColor(GUIPalette::getColour(GUIPalette::ChordNameRulerBackground));
m_compositionRefreshStatusId = m_composition->getNewRefreshStatusId();
TQObject::connect(doc->getCommandHistory(), TQT_SIGNAL(commandExecuted()),
this, TQT_SLOT(update()));
TQToolTip::add
(this, i18n("Chord name ruler.\nTurn it on and off from the Settings->Rulers menu."));
}
ChordNameRuler::ChordNameRuler(RulerScale *rulerScale,
RosegardenGUIDoc *doc,
std::vector<Segment *> &segments,
double xorigin,
int height,
TQWidget *parent,
const char *name) :
TQWidget(parent, name),
m_xorigin(xorigin),
m_height(height),
m_currentXOffset(0),
m_width( -1),
m_ready(false),
m_rulerScale(rulerScale),
m_composition(&doc->getComposition()),
m_regetSegmentsOnChange(false),
m_currentSegment(0),
m_studio(0),
m_chordSegment(0),
m_fontMetrics(m_boldFont),
TEXT_FORMAL_X("TextFormalX"),
TEXT_ACTUAL_X("TextActualX")
{
m_font.setPointSize(11);
m_font.setPixelSize(12);
m_boldFont.setPointSize(11);
m_boldFont.setPixelSize(12);
m_boldFont.setBold(true);
m_fontMetrics = TQFontMetrics(m_boldFont);
setBackgroundColor(GUIPalette::getColour(GUIPalette::ChordNameRulerBackground));
m_compositionRefreshStatusId = m_composition->getNewRefreshStatusId();
TQObject::connect(doc->getCommandHistory(), TQT_SIGNAL(commandExecuted()),
this, TQT_SLOT(update()));
for (std::vector<Segment *>::iterator i = segments.begin();
i != segments.end(); ++i) {
m_segments.insert(SegmentRefreshMap::value_type
(*i, (*i)->getNewRefreshStatusId()));
}
}
ChordNameRuler::~ChordNameRuler()
{
delete m_chordSegment;
}
void
ChordNameRuler::setReady()
{
m_ready = true;
update();
}
void
ChordNameRuler::setCurrentSegment(Segment *segment)
{
m_currentSegment = segment;
}
void
ChordNameRuler::setStudio(Studio *studio)
{
m_studio = studio;
}
void
ChordNameRuler::slotScrollHoriz(int x)
{
int w = width(), h = height();
int dx = x - ( -m_currentXOffset);
m_currentXOffset = -x;
if (dx == 0)
return ;
if (dx > w*7 / 8 || dx < -w*7 / 8) {
update();
return ;
}
if (dx > 0) { // moving right, so the existing stuff moves left
bitBlt(this, 0, 0, this, dx, 0, w - dx, h);
tqrepaint(w - dx, 0, dx, h);
} else { // moving left, so the existing stuff moves right
bitBlt(this, -dx, 0, this, 0, 0, w + dx, h);
tqrepaint(0, 0, -dx, h);
}
}
TQSize
ChordNameRuler::tqsizeHint() const
{
double width =
m_rulerScale->getBarPosition(m_rulerScale->getLastVisibleBar()) +
m_rulerScale->getBarWidth(m_rulerScale->getLastVisibleBar());
NOTATION_DEBUG << "Returning chord-label ruler width as " << width << endl;
TQSize res(std::max(int(width), m_width), m_height);
return res;
}
TQSize
ChordNameRuler::tqminimumSizeHint() const
{
double firstBarWidth = m_rulerScale->getBarWidth(0);
TQSize res = TQSize(int(firstBarWidth), m_height);
return res;
}
void
ChordNameRuler::recalculate(timeT from, timeT to)
{
if (!m_ready)
return ;
Profiler profiler("ChordNameRuler::recalculate");
NOTATION_DEBUG << "ChordNameRuler[" << this << "]::recalculate" << endl;
bool regetSegments = false;
enum RecalcLevel { RecalcNone, RecalcVisible, RecalcWhole };
RecalcLevel level = RecalcNone;
if (m_segments.empty()) {
regetSegments = true;
} else if (m_regetSegmentsOnChange) {
RefreshtqStatus &rs =
m_composition->getRefreshtqStatus(m_compositionRefreshStatusId);
if (rs.needsRefresh()) {
rs.setNeedsRefresh(false);
regetSegments = true;
}
}
if (regetSegments) {
SegmentSelection ss;
for (Composition::iterator ci = m_composition->begin();
ci != m_composition->end(); ++ci) {
if (m_studio) {
TrackId ti = (*ci)->getTrack();
Instrument *instr = m_studio->getInstrumentById
(m_composition->getTrackById(ti)->getInstrument());
if (instr &&
instr->getInstrumentType() == Instrument::Midi &&
instr->isPercussion()) {
continue;
}
}
ss.insert(*ci);
}
std::vector<SegmentRefreshMap::iterator> eraseThese;
for (SegmentRefreshMap::iterator si = m_segments.begin();
si != m_segments.end(); ++si) {
if (ss.find(si->first) == ss.end()) {
eraseThese.push_back(si);
level = RecalcWhole;
NOTATION_DEBUG << "Segment deleted, updating (now have " << m_segments.size() << " segments)" << endl;
}
}
for (std::vector<SegmentRefreshMap::iterator>::iterator ei = eraseThese.begin();
ei != eraseThese.end(); ++ei) {
m_segments.erase(*ei);
}
for (SegmentSelection::iterator si = ss.begin();
si != ss.end(); ++si) {
if (m_segments.find(*si) == m_segments.end()) {
m_segments.insert(SegmentRefreshMap::value_type
(*si, (*si)->getNewRefreshStatusId()));
level = RecalcWhole;
NOTATION_DEBUG << "Segment created, adding (now have " << m_segments.size() << " segments)" << endl;
}
}
if (m_currentSegment &&
ss.find(m_currentSegment) == ss.end()) {
m_currentSegment = 0;
level = RecalcWhole;
}
}
if (!m_chordSegment)
m_chordSegment = new Segment();
if (m_segments.empty())
return ;
SegmentRefreshtqStatus overalltqStatus;
overalltqStatus.setNeedsRefresh(false);
for (SegmentRefreshMap::iterator i = m_segments.begin();
i != m_segments.end(); ++i) {
SegmentRefreshtqStatus &status =
i->first->getRefreshtqStatus(i->second);
if (status.needsRefresh()) {
overalltqStatus.push(status.from(), status.to());
}
}
// We now have the overall area affected by these changes, across
// all segments. If it's entirely within our displayed area, just
// recalculate the displayed area; if it overlaps, calculate the
// union of the two areas; if it's entirely without, calculate
// nothing.
if (level == RecalcNone) {
if (from == to) {
NOTATION_DEBUG << "ChordNameRuler::recalculate: from==to, recalculating all" << endl;
level = RecalcWhole;
} else if (overalltqStatus.from() == overalltqStatus.to()) {
NOTATION_DEBUG << "ChordNameRuler::recalculate: overalltqStatus.from==overalltqStatus.to, ignoring" << endl;
level = RecalcNone;
} else if (overalltqStatus.from() >= from && overalltqStatus.to() <= to) {
NOTATION_DEBUG << "ChordNameRuler::recalculate: change is " << overalltqStatus.from() << "->" << overalltqStatus.to() << ", I show " << from << "->" << to << ", recalculating visible area" << endl;
level = RecalcVisible;
} else if (overalltqStatus.from() >= to || overalltqStatus.to() <= from) {
NOTATION_DEBUG << "ChordNameRuler::recalculate: change is " << overalltqStatus.from() << "->" << overalltqStatus.to() << ", I show " << from << "->" << to << ", ignoring" << endl;
level = RecalcNone;
} else {
NOTATION_DEBUG << "ChordNameRuler::recalculate: change is " << overalltqStatus.from() << "->" << overalltqStatus.to() << ", I show " << from << "->" << to << ", recalculating whole" << endl;
level = RecalcWhole;
}
}
if (level == RecalcNone)
return ;
for (SegmentRefreshMap::iterator i = m_segments.begin();
i != m_segments.end(); ++i) {
i->first->getRefreshtqStatus(i->second).setNeedsRefresh(false);
}
if (!m_currentSegment) { //!!! arbitrary, must do better
//!!! need a segment starting at zero or so with a clef and key in it!
m_currentSegment = m_segments.begin()->first;
}
/*!!!
for (Composition::iterator ci = m_composition->begin();
ci != m_composition->end(); ++ci) {
if ((*ci)->getEndMarkerTime() >= from &&
((*ci)->getStartTime() <= from ||
(clefKeySegment &&
(*ci)->getStartTime() < clefKeySegment->getStartTime()))) {
clefKeySegment = *ci;
}
}
if (!clefKeySegment) return;
}
*/
if (level == RecalcWhole) {
m_chordSegment->clear();
timeT clefKeyTime = m_currentSegment->getStartTime();
//(from < m_currentSegment->getStartTime() ?
// m_currentSegment->getStartTime() : from);
Clef clef = m_currentSegment->getClefAtTime(clefKeyTime);
m_chordSegment->insert(clef.getAsEvent( -1));
::Rosegarden::Key key = m_currentSegment->getKeyAtTime(clefKeyTime);
m_chordSegment->insert(key.getAsEvent( -1));
from = 0;
to = 0;
} else {
Segment::iterator i = m_chordSegment->findTime(from);
Segment::iterator j = m_chordSegment->findTime(to);
m_chordSegment->erase(i, j);
}
SegmentSelection selection;
for (SegmentRefreshMap::iterator si = m_segments.begin(); si != m_segments.end();
++si) {
selection.insert(si->first);
}
CompositionTimeSliceAdapter adapter(m_composition, &selection, from, to);
AnalysisHelper helper;
helper.labelChords(adapter, *m_chordSegment, m_composition->getNotationQuantizer());
}
void
ChordNameRuler::paintEvent(TQPaintEvent* e)
{
if (!m_composition || !m_ready)
return ;
NOTATION_DEBUG << "*** Chord Name Ruler: paintEvent" << endl;
Profiler profiler1("ChordNameRuler::paintEvent (whole)");
TQPainter paint(this);
paint.setPen(GUIPalette::getColour(GUIPalette::ChordNameRulerForeground));
paint.setClipRegion(e->region());
paint.setClipRect(e->rect().normalize());
TQRect clipRect = paint.clipRegion().boundingRect();
timeT from = m_rulerScale->getTimeForX
(clipRect.x() - m_currentXOffset - m_xorigin - 50);
timeT to = m_rulerScale->getTimeForX
(clipRect.x() + clipRect.width() - m_currentXOffset - m_xorigin + 50);
recalculate(from, to);
if (!m_chordSegment)
return ;
Profiler profiler2("ChordNameRuler::paintEvent (paint)");
TQRect boundsForHeight = m_fontMetrics.boundingRect("^j|lM");
int fontHeight = boundsForHeight.height();
int textY = (height() - 6) / 2 + fontHeight / 2;
double prevX = 0;
timeT keyAt = from - 1;
std::string keyText;
NOTATION_DEBUG << "*** Chord Name Ruler: paint " << from << " -> " << to << endl;
for (Segment::iterator i = m_chordSegment->findTime(from);
i != m_chordSegment->findTime(to); ++i) {
NOTATION_DEBUG << "type " << (*i)->getType() << " at " << (*i)->getAbsoluteTime()
<< endl;
if (!(*i)->isa(Text::EventType) ||
!(*i)->has(Text::TextPropertyName) ||
!(*i)->has(Text::TextTypePropertyName))
continue;
std::string text((*i)->get
<String>(Text::TextPropertyName));
if ((*i)->get
<String>(Text::TextTypePropertyName) == Text::KeyName) {
timeT myTime = (*i)->getAbsoluteTime();
if (myTime == keyAt && text == keyText)
continue;
else {
keyAt = myTime;
keyText = text;
}
}
double x = m_rulerScale->getXForTime((*i)->getAbsoluteTime());
(*i)->set
<Int>(TEXT_FORMAL_X, (long)x);
TQRect textBounds = m_fontMetrics.boundingRect(strtoqstr(text));
int width = textBounds.width();
x -= width / 2;
if (prevX >= x - 3)
x = prevX + 3;
(*i)->set
<Int>(TEXT_ACTUAL_X, long(x));
prevX = x + width;
}
for (Segment::iterator i = m_chordSegment->findTime(from);
i != m_chordSegment->findTime(to); ++i) {
if (!(*i)->isa(Text::EventType))
continue;
std::string text((*i)->get
<String>(Text::TextPropertyName));
std::string type((*i)->get
<String>(Text::TextTypePropertyName));
if (!(*i)->has(TEXT_FORMAL_X))
continue;
long formalX = (*i)->get
<Int>(TEXT_FORMAL_X);
long actualX = (*i)->get
<Int>(TEXT_ACTUAL_X);
formalX += m_currentXOffset + long(m_xorigin);
actualX += m_currentXOffset + long(m_xorigin);
paint.drawLine(formalX, height() - 4, formalX, height());
if (type == Text::KeyName) {
paint.setFont(m_boldFont);
} else {
paint.setFont(m_font);
}
paint.drawText(actualX, textY, strtoqstr(text));
}
}
}
#include "ChordNameRuler.moc"
|