Remove unused "using" declarations.
[pulseview.git] / pv / view / trace.cpp
blob0d4a4536171aacd99082465740deabe66f957793
1 /*
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, see <http://www.gnu.org/licenses/>.
20 #include <extdef.h>
22 #include <cassert>
23 #include <cmath>
25 #include <QApplication>
26 #include <QFormLayout>
27 #include <QKeyEvent>
28 #include <QLineEdit>
30 #include "trace.hpp"
31 #include "tracepalette.hpp"
32 #include "view.hpp"
34 #include "pv/globalsettings.hpp"
35 #include "pv/widgets/colourbutton.hpp"
36 #include "pv/widgets/popup.hpp"
38 using std::pair;
39 using std::shared_ptr;
41 namespace pv {
42 namespace views {
43 namespace TraceView {
45 const QPen Trace::AxisPen(QColor(0, 0, 0, 30*256/100));
46 const int Trace::LabelHitPadding = 2;
48 const QColor Trace::BrightGrayBGColour = QColor(0, 0, 0, 10*255/100);
49 const QColor Trace::DarkGrayBGColour = QColor(0, 0, 0, 15*255/100);
51 Trace::Trace(shared_ptr<data::SignalBase> channel) :
52 base_(channel),
53 popup_(nullptr),
54 popup_form_(nullptr)
56 GlobalSettings settings;
57 coloured_bg_ = settings.value(GlobalSettings::Key_View_ColouredBG).toBool();
59 connect(channel.get(), SIGNAL(name_changed(const QString&)),
60 this, SLOT(on_name_changed(const QString&)));
61 connect(channel.get(), SIGNAL(colour_changed(const QColor&)),
62 this, SLOT(on_colour_changed(const QColor&)));
65 void Trace::set_coloured_bg(bool state)
67 coloured_bg_ = state;
70 void Trace::paint_label(QPainter &p, const QRect &rect, bool hover)
72 const int y = get_visual_y();
74 p.setBrush(base_->colour());
76 if (!enabled())
77 return;
79 const QRectF r = label_rect(rect);
81 // Paint the label
82 const float label_arrow_length = r.height() / 2;
83 const QPointF points[] = {
84 r.topLeft(),
85 QPointF(r.right() - label_arrow_length, r.top()),
86 QPointF(r.right(), y),
87 QPointF(r.right() - label_arrow_length, r.bottom()),
88 r.bottomLeft()
90 const QPointF highlight_points[] = {
91 QPointF(r.left() + 1, r.top() + 1),
92 QPointF(r.right() - label_arrow_length, r.top() + 1),
93 QPointF(r.right() - 1, y),
94 QPointF(r.right() - label_arrow_length, r.bottom() - 1),
95 QPointF(r.left() + 1, r.bottom() - 1)
98 if (selected()) {
99 p.setPen(highlight_pen());
100 p.setBrush(Qt::transparent);
101 p.drawPolygon(points, countof(points));
104 p.setPen(Qt::transparent);
105 p.setBrush(hover ? base_->colour().lighter() : base_->colour());
106 p.drawPolygon(points, countof(points));
108 p.setPen(base_->colour().lighter());
109 p.setBrush(Qt::transparent);
110 p.drawPolygon(highlight_points, countof(highlight_points));
112 p.setPen(base_->colour().darker());
113 p.setBrush(Qt::transparent);
114 p.drawPolygon(points, countof(points));
116 // Paint the text
117 p.setPen(select_text_colour(base_->colour()));
118 p.setFont(QApplication::font());
119 p.drawText(QRectF(r.x(), r.y(),
120 r.width() - label_arrow_length, r.height()),
121 Qt::AlignCenter | Qt::AlignVCenter, base_->name());
124 QMenu* Trace::create_context_menu(QWidget *parent)
126 QMenu *const menu = ViewItem::create_context_menu(parent);
128 return menu;
131 pv::widgets::Popup* Trace::create_popup(QWidget *parent)
133 using pv::widgets::Popup;
135 popup_ = new Popup(parent);
136 popup_->set_position(parent->mapToGlobal(
137 point(parent->rect())), Popup::Right);
139 create_popup_form();
141 connect(popup_, SIGNAL(closed()),
142 this, SLOT(on_popup_closed()));
144 return popup_;
147 QRectF Trace::label_rect(const QRectF &rect) const
149 QFontMetrics m(QApplication::font());
150 const QSize text_size(
151 m.boundingRect(QRect(), 0, base_->name()).width(), m.height());
152 const QSizeF label_size(
153 text_size.width() + LabelPadding.width() * 2,
154 ceilf((text_size.height() + LabelPadding.height() * 2) / 2) * 2);
155 const float half_height = label_size.height() / 2;
156 return QRectF(
157 rect.right() - half_height - label_size.width() - 0.5,
158 get_visual_y() + 0.5f - half_height,
159 label_size.width() + half_height,
160 label_size.height());
163 void Trace::paint_back(QPainter &p, const ViewItemPaintParams &pp)
165 if (coloured_bg_)
166 p.setBrush(base_->bgcolour());
167 else
168 p.setBrush(bgcolour_state_ ? BrightGrayBGColour : DarkGrayBGColour);
170 p.setPen(QPen(Qt::NoPen));
172 const pair<int, int> extents = v_extents();
174 const int x = 0;
175 const int y = get_visual_y() + extents.first;
176 const int w = pp.right() - pp.left();
177 const int h = extents.second - extents.first;
179 p.drawRect(x, y, w, h);
182 void Trace::paint_axis(QPainter &p, const ViewItemPaintParams &pp, int y)
184 p.setRenderHint(QPainter::Antialiasing, false);
186 p.setPen(AxisPen);
187 p.drawLine(QPointF(pp.left(), y), QPointF(pp.right(), y));
189 p.setRenderHint(QPainter::Antialiasing, true);
192 void Trace::add_colour_option(QWidget *parent, QFormLayout *form)
194 using pv::widgets::ColourButton;
196 ColourButton *const colour_button = new ColourButton(
197 TracePalette::Rows, TracePalette::Cols, parent);
198 colour_button->set_palette(TracePalette::Colours);
199 colour_button->set_colour(base_->colour());
200 connect(colour_button, SIGNAL(selected(const QColor&)),
201 this, SLOT(on_colouredit_changed(const QColor&)));
203 form->addRow(tr("Colour"), colour_button);
206 void Trace::create_popup_form()
208 // Clear the layout
210 // Transfer the layout and the child widgets to a temporary widget
211 // which we delete after the event was handled. This way, the layout
212 // and all widgets contained therein are deleted after the event was
213 // handled, leaving the parent popup_ time to handle the change.
214 if (popup_form_) {
215 QWidget *suicidal = new QWidget();
216 suicidal->setLayout(popup_form_);
217 suicidal->deleteLater();
220 // Repopulate the popup
221 popup_form_ = new QFormLayout(popup_);
222 popup_->setLayout(popup_form_);
223 populate_popup_form(popup_, popup_form_);
226 void Trace::populate_popup_form(QWidget *parent, QFormLayout *form)
228 QLineEdit *const name_edit = new QLineEdit(parent);
229 name_edit->setText(base_->name());
230 connect(name_edit, SIGNAL(textChanged(const QString&)),
231 this, SLOT(on_nameedit_changed(const QString&)));
232 form->addRow(tr("Name"), name_edit);
234 add_colour_option(parent, form);
237 void Trace::set_name(QString name)
239 base_->set_name(name);
242 void Trace::set_colour(QColor colour)
244 base_->set_colour(colour);
247 void Trace::on_name_changed(const QString &text)
249 /* This event handler is called by SignalBase when the name was changed there */
250 (void)text;
252 if (owner_) {
253 owner_->extents_changed(true, false);
254 owner_->row_item_appearance_changed(true, false);
258 void Trace::on_colour_changed(const QColor &colour)
260 /* This event handler is called by SignalBase when the colour was changed there */
261 (void)colour;
263 if (owner_)
264 owner_->row_item_appearance_changed(true, true);
267 void Trace::on_popup_closed()
269 popup_ = nullptr;
270 popup_form_ = nullptr;
273 void Trace::on_nameedit_changed(const QString &name)
275 /* This event handler notifies SignalBase that the name changed */
276 set_name(name);
279 void Trace::on_colouredit_changed(const QColor &colour)
281 /* This event handler notifies SignalBase that the colour changed */
282 set_colour(colour);
285 } // namespace TraceView
286 } // namespace views
287 } // namespace pv