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, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26 #include <QApplication>
27 #include <QFormLayout>
32 #include "tracepalette.hpp"
35 #include <pv/widgets/colourbutton.hpp>
36 #include <pv/widgets/popup.hpp>
42 const QPen Trace::AxisPen(QColor(0, 0, 0, 30*256/100));
43 const int Trace::LabelHitPadding = 2;
45 const QColor Trace::BrightGrayBGColour = QColor(0, 0, 0, 10*255/100);
46 const QColor Trace::DarkGrayBGColour = QColor(0, 0, 0, 15*255/100);
48 Trace::Trace(std::shared_ptr<data::SignalBase> channel) :
50 coloured_bg_(true), // Default setting is set in MainWindow::setup_ui()
54 connect(channel.get(), SIGNAL(name_changed(const QString&)),
55 this, SLOT(on_name_changed(const QString&)));
56 connect(channel.get(), SIGNAL(colour_changed(const QColor&)),
57 this, SLOT(on_colour_changed(const QColor&)));
60 void Trace::set_coloured_bg(bool state)
65 void Trace::paint_label(QPainter &p, const QRect &rect, bool hover)
67 const int y = get_visual_y();
69 p.setBrush(base_->colour());
74 const QRectF r = label_rect(rect);
77 const float label_arrow_length = r.height() / 2;
78 const QPointF points[] = {
80 QPointF(r.right() - label_arrow_length, r.top()),
81 QPointF(r.right(), y),
82 QPointF(r.right() - label_arrow_length, r.bottom()),
85 const QPointF highlight_points[] = {
86 QPointF(r.left() + 1, r.top() + 1),
87 QPointF(r.right() - label_arrow_length, r.top() + 1),
88 QPointF(r.right() - 1, y),
89 QPointF(r.right() - label_arrow_length, r.bottom() - 1),
90 QPointF(r.left() + 1, r.bottom() - 1)
94 p.setPen(highlight_pen());
95 p.setBrush(Qt::transparent);
96 p.drawPolygon(points, countof(points));
99 p.setPen(Qt::transparent);
100 p.setBrush(hover ? base_->colour().lighter() : base_->colour());
101 p.drawPolygon(points, countof(points));
103 p.setPen(base_->colour().lighter());
104 p.setBrush(Qt::transparent);
105 p.drawPolygon(highlight_points, countof(highlight_points));
107 p.setPen(base_->colour().darker());
108 p.setBrush(Qt::transparent);
109 p.drawPolygon(points, countof(points));
112 p.setPen(select_text_colour(base_->colour()));
113 p.setFont(QApplication::font());
114 p.drawText(QRectF(r.x(), r.y(),
115 r.width() - label_arrow_length, r.height()),
116 Qt::AlignCenter | Qt::AlignVCenter, base_->name());
119 QMenu* Trace::create_context_menu(QWidget *parent)
121 QMenu *const menu = ViewItem::create_context_menu(parent);
126 pv::widgets::Popup* Trace::create_popup(QWidget *parent)
128 using pv::widgets::Popup;
130 popup_ = new Popup(parent);
131 popup_->set_position(parent->mapToGlobal(
132 point(parent->rect())), Popup::Right);
136 connect(popup_, SIGNAL(closed()),
137 this, SLOT(on_popup_closed()));
142 QRectF Trace::label_rect(const QRectF &rect) const
144 QFontMetrics m(QApplication::font());
145 const QSize text_size(
146 m.boundingRect(QRect(), 0, base_->name()).width(), m.height());
147 const QSizeF label_size(
148 text_size.width() + LabelPadding.width() * 2,
149 ceilf((text_size.height() + LabelPadding.height() * 2) / 2) * 2);
150 const float half_height = label_size.height() / 2;
152 rect.right() - half_height - label_size.width() - 0.5,
153 get_visual_y() + 0.5f - half_height,
154 label_size.width() + half_height,
155 label_size.height());
158 void Trace::paint_back(QPainter &p, const ViewItemPaintParams &pp)
161 p.setBrush(base_->bgcolour());
163 p.setBrush(bgcolour_state_ ? BrightGrayBGColour : DarkGrayBGColour);
165 p.setPen(QPen(Qt::NoPen));
167 const std::pair<int, int> extents = v_extents();
170 const int y = get_visual_y() + extents.first;
171 const int w = pp.right() - pp.left();
172 const int h = extents.second - extents.first;
174 p.drawRect(x, y, w, h);
177 void Trace::paint_axis(QPainter &p, const ViewItemPaintParams &pp, int y)
179 p.setRenderHint(QPainter::Antialiasing, false);
182 p.drawLine(QPointF(pp.left(), y), QPointF(pp.right(), y));
184 p.setRenderHint(QPainter::Antialiasing, true);
187 void Trace::add_colour_option(QWidget *parent, QFormLayout *form)
189 using pv::widgets::ColourButton;
191 ColourButton *const colour_button = new ColourButton(
192 TracePalette::Rows, TracePalette::Cols, parent);
193 colour_button->set_palette(TracePalette::Colours);
194 colour_button->set_colour(base_->colour());
195 connect(colour_button, SIGNAL(selected(const QColor&)),
196 this, SLOT(on_colouredit_changed(const QColor&)));
198 form->addRow(tr("Colour"), colour_button);
201 void Trace::create_popup_form()
205 // Transfer the layout and the child widgets to a temporary widget
206 // which we delete after the event was handled. This way, the layout
207 // and all widgets contained therein are deleted after the event was
208 // handled, leaving the parent popup_ time to handle the change.
210 QWidget *suicidal = new QWidget();
211 suicidal->setLayout(popup_form_);
212 suicidal->deleteLater();
215 // Repopulate the popup
216 popup_form_ = new QFormLayout(popup_);
217 popup_->setLayout(popup_form_);
218 populate_popup_form(popup_, popup_form_);
221 void Trace::populate_popup_form(QWidget *parent, QFormLayout *form)
223 QLineEdit *const name_edit = new QLineEdit(parent);
224 name_edit->setText(base_->name());
225 connect(name_edit, SIGNAL(textChanged(const QString&)),
226 this, SLOT(on_nameedit_changed(const QString&)));
227 form->addRow(tr("Name"), name_edit);
229 add_colour_option(parent, form);
232 void Trace::set_name(QString name)
234 base_->set_name(name);
237 void Trace::set_colour(QColor colour)
239 base_->set_colour(colour);
242 void Trace::on_name_changed(const QString &text)
244 /* This event handler is called by SignalBase when the name was changed there */
248 owner_->extents_changed(true, false);
249 owner_->row_item_appearance_changed(true, false);
253 void Trace::on_colour_changed(const QColor &colour)
255 /* This event handler is called by SignalBase when the colour was changed there */
259 owner_->row_item_appearance_changed(true, true);
262 void Trace::on_popup_closed()
265 popup_form_ = nullptr;
268 void Trace::on_nameedit_changed(const QString &name)
270 /* This event handler notifies SignalBase that the name changed */
274 void Trace::on_colouredit_changed(const QColor &colour)
276 /* This event handler notifies SignalBase that the colour changed */
280 } // namespace TraceView