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.h"
35 #include <pv/widgets/colourbutton.h>
36 #include <pv/widgets/popup.h>
41 const QPen Trace::AxisPen(QColor(128, 128, 128, 64));
42 const int Trace::LabelHitPadding = 2;
44 Trace::Trace(QString name) :
53 QString Trace::get_name() const
58 void Trace::set_name(QString name)
63 QColor Trace::get_colour() const
68 void Trace::set_colour(QColor colour)
73 int Trace::get_v_offset() const
78 void Trace::set_v_offset(int v_offset)
83 void Trace::set_view(pv::view::View *view)
89 void Trace::paint_back(QPainter &p, int left, int right)
96 void Trace::paint_mid(QPainter &p, int left, int right)
103 void Trace::paint_fore(QPainter &p, int left, int right)
110 void Trace::paint_label(QPainter &p, int right, bool hover)
113 const int y = _v_offset - _view->v_offset();
120 const QColor colour = get_colour();
122 const QRectF label_rect = get_label_rect(right);
125 const QPointF points[] = {
126 label_rect.topLeft(),
127 label_rect.topRight(),
129 label_rect.bottomRight(),
130 label_rect.bottomLeft()
133 const QPointF highlight_points[] = {
134 QPointF(label_rect.left() + 1, label_rect.top() + 1),
135 QPointF(label_rect.right(), label_rect.top() + 1),
136 QPointF(right - 1, y),
137 QPointF(label_rect.right(), label_rect.bottom() - 1),
138 QPointF(label_rect.left() + 1, label_rect.bottom() - 1)
142 p.setPen(highlight_pen());
143 p.setBrush(Qt::transparent);
144 p.drawPolygon(points, countof(points));
147 p.setPen(Qt::transparent);
148 p.setBrush(hover ? colour.lighter() : colour);
149 p.drawPolygon(points, countof(points));
151 p.setPen(colour.lighter());
152 p.setBrush(Qt::transparent);
153 p.drawPolygon(highlight_points, countof(highlight_points));
155 p.setPen(colour.darker());
156 p.setBrush(Qt::transparent);
157 p.drawPolygon(points, countof(points));
160 p.setPen(get_text_colour());
161 p.setFont(QApplication::font());
162 p.drawText(label_rect, Qt::AlignCenter | Qt::AlignVCenter, _name);
165 bool Trace::pt_in_label_rect(int left, int right, const QPoint &point)
169 const QRectF label = get_label_rect(right);
170 return enabled() && QRectF(
171 QPointF(label.left() - LabelHitPadding,
172 label.top() - LabelHitPadding),
173 QPointF(right, label.bottom() + LabelHitPadding)
177 QMenu* Trace::create_context_menu(QWidget *parent)
179 QMenu *const menu = SelectableItem::create_context_menu(parent);
184 pv::widgets::Popup* Trace::create_popup(QWidget *parent)
186 using pv::widgets::Popup;
188 _popup = new Popup(parent);
192 connect(_popup, SIGNAL(closed()),
193 this, SLOT(on_popup_closed()));
198 int Trace::get_y() const
200 return _v_offset - _view->v_offset();
203 QRectF Trace::get_label_rect(int right)
205 using pv::view::View;
209 QFontMetrics m(QApplication::font());
210 const QSize text_size(
211 m.boundingRect(QRect(), 0, _name).width(),
212 m.boundingRect(QRect(), 0, "Tg").height());
213 const QSizeF label_size(
214 text_size.width() + View::LabelPadding.width() * 2,
215 ceilf((text_size.height() + View::LabelPadding.height() * 2) / 2) * 2);
216 const float label_arrow_length = label_size.height() / 2;
218 right - label_arrow_length - label_size.width() - 0.5,
219 get_y() + 0.5f - label_size.height() / 2,
220 label_size.width(), label_size.height());
223 void Trace::hover_point_changed()
227 QColor Trace::get_text_colour() const
229 return (_colour.lightness() > 64) ? Qt::black : Qt::white;
232 void Trace::paint_axis(QPainter &p, int y, int left, int right)
235 p.drawLine(QPointF(left, y + 0.5f), QPointF(right, y + 0.5f));
238 void Trace::add_colour_option(QWidget *parent, QFormLayout *form)
240 using pv::widgets::ColourButton;
242 ColourButton *const colour_button = new ColourButton(
243 TracePalette::Rows, TracePalette::Cols, parent);
244 colour_button->set_palette(TracePalette::Colours);
245 colour_button->set_colour(_colour);
246 connect(colour_button, SIGNAL(selected(const QColor&)),
247 this, SLOT(on_colour_changed(const QColor&)));
249 form->addRow(tr("Colour"), colour_button);
252 void Trace::create_popup_form()
256 // Transfer the layout and the child widgets to a temporary widget
257 // which then goes out of scope destroying the layout and all the child
260 QWidget().setLayout(_popup_form);
262 // Repopulate the popup
263 _popup_form = new QFormLayout(_popup);
264 _popup->setLayout(_popup_form);
265 populate_popup_form(_popup, _popup_form);
268 void Trace::populate_popup_form(QWidget *parent, QFormLayout *form)
270 QLineEdit *const name_edit = new QLineEdit(parent);
271 name_edit->setText(_name);
272 connect(name_edit, SIGNAL(textChanged(const QString&)),
273 this, SLOT(on_text_changed(const QString&)));
274 form->addRow(tr("Name"), name_edit);
276 add_colour_option(parent, form);
279 void Trace::on_popup_closed()
285 void Trace::on_text_changed(const QString &text)
291 void Trace::on_colour_changed(const QColor &colour)