2 * This file is part of the PulseView project.
4 * Copyright (C) 2012 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>
34 const int Signal::LabelHitPadding = 2;
36 const QPen Signal::SignalAxisPen(QColor(128, 128, 128, 64));
38 const char *const ProbeNames[] = {
55 Signal::Signal(const sr_probe *const probe) :
61 _updating_name_widget(false)
65 _name_action.setDefaultWidget(&_name_widget);
67 _name_widget.setEditable(true);
68 for(unsigned int i = 0; i < countof(ProbeNames); i++)
69 _name_widget.insertItem(i, ProbeNames[i]);
70 _name_widget.setEditText(probe->name);
72 connect(&_name_widget, SIGNAL(editTextChanged(const QString&)),
73 this, SLOT(on_text_changed(const QString&)));
76 QString Signal::get_name() const
81 void Signal::set_name(QString name)
84 _updating_name_widget = true;
85 _name_widget.setEditText(name);
86 _updating_name_widget = false;
89 QColor Signal::get_colour() const
94 void Signal::set_colour(QColor colour)
99 int Signal::get_v_offset() const
104 void Signal::set_v_offset(int v_offset)
106 _v_offset = v_offset;
109 void Signal::paint_label(QPainter &p, int y, int right, bool hover)
113 if (!_probe->enabled)
116 const QColor colour = get_colour();
118 compute_text_size(p);
119 const QRectF label_rect = get_label_rect(y, right);
122 const QPointF points[] = {
123 label_rect.topLeft(),
124 label_rect.topRight(),
126 label_rect.bottomRight(),
127 label_rect.bottomLeft()
130 const QPointF highlight_points[] = {
131 QPointF(label_rect.left() + 1, label_rect.top() + 1),
132 QPointF(label_rect.right(), label_rect.top() + 1),
133 QPointF(right - 1, y),
134 QPointF(label_rect.right(), label_rect.bottom() - 1),
135 QPointF(label_rect.left() + 1, label_rect.bottom() - 1)
139 p.setPen(highlight_pen());
140 p.setBrush(Qt::transparent);
141 p.drawPolygon(points, countof(points));
144 p.setPen(Qt::transparent);
145 p.setBrush(hover ? colour.lighter() : colour);
146 p.drawPolygon(points, countof(points));
148 p.setPen(colour.lighter());
149 p.setBrush(Qt::transparent);
150 p.drawPolygon(highlight_points, countof(highlight_points));
152 p.setPen(colour.darker());
153 p.setBrush(Qt::transparent);
154 p.drawPolygon(points, countof(points));
157 p.setPen((colour.lightness() > 64) ? Qt::black : Qt::white);
158 p.drawText(label_rect, Qt::AlignCenter | Qt::AlignVCenter, _name);
161 bool Signal::pt_in_label_rect(int y, int left, int right,
166 const QRectF label = get_label_rect(y, right);
168 QPointF(label.left() - LabelHitPadding,
169 label.top() - LabelHitPadding),
170 QPointF(right, label.bottom() + LabelHitPadding)
174 void Signal::paint_axis(QPainter &p, int y, int left, int right)
176 p.setPen(SignalAxisPen);
177 p.drawLine(QPointF(left, y + 0.5f), QPointF(right, y + 0.5f));
180 void Signal::compute_text_size(QPainter &p)
183 p.boundingRect(QRectF(), 0, _name).width(),
184 p.boundingRect(QRectF(), 0, "Tg").height());
187 QRectF Signal::get_label_rect(int y, int right)
189 using pv::view::View;
191 const QSizeF label_size(
192 _text_size.width() + View::LabelPadding.width() * 2,
193 ceilf((_text_size.height() + View::LabelPadding.height() * 2) / 2) * 2);
194 const float label_arrow_length = label_size.height() / 2;
196 right - label_arrow_length - label_size.width() - 0.5,
197 y + 0.5f - label_size.height() / 2,
198 label_size.width(), label_size.height());
201 void Signal::on_text_changed(const QString &text)