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
524
525
526
527
528
529
530
531
532
|
/* -*- 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 "SegmentSelector.h"
#include "base/Event.h"
#include <klocale.h>
#include "misc/Debug.h"
#include "base/Composition.h"
#include "base/RealTime.h"
#include "base/SnapGrid.h"
#include "base/Selection.h"
#include "base/Track.h"
#include "commands/segment/SegmentQuickCopyCommand.h"
#include "commands/segment/SegmentReconfigureCommand.h"
#include "CompositionItemHelper.h"
#include "CompositionModel.h"
#include "CompositionView.h"
#include "document/RosegardenGUIDoc.h"
#include "document/ConfigGroups.h"
#include "gui/general/BaseTool.h"
#include "gui/general/RosegardenCanvasView.h"
#include "SegmentPencil.h"
#include "SegmentResizer.h"
#include "SegmentTool.h"
#include "SegmentToolBox.h"
#include <kapplication.h>
#include <kconfig.h>
#include <tqcursor.h>
#include <tqevent.h>
#include <tqpoint.h>
#include <tqrect.h>
#include <tqstring.h>
namespace Rosegarden
{
SegmentSelector::SegmentSelector(CompositionView *c, RosegardenGUIDoc *d)
: SegmentTool(c, d),
m_segmentAddMode(false),
m_segmentCopyMode(false),
m_segmentQuickCopyDone(false),
m_buttonPressed(false),
m_selectionMoveStarted(false),
m_dispatchTool(0)
{
RG_DEBUG << "SegmentSelector()\n";
}
SegmentSelector::~SegmentSelector()
{}
void SegmentSelector::ready()
{
m_canvas->viewport()->setCursor(TQt::arrowCursor);
connect(m_canvas, TQT_SIGNAL(contentsMoving (int, int)),
this, TQT_SLOT(slotCanvasScrolled(int, int)));
setContextHelp(i18n("Click and drag to select segments"));
}
void SegmentSelector::stow()
{}
void SegmentSelector::slotCanvasScrolled(int newX, int newY)
{
TQMouseEvent tmpEvent(TQEvent::MouseMove,
m_canvas->viewport()->mapFromGlobal(TQCursor::pos()) + TQPoint(newX, newY),
Qt::NoButton, Qt::NoButton);
handleMouseMove(&tmpEvent);
}
void
SegmentSelector::handleMouseButtonPress(TQMouseEvent *e)
{
RG_DEBUG << "SegmentSelector::handleMouseButtonPress\n";
m_buttonPressed = true;
CompositionItem item = m_canvas->getFirstItemAt(e->pos());
// If we're in segmentAddMode or not clicking on an item then we don't
// clear the selection vector. If we're clicking on an item and it's
// not in the selection - then also clear the selection.
//
if ((!m_segmentAddMode && !item) ||
(!m_segmentAddMode && !(m_canvas->getModel()->isSelected(item)))) {
m_canvas->getModel()->clearSelected();
}
if (item) {
// Fifteen percent of the width of the SegmentItem, up to 10px
//
int threshold = int(float(item->rect().width()) * 0.15);
if (threshold == 0) threshold = 1;
if (threshold > 10) threshold = 10;
bool start = false;
// Resize if we're dragging from the edge, provided we aren't
// in segment-add mode with at least one segment already
// selected -- as we aren't able to resize multiple segments
// at once, we should assume the segment-add aspect takes
// priority
if ((!m_segmentAddMode ||
!m_canvas->getModel()->haveSelection()) &&
SegmentResizer::cursorIsCloseEnoughToEdge(item, e->pos(), threshold, start)) {
SegmentResizer* resizer =
dynamic_cast<SegmentResizer*>(getToolBox()->getTool(SegmentResizer::ToolName));
resizer->setEdgeThreshold(threshold);
// For the moment we only allow resizing of a single segment
// at a time.
//
m_canvas->getModel()->clearSelected();
m_dispatchTool = resizer;
m_dispatchTool->ready(); // set mouse cursor
m_dispatchTool->handleMouseButtonPress(e);
return ;
}
bool selecting = true;
if (m_segmentAddMode && m_canvas->getModel()->isSelected(item)) {
selecting = false;
} else {
// put the segment in 'move' mode only if it's being selected
m_canvas->getModel()->startChange(item, CompositionModel::ChangeMove);
}
m_canvas->getModel()->setSelected(item, selecting);
// Moving
//
// RG_DEBUG << "SegmentSelector::handleMouseButtonPress - m_currentItem = " << item << endl;
m_currentItem = item;
m_clickPoint = e->pos();
int guideX = item->rect().x();
int guideY = item->rect().y();
m_canvas->setGuidesPos(guideX, guideY);
m_canvas->setDrawGuides(true);
} else {
// Add on middle button or ctrl+left - bounding box on rest
//
if (e->button() == Qt::MidButton ||
(e->button() == Qt::LeftButton && (e->state() & TQ_ControlButton))) {
m_dispatchTool = getToolBox()->getTool(SegmentPencil::ToolName);
if (m_dispatchTool) {
m_dispatchTool->ready(); // set mouse cursor
m_dispatchTool->handleMouseButtonPress(e);
}
return ;
} else {
m_canvas->setSelectionRectPos(e->pos());
m_canvas->setDrawSelectionRect(true);
if (!m_segmentAddMode)
m_canvas->getModel()->clearSelected();
}
}
// Tell the RosegardenGUIView that we've selected some new Segments -
// when the list is empty we're just unselecting.
//
m_canvas->getModel()->signalSelection();
m_passedInertiaEdge = false;
}
void
SegmentSelector::handleMouseButtonRelease(TQMouseEvent *e)
{
m_buttonPressed = false;
// Hide guides and stuff
//
m_canvas->setDrawGuides(false);
m_canvas->hideTextFloat();
if (m_dispatchTool) {
m_dispatchTool->handleMouseButtonRelease(e);
m_dispatchTool = 0;
m_canvas->viewport()->setCursor(TQt::arrowCursor);
return ;
}
int startDragTrackPos = m_canvas->grid().getYBin(m_clickPoint.y());
int currentTrackPos = m_canvas->grid().getYBin(e->pos().y());
int trackDiff = currentTrackPos - startDragTrackPos;
if (!m_currentItem) {
m_canvas->setDrawSelectionRect(false);
m_canvas->getModel()->finalizeSelectionRect();
m_canvas->getModel()->signalSelection();
return ;
}
m_canvas->viewport()->setCursor(TQt::arrowCursor);
Composition &comp = m_doc->getComposition();
if (m_canvas->getModel()->isSelected(m_currentItem)) {
CompositionModel::itemcontainer& changingItems = m_canvas->getModel()->getChangingItems();
CompositionModel::itemcontainer::iterator it;
if (changeMade()) {
SegmentReconfigureCommand *command =
new SegmentReconfigureCommand
(m_selectedItems.size() == 1 ? i18n("Move Segment") :
i18n("Move Segments"));
for (it = changingItems.begin();
it != changingItems.end();
it++) {
CompositionItem item = *it;
Segment* segment = CompositionItemHelper::getSegment(item);
TrackId origTrackId = segment->getTrack();
int trackPos = comp.getTrackPositionById(origTrackId);
trackPos += trackDiff;
if (trackPos < 0) {
trackPos = 0;
} else if (trackPos >= comp.getNbTracks()) {
trackPos = comp.getNbTracks() - 1;
}
Track *newTrack = comp.getTrackByPosition(trackPos);
int newTrackId = origTrackId;
if (newTrack) newTrackId = newTrack->getId();
timeT itemStartTime = CompositionItemHelper::getStartTime
(item, m_canvas->grid());
// We absolutely don't want to snap the end time to
// the grid. We want it to remain exactly the same as
// it was, but relative to the new start time.
timeT itemEndTime = itemStartTime + segment->getEndMarkerTime()
- segment->getStartTime();
// std::cerr << "releasing segment " << segment << ": mouse started at track " << startDragTrackPos << ", is now at " << currentTrackPos << ", diff is " << trackDiff << ", moving from track pos " << comp.getTrackPositionById(origTrackId) << " to " << trackPos << ", id " << origTrackId << " to " << newTrackId << std::endl;
command->addSegment(segment,
itemStartTime,
itemEndTime,
newTrackId);
}
addCommandToHistory(command);
}
m_canvas->getModel()->endChange();
m_canvas->slotUpdateSegmentsDrawBuffer();
}
// if we've just finished a quick copy then drop the Z level back
if (m_segmentQuickCopyDone) {
m_segmentQuickCopyDone = false;
// m_currentItem->setZ(2); // see SegmentItem::setSelected --??
}
setChangeMade(false);
m_selectionMoveStarted = false;
m_currentItem = CompositionItem();
setContextHelpFor(e->pos());
}
int
SegmentSelector::handleMouseMove(TQMouseEvent *e)
{
if (!m_buttonPressed) {
setContextHelpFor(e->pos(), (e->state() & TQt::ControlButton));
return RosegardenCanvasView::NoFollow;
}
if (m_dispatchTool) {
return m_dispatchTool->handleMouseMove(e);
}
Composition &comp = m_doc->getComposition();
if (!m_currentItem) {
// RG_DEBUG << "SegmentSelector::handleMouseMove: no current item\n";
// do a bounding box
TQRect selectionRect = m_canvas->getSelectionRect();
m_canvas->setDrawSelectionRect(true);
// same as for notation view
int w = int(e->pos().x() - selectionRect.x());
int h = int(e->pos().y() - selectionRect.y());
if (w > 0)
++w;
else
--w;
if (h > 0)
++h;
else
--h;
// Translate these points
//
m_canvas->setSelectionRectSize(w, h);
m_canvas->getModel()->signalSelection();
return RosegardenCanvasView::FollowHorizontal | RosegardenCanvasView::FollowVertical;
}
m_canvas->viewport()->setCursor(TQt::sizeAllCursor);
if (m_segmentCopyMode && !m_segmentQuickCopyDone) {
KMacroCommand *mcommand = new KMacroCommand
(SegmentQuickCopyCommand::getGlobalName());
SegmentSelection selectedItems = m_canvas->getSelectedSegments();
SegmentSelection::iterator it;
for (it = selectedItems.begin();
it != selectedItems.end();
it++) {
SegmentQuickCopyCommand *command =
new SegmentQuickCopyCommand(*it);
mcommand->addCommand(command);
}
addCommandToHistory(mcommand);
// generate SegmentItem
//
m_canvas->updateContents();
m_segmentQuickCopyDone = true;
}
m_canvas->setSnapGrain(true);
int startDragTrackPos = m_canvas->grid().getYBin(m_clickPoint.y());
int currentTrackPos = m_canvas->grid().getYBin(e->pos().y());
int trackDiff = currentTrackPos - startDragTrackPos;
if (m_canvas->getModel()->isSelected(m_currentItem)) {
if (!m_canvas->isFineGrain()) {
setContextHelp(i18n("Hold Shift to avoid snapping to beat grid"));
} else {
clearContextHelp();
}
// RG_DEBUG << "SegmentSelector::handleMouseMove: current item is selected\n";
if (!m_selectionMoveStarted) { // start move on selected items only once
m_canvas->getModel()->startChangeSelection(CompositionModel::ChangeMove);
m_selectionMoveStarted = true;
}
CompositionModel::itemcontainer& changingItems = m_canvas->getModel()->getChangingItems();
setCurrentItem(CompositionItemHelper::findSiblingCompositionItem(changingItems, m_currentItem));
CompositionModel::itemcontainer::iterator it;
int guideX = 0;
int guideY = 0;
for (it = changingItems.begin();
it != changingItems.end();
++it) {
// RG_DEBUG << "SegmentSelector::handleMouseMove() : movingItem at "
// << (*it)->rect().x() << "," << (*it)->rect().y() << endl;
int dx = e->pos().x() - m_clickPoint.x(),
dy = e->pos().y() - m_clickPoint.y();
const int inertiaDistance = m_canvas->grid().getYSnap() / 3;
if (!m_passedInertiaEdge &&
(dx < inertiaDistance && dx > -inertiaDistance) &&
(dy < inertiaDistance && dy > -inertiaDistance)) {
return RosegardenCanvasView::NoFollow;
} else {
m_passedInertiaEdge = true;
}
timeT newStartTime = m_canvas->grid().snapX((*it)->savedRect().x() + dx);
int newX = int(m_canvas->grid().getRulerScale()->getXForTime(newStartTime));
int trackPos = m_canvas->grid().getYBin((*it)->savedRect().y());
// std::cerr << "segment " << *it << ": mouse started at track " << startDragTrackPos << ", is now at " << currentTrackPos << ", trackPos from " << trackPos << " to ";
trackPos += trackDiff;
// std::cerr << trackPos << std::endl;
if (trackPos < 0) {
trackPos = 0;
} else if (trackPos >= comp.getNbTracks()) {
trackPos = comp.getNbTracks() - 1;
}
int newY = m_canvas->grid().getYBinCoordinate(trackPos);
(*it)->moveTo(newX, newY);
setChangeMade(true);
}
if (changeMade())
m_canvas->getModel()->signalContentChange();
guideX = m_currentItem->rect().x();
guideY = m_currentItem->rect().y();
m_canvas->setGuidesPos(guideX, guideY);
timeT currentItemStartTime = m_canvas->grid().snapX(m_currentItem->rect().x());
RealTime time = comp.getElapsedRealTime(currentItemStartTime);
TQString ms;
ms.sprintf("%03d", time.msec());
int bar, beat, fraction, remainder;
comp.getMusicalTimeForAbsoluteTime(currentItemStartTime, bar, beat, fraction, remainder);
TQString posString = TQString("%1.%2s (%3, %4, %5)")
.arg(time.sec).arg(ms)
.arg(bar + 1).arg(beat).arg(fraction);
m_canvas->setTextFloat(guideX + 10, guideY - 30, posString);
m_canvas->updateContents();
} else {
// RG_DEBUG << "SegmentSelector::handleMouseMove: current item not selected\n";
}
return RosegardenCanvasView::FollowHorizontal | RosegardenCanvasView::FollowVertical;
}
void SegmentSelector::setContextHelpFor(TQPoint p, bool ctrlPressed)
{
kapp->config()->setGroup(GeneralOptionsConfigGroup);
if (!kapp->config()->readBoolEntry("toolcontexthelp", true)) return;
CompositionItem item = m_canvas->getFirstItemAt(p);
if (!item) {
setContextHelp(i18n("Click and drag to select segments; middle-click and drag to draw an empty segment"));
} else {
// Same logic as in handleMouseButtonPress to establish
// whether we'd be moving or resizing
int threshold = int(float(item->rect().width()) * 0.15);
if (threshold == 0) threshold = 1;
if (threshold > 10) threshold = 10;
bool start = false;
if ((!m_segmentAddMode ||
!m_canvas->getModel()->haveSelection()) &&
SegmentResizer::cursorIsCloseEnoughToEdge(item, p,
threshold, start)) {
if (!ctrlPressed) {
setContextHelp(i18n("Click and drag to resize a segment; hold Ctrl as well to rescale its contents"));
} else {
setContextHelp(i18n("Click and drag to rescale segment"));
}
} else {
if (m_canvas->getModel()->haveMultipleSelection()) {
if (!ctrlPressed) {
setContextHelp(i18n("Click and drag to move segments; hold Ctrl as well to copy them"));
} else {
setContextHelp(i18n("Click and drag to copy segments"));
}
} else {
if (!ctrlPressed) {
setContextHelp(i18n("Click and drag to move segment; hold Ctrl as well to copy it; double-click to edit"));
} else {
setContextHelp(i18n("Click and drag to copy segment"));
}
}
}
}
}
const TQString SegmentSelector::ToolName = "segmentselector";
}
#include "SegmentSelector.moc"
|