1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2 
3 /*
4     Rosegarden
5     A MIDI and audio sequencer and musical notation editor.
6     Copyright 2000-2021 the Rosegarden development team.
7 
8     Other copyrights also apply to some parts of this work.  Please
9     see the AUTHORS file and individual file headers for details.
10 
11     This program is free software; you can redistribute it and/or
12     modify it under the terms of the GNU General Public License as
13     published by the Free Software Foundation; either version 2 of the
14     License, or (at your option) any later version.  See the file
15     COPYING included with this distribution for more information.
16 */
17 
18 #define RG_MODULE_STRING "[SegmentEraser]"
19 
20 #include "SegmentEraser.h"
21 
22 #include "misc/Debug.h"
23 #include "commands/segment/SegmentEraseCommand.h"
24 #include "CompositionView.h"
25 #include "ChangingSegment.h"
26 #include "document/RosegardenDocument.h"
27 #include "gui/general/BaseTool.h"
28 #include "gui/general/RosegardenScrollView.h"
29 #include "SegmentTool.h"
30 #include "document/Command.h"
31 #include "document/CommandHistory.h"
32 #include <QPoint>
33 #include <QString>
34 #include <QMouseEvent>
35 
36 
37 namespace Rosegarden
38 {
39 
40 
ToolName()41 QString SegmentEraser::ToolName() { return "segmenteraser"; }
42 
SegmentEraser(CompositionView * c,RosegardenDocument * d)43 SegmentEraser::SegmentEraser(CompositionView *c, RosegardenDocument *d)
44         : SegmentTool(c, d)
45 {
46     RG_DEBUG << "SegmentEraser()\n";
47 }
48 
ready()49 void SegmentEraser::ready()
50 {
51     m_canvas->viewport()->setCursor(Qt::PointingHandCursor);
52     setContextHelp(tr("Click on a segment to delete it"));
53 }
54 
mousePressEvent(QMouseEvent * e)55 void SegmentEraser::mousePressEvent(QMouseEvent *e)
56 {
57     // Let the baseclass have a go.
58     SegmentTool::mousePressEvent(e);
59 
60     // We only care about the left mouse button.
61     if (e->button() != Qt::LeftButton)
62         return;
63 
64     // No need to propagate.
65     e->accept();
66 
67     QPoint pos = m_canvas->viewportToContents(e->pos());
68 
69     // Save the Segment for the mouse release event.
70     setChangingSegment(m_canvas->getModel()->getSegmentAt(pos));
71 }
72 
mouseReleaseEvent(QMouseEvent * e)73 void SegmentEraser::mouseReleaseEvent(QMouseEvent *e)
74 {
75     // We only care about the left mouse button.
76     if (e->button() != Qt::LeftButton)
77         return;
78 
79     // No need to propagate.
80     e->accept();
81 
82     // If a Segment was selected by the press event
83     if (getChangingSegment()) {
84         // Erase it
85         CommandHistory::getInstance()->addCommand(
86                 new SegmentEraseCommand(getChangingSegment()->getSegment()));
87     }
88 
89     // Clear the current Segment.
90     setChangingSegment(ChangingSegmentPtr());
91 }
92 
mouseMoveEvent(QMouseEvent * e)93 int SegmentEraser::mouseMoveEvent(QMouseEvent *e)
94 {
95     // No need to propagate.
96     e->accept();
97 
98     return NO_FOLLOW;
99 }
100 
101 
102 }
103