2 * This file is part of the PulseView project.
4 * Copyright (C) 2013 Joel Holdsworth <joel@airwebreathe.org.uk>
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.
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.
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/>.
25 #include <QApplication>
26 #include <QFormLayout>
33 #include "tracepalette.hpp"
36 #include "pv/globalsettings.hpp"
37 #include "pv/widgets/colorbutton.hpp"
38 #include "pv/widgets/popup.hpp"
41 using std::shared_ptr;
47 const QPen Trace::AxisPen(QColor(0, 0, 0, 30 * 256 / 100));
48 const int Trace::LabelHitPadding = 2;
50 const QColor Trace::BrightGrayBGColor = QColor(0, 0, 0, 10 * 255 / 100);
51 const QColor Trace::DarkGrayBGColor = QColor(0, 0, 0, 15 * 255 / 100);
52 const QColor Trace::ErrorBgColor = QColor(0xEF, 0x29, 0x29);
54 Trace::Trace(shared_ptr<data::SignalBase> signal) :
57 segment_display_mode_(ShowLastSegmentOnly), // Will be overwritten by View
62 connect(signal.get(), SIGNAL(name_changed(const QString&)),
63 this, SLOT(on_name_changed(const QString&)));
64 connect(signal.get(), SIGNAL(color_changed(const QColor&)),
65 this, SLOT(on_color_changed(const QColor&)));
66 connect(signal.get(), SIGNAL(error_message_changed(const QString&)),
67 this, SLOT(on_error_message_changed(const QString&)));
69 GlobalSettings::add_change_handler(this);
71 GlobalSettings settings;
73 settings.value(GlobalSettings::Key_View_ShowHoverMarker).toBool();
78 GlobalSettings::remove_change_handler(this);
81 shared_ptr<data::SignalBase> Trace::base() const
86 bool Trace::is_selectable(QPoint pos) const
88 // True if the header was clicked, false if the trace area was clicked
89 const View *view = owner_->view();
92 return (pos.x() <= view->header_width());
95 bool Trace::is_draggable(QPoint pos) const
97 // While the header label that belongs to this trace is draggable,
98 // the trace itself shall not be. Hence we return true if the header
99 // was clicked and false if the trace area was clicked
100 const View *view = owner_->view();
103 return (pos.x() <= view->header_width());
106 void Trace::set_segment_display_mode(SegmentDisplayMode mode)
108 segment_display_mode_ = mode;
111 owner_->row_item_appearance_changed(true, true);
114 void Trace::on_setting_changed(const QString &key, const QVariant &value)
116 if (key == GlobalSettings::Key_View_ShowHoverMarker)
117 show_hover_marker_ = value.toBool();
119 // Force a repaint since many options alter the way traces look
121 owner_->row_item_appearance_changed(false, true);
124 void Trace::paint_label(QPainter &p, const QRect &rect, bool hover)
126 const int y = get_visual_y();
128 p.setBrush(base_->color());
133 const QRectF r = label_rect(rect);
135 // When selected, move the arrow to the left so that the border can show
136 const QPointF offs = (selected()) ? QPointF(-2, 0) : QPointF(0, 0);
139 const float label_arrow_length = r.height() / 2;
142 offs + QPointF(r.right() - label_arrow_length, r.top()),
143 offs + QPointF(r.right(), y),
144 offs + QPointF(r.right() - label_arrow_length, r.bottom()),
145 offs + r.bottomLeft()
147 QPointF highlight_points[] = {
148 offs + QPointF(r.left() + 1, r.top() + 1),
149 offs + QPointF(r.right() - label_arrow_length, r.top() + 1),
150 offs + QPointF(r.right() - 1, y),
151 offs + QPointF(r.right() - label_arrow_length, r.bottom() - 1),
152 offs + QPointF(r.left() + 1, r.bottom() - 1)
156 p.setPen(highlight_pen());
157 p.setBrush(Qt::transparent);
158 p.drawPolygon(points, countof(points));
161 p.setPen(Qt::transparent);
162 p.setBrush(hover ? base_->color().lighter() : base_->color());
163 p.drawPolygon(points, countof(points));
165 p.setPen(base_->color().lighter());
166 p.setBrush(Qt::transparent);
167 p.drawPolygon(highlight_points, countof(highlight_points));
169 p.setPen(base_->color().darker());
170 p.setBrush(Qt::transparent);
171 p.drawPolygon(points, countof(points));
174 p.setPen(select_text_color(base_->color()));
175 p.setFont(QApplication::font());
176 p.drawText(QRectF(r.x(), r.y(),
177 r.width() - label_arrow_length, r.height()),
178 Qt::AlignCenter | Qt::AlignVCenter, base_->name());
181 void Trace::paint_error(QPainter &p, const ViewItemPaintParams &pp)
183 const QString message = base_->get_error_message();
185 const int y = get_visual_y();
187 p.setPen(ErrorBgColor.darker());
188 p.setBrush(ErrorBgColor);
190 const QRectF bounding_rect = QRectF(pp.left(), INT_MIN / 2 + y, pp.right(), INT_MAX);
192 const QRectF text_rect = p.boundingRect(bounding_rect, Qt::AlignCenter, message);
193 const qreal r = text_rect.height() / 4;
195 p.drawRoundedRect(text_rect.adjusted(-r, -r, r, r), r, r, Qt::AbsoluteSize);
198 p.drawText(text_rect, message);
201 QMenu* Trace::create_header_context_menu(QWidget *parent)
203 QMenu *const menu = ViewItem::create_header_context_menu(parent);
208 QMenu* Trace::create_view_context_menu(QWidget *parent, QPoint &click_pos)
210 context_menu_x_pos_ = click_pos.x();
212 // Get entries from default menu before adding our own
213 QMenu *const menu = new QMenu(parent);
215 QMenu* default_menu = TraceTreeItem::create_view_context_menu(parent, click_pos);
217 for (QAction *action : default_menu->actions()) { // clazy:exclude=range-loop
218 menu->addAction(action);
219 if (action->parent() == default_menu)
220 action->setParent(menu);
224 // Add separator if needed
225 if (menu->actions().length() > 0)
226 menu->addSeparator();
229 QAction *const create_marker_here = new QAction(tr("Create marker here"), this);
230 connect(create_marker_here, SIGNAL(triggered()), this, SLOT(on_create_marker_here()));
231 menu->addAction(create_marker_here);
236 pv::widgets::Popup* Trace::create_popup(QWidget *parent)
238 using pv::widgets::Popup;
240 popup_ = new Popup(parent);
241 popup_->set_position(parent->mapToGlobal(
242 drag_point(parent->rect())), Popup::Right);
246 connect(popup_, SIGNAL(closed()), this, SLOT(on_popup_closed()));
251 QRectF Trace::label_rect(const QRectF &rect) const
253 QFontMetrics m(QApplication::font());
254 const QSize text_size(
255 m.boundingRect(QRect(), 0, base_->name()).width(), m.height());
256 const QSizeF label_size(
257 text_size.width() + LabelPadding.width() * 2,
258 ceilf((text_size.height() + LabelPadding.height() * 2) / 2) * 2);
259 const float half_height = label_size.height() / 2;
261 rect.right() - half_height - label_size.width() - 0.5,
262 get_visual_y() + 0.5f - half_height,
263 label_size.width() + half_height,
264 label_size.height());
267 QRectF Trace::hit_box_rect(const ViewItemPaintParams &pp) const
269 // This one is only for the trace itself, excluding the header area
270 const View *view = owner_->view();
273 pair<int, int> extents = v_extents();
274 const int top = pp.top() + get_visual_y() + extents.first;
275 const int height = extents.second - extents.first;
277 return QRectF(pp.left() + view->header_width(), top,
278 pp.width() - view->header_width(), height);
281 void Trace::set_current_segment(const int segment)
283 current_segment_ = segment;
286 int Trace::get_current_segment() const
288 return current_segment_;
291 void Trace::hover_point_changed(const QPoint &hp)
296 owner_->row_item_appearance_changed(false, true);
299 void Trace::paint_back(QPainter &p, ViewItemPaintParams &pp)
301 const View *view = owner_->view();
304 if (view->colored_bg())
305 p.setBrush(base_->bgcolor());
307 p.setBrush(pp.next_bg_color_state() ? BrightGrayBGColor : DarkGrayBGColor);
309 p.setPen(QPen(Qt::NoPen));
311 const pair<int, int> extents = v_extents();
312 p.drawRect(pp.left(), get_visual_y() + extents.first,
313 pp.width(), extents.second - extents.first);
316 void Trace::paint_axis(QPainter &p, ViewItemPaintParams &pp, int y)
318 bool was_antialiased = p.testRenderHint(QPainter::Antialiasing);
319 p.setRenderHint(QPainter::Antialiasing, false);
322 p.drawLine(QPointF(pp.left(), y), QPointF(pp.right(), y));
324 p.setRenderHint(QPainter::Antialiasing, was_antialiased);
327 void Trace::add_color_option(QWidget *parent, QFormLayout *form)
329 using pv::widgets::ColorButton;
331 ColorButton *const color_button = new ColorButton(
332 TracePalette::Rows, TracePalette::Cols, parent);
333 color_button->set_palette(TracePalette::Colors);
334 color_button->set_color(base_->color());
335 connect(color_button, SIGNAL(selected(const QColor&)),
336 this, SLOT(on_coloredit_changed(const QColor&)));
338 form->addRow(tr("Color"), color_button);
341 void Trace::paint_hover_marker(QPainter &p)
343 const View *view = owner_->view();
346 const int x = view->hover_point().x();
351 p.setPen(QPen(QColor(Qt::lightGray)));
353 const pair<int, int> extents = v_extents();
355 bool was_antialiased = p.testRenderHint(QPainter::Antialiasing);
356 p.setRenderHint(QPainter::Antialiasing, false);
357 p.drawLine(x, get_visual_y() + extents.first,
358 x, get_visual_y() + extents.second);
359 p.setRenderHint(QPainter::Antialiasing, was_antialiased);
362 void Trace::create_popup_form()
366 // Transfer the layout and the child widgets to a temporary widget
367 // which we delete after the event was handled. This way, the layout
368 // and all widgets contained therein are deleted after the event was
369 // handled, leaving the parent popup_ time to handle the change.
371 QWidget *suicidal = new QWidget();
372 suicidal->setLayout(popup_->layout());
373 suicidal->deleteLater();
376 // Repopulate the popup
377 widgets::QWidthAdjustingScrollArea* scrollarea = new widgets::QWidthAdjustingScrollArea();
378 QWidget* scrollarea_content = new QWidget(scrollarea);
380 scrollarea->setWidget(scrollarea_content);
381 scrollarea->setWidgetResizable(true);
382 scrollarea->setContentsMargins(0, 0, 0, 0);
383 scrollarea->setFrameShape(QFrame::NoFrame);
384 scrollarea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
385 scrollarea_content->setContentsMargins(0, 0, 0, 0);
387 popup_->setLayout(new QVBoxLayout());
388 popup_->layout()->addWidget(scrollarea);
389 popup_->layout()->setContentsMargins(0, 0, 0, 0);
391 popup_form_ = new QFormLayout(scrollarea_content);
392 popup_form_->setSizeConstraint(QLayout::SetMinAndMaxSize);
394 populate_popup_form(popup_, popup_form_);
397 void Trace::populate_popup_form(QWidget *parent, QFormLayout *form)
399 QLineEdit *const name_edit = new QLineEdit(parent);
400 name_edit->setText(base_->name());
401 connect(name_edit, SIGNAL(textChanged(const QString&)),
402 this, SLOT(on_nameedit_changed(const QString&)));
403 form->addRow(tr("Name"), name_edit);
405 add_color_option(parent, form);
408 void Trace::on_name_changed(const QString &text)
410 /* This event handler is called by SignalBase when the name was changed there */
414 owner_->extents_changed(true, false);
415 owner_->row_item_appearance_changed(true, false);
419 void Trace::on_color_changed(const QColor &color)
421 /* This event handler is called by SignalBase when the color was changed there */
425 owner_->row_item_appearance_changed(true, true);
428 void Trace::on_error_message_changed(const QString &msg)
433 owner_->row_item_appearance_changed(false, true);
436 void Trace::on_popup_closed()
439 popup_form_ = nullptr;
442 void Trace::on_nameedit_changed(const QString &name)
444 /* This event handler notifies SignalBase that the name changed */
445 base_->set_name(name);
448 void Trace::on_coloredit_changed(const QColor &color)
450 /* This event handler notifies SignalBase that the color changed */
451 base_->set_color(color);
454 void Trace::on_create_marker_here() const
456 View *view = owner_->view();
459 const Ruler *ruler = view->ruler();
460 QPoint p = ruler->mapFrom(view, QPoint(context_menu_x_pos_, 0));
462 view->add_flag(ruler->get_absolute_time_from_x_pos(p.x()));