DecodeTrace: Correct row calculation/tooltip position for row 0
[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 <math.h>
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 Trace::Trace(QString name) :
45         name_(name),
46         popup_(NULL),
47         popup_form_(NULL)
48 {
49 }
50
51 QString Trace::name() const
52 {
53         return name_;
54 }
55
56 void Trace::set_name(QString name)
57 {
58         name_ = name;
59 }
60
61 QColor Trace::colour() const
62 {
63         return colour_;
64 }
65
66 void Trace::set_colour(QColor colour)
67 {
68         colour_ = colour;
69 }
70
71 void Trace::paint_label(QPainter &p, const QRect &rect, bool hover)
72 {
73         const int y = get_visual_y();
74
75         p.setBrush(colour_);
76
77         if (!enabled())
78                 return;
79
80         const QRectF r = label_rect(rect);
81
82         // Paint the label
83         const float label_arrow_length = r.height() / 2;
84         const QPointF points[] = {
85                 r.topLeft(),
86                 QPointF(r.right() - label_arrow_length, r.top()),
87                 QPointF(r.right(), y),
88                 QPointF(r.right() - label_arrow_length, r.bottom()),
89                 r.bottomLeft()
90         };
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)
97         };
98
99         if (selected()) {
100                 p.setPen(highlight_pen());
101                 p.setBrush(Qt::transparent);
102                 p.drawPolygon(points, countof(points));
103         }
104
105         p.setPen(Qt::transparent);
106         p.setBrush(hover ? colour_.lighter() : colour_);
107         p.drawPolygon(points, countof(points));
108
109         p.setPen(colour_.lighter());
110         p.setBrush(Qt::transparent);
111         p.drawPolygon(highlight_points, countof(highlight_points));
112
113         p.setPen(colour_.darker());
114         p.setBrush(Qt::transparent);
115         p.drawPolygon(points, countof(points));
116
117         // Paint the text
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_);
123 }
124
125 QMenu* Trace::create_context_menu(QWidget *parent)
126 {
127         QMenu *const menu = SelectableItem::create_context_menu(parent);
128
129         return menu;
130 }
131
132 pv::widgets::Popup* Trace::create_popup(QWidget *parent)
133 {
134         using pv::widgets::Popup;
135
136         popup_ = new Popup(parent);
137
138         create_popup_form();
139
140         connect(popup_, SIGNAL(closed()),
141                 this, SLOT(on_popup_closed()));
142
143         return popup_;
144 }
145
146 QRectF Trace::label_rect(const QRectF &rect) const
147 {
148         using pv::view::View;
149
150         QFontMetrics m(QApplication::font());
151         const QSize text_size(
152                 m.boundingRect(QRect(), 0, name_).width(),
153                 m.boundingRect(QRect(), 0, "Tg").height());
154         const QSizeF label_size(
155                 text_size.width() + View::LabelPadding.width() * 2,
156                 ceilf((text_size.height() + View::LabelPadding.height() * 2) / 2) * 2);
157         const float half_height = label_size.height() / 2;
158         return QRectF(
159                 rect.right() - half_height - label_size.width() - 0.5,
160                 get_visual_y() + 0.5f - half_height,
161                 label_size.width() + half_height,
162                 label_size.height());
163 }
164
165 void Trace::paint_axis(QPainter &p, const RowItemPaintParams &pp, int y)
166 {
167         p.setPen(AxisPen);
168         p.drawLine(QPointF(pp.left(), y + 0.5f), QPointF(pp.right(), y + 0.5f));
169 }
170
171 void Trace::add_colour_option(QWidget *parent, QFormLayout *form)
172 {
173         using pv::widgets::ColourButton;
174
175         ColourButton *const colour_button = new ColourButton(
176                 TracePalette::Rows, TracePalette::Cols, parent);
177         colour_button->set_palette(TracePalette::Colours);
178         colour_button->set_colour(colour_);
179         connect(colour_button, SIGNAL(selected(const QColor&)),
180                 this, SLOT(on_colour_changed(const QColor&)));
181
182         form->addRow(tr("Colour"), colour_button);
183 }
184
185 void Trace::create_popup_form()
186 {
187         // Clear the layout
188
189         // Transfer the layout and the child widgets to a temporary widget
190         // which then goes out of scope destroying the layout and all the child
191         // widgets.
192         if (popup_form_)
193                 QWidget().setLayout(popup_form_);
194
195         // Repopulate the popup
196         popup_form_ = new QFormLayout(popup_);
197         popup_->setLayout(popup_form_);
198         populate_popup_form(popup_, popup_form_);
199 }
200
201 void Trace::populate_popup_form(QWidget *parent, QFormLayout *form)
202 {
203         QLineEdit *const name_edit = new QLineEdit(parent);
204         name_edit->setText(name_);
205         connect(name_edit, SIGNAL(textChanged(const QString&)),
206                 this, SLOT(on_text_changed(const QString&)));
207         form->addRow(tr("Name"), name_edit);
208
209         add_colour_option(parent, form);
210 }
211
212 void Trace::on_popup_closed()
213 {
214         popup_ = NULL;
215         popup_form_ = NULL;
216 }
217
218 void Trace::on_text_changed(const QString &text)
219 {
220         set_name(text);
221
222         if (owner_)
223                 owner_->extents_changed(true, false);
224 }
225
226 void Trace::on_colour_changed(const QColor &colour)
227 {
228         set_colour(colour);
229
230         if (owner_)
231                 owner_->appearance_changed(true, false);
232 }
233
234 } // namespace view
235 } // namespace pv