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