]>
Commit | Line | Data |
---|---|---|
931f20b0 JH |
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> | |
d9e71737 | 24 | #include <cmath> |
931f20b0 | 25 | |
d7c0ca4a | 26 | #include <QApplication> |
b213ef09 | 27 | #include <QFormLayout> |
0891e69b | 28 | #include <QKeyEvent> |
b213ef09 JH |
29 | #include <QLineEdit> |
30 | ||
2acdb232 JH |
31 | #include "trace.hpp" |
32 | #include "tracepalette.hpp" | |
33 | #include "view.hpp" | |
931f20b0 | 34 | |
2acdb232 JH |
35 | #include <pv/widgets/colourbutton.hpp> |
36 | #include <pv/widgets/popup.hpp> | |
569d1e41 | 37 | |
931f20b0 JH |
38 | namespace pv { |
39 | namespace view { | |
40 | ||
fe08b6e8 | 41 | const QPen Trace::AxisPen(QColor(128, 128, 128, 64)); |
931f20b0 JH |
42 | const int Trace::LabelHitPadding = 2; |
43 | ||
ac0708fb SA |
44 | const QColor Trace::DarkBGColour(235, 235, 235); // Quite light grey |
45 | const QColor Trace::BrightBGColour(245, 245, 245); // Very light grey | |
46 | ||
83c23cc9 | 47 | Trace::Trace(QString name) : |
8dbbc7f0 | 48 | name_(name), |
574c568d | 49 | coloured_bg_(true), // Default setting is set in MainWindow::setup_ui() |
4c60462b JH |
50 | popup_(nullptr), |
51 | popup_form_(nullptr) | |
931f20b0 JH |
52 | { |
53 | } | |
54 | ||
0a47889b | 55 | QString Trace::name() const |
931f20b0 | 56 | { |
8dbbc7f0 | 57 | return name_; |
931f20b0 JH |
58 | } |
59 | ||
60 | void Trace::set_name(QString name) | |
61 | { | |
8dbbc7f0 | 62 | name_ = name; |
931f20b0 JH |
63 | } |
64 | ||
06149d43 | 65 | QColor Trace::colour() const |
931f20b0 | 66 | { |
8dbbc7f0 | 67 | return colour_; |
931f20b0 JH |
68 | } |
69 | ||
70 | void Trace::set_colour(QColor colour) | |
71 | { | |
8dbbc7f0 | 72 | colour_ = colour; |
99af6802 SA |
73 | |
74 | bgcolour_ = colour; | |
75 | bgcolour_.setAlpha(20); | |
931f20b0 JH |
76 | } |
77 | ||
574c568d SA |
78 | void Trace::set_coloured_bg(bool state) |
79 | { | |
80 | coloured_bg_ = state; | |
81 | } | |
82 | ||
e081bf3d SA |
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 | ||
35365c66 SA |
94 | void Trace::select(bool select) |
95 | { | |
96 | // Trace can only be selected if the mouse cursor is in the header area; | |
97 | // as is_draggable() checks the same thing, we re-use it here | |
98 | if (is_draggable() && select) | |
99 | selected_ = true; | |
100 | else | |
101 | selected_ = false; | |
102 | } | |
103 | ||
b3f44329 | 104 | void Trace::paint_label(QPainter &p, const QRect &rect, bool hover) |
01fd3263 | 105 | { |
be9e7b4b | 106 | const int y = get_visual_y(); |
01fd3263 | 107 | |
8dbbc7f0 | 108 | p.setBrush(colour_); |
931f20b0 JH |
109 | |
110 | if (!enabled()) | |
111 | return; | |
112 | ||
b3f44329 | 113 | const QRectF r = label_rect(rect); |
931f20b0 JH |
114 | |
115 | // Paint the label | |
b5cb6c35 | 116 | const float label_arrow_length = r.height() / 2; |
931f20b0 | 117 | const QPointF points[] = { |
1053d05c | 118 | r.topLeft(), |
b5cb6c35 JH |
119 | QPointF(r.right() - label_arrow_length, r.top()), |
120 | QPointF(r.right(), y), | |
121 | QPointF(r.right() - label_arrow_length, r.bottom()), | |
1053d05c | 122 | r.bottomLeft() |
931f20b0 | 123 | }; |
931f20b0 | 124 | const QPointF highlight_points[] = { |
1053d05c | 125 | QPointF(r.left() + 1, r.top() + 1), |
b5cb6c35 JH |
126 | QPointF(r.right() - label_arrow_length, r.top() + 1), |
127 | QPointF(r.right() - 1, y), | |
128 | QPointF(r.right() - label_arrow_length, r.bottom() - 1), | |
1053d05c | 129 | QPointF(r.left() + 1, r.bottom() - 1) |
931f20b0 JH |
130 | }; |
131 | ||
132 | if (selected()) { | |
133 | p.setPen(highlight_pen()); | |
134 | p.setBrush(Qt::transparent); | |
135 | p.drawPolygon(points, countof(points)); | |
136 | } | |
137 | ||
138 | p.setPen(Qt::transparent); | |
8dbbc7f0 | 139 | p.setBrush(hover ? colour_.lighter() : colour_); |
931f20b0 JH |
140 | p.drawPolygon(points, countof(points)); |
141 | ||
8dbbc7f0 | 142 | p.setPen(colour_.lighter()); |
931f20b0 JH |
143 | p.setBrush(Qt::transparent); |
144 | p.drawPolygon(highlight_points, countof(highlight_points)); | |
145 | ||
8dbbc7f0 | 146 | p.setPen(colour_.darker()); |
931f20b0 JH |
147 | p.setBrush(Qt::transparent); |
148 | p.drawPolygon(points, countof(points)); | |
149 | ||
150 | // Paint the text | |
d8d724cc | 151 | p.setPen(select_text_colour(colour_)); |
d7c0ca4a | 152 | p.setFont(QApplication::font()); |
b5cb6c35 JH |
153 | p.drawText(QRectF(r.x(), r.y(), |
154 | r.width() - label_arrow_length, r.height()), | |
8dbbc7f0 | 155 | Qt::AlignCenter | Qt::AlignVCenter, name_); |
931f20b0 JH |
156 | } |
157 | ||
9b6378f1 JH |
158 | QMenu* Trace::create_context_menu(QWidget *parent) |
159 | { | |
26e3af6b | 160 | QMenu *const menu = ViewItem::create_context_menu(parent); |
9b6378f1 | 161 | |
9b6378f1 JH |
162 | return menu; |
163 | } | |
164 | ||
569d1e41 JH |
165 | pv::widgets::Popup* Trace::create_popup(QWidget *parent) |
166 | { | |
167 | using pv::widgets::Popup; | |
20eaae39 | 168 | |
8dbbc7f0 | 169 | popup_ = new Popup(parent); |
786b7678 JH |
170 | popup_->set_position(parent->mapToGlobal( |
171 | point(parent->rect())), Popup::Right); | |
20eaae39 | 172 | |
37fd11b1 | 173 | create_popup_form(); |
20eaae39 | 174 | |
8dbbc7f0 | 175 | connect(popup_, SIGNAL(closed()), |
20eaae39 JH |
176 | this, SLOT(on_popup_closed())); |
177 | ||
8dbbc7f0 | 178 | return popup_; |
569d1e41 JH |
179 | } |
180 | ||
b3f44329 | 181 | QRectF Trace::label_rect(const QRectF &rect) const |
d7c0ca4a JH |
182 | { |
183 | using pv::view::View; | |
184 | ||
d7c0ca4a JH |
185 | QFontMetrics m(QApplication::font()); |
186 | const QSize text_size( | |
c0096500 | 187 | m.boundingRect(QRect(), 0, name_).width(), m.height()); |
d7c0ca4a | 188 | const QSizeF label_size( |
ec39632d JH |
189 | text_size.width() + LabelPadding.width() * 2, |
190 | ceilf((text_size.height() + LabelPadding.height() * 2) / 2) * 2); | |
b5cb6c35 | 191 | const float half_height = label_size.height() / 2; |
d7c0ca4a | 192 | return QRectF( |
b3f44329 | 193 | rect.right() - half_height - label_size.width() - 0.5, |
be9e7b4b | 194 | get_visual_y() + 0.5f - half_height, |
b5cb6c35 JH |
195 | label_size.width() + half_height, |
196 | label_size.height()); | |
d7c0ca4a JH |
197 | } |
198 | ||
cbf0f87e | 199 | QRectF Trace::hit_box_rect(const ViewItemPaintParams &pp) const |
7708eb92 JH |
200 | { |
201 | const float h = QFontMetrics(QApplication::font()).height(); | |
cbf0f87e JH |
202 | return QRectF(pp.left(), get_visual_y() - h / 2.0f, |
203 | pp.width(), h); | |
7708eb92 JH |
204 | } |
205 | ||
99af6802 SA |
206 | void Trace::paint_back(QPainter &p, const ViewItemPaintParams &pp) |
207 | { | |
ac0708fb | 208 | if (coloured_bg_) |
574c568d | 209 | p.setBrush(bgcolour_); |
ac0708fb SA |
210 | else |
211 | p.setBrush(bgcolour_state_ ? BrightBGColour : DarkBGColour); | |
99af6802 | 212 | |
ac0708fb | 213 | p.setPen(QPen(Qt::NoPen)); |
99af6802 | 214 | |
ac0708fb | 215 | const std::pair<int, int> extents = v_extents(); |
99af6802 | 216 | |
ac0708fb SA |
217 | const int x = 0; |
218 | const int y = get_visual_y() + extents.first; | |
219 | const int w = pp.right() - pp.left(); | |
220 | const int h = extents.second - extents.first; | |
221 | ||
222 | p.drawRect(x, y, w, h); | |
99af6802 SA |
223 | } |
224 | ||
5b5fa4da | 225 | void Trace::paint_axis(QPainter &p, const ViewItemPaintParams &pp, int y) |
fe08b6e8 JH |
226 | { |
227 | p.setPen(AxisPen); | |
97904bf7 | 228 | p.drawLine(QPointF(pp.left(), y + 0.5f), QPointF(pp.right(), y + 0.5f)); |
fe08b6e8 JH |
229 | } |
230 | ||
91e8bf08 JH |
231 | void Trace::add_colour_option(QWidget *parent, QFormLayout *form) |
232 | { | |
233 | using pv::widgets::ColourButton; | |
234 | ||
235 | ColourButton *const colour_button = new ColourButton( | |
236 | TracePalette::Rows, TracePalette::Cols, parent); | |
237 | colour_button->set_palette(TracePalette::Colours); | |
8dbbc7f0 | 238 | colour_button->set_colour(colour_); |
91e8bf08 JH |
239 | connect(colour_button, SIGNAL(selected(const QColor&)), |
240 | this, SLOT(on_colour_changed(const QColor&))); | |
241 | ||
242 | form->addRow(tr("Colour"), colour_button); | |
243 | } | |
244 | ||
37fd11b1 JH |
245 | void Trace::create_popup_form() |
246 | { | |
247 | // Clear the layout | |
248 | ||
249 | // Transfer the layout and the child widgets to a temporary widget | |
250 | // which then goes out of scope destroying the layout and all the child | |
251 | // widgets. | |
8dbbc7f0 JH |
252 | if (popup_form_) |
253 | QWidget().setLayout(popup_form_); | |
37fd11b1 JH |
254 | |
255 | // Repopulate the popup | |
8dbbc7f0 JH |
256 | popup_form_ = new QFormLayout(popup_); |
257 | popup_->setLayout(popup_form_); | |
258 | populate_popup_form(popup_, popup_form_); | |
37fd11b1 JH |
259 | } |
260 | ||
569d1e41 JH |
261 | void Trace::populate_popup_form(QWidget *parent, QFormLayout *form) |
262 | { | |
68456dab | 263 | QLineEdit *const name_edit = new QLineEdit(parent); |
8dbbc7f0 | 264 | name_edit->setText(name_); |
68456dab JH |
265 | connect(name_edit, SIGNAL(textChanged(const QString&)), |
266 | this, SLOT(on_text_changed(const QString&))); | |
267 | form->addRow(tr("Name"), name_edit); | |
91e8bf08 JH |
268 | |
269 | add_colour_option(parent, form); | |
569d1e41 JH |
270 | } |
271 | ||
20eaae39 JH |
272 | void Trace::on_popup_closed() |
273 | { | |
4c60462b JH |
274 | popup_ = nullptr; |
275 | popup_form_ = nullptr; | |
20eaae39 JH |
276 | } |
277 | ||
68456dab JH |
278 | void Trace::on_text_changed(const QString &text) |
279 | { | |
280 | set_name(text); | |
32218d3e | 281 | |
8dbbc7f0 JH |
282 | if (owner_) |
283 | owner_->extents_changed(true, false); | |
68456dab | 284 | } |
9b6378f1 | 285 | |
91e8bf08 JH |
286 | void Trace::on_colour_changed(const QColor &colour) |
287 | { | |
288 | set_colour(colour); | |
32218d3e | 289 | |
8dbbc7f0 | 290 | if (owner_) |
99af6802 | 291 | owner_->row_item_appearance_changed(true, true); |
91e8bf08 JH |
292 | } |
293 | ||
931f20b0 JH |
294 | } // namespace view |
295 | } // namespace pv |