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>
41 const QPen Trace::AxisPen(QColor(128, 128, 128, 64));
42 const int Trace::LabelHitPadding = 2;
44 Trace::Trace(QString name) :
51 QString Trace::name() const
56 void Trace::set_name(QString name)
61 QColor Trace::colour() const
66 void Trace::set_colour(QColor colour)
71 void Trace::paint_label(QPainter &p, const QRect &rect, bool hover)
73 const int y = get_visual_y();
80 const QRectF r = label_rect(rect);
83 const float label_arrow_length = r.height() / 2;
84 const QPointF points[] = {
86 QPointF(r.right() - label_arrow_length, r.top()),
87 QPointF(r.right(), y),
88 QPointF(r.right() - label_arrow_length, r.bottom()),
91 const QPointF highlight_points[] = {
92 QPointF(r.left() + 1, r.top() + 1),
93 QPointF(r.right() - label_arrow_length, r.top() + 1),
94 QPointF(r.right() - 1, y),
95 QPointF(r.right() - label_arrow_length, r.bottom() - 1),
96 QPointF(r.left() + 1, r.bottom() - 1)
100 p.setPen(highlight_pen());
101 p.setBrush(Qt::transparent);
102 p.drawPolygon(points, countof(points));
105 p.setPen(Qt::transparent);
106 p.setBrush(hover ? colour_.lighter() : colour_);
107 p.drawPolygon(points, countof(points));
109 p.setPen(colour_.lighter());
110 p.setBrush(Qt::transparent);
111 p.drawPolygon(highlight_points, countof(highlight_points));
113 p.setPen(colour_.darker());
114 p.setBrush(Qt::transparent);
115 p.drawPolygon(points, countof(points));
118 p.setPen(select_text_colour(colour_));
119 p.setFont(QApplication::font());
120 p.drawText(QRectF(r.x(), r.y(),
121 r.width() - label_arrow_length, r.height()),
122 Qt::AlignCenter | Qt::AlignVCenter, name_);
125 QMenu* Trace::create_context_menu(QWidget *parent)
127 QMenu *const menu = ViewItem::create_context_menu(parent);
132 pv::widgets::Popup* Trace::create_popup(QWidget *parent)
134 using pv::widgets::Popup;
136 popup_ = new Popup(parent);
137 popup_->set_position(parent->mapToGlobal(
138 point(parent->rect())), Popup::Right);
142 connect(popup_, SIGNAL(closed()),
143 this, SLOT(on_popup_closed()));
148 QRectF Trace::label_rect(const QRectF &rect) const
150 using pv::view::View;
152 QFontMetrics m(QApplication::font());
153 const QSize text_size(
154 m.boundingRect(QRect(), 0, name_).width(), m.height());
155 const QSizeF label_size(
156 text_size.width() + LabelPadding.width() * 2,
157 ceilf((text_size.height() + LabelPadding.height() * 2) / 2) * 2);
158 const float half_height = label_size.height() / 2;
160 rect.right() - half_height - label_size.width() - 0.5,
161 get_visual_y() + 0.5f - half_height,
162 label_size.width() + half_height,
163 label_size.height());
166 void Trace::paint_axis(QPainter &p, const ViewItemPaintParams &pp, int y)
169 p.drawLine(QPointF(pp.left(), y + 0.5f), QPointF(pp.right(), y + 0.5f));
172 void Trace::add_colour_option(QWidget *parent, QFormLayout *form)
174 using pv::widgets::ColourButton;
176 ColourButton *const colour_button = new ColourButton(
177 TracePalette::Rows, TracePalette::Cols, parent);
178 colour_button->set_palette(TracePalette::Colours);
179 colour_button->set_colour(colour_);
180 connect(colour_button, SIGNAL(selected(const QColor&)),
181 this, SLOT(on_colour_changed(const QColor&)));
183 form->addRow(tr("Colour"), colour_button);
186 void Trace::create_popup_form()
190 // Transfer the layout and the child widgets to a temporary widget
191 // which then goes out of scope destroying the layout and all the child
194 QWidget().setLayout(popup_form_);
196 // Repopulate the popup
197 popup_form_ = new QFormLayout(popup_);
198 popup_->setLayout(popup_form_);
199 populate_popup_form(popup_, popup_form_);
202 void Trace::populate_popup_form(QWidget *parent, QFormLayout *form)
204 QLineEdit *const name_edit = new QLineEdit(parent);
205 name_edit->setText(name_);
206 connect(name_edit, SIGNAL(textChanged(const QString&)),
207 this, SLOT(on_text_changed(const QString&)));
208 form->addRow(tr("Name"), name_edit);
210 add_colour_option(parent, form);
213 void Trace::on_popup_closed()
219 void Trace::on_text_changed(const QString &text)
224 owner_->extents_changed(true, false);
227 void Trace::on_colour_changed(const QColor &colour)
232 owner_->row_item_appearance_changed(true, false);