4124ece2e3705415fff359214d3184746293c124
[pulseview.git] / pv / views / trace / trace.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_VIEWS_TRACEVIEW_TRACE_HPP
21 #define PULSEVIEW_PV_VIEWS_TRACEVIEW_TRACE_HPP
22
23 #include <QColor>
24 #include <QPainter>
25 #include <QPen>
26 #include <QRect>
27 #include <QString>
28
29 #include <cstdint>
30
31 #include "tracetreeitem.hpp"
32
33 #include <pv/globalsettings.hpp>
34 #include "pv/data/signalbase.hpp"
35
36 using std::shared_ptr;
37
38 class QFormLayout;
39
40 namespace pv {
41
42 namespace data {
43 class SignalBase;
44 }
45
46 namespace widgets {
47 class Popup;
48 }
49
50 namespace views {
51 namespace trace {
52
53 /**
54  * The Trace class represents a @ref TraceTreeItem which occupies some vertical
55  * space on the canvas and spans across its entire width, essentially showing
56  * a time series of values, events, objects or similar. While easily confused
57  * with @ref Signal, the difference is that Trace may represent anything that
58  * can be drawn, not just numeric values. One example is a @ref DecodeTrace.
59  *
60  * For this reason, Trace is more generic and contains properties and helpers
61  * that benefit any kind of time series items.
62  */
63 class Trace : public TraceTreeItem, public GlobalSettingsInterface
64 {
65         Q_OBJECT
66
67 public:
68         /**
69          * Allowed values for the multi-segment display mode.
70          *
71          * Note: Consider these locations when updating the list:
72          * *
73          * @ref View::set_segment_display_mode
74          * @ref View::on_segment_changed
75          * @ref AnalogSignal::get_analog_segment_to_paint
76          * @ref AnalogSignal::get_logic_segment_to_paint
77          * @ref LogicSignal::get_logic_segment_to_paint
78          * @ref StandardBar
79          */
80         enum SegmentDisplayMode {
81                 ShowLastSegmentOnly = 1,
82                 ShowLastCompleteSegmentOnly,
83                 ShowSingleSegmentOnly,
84                 ShowAllSegments,
85                 ShowAccumulatedIntensity
86         };
87
88 private:
89         static const QPen AxisPen;
90         static const int LabelHitPadding;
91
92         static const QColor BrightGrayBGColor;
93         static const QColor DarkGrayBGColor;
94
95 protected:
96         Trace(shared_ptr<data::SignalBase> channel);
97         ~Trace();
98
99 public:
100         /**
101          * Returns the underlying SignalBase instance.
102          */
103         shared_ptr<data::SignalBase> base() const;
104
105         /**
106          * Returns true if the item may be selected.
107          */
108         virtual bool is_selectable(QPoint pos) const;
109
110         /**
111          * Returns true if the item may be dragged/moved.
112          */
113         virtual bool is_draggable() const;
114
115         /**
116          * Configures the segment display mode to use.
117          */
118         virtual void set_segment_display_mode(SegmentDisplayMode mode);
119
120         virtual void on_setting_changed(const QString &key, const QVariant &value);
121
122         /**
123          * Paints the signal label.
124          * @param p the QPainter to paint into.
125          * @param rect the rectangle of the header area.
126          * @param hover true if the label is being hovered over by the mouse.
127          */
128         virtual void paint_label(QPainter &p, const QRect &rect, bool hover);
129
130         virtual QMenu* create_header_context_menu(QWidget *parent);
131
132         pv::widgets::Popup* create_popup(QWidget *parent);
133
134         /**
135          * Computes the outline rectangle of a label.
136          * @param rect the rectangle of the header area.
137          * @return Returns the rectangle of the signal label.
138          */
139         QRectF label_rect(const QRectF &rect) const;
140
141         /**
142          * Computes the outline rectangle of the viewport hit-box.
143          * @param rect the rectangle of the viewport area.
144          * @return Returns the rectangle of the hit-box.
145          * @remarks The default implementation returns an empty hit-box.
146          */
147         virtual QRectF hit_box_rect(const ViewItemPaintParams &pp) const;
148
149         void set_current_segment(const int segment);
150
151         int get_current_segment() const;
152
153         virtual void hover_point_changed(const QPoint &hp);
154
155 protected:
156         /**
157          * Paints the background layer of the signal with a QPainter.
158          * @param p The QPainter to paint into.
159          * @param pp The painting parameters object to paint with.
160          */
161         virtual void paint_back(QPainter &p, ViewItemPaintParams &pp);
162
163         /**
164          * Paints a zero axis across the viewport.
165          * @param p the QPainter to paint into.
166          * @param pp the painting parameters object to paint with.
167          * @param y the y-offset of the axis.
168          */
169         void paint_axis(QPainter &p, ViewItemPaintParams &pp, int y);
170
171         /**
172          * Draw a hover marker under the cursor position.
173          * @param p The painter to draw into.
174          */
175         void paint_hover_marker(QPainter &p);
176
177         void add_color_option(QWidget *parent, QFormLayout *form);
178
179         void create_popup_form();
180
181         virtual void populate_popup_form(QWidget *parent, QFormLayout *form);
182
183 protected Q_SLOTS:
184         virtual void on_name_changed(const QString &text);
185
186         virtual void on_color_changed(const QColor &color);
187
188         void on_popup_closed();
189
190 private Q_SLOTS:
191         void on_nameedit_changed(const QString &name);
192
193         void on_coloredit_changed(const QColor &color);
194
195 protected:
196         shared_ptr<data::SignalBase> base_;
197         QPen axis_pen_;
198
199         SegmentDisplayMode segment_display_mode_;
200         bool show_hover_marker_;
201
202         /// The ID of the currently displayed segment
203         int current_segment_;
204
205 private:
206         pv::widgets::Popup *popup_;
207         QFormLayout *popup_form_;
208 };
209
210 } // namespace trace
211 } // namespace views
212 } // namespace pv
213
214 #endif // PULSEVIEW_PV_VIEWS_TRACEVIEW_TRACE_HPP