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