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