]>
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 | 28 | #include <QLineEdit> |
79b53a1a | 29 | #include <QMenu> |
b213ef09 | 30 | |
79b53a1a | 31 | #include "ruler.hpp" |
2acdb232 JH |
32 | #include "trace.hpp" |
33 | #include "tracepalette.hpp" | |
34 | #include "view.hpp" | |
931f20b0 | 35 | |
24c29d4f | 36 | #include "pv/globalsettings.hpp" |
641574bc | 37 | #include "pv/widgets/colorbutton.hpp" |
24c29d4f | 38 | #include "pv/widgets/popup.hpp" |
569d1e41 | 39 | |
6f925ba9 UH |
40 | using std::pair; |
41 | using std::shared_ptr; | |
42 | ||
931f20b0 | 43 | namespace pv { |
f4e57597 | 44 | namespace views { |
1573bf16 | 45 | namespace trace { |
931f20b0 | 46 | |
c063290a | 47 | const QPen Trace::AxisPen(QColor(0, 0, 0, 30 * 256 / 100)); |
931f20b0 JH |
48 | const int Trace::LabelHitPadding = 2; |
49 | ||
641574bc SA |
50 | const QColor Trace::BrightGrayBGColor = QColor(0, 0, 0, 10 * 255 / 100); |
51 | const QColor Trace::DarkGrayBGColor = QColor(0, 0, 0, 15 * 255 / 100); | |
ac0708fb | 52 | |
6f925ba9 | 53 | Trace::Trace(shared_ptr<data::SignalBase> channel) : |
73a25a6e | 54 | base_(channel), |
561ba3ae | 55 | axis_pen_(AxisPen), |
7daebd05 | 56 | segment_display_mode_(ShowLastSegmentOnly), // Will be overwritten by View |
2749b858 | 57 | current_segment_(0), |
4c60462b JH |
58 | popup_(nullptr), |
59 | popup_form_(nullptr) | |
931f20b0 | 60 | { |
bf0edd2b SA |
61 | connect(channel.get(), SIGNAL(name_changed(const QString&)), |
62 | this, SLOT(on_name_changed(const QString&))); | |
641574bc SA |
63 | connect(channel.get(), SIGNAL(color_changed(const QColor&)), |
64 | this, SLOT(on_color_changed(const QColor&))); | |
1931b5f9 SA |
65 | |
66 | GlobalSettings::add_change_handler(this); | |
67 | ||
68 | GlobalSettings settings; | |
69 | show_hover_marker_ = | |
70 | settings.value(GlobalSettings::Key_View_ShowHoverMarker).toBool(); | |
931f20b0 JH |
71 | } |
72 | ||
99c49526 SA |
73 | Trace::~Trace() |
74 | { | |
75 | GlobalSettings::remove_change_handler(this); | |
76 | } | |
77 | ||
5ed05b69 SA |
78 | shared_ptr<data::SignalBase> Trace::base() const |
79 | { | |
80 | return base_; | |
81 | } | |
82 | ||
cbd9ec7f SA |
83 | bool Trace::is_selectable(QPoint pos) const |
84 | { | |
85 | // True if the header was clicked, false if the trace area was clicked | |
86 | const View *view = owner_->view(); | |
87 | assert(view); | |
88 | ||
89 | return (pos.x() <= view->header_width()); | |
90 | } | |
91 | ||
119c5c23 | 92 | bool Trace::is_draggable(QPoint pos) const |
cbd9ec7f SA |
93 | { |
94 | // While the header label that belongs to this trace is draggable, | |
119c5c23 SA |
95 | // the trace itself shall not be. Hence we return true if the header |
96 | // was clicked and false if the trace area was clicked | |
97 | const View *view = owner_->view(); | |
98 | assert(view); | |
99 | ||
100 | return (pos.x() <= view->header_width()); | |
cbd9ec7f SA |
101 | } |
102 | ||
1931b5f9 SA |
103 | void Trace::set_segment_display_mode(SegmentDisplayMode mode) |
104 | { | |
105 | segment_display_mode_ = mode; | |
106 | ||
107 | if (owner_) | |
108 | owner_->row_item_appearance_changed(true, true); | |
109 | } | |
110 | ||
111 | void Trace::on_setting_changed(const QString &key, const QVariant &value) | |
112 | { | |
113 | if (key == GlobalSettings::Key_View_ShowHoverMarker) | |
114 | show_hover_marker_ = value.toBool(); | |
39dce56b SA |
115 | |
116 | // Force a repaint since many options alter the way traces look | |
117 | if (owner_) | |
118 | owner_->row_item_appearance_changed(false, true); | |
1931b5f9 SA |
119 | } |
120 | ||
b3f44329 | 121 | void Trace::paint_label(QPainter &p, const QRect &rect, bool hover) |
01fd3263 | 122 | { |
be9e7b4b | 123 | const int y = get_visual_y(); |
01fd3263 | 124 | |
641574bc | 125 | p.setBrush(base_->color()); |
931f20b0 JH |
126 | |
127 | if (!enabled()) | |
128 | return; | |
129 | ||
b3f44329 | 130 | const QRectF r = label_rect(rect); |
931f20b0 | 131 | |
c1a6513b SA |
132 | // When selected, move the arrow to the left so that the border can show |
133 | const QPointF offs = (selected()) ? QPointF(-2, 0) : QPointF(0, 0); | |
134 | ||
931f20b0 | 135 | // Paint the label |
b5cb6c35 | 136 | const float label_arrow_length = r.height() / 2; |
c1a6513b SA |
137 | QPointF points[] = { |
138 | offs + r.topLeft(), | |
139 | offs + QPointF(r.right() - label_arrow_length, r.top()), | |
140 | offs + QPointF(r.right(), y), | |
141 | offs + QPointF(r.right() - label_arrow_length, r.bottom()), | |
142 | offs + r.bottomLeft() | |
931f20b0 | 143 | }; |
c1a6513b SA |
144 | QPointF highlight_points[] = { |
145 | offs + QPointF(r.left() + 1, r.top() + 1), | |
146 | offs + QPointF(r.right() - label_arrow_length, r.top() + 1), | |
147 | offs + QPointF(r.right() - 1, y), | |
148 | offs + QPointF(r.right() - label_arrow_length, r.bottom() - 1), | |
149 | offs + QPointF(r.left() + 1, r.bottom() - 1) | |
931f20b0 JH |
150 | }; |
151 | ||
152 | if (selected()) { | |
153 | p.setPen(highlight_pen()); | |
154 | p.setBrush(Qt::transparent); | |
155 | p.drawPolygon(points, countof(points)); | |
156 | } | |
157 | ||
158 | p.setPen(Qt::transparent); | |
641574bc | 159 | p.setBrush(hover ? base_->color().lighter() : base_->color()); |
931f20b0 JH |
160 | p.drawPolygon(points, countof(points)); |
161 | ||
641574bc | 162 | p.setPen(base_->color().lighter()); |
931f20b0 JH |
163 | p.setBrush(Qt::transparent); |
164 | p.drawPolygon(highlight_points, countof(highlight_points)); | |
165 | ||
641574bc | 166 | p.setPen(base_->color().darker()); |
931f20b0 JH |
167 | p.setBrush(Qt::transparent); |
168 | p.drawPolygon(points, countof(points)); | |
169 | ||
170 | // Paint the text | |
641574bc | 171 | p.setPen(select_text_color(base_->color())); |
d7c0ca4a | 172 | p.setFont(QApplication::font()); |
b5cb6c35 JH |
173 | p.drawText(QRectF(r.x(), r.y(), |
174 | r.width() - label_arrow_length, r.height()), | |
73a25a6e | 175 | Qt::AlignCenter | Qt::AlignVCenter, base_->name()); |
931f20b0 JH |
176 | } |
177 | ||
9e773fec | 178 | QMenu* Trace::create_header_context_menu(QWidget *parent) |
9b6378f1 | 179 | { |
9e773fec | 180 | QMenu *const menu = ViewItem::create_header_context_menu(parent); |
9b6378f1 | 181 | |
9b6378f1 JH |
182 | return menu; |
183 | } | |
184 | ||
79b53a1a SA |
185 | QMenu* Trace::create_view_context_menu(QWidget *parent, QPoint &click_pos) |
186 | { | |
187 | context_menu_x_pos_ = click_pos.x(); | |
188 | ||
189 | // Get entries from default menu before adding our own | |
190 | QMenu *const menu = new QMenu(parent); | |
191 | ||
192 | QMenu* default_menu = TraceTreeItem::create_view_context_menu(parent, click_pos); | |
193 | if (default_menu) { | |
7a0d99e6 | 194 | for (QAction *action : default_menu->actions()) { // clazy:exclude=range-loop |
79b53a1a SA |
195 | menu->addAction(action); |
196 | if (action->parent() == default_menu) | |
197 | action->setParent(menu); | |
198 | } | |
199 | delete default_menu; | |
200 | ||
201 | // Add separator if needed | |
202 | if (menu->actions().length() > 0) | |
203 | menu->addSeparator(); | |
204 | } | |
205 | ||
206 | QAction *const create_marker_here = new QAction(tr("Create marker here"), this); | |
207 | connect(create_marker_here, SIGNAL(triggered()), this, SLOT(on_create_marker_here())); | |
208 | menu->addAction(create_marker_here); | |
209 | ||
210 | return menu; | |
211 | } | |
212 | ||
569d1e41 JH |
213 | pv::widgets::Popup* Trace::create_popup(QWidget *parent) |
214 | { | |
215 | using pv::widgets::Popup; | |
20eaae39 | 216 | |
8dbbc7f0 | 217 | popup_ = new Popup(parent); |
786b7678 | 218 | popup_->set_position(parent->mapToGlobal( |
a3d5a7c7 | 219 | drag_point(parent->rect())), Popup::Right); |
20eaae39 | 220 | |
37fd11b1 | 221 | create_popup_form(); |
20eaae39 | 222 | |
c063290a | 223 | connect(popup_, SIGNAL(closed()), this, SLOT(on_popup_closed())); |
20eaae39 | 224 | |
8dbbc7f0 | 225 | return popup_; |
569d1e41 JH |
226 | } |
227 | ||
b3f44329 | 228 | QRectF Trace::label_rect(const QRectF &rect) const |
d7c0ca4a | 229 | { |
d7c0ca4a JH |
230 | QFontMetrics m(QApplication::font()); |
231 | const QSize text_size( | |
73a25a6e | 232 | m.boundingRect(QRect(), 0, base_->name()).width(), m.height()); |
d7c0ca4a | 233 | const QSizeF label_size( |
ec39632d JH |
234 | text_size.width() + LabelPadding.width() * 2, |
235 | ceilf((text_size.height() + LabelPadding.height() * 2) / 2) * 2); | |
b5cb6c35 | 236 | const float half_height = label_size.height() / 2; |
d7c0ca4a | 237 | return QRectF( |
b3f44329 | 238 | rect.right() - half_height - label_size.width() - 0.5, |
be9e7b4b | 239 | get_visual_y() + 0.5f - half_height, |
b5cb6c35 JH |
240 | label_size.width() + half_height, |
241 | label_size.height()); | |
d7c0ca4a JH |
242 | } |
243 | ||
d9b55cc8 SA |
244 | QRectF Trace::hit_box_rect(const ViewItemPaintParams &pp) const |
245 | { | |
cbd9ec7f SA |
246 | // This one is only for the trace itself, excluding the header area |
247 | const View *view = owner_->view(); | |
248 | assert(view); | |
249 | ||
d9b55cc8 SA |
250 | pair<int, int> extents = v_extents(); |
251 | const int top = pp.top() + get_visual_y() + extents.first; | |
252 | const int height = extents.second - extents.first; | |
cbd9ec7f SA |
253 | |
254 | return QRectF(pp.left() + view->header_width(), top, | |
255 | pp.width() - view->header_width(), height); | |
d9b55cc8 SA |
256 | } |
257 | ||
2749b858 SA |
258 | void Trace::set_current_segment(const int segment) |
259 | { | |
260 | current_segment_ = segment; | |
261 | } | |
262 | ||
263 | int Trace::get_current_segment() const | |
264 | { | |
265 | return current_segment_; | |
266 | } | |
267 | ||
1931b5f9 SA |
268 | void Trace::hover_point_changed(const QPoint &hp) |
269 | { | |
270 | (void)hp; | |
271 | ||
272 | if (owner_) | |
273 | owner_->row_item_appearance_changed(false, true); | |
274 | } | |
275 | ||
60938e04 | 276 | void Trace::paint_back(QPainter &p, ViewItemPaintParams &pp) |
99af6802 | 277 | { |
48995388 JH |
278 | const View *view = owner_->view(); |
279 | assert(view); | |
280 | ||
641574bc SA |
281 | if (view->colored_bg()) |
282 | p.setBrush(base_->bgcolor()); | |
ac0708fb | 283 | else |
641574bc | 284 | p.setBrush(pp.next_bg_color_state() ? BrightGrayBGColor : DarkGrayBGColor); |
99af6802 | 285 | |
ac0708fb | 286 | p.setPen(QPen(Qt::NoPen)); |
99af6802 | 287 | |
6f925ba9 | 288 | const pair<int, int> extents = v_extents(); |
857ebbef JH |
289 | p.drawRect(pp.left(), get_visual_y() + extents.first, |
290 | pp.width(), extents.second - extents.first); | |
99af6802 SA |
291 | } |
292 | ||
60938e04 | 293 | void Trace::paint_axis(QPainter &p, ViewItemPaintParams &pp, int y) |
fe08b6e8 | 294 | { |
46a0cadc SA |
295 | p.setRenderHint(QPainter::Antialiasing, false); |
296 | ||
561ba3ae | 297 | p.setPen(axis_pen_); |
46a0cadc SA |
298 | p.drawLine(QPointF(pp.left(), y), QPointF(pp.right(), y)); |
299 | ||
300 | p.setRenderHint(QPainter::Antialiasing, true); | |
fe08b6e8 JH |
301 | } |
302 | ||
641574bc | 303 | void Trace::add_color_option(QWidget *parent, QFormLayout *form) |
91e8bf08 | 304 | { |
641574bc | 305 | using pv::widgets::ColorButton; |
91e8bf08 | 306 | |
641574bc | 307 | ColorButton *const color_button = new ColorButton( |
91e8bf08 | 308 | TracePalette::Rows, TracePalette::Cols, parent); |
641574bc SA |
309 | color_button->set_palette(TracePalette::Colors); |
310 | color_button->set_color(base_->color()); | |
311 | connect(color_button, SIGNAL(selected(const QColor&)), | |
312 | this, SLOT(on_coloredit_changed(const QColor&))); | |
91e8bf08 | 313 | |
641574bc | 314 | form->addRow(tr("Color"), color_button); |
91e8bf08 JH |
315 | } |
316 | ||
1931b5f9 SA |
317 | void Trace::paint_hover_marker(QPainter &p) |
318 | { | |
319 | const View *view = owner_->view(); | |
320 | assert(view); | |
321 | ||
322 | const int x = view->hover_point().x(); | |
323 | ||
324 | if (x == -1) | |
325 | return; | |
326 | ||
327 | p.setPen(QPen(QColor(Qt::lightGray))); | |
328 | ||
329 | const pair<int, int> extents = v_extents(); | |
330 | ||
331 | p.setRenderHint(QPainter::Antialiasing, false); | |
332 | p.drawLine(x, get_visual_y() + extents.first, | |
333 | x, get_visual_y() + extents.second); | |
334 | p.setRenderHint(QPainter::Antialiasing, true); | |
335 | } | |
336 | ||
37fd11b1 JH |
337 | void Trace::create_popup_form() |
338 | { | |
339 | // Clear the layout | |
340 | ||
341 | // Transfer the layout and the child widgets to a temporary widget | |
e3004449 EO |
342 | // which we delete after the event was handled. This way, the layout |
343 | // and all widgets contained therein are deleted after the event was | |
344 | // handled, leaving the parent popup_ time to handle the change. | |
345 | if (popup_form_) { | |
346 | QWidget *suicidal = new QWidget(); | |
347 | suicidal->setLayout(popup_form_); | |
348 | suicidal->deleteLater(); | |
349 | } | |
37fd11b1 JH |
350 | |
351 | // Repopulate the popup | |
8dbbc7f0 JH |
352 | popup_form_ = new QFormLayout(popup_); |
353 | popup_->setLayout(popup_form_); | |
354 | populate_popup_form(popup_, popup_form_); | |
37fd11b1 JH |
355 | } |
356 | ||
569d1e41 JH |
357 | void Trace::populate_popup_form(QWidget *parent, QFormLayout *form) |
358 | { | |
68456dab | 359 | QLineEdit *const name_edit = new QLineEdit(parent); |
73a25a6e | 360 | name_edit->setText(base_->name()); |
68456dab | 361 | connect(name_edit, SIGNAL(textChanged(const QString&)), |
bf0edd2b | 362 | this, SLOT(on_nameedit_changed(const QString&))); |
68456dab | 363 | form->addRow(tr("Name"), name_edit); |
91e8bf08 | 364 | |
641574bc | 365 | add_color_option(parent, form); |
569d1e41 JH |
366 | } |
367 | ||
bf0edd2b SA |
368 | void Trace::on_name_changed(const QString &text) |
369 | { | |
370 | /* This event handler is called by SignalBase when the name was changed there */ | |
371 | (void)text; | |
372 | ||
373 | if (owner_) { | |
8dbbc7f0 | 374 | owner_->extents_changed(true, false); |
bf0edd2b SA |
375 | owner_->row_item_appearance_changed(true, false); |
376 | } | |
68456dab | 377 | } |
9b6378f1 | 378 | |
641574bc | 379 | void Trace::on_color_changed(const QColor &color) |
91e8bf08 | 380 | { |
641574bc SA |
381 | /* This event handler is called by SignalBase when the color was changed there */ |
382 | (void)color; | |
32218d3e | 383 | |
8dbbc7f0 | 384 | if (owner_) |
99af6802 | 385 | owner_->row_item_appearance_changed(true, true); |
91e8bf08 JH |
386 | } |
387 | ||
bf0edd2b SA |
388 | void Trace::on_popup_closed() |
389 | { | |
390 | popup_ = nullptr; | |
391 | popup_form_ = nullptr; | |
392 | } | |
393 | ||
394 | void Trace::on_nameedit_changed(const QString &name) | |
395 | { | |
396 | /* This event handler notifies SignalBase that the name changed */ | |
0acf629d | 397 | base_->set_name(name); |
bf0edd2b SA |
398 | } |
399 | ||
641574bc | 400 | void Trace::on_coloredit_changed(const QColor &color) |
bf0edd2b | 401 | { |
641574bc | 402 | /* This event handler notifies SignalBase that the color changed */ |
0acf629d | 403 | base_->set_color(color); |
bf0edd2b SA |
404 | } |
405 | ||
79b53a1a SA |
406 | void Trace::on_create_marker_here() const |
407 | { | |
408 | View *view = owner_->view(); | |
409 | assert(view); | |
410 | ||
411 | const Ruler *ruler = view->ruler(); | |
412 | QPoint p = ruler->mapFrom(view, QPoint(context_menu_x_pos_, 0)); | |
413 | ||
fe68068b | 414 | view->add_flag(ruler->get_absolute_time_from_x_pos(p.x())); |
79b53a1a SA |
415 | } |
416 | ||
1573bf16 | 417 | } // namespace trace |
f4e57597 | 418 | } // namespace views |
931f20b0 | 419 | } // namespace pv |