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);
53 Trace::Trace(shared_ptr<data::SignalBase> channel) :
56 segment_display_mode_(ShowLastSegmentOnly), // Will be overwritten by View
61 connect(channel.get(), SIGNAL(name_changed(const QString&)),
62 this, SLOT(on_name_changed(const QString&)));
63 connect(channel.get(), SIGNAL(color_changed(const QColor&)),
64 this, SLOT(on_color_changed(const QColor&)));
66 GlobalSettings::add_change_handler(this);
68 GlobalSettings settings;
70 settings.value(GlobalSettings::Key_View_ShowHoverMarker).toBool();
75 GlobalSettings::remove_change_handler(this);
78 shared_ptr<data::SignalBase> Trace::base() const
83 bool Trace::is_selectable(QPoint pos) const
85 // True if the header was clicked, false if the trace area was clicked
86 const View *view = owner_->view();
89 return (pos.x() <= view->header_width());
92 bool Trace::is_draggable(QPoint pos) const
94 // While the header label that belongs to this trace is draggable,
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();
100 return (pos.x() <= view->header_width());
103 void Trace::set_segment_display_mode(SegmentDisplayMode mode)
105 segment_display_mode_ = mode;
108 owner_->row_item_appearance_changed(true, true);
111 void Trace::on_setting_changed(const QString &key, const QVariant &value)
113 if (key == GlobalSettings::Key_View_ShowHoverMarker)
114 show_hover_marker_ = value.toBool();
116 // Force a repaint since many options alter the way traces look
118 owner_->row_item_appearance_changed(false, true);
121 void Trace::paint_label(QPainter &p, const QRect &rect, bool hover)
123 const int y = get_visual_y();
125 p.setBrush(base_->color());
130 const QRectF r = label_rect(rect);
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);
136 const float label_arrow_length = r.height() / 2;
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()
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)
153 p.setPen(highlight_pen());
154 p.setBrush(Qt::transparent);
155 p.drawPolygon(points, countof(points));
158 p.setPen(Qt::transparent);
159 p.setBrush(hover ? base_->color().lighter() : base_->color());
160 p.drawPolygon(points, countof(points));
162 p.setPen(base_->color().lighter());
163 p.setBrush(Qt::transparent);
164 p.drawPolygon(highlight_points, countof(highlight_points));
166 p.setPen(base_->color().darker());
167 p.setBrush(Qt::transparent);
168 p.drawPolygon(points, countof(points));
171 p.setPen(select_text_color(base_->color()));
172 p.setFont(QApplication::font());
173 p.drawText(QRectF(r.x(), r.y(),
174 r.width() - label_arrow_length, r.height()),
175 Qt::AlignCenter | Qt::AlignVCenter, base_->name());
178 QMenu* Trace::create_header_context_menu(QWidget *parent)
180 QMenu *const menu = ViewItem::create_header_context_menu(parent);
185 QMenu* Trace::create_view_context_menu(QWidget *parent, QPoint &click_pos)
187 context_menu_x_pos_ = click_pos.x();
189 // Get entries from default menu before adding our own
190 QMenu *const menu = new QMenu(parent);
192 QMenu* default_menu = TraceTreeItem::create_view_context_menu(parent, click_pos);
194 for (QAction *action : default_menu->actions()) { // clazy:exclude=range-loop
195 menu->addAction(action);
196 if (action->parent() == default_menu)
197 action->setParent(menu);
201 // Add separator if needed
202 if (menu->actions().length() > 0)
203 menu->addSeparator();
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);
213 pv::widgets::Popup* Trace::create_popup(QWidget *parent)
215 using pv::widgets::Popup;
217 popup_ = new Popup(parent);
218 popup_->set_position(parent->mapToGlobal(
219 drag_point(parent->rect())), Popup::Right);
223 connect(popup_, SIGNAL(closed()), this, SLOT(on_popup_closed()));
228 QRectF Trace::label_rect(const QRectF &rect) const
230 QFontMetrics m(QApplication::font());
231 const QSize text_size(
232 m.boundingRect(QRect(), 0, base_->name()).width(), m.height());
233 const QSizeF label_size(
234 text_size.width() + LabelPadding.width() * 2,
235 ceilf((text_size.height() + LabelPadding.height() * 2) / 2) * 2);
236 const float half_height = label_size.height() / 2;
238 rect.right() - half_height - label_size.width() - 0.5,
239 get_visual_y() + 0.5f - half_height,
240 label_size.width() + half_height,
241 label_size.height());
244 QRectF Trace::hit_box_rect(const ViewItemPaintParams &pp) const
246 // This one is only for the trace itself, excluding the header area
247 const View *view = owner_->view();
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;
254 return QRectF(pp.left() + view->header_width(), top,
255 pp.width() - view->header_width(), height);
258 void Trace::set_current_segment(const int segment)
260 current_segment_ = segment;
263 int Trace::get_current_segment() const
265 return current_segment_;
268 void Trace::hover_point_changed(const QPoint &hp)
273 owner_->row_item_appearance_changed(false, true);
276 void Trace::paint_back(QPainter &p, ViewItemPaintParams &pp)
278 const View *view = owner_->view();
281 if (view->colored_bg())
282 p.setBrush(base_->bgcolor());
284 p.setBrush(pp.next_bg_color_state() ? BrightGrayBGColor : DarkGrayBGColor);
286 p.setPen(QPen(Qt::NoPen));
288 const pair<int, int> extents = v_extents();
289 p.drawRect(pp.left(), get_visual_y() + extents.first,
290 pp.width(), extents.second - extents.first);
293 void Trace::paint_axis(QPainter &p, ViewItemPaintParams &pp, int y)
295 p.setRenderHint(QPainter::Antialiasing, false);
298 p.drawLine(QPointF(pp.left(), y), QPointF(pp.right(), y));
300 p.setRenderHint(QPainter::Antialiasing, true);
303 void Trace::add_color_option(QWidget *parent, QFormLayout *form)
305 using pv::widgets::ColorButton;
307 ColorButton *const color_button = new ColorButton(
308 TracePalette::Rows, TracePalette::Cols, parent);
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&)));
314 form->addRow(tr("Color"), color_button);
317 void Trace::paint_hover_marker(QPainter &p)
319 const View *view = owner_->view();
322 const int x = view->hover_point().x();
327 p.setPen(QPen(QColor(Qt::lightGray)));
329 const pair<int, int> extents = v_extents();
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);
337 void Trace::create_popup_form()
341 // Transfer the layout and the child widgets to a temporary widget
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.
346 QWidget *suicidal = new QWidget();
347 suicidal->setLayout(popup_form_);
348 suicidal->deleteLater();
351 // Repopulate the popup
352 popup_form_ = new QFormLayout(popup_);
353 popup_->setLayout(popup_form_);
354 populate_popup_form(popup_, popup_form_);
357 void Trace::populate_popup_form(QWidget *parent, QFormLayout *form)
359 QLineEdit *const name_edit = new QLineEdit(parent);
360 name_edit->setText(base_->name());
361 connect(name_edit, SIGNAL(textChanged(const QString&)),
362 this, SLOT(on_nameedit_changed(const QString&)));
363 form->addRow(tr("Name"), name_edit);
365 add_color_option(parent, form);
368 void Trace::on_name_changed(const QString &text)
370 /* This event handler is called by SignalBase when the name was changed there */
374 owner_->extents_changed(true, false);
375 owner_->row_item_appearance_changed(true, false);
379 void Trace::on_color_changed(const QColor &color)
381 /* This event handler is called by SignalBase when the color was changed there */
385 owner_->row_item_appearance_changed(true, true);
388 void Trace::on_popup_closed()
391 popup_form_ = nullptr;
394 void Trace::on_nameedit_changed(const QString &name)
396 /* This event handler notifies SignalBase that the name changed */
397 base_->set_name(name);
400 void Trace::on_coloredit_changed(const QColor &color)
402 /* This event handler notifies SignalBase that the color changed */
403 base_->set_color(color);
406 void Trace::on_create_marker_here() const
408 View *view = owner_->view();
411 const Ruler *ruler = view->ruler();
412 QPoint p = ruler->mapFrom(view, QPoint(context_menu_x_pos_, 0));
414 view->add_flag(ruler->get_time_from_x_pos(p.x()));