]> sigrok.org Git - pulseview.git/blob - pv/view/trace.cpp
f7e7ebf25b8ec00a351bb415f7fe696de155e8b6
[pulseview.git] / pv / view / trace.cpp
1 /*
2  * This file is part of the PulseView project.
3  *
4  * Copyright (C) 2013 Joel Holdsworth <joel@airwebreathe.org.uk>
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include <extdef.h>
22
23 #include <assert.h>
24 #include <cmath>
25
26 #include <QApplication>
27 #include <QFormLayout>
28 #include <QKeyEvent>
29 #include <QLineEdit>
30
31 #include "trace.hpp"
32 #include "tracepalette.hpp"
33 #include "view.hpp"
34
35 #include <pv/widgets/colourbutton.hpp>
36 #include <pv/widgets/popup.hpp>
37
38 namespace pv {
39 namespace view {
40
41 const QPen Trace::AxisPen(QColor(128, 128, 128, 64));
42 const int Trace::LabelHitPadding = 2;
43
44 const QColor Trace::DarkBGColour(235, 235, 235);    // Quite light grey
45 const QColor Trace::BrightBGColour(245, 245, 245);  // Very light grey
46
47 Trace::Trace(QString name) :
48         name_(name),
49         coloured_bg_(true), // Default setting is set in MainWindow::setup_ui()
50         popup_(nullptr),
51         popup_form_(nullptr)
52 {
53 }
54
55 QString Trace::name() const
56 {
57         return name_;
58 }
59
60 void Trace::set_name(QString name)
61 {
62         name_ = name;
63 }
64
65 QColor Trace::colour() const
66 {
67         return colour_;
68 }
69
70 void Trace::set_colour(QColor colour)
71 {
72         colour_ = colour;
73
74         bgcolour_ = colour;
75         bgcolour_.setAlpha(20);
76 }
77
78 void Trace::set_coloured_bg(bool state)
79 {
80         coloured_bg_ = state;
81 }
82
83 bool Trace::is_draggable() const
84 {
85         const View *const view = owner_->view();
86         assert(view);
87
88         QPoint cursor_pos = view->mapFromGlobal(QCursor::pos());
89
90         // The signal is draggable only in the header area
91         return (cursor_pos.x() <= view->header_size().width());
92 }
93
94 void Trace::paint_label(QPainter &p, const QRect &rect, bool hover)
95 {
96         const int y = get_visual_y();
97
98         p.setBrush(colour_);
99
100         if (!enabled())
101                 return;
102
103         const QRectF r = label_rect(rect);
104
105         // Paint the label
106         const float label_arrow_length = r.height() / 2;
107         const QPointF points[] = {
108                 r.topLeft(),
109                 QPointF(r.right() - label_arrow_length, r.top()),
110                 QPointF(r.right(), y),
111                 QPointF(r.right() - label_arrow_length, r.bottom()),
112                 r.bottomLeft()
113         };
114         const QPointF highlight_points[] = {
115                 QPointF(r.left() + 1, r.top() + 1),
116                 QPointF(r.right() - label_arrow_length, r.top() + 1),
117                 QPointF(r.right() - 1, y),
118                 QPointF(r.right() - label_arrow_length, r.bottom() - 1),
119                 QPointF(r.left() + 1, r.bottom() - 1)
120         };
121
122         if (selected()) {
123                 p.setPen(highlight_pen());
124                 p.setBrush(Qt::transparent);
125                 p.drawPolygon(points, countof(points));
126         }
127
128         p.setPen(Qt::transparent);
129         p.setBrush(hover ? colour_.lighter() : colour_);
130         p.drawPolygon(points, countof(points));
131
132         p.setPen(colour_.lighter());
133         p.setBrush(Qt::transparent);
134         p.drawPolygon(highlight_points, countof(highlight_points));
135
136         p.setPen(colour_.darker());
137         p.setBrush(Qt::transparent);
138         p.drawPolygon(points, countof(points));
139
140         // Paint the text
141         p.setPen(select_text_colour(colour_));
142         p.setFont(QApplication::font());
143         p.drawText(QRectF(r.x(), r.y(),
144                 r.width() - label_arrow_length, r.height()),
145                 Qt::AlignCenter | Qt::AlignVCenter, name_);
146 }
147
148 QMenu* Trace::create_context_menu(QWidget *parent)
149 {
150         QMenu *const menu = ViewItem::create_context_menu(parent);
151
152         return menu;
153 }
154
155 pv::widgets::Popup* Trace::create_popup(QWidget *parent)
156 {
157         using pv::widgets::Popup;
158
159         popup_ = new Popup(parent);
160         popup_->set_position(parent->mapToGlobal(
161                 point(parent->rect())), Popup::Right);
162
163         create_popup_form();
164
165         connect(popup_, SIGNAL(closed()),
166                 this, SLOT(on_popup_closed()));
167
168         return popup_;
169 }
170
171 QRectF Trace::label_rect(const QRectF &rect) const
172 {
173         using pv::view::View;
174
175         QFontMetrics m(QApplication::font());
176         const QSize text_size(
177                 m.boundingRect(QRect(), 0, name_).width(), m.height());
178         const QSizeF label_size(
179                 text_size.width() + LabelPadding.width() * 2,
180                 ceilf((text_size.height() + LabelPadding.height() * 2) / 2) * 2);
181         const float half_height = label_size.height() / 2;
182         return QRectF(
183                 rect.right() - half_height - label_size.width() - 0.5,
184                 get_visual_y() + 0.5f - half_height,
185                 label_size.width() + half_height,
186                 label_size.height());
187 }
188
189 QRectF Trace::hit_box_rect(const ViewItemPaintParams &pp) const
190 {
191         const float h = QFontMetrics(QApplication::font()).height();
192         return QRectF(pp.left(), get_visual_y() - h / 2.0f,
193                 pp.width(), h);
194 }
195
196 void Trace::paint_back(QPainter &p, const ViewItemPaintParams &pp)
197 {
198         if (coloured_bg_)
199                 p.setBrush(bgcolour_);
200         else
201                 p.setBrush(bgcolour_state_ ? BrightBGColour : DarkBGColour);
202
203         p.setPen(QPen(Qt::NoPen));
204
205         const std::pair<int, int> extents = v_extents();
206
207         const int x = 0;
208         const int y = get_visual_y() + extents.first;
209         const int w = pp.right() - pp.left();
210         const int h = extents.second - extents.first;
211
212         p.drawRect(x, y, w, h);
213 }
214
215 void Trace::paint_axis(QPainter &p, const ViewItemPaintParams &pp, int y)
216 {
217         p.setPen(AxisPen);
218         p.drawLine(QPointF(pp.left(), y + 0.5f), QPointF(pp.right(), y + 0.5f));
219 }
220
221 void Trace::add_colour_option(QWidget *parent, QFormLayout *form)
222 {
223         using pv::widgets::ColourButton;
224
225         ColourButton *const colour_button = new ColourButton(
226                 TracePalette::Rows, TracePalette::Cols, parent);
227         colour_button->set_palette(TracePalette::Colours);
228         colour_button->set_colour(colour_);
229         connect(colour_button, SIGNAL(selected(const QColor&)),
230                 this, SLOT(on_colour_changed(const QColor&)));
231
232         form->addRow(tr("Colour"), colour_button);
233 }
234
235 void Trace::create_popup_form()
236 {
237         // Clear the layout
238
239         // Transfer the layout and the child widgets to a temporary widget
240         // which then goes out of scope destroying the layout and all the child
241         // widgets.
242         if (popup_form_)
243                 QWidget().setLayout(popup_form_);
244
245         // Repopulate the popup
246         popup_form_ = new QFormLayout(popup_);
247         popup_->setLayout(popup_form_);
248         populate_popup_form(popup_, popup_form_);
249 }
250
251 void Trace::populate_popup_form(QWidget *parent, QFormLayout *form)
252 {
253         QLineEdit *const name_edit = new QLineEdit(parent);
254         name_edit->setText(name_);
255         connect(name_edit, SIGNAL(textChanged(const QString&)),
256                 this, SLOT(on_text_changed(const QString&)));
257         form->addRow(tr("Name"), name_edit);
258
259         add_colour_option(parent, form);
260 }
261
262 void Trace::on_popup_closed()
263 {
264         popup_ = nullptr;
265         popup_form_ = nullptr;
266 }
267
268 void Trace::on_text_changed(const QString &text)
269 {
270         set_name(text);
271
272         if (owner_)
273                 owner_->extents_changed(true, false);
274 }
275
276 void Trace::on_colour_changed(const QColor &colour)
277 {
278         set_colour(colour);
279
280         if (owner_)
281                 owner_->row_item_appearance_changed(true, true);
282 }
283
284 } // namespace view
285 } // namespace pv