1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2 
3 
4 /*
5     Rosegarden
6     A sequencer and musical notation editor.
7     Copyright 2000-2021 the Rosegarden development team.
8     See the AUTHORS file for more details.
9 
10     This program is free software; you can redistribute it and/or
11     modify it under the terms of the GNU General Public License as
12     published by the Free Software Foundation; either version 2 of the
13     License, or (at your option) any later version.  See the file
14     COPYING included with this distribution for more information.
15 */
16 
17 #ifndef RG_VIEWELEMENT_H
18 #define RG_VIEWELEMENT_H
19 
20 
21 #include "Event.h"
22 
23 #include <set>
24 #include <list>
25 
26 namespace Rosegarden
27 {
28 
29 /**
30  * The abstract base for classes which represent an Event as an
31  * on-screen graphic item (a note, a rectangle on a piano roll).
32  */
33 
34 class ViewElement
35 {
36     friend class ViewElementList;
37     friend class Staff;
38 
39 public:
40     virtual ~ViewElement();
41 
event()42     const Event* event() const { return m_event; }
event()43     Event*       event()       { return m_event; }
44 
getViewAbsoluteTime()45     virtual timeT getViewAbsoluteTime() const  { return event()->getAbsoluteTime(); }
getViewDuration()46     virtual timeT getViewDuration() const      { return event()->getDuration();     }
47 
48     /**
49      * Returns the X coordinate of the element, as computed by the
50      * layout. This is not the coordinate of the associated canvas
51      * item.
52      *
53      * @see getCanvasX()
54      */
getLayoutX()55     virtual double getLayoutX() const   { return m_layoutX; }
56 
57     /**
58      * Returns the Y coordinate of the element, as computed by the
59      * layout. This is not the coordinate of the associated canvas
60      * item.
61      *
62      * @see getCanvasY()
63      */
getLayoutY()64     virtual double getLayoutY() const   { return m_layoutY; }
65 
66     /**
67      * Sets the X coordinate which was computed by the layout engine
68      * @see getLayoutX()
69      */
setLayoutX(double x)70     virtual void   setLayoutX(double x) { m_layoutX = x; }
71 
72     /**
73      * Sets the Y coordinate which was computed by the layout engine
74      * @see getLayoutY()
75      */
setLayoutY(double y)76     virtual void   setLayoutY(double y) { m_layoutY = y; }
77 
78     void dump(std::ostream&) const;
79 
80     friend bool operator<(const ViewElement&, const ViewElement&);
81 
82 protected:
83     ViewElement(Event *);
84 
85     double m_layoutX;
86     double m_layoutY;
87 
88     Event *m_event;
89 };
90 
91 
92 
93 class ViewElementComparator
94 {
95 public:
operator()96     bool operator()(const ViewElement *e1, const ViewElement *e2) const {
97         return *e1 < *e2;
98     }
99 };
100 
101 /**
102  * This class owns the objects its items are pointing at.
103  */
104 class ViewElementList : public std::multiset<ViewElement *, ViewElementComparator >
105 {
106     typedef std::multiset<ViewElement *, ViewElementComparator > set_type;
107 public:
108     typedef set_type::iterator iterator;
109 
ViewElementList()110     ViewElementList() : set_type() { }
111     virtual ~ViewElementList();
112 
113     void insert(ViewElement *);
114     void erase(iterator i);
115     void erase(iterator from, iterator to);
116     void eraseSingle(ViewElement *);
117 
118     iterator findPrevious(const std::string &type, iterator i);
119     iterator findNext(const std::string &type, iterator i);
120 
121     /**
122      * Returns an iterator pointing to that specific element,
123      * end() otherwise
124      */
125     iterator findSingle(ViewElement *);
126 
findSingle(ViewElement * e)127     const_iterator findSingle(ViewElement *e) const {
128         return const_iterator(((const ViewElementList *)this)->findSingle(e));
129     }
130 
131     /**
132      * Returns first iterator pointing at or after the given time,
133      * end() if time is beyond the end of the list
134      */
135     iterator findTime(timeT time);
136 
findTime(timeT time)137     const_iterator findTime(timeT time) const {
138         return const_iterator(((const ViewElementList *)this)->findTime(time));
139     }
140 
141     /**
142      * Returns iterator pointing to the first element starting at
143      * or before the given absolute time
144      */
145     iterator findNearestTime(timeT time);
146 
findNearestTime(timeT time)147     const_iterator findNearestTime(timeT time) const {
148         return const_iterator(((const ViewElementList *)this)->findNearestTime(time));
149     }
150 };
151 
152 }
153 
154 
155 #endif
156 
157