]> sigrok.org Git - pulseview.git/blob - pv/view/trace.cpp
Trace: Removed coloured_bg state
[pulseview.git] / pv / view / trace.cpp
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 #include <extdef.h>
21
22 #include <cassert>
23 #include <cmath>
24
25 #include <QApplication>
26 #include <QFormLayout>
27 #include <QKeyEvent>
28 #include <QLineEdit>
29
30 #include "trace.hpp"
31 #include "tracepalette.hpp"
32 #include "view.hpp"
33
34 #include "pv/globalsettings.hpp"
35 #include "pv/widgets/colourbutton.hpp"
36 #include "pv/widgets/popup.hpp"
37
38 using std::pair;
39 using std::shared_ptr;
40
41 namespace pv {
42 namespace views {
43 namespace TraceView {
44
45 const QPen Trace::AxisPen(QColor(0, 0, 0, 30 * 256 / 100));
46 const int Trace::LabelHitPadding = 2;
47
48 const QColor Trace::BrightGrayBGColour = QColor(0, 0, 0, 10 * 255 / 100);
49 const QColor Trace::DarkGrayBGColour = QColor(0, 0, 0, 15 * 255 / 100);
50
51 Trace::Trace(shared_ptr<data::SignalBase> channel) :
52         base_(channel),
53         popup_(nullptr),
54         popup_form_(nullptr)
55 {
56         connect(channel.get(), SIGNAL(name_changed(const QString&)),
57                 this, SLOT(on_name_changed(const QString&)));
58         connect(channel.get(), SIGNAL(colour_changed(const QColor&)),
59                 this, SLOT(on_colour_changed(const QColor&)));
60 }
61
62 void Trace::paint_label(QPainter &p, const QRect &rect, bool hover)
63 {
64         const int y = get_visual_y();
65
66         p.setBrush(base_->colour());
67
68         if (!enabled())
69                 return;
70
71         const QRectF r = label_rect(rect);
72
73         // Paint the label
74         const float label_arrow_length = r.height() / 2;
75         const QPointF points[] = {
76                 r.topLeft(),
77                 QPointF(r.right() - label_arrow_length, r.top()),
78                 QPointF(r.right(), y),
79                 QPointF(r.right() - label_arrow_length, r.bottom()),
80                 r.bottomLeft()
81         };
82         const QPointF highlight_points[] = {
83                 QPointF(r.left() + 1, r.top() + 1),
84                 QPointF(r.right() - label_arrow_length, r.top() + 1),
85                 QPointF(r.right() - 1, y),
86                 QPointF(r.right() - label_arrow_length, r.bottom() - 1),
87                 QPointF(r.left() + 1, r.bottom() - 1)
88         };
89
90         if (selected()) {
91                 p.setPen(highlight_pen());
92                 p.setBrush(Qt::transparent);
93                 p.drawPolygon(points, countof(points));
94         }
95
96         p.setPen(Qt::transparent);
97         p.setBrush(hover ? base_->colour().lighter() : base_->colour());
98         p.drawPolygon(points, countof(points));
99
100         p.setPen(base_->colour().lighter());
101         p.setBrush(Qt::transparent);
102         p.drawPolygon(highlight_points, countof(highlight_points));
103
104         p.setPen(base_->colour().darker());
105         p.setBrush(Qt::transparent);
106         p.drawPolygon(points, countof(points));
107
108         // Paint the text
109         p.setPen(select_text_colour(base_->colour()));
110         p.setFont(QApplication::font());
111         p.drawText(QRectF(r.x(), r.y(),
112                 r.width() - label_arrow_length, r.height()),
113                 Qt::AlignCenter | Qt::AlignVCenter, base_->name());
114 }
115
116 QMenu* Trace::create_context_menu(QWidget *parent)
117 {
118         QMenu *const menu = ViewItem::create_context_menu(parent);
119
120         return menu;
121 }
122
123 pv::widgets::Popup* Trace::create_popup(QWidget *parent)
124 {
125         using pv::widgets::Popup;
126
127         popup_ = new Popup(parent);
128         popup_->set_position(parent->mapToGlobal(
129                 point(parent->rect())), Popup::Right);
130
131         create_popup_form();
132
133         connect(popup_, SIGNAL(closed()), this, SLOT(on_popup_closed()));
134
135         return popup_;
136 }
137
138 QRectF Trace::label_rect(const QRectF &rect) const
139 {
140         QFontMetrics m(QApplication::font());
141         const QSize text_size(
142                 m.boundingRect(QRect(), 0, base_->name()).width(), m.height());
143         const QSizeF label_size(
144                 text_size.width() + LabelPadding.width() * 2,
145                 ceilf((text_size.height() + LabelPadding.height() * 2) / 2) * 2);
146         const float half_height = label_size.height() / 2;
147         return QRectF(
148                 rect.right() - half_height - label_size.width() - 0.5,
149                 get_visual_y() + 0.5f - half_height,
150                 label_size.width() + half_height,
151                 label_size.height());
152 }
153
154 void Trace::paint_back(QPainter &p, const ViewItemPaintParams &pp)
155 {
156         const View *view = owner_->view();
157         assert(view);
158
159         if (view->coloured_bg())
160                 p.setBrush(base_->bgcolour());
161         else
162                 p.setBrush(bgcolour_state_ ? BrightGrayBGColour : DarkGrayBGColour);
163
164         p.setPen(QPen(Qt::NoPen));
165
166         const pair<int, int> extents = v_extents();
167
168         const int x = 0;
169         const int y = get_visual_y() + extents.first;
170         const int w = pp.right() - pp.left();
171         const int h = extents.second - extents.first;
172
173         p.drawRect(x, y, w, h);
174 }
175
176 void Trace::paint_axis(QPainter &p, const ViewItemPaintParams &pp, int y)
177 {
178         p.setRenderHint(QPainter::Antialiasing, false);
179
180         p.setPen(AxisPen);
181         p.drawLine(QPointF(pp.left(), y), QPointF(pp.right(), y));
182
183         p.setRenderHint(QPainter::Antialiasing, true);
184 }
185
186 void Trace::add_colour_option(QWidget *parent, QFormLayout *form)
187 {
188         using pv::widgets::ColourButton;
189
190         ColourButton *const colour_button = new ColourButton(
191                 TracePalette::Rows, TracePalette::Cols, parent);
192         colour_button->set_palette(TracePalette::Colours);
193         colour_button->set_colour(base_->colour());
194         connect(colour_button, SIGNAL(selected(const QColor&)),
195                 this, SLOT(on_colouredit_changed(const QColor&)));
196
197         form->addRow(tr("Colour"), colour_button);
198 }
199
200 void Trace::create_popup_form()
201 {
202         // Clear the layout
203
204         // Transfer the layout and the child widgets to a temporary widget
205         // which we delete after the event was handled. This way, the layout
206         // and all widgets contained therein are deleted after the event was
207         // handled, leaving the parent popup_ time to handle the change.
208         if (popup_form_) {
209                 QWidget *suicidal = new QWidget();
210                 suicidal->setLayout(popup_form_);
211                 suicidal->deleteLater();
212         }
213
214         // Repopulate the popup
215         popup_form_ = new QFormLayout(popup_);
216         popup_->setLayout(popup_form_);
217         populate_popup_form(popup_, popup_form_);
218 }
219
220 void Trace::populate_popup_form(QWidget *parent, QFormLayout *form)
221 {
222         QLineEdit *const name_edit = new QLineEdit(parent);
223         name_edit->setText(base_->name());
224         connect(name_edit, SIGNAL(textChanged(const QString&)),
225                 this, SLOT(on_nameedit_changed(const QString&)));
226         form->addRow(tr("Name"), name_edit);
227
228         add_colour_option(parent, form);
229 }
230
231 void Trace::set_name(QString name)
232 {
233         base_->set_name(name);
234 }
235
236 void Trace::set_colour(QColor colour)
237 {
238         base_->set_colour(colour);
239 }
240
241 void Trace::on_name_changed(const QString &text)
242 {
243         /* This event handler is called by SignalBase when the name was changed there */
244         (void)text;
245
246         if (owner_) {
247                 owner_->extents_changed(true, false);
248                 owner_->row_item_appearance_changed(true, false);
249         }
250 }
251
252 void Trace::on_colour_changed(const QColor &colour)
253 {
254         /* This event handler is called by SignalBase when the colour was changed there */
255         (void)colour;
256
257         if (owner_)
258                 owner_->row_item_appearance_changed(true, true);
259 }
260
261 void Trace::on_popup_closed()
262 {
263         popup_ = nullptr;
264         popup_form_ = nullptr;
265 }
266
267 void Trace::on_nameedit_changed(const QString &name)
268 {
269         /* This event handler notifies SignalBase that the name changed */
270         set_name(name);
271 }
272
273 void Trace::on_colouredit_changed(const QColor &colour)
274 {
275         /* This event handler notifies SignalBase that the colour changed */
276         set_colour(colour);
277 }
278
279 } // namespace TraceView
280 } // namespace views
281 } // namespace pv