summaryrefslogtreecommitdiffstats
path: root/src/gui/editors/matrix/MatrixSelector.cpp
blob: 14d6a0c3562e44a42e21325674b2232490a69d09 (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
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
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
/* -*- 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 "MatrixSelector.h"

#include "base/BaseProperties.h"
#include <klocale.h>
#include <kstddirs.h>
#include "base/Event.h"
#include "base/NotationTypes.h"
#include "base/Selection.h"
#include "base/ViewElement.h"
#include "commands/edit/EventEditCommand.h"
#include "gui/dialogs/EventEditDialog.h"
#include "gui/dialogs/SimpleEventEditDialog.h"
#include "gui/general/EditTool.h"
#include "gui/general/EditToolBox.h"
#include "gui/general/GUIPalette.h"
#include "gui/general/RosegardenCanvasView.h"
#include "MatrixElement.h"
#include "MatrixMover.h"
#include "MatrixPainter.h"
#include "MatrixResizer.h"
#include "MatrixStaff.h"
#include "MatrixTool.h"
#include "MatrixView.h"
#include <kaction.h>
#include <kglobal.h>
#include <kapplication.h>
#include <kconfig.h>
#include <tqdialog.h>
#include <tqiconset.h>
#include <tqpoint.h>
#include <tqstring.h>
#include "misc/Debug.h"


namespace Rosegarden
{

MatrixSelector::MatrixSelector(MatrixView* view)
        : MatrixTool("MatrixSelector", view),
        m_selectionRect(0),
        m_updateRect(false),
        m_currentStaff(0),
        m_clickedElement(0),
        m_dispatchTool(0),
        m_justSelectedBar(false),
        m_matrixView(view),
        m_selectionToMerge(0)
{
    connect(m_parentView, TQT_SIGNAL(usedSelection()),
            this, TQT_SLOT(slotHideSelection()));

    new KAction(i18n("Switch to Draw Tool"), "pencil", 0, this,
                TQT_SLOT(slotDrawSelected()), actionCollection(),
                "draw");

    new KAction(i18n("Switch to Erase Tool"), "eraser", 0, this,
                TQT_SLOT(slotEraseSelected()), actionCollection(),
                "erase");

    new KAction(i18n("Switch to Move Tool"), "move", 0, this,
                TQT_SLOT(slotMoveSelected()), actionCollection(),
                "move");

    TQString pixmapDir = KGlobal::dirs()->findResource("appdata", "pixmaps/");
    TQCanvasPixmap pixmap(pixmapDir + "/toolbar/resize.xpm");
    TQIconSet icon = TQIconSet(pixmap);

    new KAction(i18n("Switch to Resize Tool"), icon, 0, this,
                TQT_SLOT(slotResizeSelected()), actionCollection(),
                "resize");

    createMenu("matrixselector.rc");
}

void MatrixSelector::handleEventRemoved(Event *event)
{
    if (m_dispatchTool)
        m_dispatchTool->handleEventRemoved(event);
    if (m_clickedElement && m_clickedElement->event() == event) {
        m_clickedElement = 0;
    }
}

void MatrixSelector::slotClickTimeout()
{
    m_justSelectedBar = false;
}

void MatrixSelector::handleLeftButtonPress(timeT time,
        int height,
        int staffNo,
        TQMouseEvent* e,
        ViewElement *element)
{
    MATRIX_DEBUG << "MatrixSelector::handleMousePress" << endl;

    if (m_justSelectedBar) {
        handleMouseTripleClick(time, height, staffNo, e, element);
        m_justSelectedBar = false;
        return ;
    }

    TQPoint p = m_mParentView->inverseMapPoint(e->pos());

    m_currentStaff = m_mParentView->getStaff(staffNo);

    // Do the merge selection thing
    //
    delete m_selectionToMerge; // you can safely delete 0, you know?
    const EventSelection *selectionToMerge = 0;
    if (e->state() & TQt::ShiftButton)
        selectionToMerge = m_mParentView->getCurrentSelection();

    m_selectionToMerge =
        (selectionToMerge ? new EventSelection(*selectionToMerge) : 0);

    // Now the rest of the element stuff
    //
    m_clickedElement = dynamic_cast<MatrixElement*>(element);

    if (m_clickedElement) {
        int x = int(m_clickedElement->getLayoutX());
        int width = m_clickedElement->getWidth();
        int resizeStart = int(double(width) * 0.85) + x;

        // max size of 10
        if ((x + width ) - resizeStart > 10)
            resizeStart = x + width - 10;

        if (p.x() > resizeStart) {
            m_dispatchTool = m_parentView->
                             getToolBox()->getTool(MatrixResizer::ToolName);
        } else {
            m_dispatchTool = m_parentView->
                             getToolBox()->getTool(MatrixMover::ToolName);
        }

        m_dispatchTool->ready();

        m_dispatchTool->handleLeftButtonPress(time,
                                              height,
                                              staffNo,
                                              e,
                                              element);
        return ;

    } else if (e->state() & TQt::ControlButton) {

        handleMidButtonPress(time, height, staffNo, e, element);
        return;

    } else {

        // Workaround for #930420 Positional error in sweep-selection box
        // boundary
        int zoomValue = (int)m_matrixView->m_hZoomSlider->getCurrentSize();
        MatrixStaff *staff = m_mParentView->getStaff(staffNo);
        int pitch = m_currentStaff->getHeightAtCanvasCoords(p.x(), p.y());
        int pitchCentreHeight = staff->getTotalHeight() -
                                pitch * staff->getLineSpacing() - 2; // 2 or ?
        int pitchLineHeight = pitchCentreHeight + staff->getLineSpacing() / 2;
        int drawHeight = p.y();
        if (drawHeight <= pitchLineHeight + 1 &&
                drawHeight >= pitchLineHeight - 1) {
            if (drawHeight == pitchLineHeight)
                drawHeight += 2;
            else
                drawHeight += 2 * (drawHeight - pitchLineHeight);
        }
        MATRIX_DEBUG << "#### MatrixSelector::handleLeftButtonPress() : zoom "
        << zoomValue
        << " pitch " << pitch
        << " pitchCentreHeight " << pitchCentreHeight
        << " pitchLineHeight " << pitchLineHeight
        << " lineSpacing " << staff->getLineSpacing()
        << " drawHeight " << drawHeight << endl;
        m_selectionRect->setX(int(p.x() / 4)*4); // more workaround for #930420
        m_selectionRect->setY(drawHeight);
        m_selectionRect->setSize(0, 0);

        m_selectionRect->show();
        m_updateRect = true;

        // Clear existing selection if we're not merging
        //
        if (!m_selectionToMerge) {
            m_mParentView->setCurrentSelection(0, false, true);
            m_mParentView->canvas()->update();
        }
    }

    //m_parentView->setCursorPosition(p.x());
}

void MatrixSelector::handleMidButtonPress(timeT time,
        int height,
        int staffNo,
        TQMouseEvent* e,
        ViewElement *element)
{
    m_clickedElement = 0; // should be used for left-button clicks only

    // Don't allow overlapping elements on the same channel
    if (dynamic_cast<MatrixElement*>(element))
        return ;

    m_dispatchTool = m_parentView->
                     getToolBox()->getTool(MatrixPainter::ToolName);

    m_dispatchTool->ready();

    m_dispatchTool->handleLeftButtonPress(time, height, staffNo, e, element);
}

void MatrixSelector::handleMouseDoubleClick(timeT ,
        int ,
        int staffNo,
        TQMouseEvent *ev,
        ViewElement *element)
{
    /*
        if (m_dispatchTool)
        {
            m_dispatchTool->handleMouseDoubleClick(time, height, staffNo, e, element);
        }
    */

    m_clickedElement = dynamic_cast<MatrixElement*>(element);

    MatrixStaff *staff = m_mParentView->getStaff(staffNo);
    if (!staff)
        return ;

    if (m_clickedElement) {

        if (m_clickedElement->event()->isa(Note::EventType) &&
                m_clickedElement->event()->has(BaseProperties::TRIGGER_SEGMENT_ID)) {

            int id = m_clickedElement->event()->get
                     <Int>
                     (BaseProperties::TRIGGER_SEGMENT_ID);
            emit editTriggerSegment(id);
            return ;
        }

        if (ev->state() & ShiftButton) { // advanced edit

            EventEditDialog dialog(m_mParentView, *m_clickedElement->event(), true);

            if (dialog.exec() == TQDialog::Accepted &&
                    dialog.isModified()) {

                EventEditCommand *command = new EventEditCommand
                                            (staff->getSegment(),
                                             m_clickedElement->event(),
                                             dialog.getEvent());

                m_mParentView->addCommandToHistory(command);
            }
        } else {

            SimpleEventEditDialog dialog(m_mParentView, m_mParentView->getDocument(),
                                         *m_clickedElement->event(), false);

            if (dialog.exec() == TQDialog::Accepted &&
                    dialog.isModified()) {

                EventEditCommand *command = new EventEditCommand
                                            (staff->getSegment(),
                                             m_clickedElement->event(),
                                             dialog.getEvent());

                m_mParentView->addCommandToHistory(command);
            }
        }

    } /*
    	  
          #988167: Matrix:Multiclick select methods don't work in matrix editor
          Postponing this, as it falls foul of world-matrix transformation
          etiquette and other such niceties
     
    	  else {
     
    	TQRect rect = staff->getBarExtents(ev->x(), ev->y());
     
    	m_selectionRect->setX(rect.x() + 2);
    	m_selectionRect->setY(rect.y());
    	m_selectionRect->setSize(rect.width() - 4, rect.height());
     
    	m_selectionRect->show();
    	m_updateRect = false;
    	
    	m_justSelectedBar = true;
    	TQTimer::singleShot(TQApplication::doubleClickInterval(), this,
    			   TQT_SLOT(slotClickTimeout()));
        } */
}

