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 <QFormLayout>
30 #include "tracepalette.h"
33 #include <pv/widgets/colourbutton.h>
38 const QPen Trace::AxisPen(QColor(128, 128, 128, 64));
39 const int Trace::LabelHitPadding = 2;
41 Trace::Trace(pv::SigSession &session, QString name) :
48 QString Trace::get_name() const
53 void Trace::set_name(QString name)
58 QColor Trace::get_colour() const
63 void Trace::set_colour(QColor colour)
68 int Trace::get_v_offset() const
73 void Trace::set_v_offset(int v_offset)
78 void Trace::set_view(pv::view::View *view)
84 void Trace::paint_back(QPainter &p, int left, int right)
91 void Trace::paint_mid(QPainter &p, int left, int right)
98 void Trace::paint_fore(QPainter &p, int left, int right)
105 void Trace::paint_label(QPainter &p, int right, bool hover)
108 const int y = _v_offset - _view->v_offset();
115 const QColor colour = get_colour();
117 compute_text_size(p);
118 const QRectF label_rect = get_label_rect(right);
121 const QPointF points[] = {
122 label_rect.topLeft(),
123 label_rect.topRight(),
125 label_rect.bottomRight(),
126 label_rect.bottomLeft()
129 const QPointF highlight_points[] = {
130 QPointF(label_rect.left() + 1, label_rect.top() + 1),
131 QPointF(label_rect.right(), label_rect.top() + 1),
132 QPointF(right - 1, y),
133 QPointF(label_rect.right(), label_rect.bottom() - 1),
134 QPointF(label_rect.left() + 1, label_rect.bottom() - 1)
138 p.setPen(highlight_pen());
139 p.setBrush(Qt::transparent);
140 p.drawPolygon(points, countof(points));
143 p.setPen(Qt::transparent);
144 p.setBrush(hover ? colour.lighter() : colour);
145 p.drawPolygon(points, countof(points));
147 p.setPen(colour.lighter());
148 p.setBrush(Qt::transparent);
149 p.drawPolygon(highlight_points, countof(highlight_points));
151 p.setPen(colour.darker());
152 p.setBrush(Qt::transparent);
153 p.drawPolygon(points, countof(points));
156 p.setPen(get_text_colour());
157 p.drawText(label_rect, Qt::AlignCenter | Qt::AlignVCenter, _name);
160 bool Trace::pt_in_label_rect(int left, int right, const QPoint &point)
164 const QRectF label = get_label_rect(right);
166 QPointF(label.left() - LabelHitPadding,
167 label.top() - LabelHitPadding),
168 QPointF(right, label.bottom() + LabelHitPadding)
172 QMenu* Trace::create_context_menu(QWidget *parent)
174 QMenu *const menu = SelectableItem::create_context_menu(parent);
179 pv::widgets::Popup* Trace::create_popup(QWidget *parent)
181 using pv::widgets::Popup;
182 Popup *const popup = new Popup(parent);
183 QFormLayout *const form = new QFormLayout(popup);
184 popup->setLayout(form);
185 populate_popup_form(popup, form);
189 int Trace::get_y() const
191 return _v_offset - _view->v_offset();
194 QColor Trace::get_text_colour() const
196 return (_colour.lightness() > 64) ? Qt::black : Qt::white;
199 void Trace::paint_axis(QPainter &p, int y, int left, int right)
202 p.drawLine(QPointF(left, y + 0.5f), QPointF(right, y + 0.5f));
205 void Trace::add_colour_option(QWidget *parent, QFormLayout *form)
207 using pv::widgets::ColourButton;
209 ColourButton *const colour_button = new ColourButton(
210 TracePalette::Rows, TracePalette::Cols, parent);
211 colour_button->set_palette(TracePalette::Colours);
212 colour_button->set_colour(_colour);
213 connect(colour_button, SIGNAL(selected(const QColor&)),
214 this, SLOT(on_colour_changed(const QColor&)));
216 form->addRow(tr("Colour"), colour_button);
219 void Trace::populate_popup_form(QWidget *parent, QFormLayout *form)
221 QLineEdit *const name_edit = new QLineEdit(parent);
222 name_edit->setText(_name);
223 connect(name_edit, SIGNAL(textChanged(const QString&)),
224 this, SLOT(on_text_changed(const QString&)));
225 form->addRow(tr("Name"), name_edit);
227 add_colour_option(parent, form);
230 void Trace::compute_text_size(QPainter &p)
233 p.boundingRect(QRectF(), 0, _name).width(),
234 p.boundingRect(QRectF(), 0, "Tg").height());
237 QRectF Trace::get_label_rect(int right)
239 using pv::view::View;
243 const QSizeF label_size(
244 _text_size.width() + View::LabelPadding.width() * 2,
245 ceilf((_text_size.height() + View::LabelPadding.height() * 2) / 2) * 2);
246 const float label_arrow_length = label_size.height() / 2;
248 right - label_arrow_length - label_size.width() - 0.5,
249 get_y() + 0.5f - label_size.height() / 2,
250 label_size.width(), label_size.height());
253 void Trace::on_text_changed(const QString &text)
259 void Trace::on_colour_changed(const QColor &colour)