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