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