void MatrixSelector::handleMouseTripleClick(timeT t,
        int height,
        int staffNo,
        TQMouseEvent *ev,
        ViewElement *element)
{
    if (!m_justSelectedBar)
        return ;
    m_justSelectedBar = false;

    MatrixStaff *staff = m_mParentView->getStaff(staffNo);
    if (!staff)
        return ;

    if (m_clickedElement) {

        // should be safe, as we've already set m_justSelectedBar false
        handleLeftButtonPress(t, height, staffNo, ev, element);
        return ;

    } else {

        m_selectionRect->setX(staff->getX());
        m_selectionRect->setY(staff->getY());
        m_selectionRect->setSize(int(staff->getTotalWidth()) - 1,
                                 staff->getTotalHeight() - 1);

        m_selectionRect->show();
        m_updateRect = false;
    }
}

int MatrixSelector::handleMouseMove(timeT time, int height,
                                    TQMouseEvent *e)
{
    TQPoint p = m_mParentView->inverseMapPoint(e->pos());

    if (m_dispatchTool) {
        return m_dispatchTool->handleMouseMove(time, height, e);
    }


    if (!m_updateRect) {
        setContextHelpFor(e->pos(), 
                          getSnapGrid().getSnapSetting() == SnapGrid::NoSnap);
        return RosegardenCanvasView::NoFollow;
    } else {
        clearContextHelp();
    }

    int w = int(p.x() - m_selectionRect->x());
    int h = int(p.y() - m_selectionRect->y());

    // TQt rectangle dimensions appear to be 1-based
    if (w > 0)
        ++w;
    else
        --w;
    if (h > 0)
        ++h;
    else
        --h;

    // Workaround for #930420 Positional error in sweep-selection box boundary
    int wFix = (w > 0) ? 3 : 0;
    int hFix = (h > 0) ? 3 : 0;
    int xFix = (w < 0) ? 3 : 0;
    m_selectionRect->setSize(w - wFix, h - hFix);
    m_selectionRect->setX(m_selectionRect->x() + xFix);
    setViewCurrentSelection();
    m_selectionRect->setSize(w, h);
    m_selectionRect->setX(m_selectionRect->x() - xFix);
    m_mParentView->canvas()->update();

    return RosegardenCanvasView::FollowHorizontal | RosegardenCanvasView::FollowVertical;
}

