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 <QColorDialog>
27 #include <QInputDialog>
35 const QPen Trace::AxisPen(QColor(128, 128, 128, 64));
36 const int Trace::LabelHitPadding = 2;
38 Trace::Trace(pv::SigSession &session, QString name) :
45 QString Trace::get_name() const
50 void Trace::set_name(QString name)
55 QColor Trace::get_colour() const
60 void Trace::set_colour(QColor colour)
65 int Trace::get_v_offset() const
70 void Trace::set_v_offset(int v_offset)
75 void Trace::set_view(pv::view::View *view)
81 void Trace::paint_back(QPainter &p, int left, int right)
88 void Trace::paint_mid(QPainter &p, int left, int right)
95 void Trace::paint_fore(QPainter &p, int left, int right)
102 void Trace::paint_label(QPainter &p, int right, bool hover)
105 const int y = _v_offset - _view->v_offset();
112 const QColor colour = get_colour();
114 compute_text_size(p);
115 const QRectF label_rect = get_label_rect(right);
118 const QPointF points[] = {
119 label_rect.topLeft(),
120 label_rect.topRight(),
122 label_rect.bottomRight(),
123 label_rect.bottomLeft()
126 const QPointF highlight_points[] = {
127 QPointF(label_rect.left() + 1, label_rect.top() + 1),
128 QPointF(label_rect.right(), label_rect.top() + 1),
129 QPointF(right - 1, y),
130 QPointF(label_rect.right(), label_rect.bottom() - 1),
131 QPointF(label_rect.left() + 1, label_rect.bottom() - 1)
135 p.setPen(highlight_pen());
136 p.setBrush(Qt::transparent);
137 p.drawPolygon(points, countof(points));
140 p.setPen(Qt::transparent);
141 p.setBrush(hover ? colour.lighter() : colour);
142 p.drawPolygon(points, countof(points));
144 p.setPen(colour.lighter());
145 p.setBrush(Qt::transparent);
146 p.drawPolygon(highlight_points, countof(highlight_points));
148 p.setPen(colour.darker());
149 p.setBrush(Qt::transparent);
150 p.drawPolygon(points, countof(points));
153 p.setPen(get_text_colour());
154 p.drawText(label_rect, Qt::AlignCenter | Qt::AlignVCenter, _name);
157 bool Trace::pt_in_label_rect(int left, int right, const QPoint &point)
161 const QRectF label = get_label_rect(right);
163 QPointF(label.left() - LabelHitPadding,
164 label.top() - LabelHitPadding),
165 QPointF(right, label.bottom() + LabelHitPadding)
169 QMenu* Trace::create_context_menu(QWidget *parent)
171 QMenu *const menu = SelectableItem::create_context_menu(parent);
173 QAction *const set_name = new QAction(tr("Set &Name..."), this);
174 connect(set_name, SIGNAL(triggered()),
175 this, SLOT(on_action_set_name_triggered()));
176 menu->addAction(set_name);
178 QAction *const set_colour = new QAction(tr("Set &Colour..."), this);
179 connect(set_colour, SIGNAL(triggered()),
180 this, SLOT(on_action_set_colour_triggered()));
181 menu->addAction(set_colour);
186 int Trace::get_y() const
188 return _v_offset - _view->v_offset();
191 QColor Trace::get_text_colour() const
193 return (_colour.lightness() > 64) ? Qt::black : Qt::white;
196 void Trace::paint_axis(QPainter &p, int y, int left, int right)
199 p.drawLine(QPointF(left, y + 0.5f), QPointF(right, y + 0.5f));
202 void Trace::compute_text_size(QPainter &p)
205 p.boundingRect(QRectF(), 0, _name).width(),
206 p.boundingRect(QRectF(), 0, "Tg").height());
209 QRectF Trace::get_label_rect(int right)
211 using pv::view::View;
214 const int y = _v_offset - _view->v_offset();
216 const QSizeF label_size(
217 _text_size.width() + View::LabelPadding.width() * 2,
218 ceilf((_text_size.height() + View::LabelPadding.height() * 2) / 2) * 2);
219 const float label_arrow_length = label_size.height() / 2;
221 right - label_arrow_length - label_size.width() - 0.5,
222 y + 0.5f - label_size.height() / 2,
223 label_size.width(), label_size.height());
226 void Trace::on_action_set_name_triggered()
230 const QString new_label = QInputDialog::getText(_context_parent,
231 tr("Set Name"), tr("Name"), QLineEdit::Normal, get_name(),
238 void Trace::on_action_set_colour_triggered()
240 const QColor new_colour = QColorDialog::getColor(
241 get_colour(), _context_parent, tr("Set Colour"));
243 if (new_colour.isValid())
244 set_colour(new_colour);