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