void MatrixSelector::handleMouseRelease(timeT time, int height, TQMouseEvent *e)
{
    MATRIX_DEBUG << "MatrixSelector::handleMouseRelease" << endl;

    if (m_dispatchTool) {
        m_dispatchTool->handleMouseRelease(time, height, e);

        m_dispatchTool->stow();
        ready();

        // don't delete the tool as it's still part of the toolbox
        m_dispatchTool = 0;

        return ;
    }

    m_updateRect = false;

    if (m_clickedElement) {
        m_mParentView->setSingleSelectedEvent(m_currentStaff->getSegment(),
                                              m_clickedElement->event(),
                                              false, true);
        m_mParentView->canvas()->update();
        m_clickedElement = 0;

    } else if (m_selectionRect) {
        setViewCurrentSelection();
        m_selectionRect->hide();
        m_mParentView->canvas()->update();
    }

    // Tell anyone who's interested that the selection has changed
    emit gotSelection();

    setContextHelpFor(e->pos());
}

void MatrixSelector::ready()
{
    if (m_mParentView) {
        m_selectionRect = new TQCanvasRectangle(m_mParentView->canvas());
        m_selectionRect->hide();
        m_selectionRect->setPen(TQPen(GUIPalette::getColour(GUIPalette::SelectionRectangle), 2));

        m_mParentView->setCanvasCursor(TQt::arrowCursor);
        //m_mParentView->setPositionTracking(false);
    }

    connect(m_parentView->getCanvasView(), TQT_SIGNAL(contentsMoving (int, int)),
            this, TQT_SLOT(slotMatrixScrolled(int, int)));

    setContextHelp(i18n("Click and drag to select; middle-click and drag to draw new note"));
}

void MatrixSelector::stow()
{
    if (m_selectionRect) {
        delete m_selectionRect;
        m_selectionRect = 0;
        m_mParentView->canvas()->update();
    }

    disconnect(m_parentView->getCanvasView(), TQT_SIGNAL(contentsMoving (int, int)),
               this, TQT_SLOT(slotMatrixScrolled(int, int)));

}

