]> sigrok.org Git - pulseview.git/blob - pv/view/trace.h
72f8e8a8986a092d82870c666e64d8a361c8bb3a
[pulseview.git] / pv / view / trace.h
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, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
19  */
20
21 #ifndef PULSEVIEW_PV_VIEW_TRACE_H
22 #define PULSEVIEW_PV_VIEW_TRACE_H
23
24 #include <QColor>
25 #include <QPainter>
26 #include <QPen>
27 #include <QRect>
28 #include <QString>
29
30 #include <stdint.h>
31
32 #include "selectableitem.h"
33
34 class QFormLayout;
35
36 namespace pv {
37
38 class SigSession;
39
40 namespace view {
41
42 class View;
43
44 class Trace : public SelectableItem
45 {
46         Q_OBJECT
47
48 private:
49         static const QPen AxisPen;
50         static const int LabelHitPadding;
51
52 protected:
53         Trace(SigSession &session, QString name);
54
55 public:
56         /**
57          * Gets the name of this signal.
58          */
59         QString get_name() const;
60
61         /**
62          * Sets the name of the signal.
63          */
64         virtual void set_name(QString name);
65
66         /**
67          * Get the colour of the signal.
68          */
69         QColor get_colour() const;
70
71         /**
72          * Set the colour of the signal.
73          */
74         void set_colour(QColor colour);
75
76         /**
77          * Gets the vertical layout offset of this signal.
78          */
79         int get_v_offset() const;
80
81         /**
82          * Sets the vertical layout offset of this signal.
83          */
84         void set_v_offset(int v_offset);
85
86         /**
87          * Returns true if the trace is visible and enabled.
88          */
89         virtual bool enabled() const = 0;
90
91         virtual void set_view(pv::view::View *view);
92
93         /**
94          * Paints the background layer of the trace with a QPainter
95          * @param p the QPainter to paint into.
96          * @param left the x-coordinate of the left edge of the signal
97          * @param right the x-coordinate of the right edge of the signal
98          **/
99         virtual void paint_back(QPainter &p, int left, int right);
100
101         /**
102          * Paints the mid-layer of the trace with a QPainter
103          * @param p the QPainter to paint into.
104          * @param left the x-coordinate of the left edge of the signal
105          * @param right the x-coordinate of the right edge of the signal
106          **/
107         virtual void paint_mid(QPainter &p, int left, int right);
108
109         /**
110          * Paints the foreground layer of the trace with a QPainter
111          * @param p the QPainter to paint into.
112          * @param left the x-coordinate of the left edge of the signal
113          * @param right the x-coordinate of the right edge of the signal
114          **/
115         virtual void paint_fore(QPainter &p, int left, int right);
116
117         /**
118          * Paints the signal label into a QGLWidget.
119          * @param p the QPainter to paint into.
120          * @param right the x-coordinate of the right edge of the header
121          *      area.
122          * @param hover true if the label is being hovered over by the mouse.
123          */
124         virtual void paint_label(QPainter &p, int right, bool hover);
125
126         /**
127          * Determines if a point is in the header label rect.
128          * @param left the x-coordinate of the left edge of the header
129          *      area.
130          * @param right the x-coordinate of the right edge of the header
131          *      area.
132          * @param point the point to test.
133          */
134         bool pt_in_label_rect(int left, int right, const QPoint &point);
135
136         virtual QMenu* create_context_menu(QWidget *parent);
137
138         pv::widgets::Popup* create_popup(QWidget *parent);
139
140         /**
141          * Gets the y-offset of the axis.
142          */
143         int get_y() const;
144
145 protected:
146         /**
147          * Gets the text colour.
148          * @remarks This colour is computed by comparing the lightness
149          * of the trace colour against a threshold to determine whether
150          * white or black would be more visible.
151          */
152         QColor get_text_colour() const;
153
154         /**
155          * Paints a zero axis across the viewport.
156          * @param p the QPainter to paint into.
157          * @param y the y-offset of the axis.
158          * @param left the x-coordinate of the left edge of the view.
159          * @param right the x-coordinate of the right edge of the view.
160          */
161         void paint_axis(QPainter &p, int y, int left, int right);
162
163         void add_colour_option(QWidget *parent, QFormLayout *form);
164
165         void create_popup_form();
166
167         virtual void populate_popup_form(QWidget *parent, QFormLayout *form);
168
169 private:
170
171         /**
172          * Computes an caches the size of the label text.
173          */
174         void compute_text_size(QPainter &p);
175
176         /**
177          * Computes the outline rectangle of a label.
178          * @param p the QPainter to lay out text with.
179          * @param right the x-coordinate of the right edge of the header
180          *      area.
181          * @return Returns the rectangle of the signal label.
182          */
183         QRectF get_label_rect(int right);
184
185 private slots:
186         void on_text_changed(const QString &text);
187
188         void on_colour_changed(const QColor &colour);
189
190         void on_popup_closed();
191
192 signals:
193         void visibility_changed();
194         void text_changed();    
195         void colour_changed();
196
197 protected:
198         pv::SigSession &_session;
199         pv::view::View *_view;
200
201         QString _name;
202         QColor _colour;
203         int _v_offset;
204
205         QSizeF _text_size;
206
207 private:
208         pv::widgets::Popup *_popup;
209         QFormLayout *_popup_form;
210 };
211
212 } // namespace view
213 } // namespace pv
214
215 #endif // PULSEVIEW_PV_VIEW_TRACE_H