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