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 #ifndef RG_CONTROLRULER_H
19 #define RG_CONTROLRULER_H
20 
21 #include "ControlItem.h"
22 
23 #include "gui/general/AutoScroller.h"
24 #include "base/Segment.h"
25 #include "base/Selection.h"
26 
27 #include <QColor>
28 #include <QPoint>
29 #include <QString>
30 #include <QWidget>
31 
32 #include <utility>
33 
34 //class QWidget;
35 class QMenu;
36 class QWheelEvent;
37 class QScrollBar;
38 class QMouseEvent;
39 class QContextMenuEvent;
40 
41 
42 namespace Rosegarden
43 {
44 
45 class ControlTool;
46 class ControlToolBox;
47 class ControlSelector;
48 class ControlMouseEvent;
49 class Segment;
50 class RulerScale;
51 class EventSelection;
52 class EditViewBase;
53 class NotationStaff;
54 class ViewSegment;
55 
56 /**
57  * ControlRuler : base class for Control Rulers
58  */
59 class ControlRuler : public QWidget //, public ViewSegmentObserver
60 {
61     Q_OBJECT
62 
63     friend class ControlItem;
64 
65 public:
66     ControlRuler(ViewSegment*,
67                  RulerScale*,
68                  QWidget* parent = nullptr);
69     ~ControlRuler() override;
70 
71     virtual QString getName() = 0;
72 
sizeHint()73     QSize sizeHint() const override { return QSize(1,100); }
74 
75     void paintEvent(QPaintEvent *) override;
76 
getMaxItemValue()77     long getMaxItemValue() const { return m_maxItemValue; }
setMaxItemValue(long val)78     void setMaxItemValue(long val) { m_maxItemValue = val; }
79 
getMinItemValue()80     long getMinItemValue() const { return m_minItemValue; }
setMinItemValue(long val)81     void setMinItemValue(long val) { m_minItemValue = val; }
82 
83     void clear();
84 
85     //void setControlTool(ControlTool *);
86     //int applyTool(double x, int val);
87     virtual void setTool(const QString &name);
88 
getSelectedItems()89     ControlItemList *getSelectedItems() { return &m_selectedItems; }
90 
getSelectionRectangle()91     QRectF* getSelectionRectangle() { return m_selectionRect; }
setSelectionRect(QRectF * rect)92     void setSelectionRect(QRectF *rect) { m_selectionRect = rect; }
93 
94     virtual void setSegment(Segment *);
95     virtual void setViewSegment(ViewSegment *);
getSegment()96     Segment* getSegment() { return m_segment; }
97 
98     void updateSegment();
99 
100     virtual void notationLayoutUpdated(timeT,timeT);
101 
setRulerScale(RulerScale * rulerscale)102     void setRulerScale(RulerScale *rulerscale) { m_rulerScale = rulerscale; }
getRulerScale()103     RulerScale* getRulerScale() { return m_rulerScale; }
104 
setXOffset(int offset)105     void setXOffset(int offset) { m_xOffset = offset; }
106 
107     float valueToY(long val);
108     long yToValue(float height);
109 
getXScale()110     double getXScale() const { return m_xScale; }
getYScale()111     double getYScale() const { return m_yScale; }
112     float getXMax();
113     float getXMin();
114 
115     void clearSelectedItems();
116     void addToSelection(QSharedPointer<ControlItem>);
117     void removeFromSelection(QSharedPointer<ControlItem>);
getEventSelection()118     EventSelection *getEventSelection()
119     { return m_eventSelection; }
120 
121     virtual ControlItemMap::iterator findControlItem(float x);
122     virtual void moveItem(ControlItem*);
123 
124     /// EventSelectionObserver
125 //    virtual void eventSelected(EventSelection *,Event *);
126 //    virtual void eventDeselected(EventSelection *,Event *);
127 //    virtual void eventSelectionDestroyed(EventSelection *);
128 
129 //    void assignEventSelection(EventSelection *);
130 
131     // SegmentObserver interface
132 //    virtual void viewSegmentDeleted(const ViewSegment *);
133 
134     void flipForwards();
135     void flipBackwards();
136 
137 signals:
138     void mousePress();
139     void mouseMove(FollowMode);
140     void mouseRelease();
141 
142     /**
143      * Emitted whenever the ruler changes its selection, so the ruler owner can
144      * update its own selection to include the events selected on the ruler.
145      *
146      * See MatrixScene::slotRulerSelectionChanged().
147      *
148      * This allows the user to add CC events to the selection for copy/cut/paste.
149      * It is a bit confusing, however, since it doesn't affect the move tool.
150      *
151      * ??? This would be even more useful if the PropertyControlRuler (velocity
152      *     ruler) properly implemented selection.  That would then allow selecting
153      *     in the velocity ruler which would make it a lot easier to use.
154      */
155     void rulerSelectionChanged(EventSelection *);
156 
157     void showContextHelp(const QString &);
158 
159 public slots:
160     /// override RosegardenCanvasView - we don't want to change the main hscrollbar
161     virtual void slotUpdate();
162 //    virtual void slotUpdateElementsHPos();
163     virtual void slotScrollHorizSmallSteps(int);
164     virtual void slotSetPannedRect(QRectF);
165 //    virtual void slotSetScale(double);
166 
167 protected:
168     void mousePressEvent(QMouseEvent*) override;
169     void mouseReleaseEvent(QMouseEvent*) override;
170     void mouseMoveEvent(QMouseEvent*) override;
171     void contextMenuEvent(QContextMenuEvent*) override;
172     void wheelEvent(QWheelEvent*) override;
173     void resizeEvent(QResizeEvent *) override;
174 
175     virtual ControlMouseEvent createControlMouseEvent(QMouseEvent* e);
176 
177 //    virtual QScrollBar* getMainHorizontalScrollBar();
178 
179 //    virtual void computeViewSegmentOffset() {};
180 
181 //    virtual void layoutItem(ControlItem*);
182     virtual ControlItemMap::iterator findControlItem(const ControlItem*);
183     virtual ControlItemMap::iterator findControlItem(const Event*);
184     virtual void addControlItem(QSharedPointer<ControlItem>);
185     virtual void addCheckVisibleLimits(ControlItemMap::iterator);
186     virtual void removeControlItem(ControlItem*);
187     virtual void removeControlItem(const Event*);
188     virtual void removeControlItem(const ControlItemMap::iterator&);
189     virtual void removeCheckVisibleLimits(const ControlItemMap::iterator&);
190     virtual void eraseControlItem(const Event*);
191     virtual void eraseControlItem(const ControlItemMap::iterator&);
192     virtual int visiblePosition(QSharedPointer<ControlItem>);
193 
194     // Stacking of the SegmentItems on the canvas
195     //
196     std::pair<int, int> getZMinMax();
197 
198 //    virtual void init();
199 //virtual void drawBackground() = 0;
200 
xMapToWidget(double x)201     int xMapToWidget(double x) {return (x-m_pannedRect.left())*width()/m_pannedRect.width();};
202     int mapXToWidget(float);
203     int mapYToWidget(float);
204     QRect mapRectToWidget(QRectF *);
205     QPolygon mapItemToWidget(QSharedPointer<ControlItem>);
206     QPointF mapWidgetToItem(QPoint*);
207 
208     QColor valueToColour(int max, int val);
209 
210     void updateSelection();
211 
setMenuName(QString menuName)212     void setMenuName(QString menuName) { m_menuName = menuName; }
213     void createMenu();
214 
215     //--------------- Data members ---------------------------------
216 
217 //    EditViewBase*               m_parentEditView;
218 //    QScrollBar*                 m_mainHorizontalScrollBar;
219     RulerScale*     m_rulerScale;
220     EventSelection* m_eventSelection; //,*m_assignedEventSelection;
221 
222 //    MatrixScene *m_scene;
223 
224     ViewSegment *m_viewSegment;
225     NotationStaff *m_notationStaff;
226     Segment *m_segment;
227 
228     // ??? MEMORY LEAK.
229     //     This map stores pointers and never deletes them.
230     //     Recommend switching to QSharedPointer.
231     ControlItemMap m_controlItemMap;
232 
233     // Iterators to the first visible and the last visible item
234     // NB these iterators are only really useful for zero duration items as the
235     //   interval is determined by start position and will omit items that start
236     //   to the left of the screen but end on screen. For this reason, the
237     //   m_visibleItems list all includes items that are actually visible.
238     ControlItemMap::iterator m_firstVisibleItem;
239     ControlItemMap::iterator m_lastVisibleItem;
240     ControlItemMap::iterator m_nextItemLeft;
241 
242     ControlItemList m_selectedItems;
243     ControlItemList m_visibleItems;
244 
245     ControlItem *m_currentIndex;
246 
247     ControlTool *m_currentTool;
248     ControlToolBox *m_toolBox;
249     QString m_currentToolName;
250 
251     QRectF m_pannedRect;
252     double m_xScale;
253     double m_yScale;
254 
255     long m_maxItemValue;
256     long m_minItemValue;
257 
258     double m_viewSegmentOffset;
259 
260     int m_xOffset;
261 
262     double m_currentX;
263 
264     QPoint m_lastEventPos;
265     bool m_itemMoved;
266 
267     bool m_selecting;
268     ControlSelector* m_selector;
269     QRectF* m_selectionRect;
270 
271     QString m_menuName;
272     QMenu* 	m_menu;
273 
274     //bool m_hposUpdatePending;
275 
276     typedef std::list<Event *> SelectionSet;
277     SelectionSet m_selectedEvents;
278 };
279 
280 
281 }
282 
283 #endif
284