8421d8614914078fc61b7c3a26a3a647f57f99c4
[pulseview.git] / pv / views / trace / viewitem.hpp
1 /*
2  * This file is part of the PulseView project.
3  *
4  * Copyright (C) 2013 Joel Holdsworth <joel@airwebreathe.org.uk>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #ifndef PULSEVIEW_PV_VIEWITEM_HPP
21 #define PULSEVIEW_PV_VIEWITEM_HPP
22
23 #include <list>
24
25 #include <QPen>
26 #include <QPoint>
27
28 #include "viewitempaintparams.hpp"
29
30 class QAction;
31 class QMenu;
32 class QWidget;
33
34 namespace pv {
35
36 namespace widgets {
37 class Popup;
38 }
39
40 namespace views {
41 namespace trace {
42
43 class ViewItemOwner;
44
45 class ViewItem : public QObject
46 {
47         Q_OBJECT
48
49 public:
50         static const QSizeF LabelPadding;
51         static const int HighlightRadius;
52
53 public:
54         ViewItem();
55
56 public:
57         /**
58          * Returns true if the item is visible and enabled.
59          */
60         virtual bool enabled() const = 0;
61
62         /**
63          * Returns true if the item may be selected.
64          */
65         virtual bool is_selectable(QPoint pos) const;
66
67         /**
68          * Returns true if the item has been selected by the user.
69          */
70         bool selected() const;
71
72         /**
73          * Selects or deselects the signal.
74          */
75         virtual void select(bool select = true);
76
77         /**
78          * Returns true if the item may be dragged/moved.
79          */
80         virtual bool is_draggable() const;
81
82         /**
83          * Returns true if the item is being dragged.
84          */
85         bool dragging() const;
86
87         /**
88          * Sets this item into the dragged state.
89          */
90         void drag();
91
92         /**
93          * Sets this item into the un-dragged state.
94          */
95         virtual void drag_release();
96
97         /**
98          * Drags the item to a delta relative to the drag point.
99          * @param delta the offset from the drag point.
100          */
101         virtual void drag_by(const QPoint &delta) = 0;
102
103         /**
104          * Get the drag point.
105          * @param rect the rectangle of the widget area.
106          */
107         virtual QPoint drag_point(const QRect &rect) const = 0;
108
109         /**
110          * Computes the outline rectangle of a label.
111          * @param rect the rectangle of the header area.
112          * @return Returns the rectangle of the signal label.
113          * @remarks The default implementation returns an empty rectangle.
114          */
115         virtual QRectF label_rect(const QRectF &rect) const;
116
117         /**
118          * Computes the outline rectangle of the viewport hit-box.
119          * @param rect the rectangle of the viewport area.
120          * @return Returns the rectangle of the hit-box.
121          * @remarks The default implementation returns an empty hit-box.
122          */
123         virtual QRectF hit_box_rect(const ViewItemPaintParams &pp) const;
124
125         /**
126          * Paints the signal label.
127          * @param p the QPainter to paint into.
128          * @param rect the rectangle of the header area.
129          * @param hover true if the label is being hovered over by the mouse.
130          */
131         virtual void paint_label(QPainter &p, const QRect &rect, bool hover);
132
133         /**
134          * Paints the background layer of the item with a QPainter
135          * @param p the QPainter to paint into.
136          * @param pp the painting parameters object to paint with.
137          */
138         virtual void paint_back(QPainter &p, ViewItemPaintParams &pp);
139
140         /**
141          * Paints the mid-layer of the item with a QPainter
142          * @param p the QPainter to paint into.
143          * @param pp the painting parameters object to paint with.
144          */
145         virtual void paint_mid(QPainter &p, ViewItemPaintParams &pp);
146
147         /**
148          * Paints the foreground layer of the item with a QPainter
149          * @param p the QPainter to paint into.
150          * @param pp the painting parameters object to paint with.
151          */
152         virtual void paint_fore(QPainter &p, ViewItemPaintParams &pp);
153
154 public:
155         /**
156          * Gets the text color.
157          * @remarks This color is computed by comparing the lightness
158          * of the trace color against a threshold to determine whether
159          * white or black would be more visible.
160          */
161         static QColor select_text_color(QColor background);
162
163 public:
164         virtual QMenu* create_header_context_menu(QWidget *parent);
165
166         virtual QMenu* create_view_context_menu(QWidget *parent, QPoint &click_pos);
167
168         virtual pv::widgets::Popup* create_popup(QWidget *parent);
169
170         virtual void delete_pressed();
171
172 protected:
173         static QPen highlight_pen();
174
175 protected:
176         QWidget *context_parent_;
177         QPoint drag_point_;
178
179 private:
180         bool selected_;
181 };
182
183 } // namespace trace
184 } // namespace views
185 } // namespace pv
186
187 #endif // PULSEVIEW_PV_VIEWITEM_HPP