void MatrixSelector::slotHideSelection()
{
    if (!m_selectionRect)
        return ;
    m_selectionRect->hide();
    m_selectionRect->setSize(0, 0);
    m_mParentView->canvas()->update();
}

void MatrixSelector::slotMatrixScrolled(int newX, int newY)
{
    if (m_updateRect) {
        int offsetX = newX - m_parentView->getCanvasView()->contentsX();
        int offsetY = newY - m_parentView->getCanvasView()->contentsY();

        int w = int(m_selectionRect->width() + offsetX);
        int h = int(m_selectionRect->height() + offsetY);

        // TQt rectangle dimensions appear to be 1-based
        if (w > 0)
            ++w;
        else
            --w;
        if (h > 0)
            ++h;
        else
            --h;

        m_selectionRect->setSize(w, h);
        setViewCurrentSelection();
        m_mParentView->canvas()->update();
    }
}

void MatrixSelector::setViewCurrentSelection()
{
    EventSelection* selection = getSelection();

    if (m_selectionToMerge && selection &&
        m_selectionToMerge->getSegment() == selection->getSegment()) {
        
        selection->addFromSelection(m_selectionToMerge);
        m_mParentView->setCurrentSelection(selection, true, true);

    } else if (!m_selectionToMerge) {

        m_mParentView->setCurrentSelection(selection, true, true);

    }

}

EventSelection* MatrixSelector::getSelection()
{
    if (!m_selectionRect->visible()) return 0;

    Segment& originalSegment = m_currentStaff->getSegment();
    EventSelection* selection = new EventSelection(originalSegment);

    // get the selections
    //
    TQCanvasItemList l = m_selectionRect->collisions(true);

    if (l.count())
    {
        for (TQCanvasItemList::Iterator it=l.begin(); it!=l.end(); ++it)
        {
            TQCanvasItem *item = *it;
            QCanvasMatrixRectangle *matrixRect = 0;

            if ((matrixRect = dynamic_cast<QCanvasMatrixRectangle*>(item)))
            {
                MatrixElement *mE = &matrixRect->getMatrixElement();
                selection->addEvent(mE->event());
            }
        }
    }

    if (selection->getAddedEvents() > 0) {
        return selection;
    } else {
        delete selection;
        return 0;
    }
}

void MatrixSelector::setContextHelpFor(TQPoint p, bool ctrlPressed)
{
    kapp->config()->setGroup(GeneralOptionsConfigGroup);
    if (!kapp->config()->readBoolEntry("toolcontexthelp", true)) return;

    p = m_mParentView->inverseMapPoint(p);

    // same logic as in MatrixCanvasView::contentsMousePressEvent

    TQCanvasItemList itemList = m_mParentView->canvas()->collisions(p);
    TQCanvasItemList::Iterator it;
    MatrixElement* mel = 0;
    TQCanvasItem* activeItem = 0;

    for (it = itemList.begin(); it != itemList.end(); ++it) {

        TQCanvasItem *item = *it;
        QCanvasMatrixRectangle *mRect = 0;

        if (item->active()) {
            break;
        }

        if ((mRect = dynamic_cast<QCanvasMatrixRectangle*>(item))) {
            if (! mRect->rect().tqcontains(p, true)) continue;
            mel = &(mRect->getMatrixElement());
            break;
        }
    }

    if (!mel) {
        setContextHelp(i18n("Click and drag to select; middle-click and drag to draw new note"));

    } else {
        
        // same logic as in handleMouseButtonPress
        
        int x = int(mel->getLayoutX());
        int width = mel->getWidth();
        int resizeStart = int(double(width) * 0.85) + x;

        // max size of 10
        if ((x + width ) - resizeStart > 10)
            resizeStart = x + width - 10;

        EventSelection *s = m_mParentView->getCurrentSelection();

        if (p.x() > resizeStart) {
            if (s && s->getAddedEvents() > 1) {
                setContextHelp(i18n("Click and drag to resize selected notes"));
            } else {
                setContextHelp(i18n("Click and drag to resize note"));
            }
        } else {
            if (s && s->getAddedEvents() > 1) {
                if (!ctrlPressed) {
                    setContextHelp(i18n("Click and drag to move selected notes; hold Ctrl as well to copy"));
                } else {
                    setContextHelp(i18n("Click and drag to copy selected notes"));
                }
            } else {
                if (!ctrlPressed) {
                    setContextHelp(i18n("Click and drag to move note; hold Ctrl as well to copy"));
                } else {
                    setContextHelp(i18n("Click and drag to copy note"));
                }
            }                
        }
    }
}

const TQString MatrixSelector::ToolName  = "selector";

}
#include "MatrixSelector.moc"