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