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