summaryrefslogtreecommitdiffstats
path: root/src/base/ViewElement.h
blob: f898e516f73072732f2a607998288e08b42e4a68 (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
// -*- c-basic-offset: 4 -*-


/*
    Rosegarden
    A 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        <bownie@bownie.com>

    The moral right of the authors to claim authorship of this work
    has been asserted.

    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.
*/

#ifndef _VIEWELEMENT_H_
#define _VIEWELEMENT_H_


#include "Event.h"

#include <set>
#include <list>

namespace Rosegarden 
{

/**
 * The abstract base for classes which represent an Event as an
 * on-screen graphic item (a note, a rectangle on a piano roll).
 */

class ViewElement
{
    friend class ViewElementList;
    friend class Staff;

public:
    virtual ~ViewElement();

    const Event* event() const { return m_event; }
    Event*       event()       { return m_event; }

    virtual timeT getViewAbsoluteTime() const  { return event()->getAbsoluteTime(); }
    virtual timeT getViewDuration() const      { return event()->getDuration();     }

    /**
     * Returns the X coordinate of the element, as computed by the
     * tqlayout. This is not the coordinate of the associated canvas
     * item.
     *
     * @see getCanvasX()
     */
    virtual double getLayoutX() const   { return m_tqlayoutX; }

    /**
     * Returns the Y coordinate of the element, as computed by the
     * tqlayout. This is not the coordinate of the associated canvas
     * item.
     *
     * @see getCanvasY()
     */
    virtual double getLayoutY() const   { return m_tqlayoutY; }

    /**
     * Sets the X coordinate which was computed by the tqlayout engine
     * @see getLayoutX()
     */
    virtual void   setLayoutX(double x) { m_tqlayoutX = x; }

    /**
     * Sets the Y coordinate which was computed by the tqlayout engine
     * @see getLayoutY()
     */
    virtual void   setLayoutY(double y) { m_tqlayoutY = y; }

    void dump(std::ostream&) const;

    friend bool operator<(const ViewElement&, const ViewElement&);

protected:
    ViewElement(Event *);

    double m_tqlayoutX;
    double m_tqlayoutY;
    
    Event *m_event;
};



class ViewElementComparator
{
public:
    bool operator()(const ViewElement *e1, const ViewElement *e2) const {
        return *e1 < *e2;
    }
};

/**
 * This class owns the objects its items are pointing at.
 *
 * The template argument T must be a subclass of ViewElement.
 */
class ViewElementList : public std::multiset<ViewElement *, ViewElementComparator >
{
    typedef std::multiset<ViewElement *, ViewElementComparator > set_type;
public:
    typedef set_type::iterator iterator;

    ViewElementList() : set_type() { }
    virtual ~ViewElementList();

    void insert(ViewElement *);
    void erase(iterator i);
    void erase(iterator from, iterator to);
    void eraseSingle(ViewElement *);

    iterator findPrevious(const std::string &type, iterator i);
    iterator findNext(const std::string &type, iterator i);

    /**
     * Returns an iterator pointing to that specific element,
     * end() otherwise
     */
    iterator findSingle(ViewElement *);

    const_iterator findSingle(ViewElement *e) const {
        return const_iterator(((const ViewElementList *)this)->findSingle(e));
    }

    /**
     * Returns first iterator pointing at or after the given time,
     * end() if time is beyond the end of the list
     */ 
    iterator findTime(timeT time);

    const_iterator findTime(timeT time) const {
        return const_iterator(((const ViewElementList *)this)->findTime(time));
    }

    /**
     * Returns iterator pointing to the first element starting at
     * or before the given absolute time
     */
    iterator findNearestTime(timeT time);

    const_iterator findNearestTime(timeT time) const {
        return const_iterator(((const ViewElementList *)this)->findNearestTime(time));
    }
};

}


#endif