]> sigrok.org Git - pulseview.git/blob - pv/view/trace.h
Make header width responsive to label text
[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         /**
146          * Computes the outline rectangle of a label.
147          * @param p the QPainter to lay out text with.
148          * @param right the x-coordinate of the right edge of the header
149          *      area.
150          * @return Returns the rectangle of the signal label.
151          */
152         QRectF get_label_rect(int right);
153
154 protected:
155
156         /**
157          * Gets the text colour.
158          * @remarks This colour is computed by comparing the lightness
159          * of the trace colour against a threshold to determine whether
160          * white or black would be more visible.
161          */
162         QColor get_text_colour() const;
163
164         /**
165          * Paints a zero axis across the viewport.
166          * @param p the QPainter to paint into.
167          * @param y the y-offset of the axis.
168          * @param left the x-coordinate of the left edge of the view.
169          * @param right the x-coordinate of the right edge of the view.
170          */
171         void paint_axis(QPainter &p, int y, int left, int right);
172
173         void add_colour_option(QWidget *parent, QFormLayout *form);
174
175         void create_popup_form();
176
177         virtual void populate_popup_form(QWidget *parent, QFormLayout *form);
178
179 private slots:
180         void on_text_changed(const QString &text);
181
182         void on_colour_changed(const QColor &colour);
183
184         void on_popup_closed();
185
186 signals:
187         void visibility_changed();
188         void text_changed();    
189         void colour_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 private:
200         pv::widgets::Popup *_popup;
201         QFormLayout *_popup_form;
202 };
203
204 } // namespace view
205 } // namespace pv
206
207 #endif // PULSEVIEW_PV_VIEW_TRACE_H