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 #include "GuitarChordInserter.h"
19 
20 #include "base/Event.h"
21 #include "base/Exception.h"
22 #include "commands/notation/EraseEventCommand.h"
23 #include "commands/notation/GuitarChordInsertionCommand.h"
24 #include "document/CommandHistory.h"
25 #include "gui/editors/guitar/GuitarChordSelectorDialog.h"
26 #include "misc/Debug.h"
27 #include "NotationMouseEvent.h"
28 #include "NotationElement.h"
29 #include "NotationStaff.h"
30 #include "NotationWidget.h"
31 #include "NotePixmapFactory.h"
32 #include "gui/widgets/Panned.h"
33 
34 namespace Rosegarden
35 {
36 
GuitarChordInserter(NotationWidget * widget)37 GuitarChordInserter::GuitarChordInserter(NotationWidget *widget) :
38     NotationTool("guitarchordinserter.rc", "GuitarChordInserter", widget),
39     m_guitarChordSelector(nullptr)
40 {
41     createAction("select", SLOT(slotSelectSelected()));
42     createAction("erase", SLOT(slotEraseSelected()));
43     createAction("notes", SLOT(slotNotesSelected()));
44 
45     m_guitarChordSelector = new GuitarChordSelectorDialog(m_widget);
46     m_guitarChordSelector->init();
47 }
48 
49 void
slotNotesSelected()50 GuitarChordInserter::slotNotesSelected()
51 {
52     invokeInParentView("draw");
53 }
54 
55 void
slotGuitarChordSelected()56 GuitarChordInserter::slotGuitarChordSelected()
57 {
58     // Switch to last selected Guitar Chord
59     // m_nParentView->slotLastGuitarChordAction();
60 }
61 
62 void
slotEraseSelected()63 GuitarChordInserter::slotEraseSelected()
64 {
65     invokeInParentView("erase");
66 }
67 
68 void
slotSelectSelected()69 GuitarChordInserter::slotSelectSelected()
70 {
71     invokeInParentView("select");
72 }
73 
74 void
ready()75 GuitarChordInserter::ready()
76 {
77     m_widget->setCanvasCursor(Qt::CrossCursor);
78 //!!!    m_nParentView->setHeightTracking(false);
79 
80     // The guitar chord tool doesn't use the wheel.
81     m_widget->getView()->setWheelZoomPan(true);
82 }
83 
84 void
handleLeftButtonPress(const NotationMouseEvent * e)85 GuitarChordInserter::handleLeftButtonPress(const NotationMouseEvent *e)
86 {
87     if (!e->staff) return;
88 
89     if (e->element && e->exact &&
90         e->element->event()->isa(Guitar::Chord::EventType)) {
91         handleSelectedGuitarChord(e);
92     } else {
93         createNewGuitarChord(e);
94     }
95 }
96 
97 bool
processDialog(NotationStaff * staff,timeT & insertionTime)98 GuitarChordInserter::processDialog(NotationStaff* staff,
99                                    timeT& insertionTime)
100 {
101     bool result = false;
102 
103     if (m_guitarChordSelector->exec() == QDialog::Accepted) {
104         Guitar::Chord chord = m_guitarChordSelector->getChord();
105 
106         GuitarChordInsertionCommand *command =
107             new GuitarChordInsertionCommand
108             (staff->getSegment(), insertionTime, chord);
109 
110         CommandHistory::getInstance()->addCommand(command);
111         result = true;
112     }
113 
114     return result;
115 }
116 
117 void
handleSelectedGuitarChord(const NotationMouseEvent * e)118 GuitarChordInserter::handleSelectedGuitarChord(const NotationMouseEvent *e)
119 {
120     // Get time of where guitar chord is inserted
121     timeT insertionTime = e->element->event()->getAbsoluteTime(); // not getViewAbsoluteTime()
122 
123     // edit an existing guitar chord, if that's what we clicked on
124     try {
125         Guitar::Chord chord(*(e->element->event()));
126 
127         m_guitarChordSelector->setChord(chord);
128 
129         if (processDialog(e->staff, insertionTime)) {
130             // Erase old guitar chord
131             EraseEventCommand *command =
132                 new EraseEventCommand(e->staff->getSegment(),
133                                       e->element->event(),
134                                       false);
135 
136             CommandHistory::getInstance()->addCommand(command);
137         }
138     } catch (const Exception &e) {}
139 }
140 
createNewGuitarChord(const NotationMouseEvent * e)141 void GuitarChordInserter::createNewGuitarChord(const NotationMouseEvent *e)
142 {
143     timeT insertionTime = e->element->event()->getAbsoluteTime(); // not getViewAbsoluteTime()
144     processDialog(e->staff, insertionTime);
145 }
146 
ToolName()147 QString GuitarChordInserter::ToolName() { return "guitarchordinserter"; }
148 
149 }
150 
151