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>
31 #include "tracepalette.hpp"
34 #include "pv/globalsettings.hpp"
35 #include "pv/widgets/colourbutton.hpp"
36 #include "pv/widgets/popup.hpp"
39 using std::shared_ptr;
45 const QPen Trace::AxisPen(QColor(0, 0, 0, 30 * 256 / 100));
46 const int Trace::LabelHitPadding = 2;
48 const QColor Trace::BrightGrayBGColour = QColor(0, 0, 0, 10 * 255 / 100);
49 const QColor Trace::DarkGrayBGColour = QColor(0, 0, 0, 15 * 255 / 100);
51 Trace::Trace(shared_ptr<data::SignalBase> channel) :
54 segment_display_mode_(ShowLastSegmentOnly), // Will be overwritten by View
59 connect(channel.get(), SIGNAL(name_changed(const QString&)),
60 this, SLOT(on_name_changed(const QString&)));
61 connect(channel.get(), SIGNAL(colour_changed(const QColor&)),
62 this, SLOT(on_colour_changed(const QColor&)));
65 shared_ptr<data::SignalBase> Trace::base() const
70 void Trace::paint_label(QPainter &p, const QRect &rect, bool hover)
72 const int y = get_visual_y();
74 p.setBrush(base_->colour());
79 const QRectF r = label_rect(rect);
81 // When selected, move the arrow to the left so that the border can show
82 const QPointF offs = (selected()) ? QPointF(-2, 0) : QPointF(0, 0);
85 const float label_arrow_length = r.height() / 2;
88 offs + QPointF(r.right() - label_arrow_length, r.top()),
89 offs + QPointF(r.right(), y),
90 offs + QPointF(r.right() - label_arrow_length, r.bottom()),
93 QPointF highlight_points[] = {
94 offs + QPointF(r.left() + 1, r.top() + 1),
95 offs + QPointF(r.right() - label_arrow_length, r.top() + 1),
96 offs + QPointF(r.right() - 1, y),
97 offs + QPointF(r.right() - label_arrow_length, r.bottom() - 1),
98 offs + QPointF(r.left() + 1, r.bottom() - 1)
102 p.setPen(highlight_pen());
103 p.setBrush(Qt::transparent);
104 p.drawPolygon(points, countof(points));
107 p.setPen(Qt::transparent);
108 p.setBrush(hover ? base_->colour().lighter() : base_->colour());
109 p.drawPolygon(points, countof(points));
111 p.setPen(base_->colour().lighter());
112 p.setBrush(Qt::transparent);
113 p.drawPolygon(highlight_points, countof(highlight_points));
115 p.setPen(base_->colour().darker());
116 p.setBrush(Qt::transparent);
117 p.drawPolygon(points, countof(points));
120 p.setPen(select_text_colour(base_->colour()));
121 p.setFont(QApplication::font());
122 p.drawText(QRectF(r.x(), r.y(),
123 r.width() - label_arrow_length, r.height()),
124 Qt::AlignCenter | Qt::AlignVCenter, base_->name());
127 QMenu* Trace::create_context_menu(QWidget *parent)
129 QMenu *const menu = ViewItem::create_context_menu(parent);
134 pv::widgets::Popup* Trace::create_popup(QWidget *parent)
136 using pv::widgets::Popup;
138 popup_ = new Popup(parent);
139 popup_->set_position(parent->mapToGlobal(
140 drag_point(parent->rect())), Popup::Right);
144 connect(popup_, SIGNAL(closed()), this, SLOT(on_popup_closed()));
149 QRectF Trace::label_rect(const QRectF &rect) const
151 QFontMetrics m(QApplication::font());
152 const QSize text_size(
153 m.boundingRect(QRect(), 0, base_->name()).width(), m.height());
154 const QSizeF label_size(
155 text_size.width() + LabelPadding.width() * 2,
156 ceilf((text_size.height() + LabelPadding.height() * 2) / 2) * 2);
157 const float half_height = label_size.height() / 2;
159 rect.right() - half_height - label_size.width() - 0.5,
160 get_visual_y() + 0.5f - half_height,
161 label_size.width() + half_height,
162 label_size.height());
165 void Trace::set_current_segment(const int segment)
167 current_segment_ = segment;
170 int Trace::get_current_segment() const
172 return current_segment_;
175 void Trace::paint_back(QPainter &p, ViewItemPaintParams &pp)
177 const View *view = owner_->view();
180 if (view->coloured_bg())
181 p.setBrush(base_->bgcolour());
183 p.setBrush(pp.next_bg_colour_state() ? BrightGrayBGColour : DarkGrayBGColour);
185 p.setPen(QPen(Qt::NoPen));
187 const pair<int, int> extents = v_extents();
188 p.drawRect(pp.left(), get_visual_y() + extents.first,
189 pp.width(), extents.second - extents.first);
192 void Trace::paint_axis(QPainter &p, ViewItemPaintParams &pp, int y)
194 p.setRenderHint(QPainter::Antialiasing, false);
197 p.drawLine(QPointF(pp.left(), y), QPointF(pp.right(), y));
199 p.setRenderHint(QPainter::Antialiasing, true);
202 void Trace::add_colour_option(QWidget *parent, QFormLayout *form)
204 using pv::widgets::ColourButton;
206 ColourButton *const colour_button = new ColourButton(
207 TracePalette::Rows, TracePalette::Cols, parent);
208 colour_button->set_palette(TracePalette::Colours);
209 colour_button->set_colour(base_->colour());
210 connect(colour_button, SIGNAL(selected(const QColor&)),
211 this, SLOT(on_colouredit_changed(const QColor&)));
213 form->addRow(tr("Colour"), colour_button);
216 void Trace::create_popup_form()
220 // Transfer the layout and the child widgets to a temporary widget
221 // which we delete after the event was handled. This way, the layout
222 // and all widgets contained therein are deleted after the event was
223 // handled, leaving the parent popup_ time to handle the change.
225 QWidget *suicidal = new QWidget();
226 suicidal->setLayout(popup_form_);
227 suicidal->deleteLater();
230 // Repopulate the popup
231 popup_form_ = new QFormLayout(popup_);
232 popup_->setLayout(popup_form_);
233 populate_popup_form(popup_, popup_form_);
236 void Trace::populate_popup_form(QWidget *parent, QFormLayout *form)
238 QLineEdit *const name_edit = new QLineEdit(parent);
239 name_edit->setText(base_->name());
240 connect(name_edit, SIGNAL(textChanged(const QString&)),
241 this, SLOT(on_nameedit_changed(const QString&)));
242 form->addRow(tr("Name"), name_edit);
244 add_colour_option(parent, form);
247 void Trace::set_name(QString name)
249 base_->set_name(name);
252 void Trace::set_colour(QColor colour)
254 base_->set_colour(colour);
257 void Trace::set_segment_display_mode(SegmentDisplayMode mode)
259 segment_display_mode_ = mode;
262 owner_->row_item_appearance_changed(true, true);
265 void Trace::on_name_changed(const QString &text)
267 /* This event handler is called by SignalBase when the name was changed there */
271 owner_->extents_changed(true, false);
272 owner_->row_item_appearance_changed(true, false);
276 void Trace::on_colour_changed(const QColor &colour)
278 /* This event handler is called by SignalBase when the colour was changed there */
282 owner_->row_item_appearance_changed(true, true);
285 void Trace::on_popup_closed()
288 popup_form_ = nullptr;
291 void Trace::on_nameedit_changed(const QString &name)
293 /* This event handler notifies SignalBase that the name changed */
297 void Trace::on_colouredit_changed(const QColor &colour)
299 /* This event handler notifies SignalBase that the colour changed */