]>
Commit | Line | Data |
---|---|---|
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, see <http://www.gnu.org/licenses/>. | |
18 | */ | |
19 | ||
20 | #include <extdef.h> | |
21 | ||
22 | #include <cassert> | |
23 | #include <cmath> | |
24 | ||
25 | #include <QApplication> | |
26 | #include <QFormLayout> | |
27 | #include <QKeyEvent> | |
28 | #include <QLineEdit> | |
29 | ||
30 | #include "trace.hpp" | |
31 | #include "tracepalette.hpp" | |
32 | #include "view.hpp" | |
33 | ||
34 | #include "pv/globalsettings.hpp" | |
35 | #include "pv/widgets/colorbutton.hpp" | |
36 | #include "pv/widgets/popup.hpp" | |
37 | ||
38 | using std::pair; | |
39 | using std::shared_ptr; | |
40 | ||
41 | namespace pv { | |
42 | namespace views { | |
43 | namespace trace { | |
44 | ||
45 | const QPen Trace::AxisPen(QColor(0, 0, 0, 30 * 256 / 100)); | |
46 | const int Trace::LabelHitPadding = 2; | |
47 | ||
48 | const QColor Trace::BrightGrayBGColor = QColor(0, 0, 0, 10 * 255 / 100); | |
49 | const QColor Trace::DarkGrayBGColor = QColor(0, 0, 0, 15 * 255 / 100); | |
50 | ||
51 | Trace::Trace(shared_ptr<data::SignalBase> channel) : | |
52 | base_(channel), | |
53 | axis_pen_(AxisPen), | |
54 | segment_display_mode_(ShowLastSegmentOnly), // Will be overwritten by View | |
55 | current_segment_(0), | |
56 | popup_(nullptr), | |
57 | popup_form_(nullptr) | |
58 | { | |
59 | connect(channel.get(), SIGNAL(name_changed(const QString&)), | |
60 | this, SLOT(on_name_changed(const QString&))); | |
61 | connect(channel.get(), SIGNAL(color_changed(const QColor&)), | |
62 | this, SLOT(on_color_changed(const QColor&))); | |
63 | } | |
64 | ||
65 | shared_ptr<data::SignalBase> Trace::base() const | |
66 | { | |
67 | return base_; | |
68 | } | |
69 | ||
70 | void Trace::paint_label(QPainter &p, const QRect &rect, bool hover) | |
71 | { | |
72 | const int y = get_visual_y(); | |
73 | ||
74 | p.setBrush(base_->color()); | |
75 | ||
76 | if (!enabled()) | |
77 | return; | |
78 | ||
79 | const QRectF r = label_rect(rect); | |
80 | ||
81 | // When selected, move the arrow to the left so that the border can show | |
82 | const QPointF offs = (selected()) ? QPointF(-2, 0) : QPointF(0, 0); | |
83 | ||
84 | // Paint the label | |
85 | const float label_arrow_length = r.height() / 2; | |
86 | QPointF points[] = { | |
87 | offs + r.topLeft(), | |
88 | offs + QPointF(r.right() - label_arrow_length, r.top()), | |
89 | offs + QPointF(r.right(), y), | |
90 | offs + QPointF(r.right() - label_arrow_length, r.bottom()), | |
91 | offs + r.bottomLeft() | |
92 | }; | |
93 | QPointF highlight_points[] = { | |
94 | offs + QPointF(r.left() + 1, r.top() + 1), | |
95 | offs + QPointF(r.right() - label_arrow_length, r.top() + 1), | |
96 | offs + QPointF(r.right() - 1, y), | |
97 | offs + QPointF(r.right() - label_arrow_length, r.bottom() - 1), | |
98 | offs + QPointF(r.left() + 1, r.bottom() - 1) | |
99 | }; | |
100 | ||
101 | if (selected()) { | |
102 | p.setPen(highlight_pen()); | |
103 | p.setBrush(Qt::transparent); | |
104 | p.drawPolygon(points, countof(points)); | |
105 | } | |
106 | ||
107 | p.setPen(Qt::transparent); | |
108 | p.setBrush(hover ? base_->color().lighter() : base_->color()); | |
109 | p.drawPolygon(points, countof(points)); | |
110 | ||
111 | p.setPen(base_->color().lighter()); | |
112 | p.setBrush(Qt::transparent); | |
113 | p.drawPolygon(highlight_points, countof(highlight_points)); | |
114 | ||
115 | p.setPen(base_->color().darker()); | |
116 | p.setBrush(Qt::transparent); | |
117 | p.drawPolygon(points, countof(points)); | |
118 | ||
119 | // Paint the text | |
120 | p.setPen(select_text_color(base_->color())); | |
121 | p.setFont(QApplication::font()); | |
122 | p.drawText(QRectF(r.x(), r.y(), | |
123 | r.width() - label_arrow_length, r.height()), | |
124 | Qt::AlignCenter | Qt::AlignVCenter, base_->name()); | |
125 | } | |
126 | ||
127 | QMenu* Trace::create_context_menu(QWidget *parent) | |
128 | { | |
129 | QMenu *const menu = ViewItem::create_context_menu(parent); | |
130 | ||
131 | return menu; | |
132 | } | |
133 | ||
134 | pv::widgets::Popup* Trace::create_popup(QWidget *parent) | |
135 | { | |
136 | using pv::widgets::Popup; | |
137 | ||
138 | popup_ = new Popup(parent); | |
139 | popup_->set_position(parent->mapToGlobal( | |
140 | drag_point(parent->rect())), Popup::Right); | |
141 | ||
142 | create_popup_form(); | |
143 | ||
144 | connect(popup_, SIGNAL(closed()), this, SLOT(on_popup_closed())); | |
145 | ||
146 | return popup_; | |
147 | } | |
148 | ||
149 | QRectF Trace::label_rect(const QRectF &rect) const | |
150 | { | |
151 | QFontMetrics m(QApplication::font()); | |
152 | const QSize text_size( | |
153 | m.boundingRect(QRect(), 0, base_->name()).width(), m.height()); | |
154 | const QSizeF label_size( | |
155 | text_size.width() + LabelPadding.width() * 2, | |
156 | ceilf((text_size.height() + 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::set_current_segment(const int segment) | |
166 | { | |
167 | current_segment_ = segment; | |
168 | } | |
169 | ||
170 | int Trace::get_current_segment() const | |
171 | { | |
172 | return current_segment_; | |
173 | } | |
174 | ||
175 | void Trace::paint_back(QPainter &p, ViewItemPaintParams &pp) | |
176 | { | |
177 | const View *view = owner_->view(); | |
178 | assert(view); | |
179 | ||
180 | if (view->colored_bg()) | |
181 | p.setBrush(base_->bgcolor()); | |
182 | else | |
183 | p.setBrush(pp.next_bg_color_state() ? BrightGrayBGColor : DarkGrayBGColor); | |
184 | ||
185 | p.setPen(QPen(Qt::NoPen)); | |
186 | ||
187 | const pair<int, int> extents = v_extents(); | |
188 | p.drawRect(pp.left(), get_visual_y() + extents.first, | |
189 | pp.width(), extents.second - extents.first); | |
190 | } | |
191 | ||
192 | void Trace::paint_axis(QPainter &p, ViewItemPaintParams &pp, int y) | |
193 | { | |
194 | p.setRenderHint(QPainter::Antialiasing, false); | |
195 | ||
196 | p.setPen(axis_pen_); | |
197 | p.drawLine(QPointF(pp.left(), y), QPointF(pp.right(), y)); | |
198 | ||
199 | p.setRenderHint(QPainter::Antialiasing, true); | |
200 | } | |
201 | ||
202 | void Trace::add_color_option(QWidget *parent, QFormLayout *form) | |
203 | { | |
204 | using pv::widgets::ColorButton; | |
205 | ||
206 | ColorButton *const color_button = new ColorButton( | |
207 | TracePalette::Rows, TracePalette::Cols, parent); | |
208 | color_button->set_palette(TracePalette::Colors); | |
209 | color_button->set_color(base_->color()); | |
210 | connect(color_button, SIGNAL(selected(const QColor&)), | |
211 | this, SLOT(on_coloredit_changed(const QColor&))); | |
212 | ||
213 | form->addRow(tr("Color"), color_button); | |
214 | } | |
215 | ||
216 | void Trace::create_popup_form() | |
217 | { | |
218 | // Clear the layout | |
219 | ||
220 | // Transfer the layout and the child widgets to a temporary widget | |
221 | // which we delete after the event was handled. This way, the layout | |
222 | // and all widgets contained therein are deleted after the event was | |
223 | // handled, leaving the parent popup_ time to handle the change. | |
224 | if (popup_form_) { | |
225 | QWidget *suicidal = new QWidget(); | |
226 | suicidal->setLayout(popup_form_); | |
227 | suicidal->deleteLater(); | |
228 | } | |
229 | ||
230 | // Repopulate the popup | |
231 | popup_form_ = new QFormLayout(popup_); | |
232 | popup_->setLayout(popup_form_); | |
233 | populate_popup_form(popup_, popup_form_); | |
234 | } | |
235 | ||
236 | void Trace::populate_popup_form(QWidget *parent, QFormLayout *form) | |
237 | { | |
238 | QLineEdit *const name_edit = new QLineEdit(parent); | |
239 | name_edit->setText(base_->name()); | |
240 | connect(name_edit, SIGNAL(textChanged(const QString&)), | |
241 | this, SLOT(on_nameedit_changed(const QString&))); | |
242 | form->addRow(tr("Name"), name_edit); | |
243 | ||
244 | add_color_option(parent, form); | |
245 | } | |
246 | ||
247 | void Trace::set_name(QString name) | |
248 | { | |
249 | base_->set_name(name); | |
250 | } | |
251 | ||
252 | void Trace::set_color(QColor color) | |
253 | { | |
254 | base_->set_color(color); | |
255 | } | |
256 | ||
257 | void Trace::set_segment_display_mode(SegmentDisplayMode mode) | |
258 | { | |
259 | segment_display_mode_ = mode; | |
260 | ||
261 | if (owner_) | |
262 | owner_->row_item_appearance_changed(true, true); | |
263 | } | |
264 | ||
265 | void Trace::on_name_changed(const QString &text) | |
266 | { | |
267 | /* This event handler is called by SignalBase when the name was changed there */ | |
268 | (void)text; | |
269 | ||
270 | if (owner_) { | |
271 | owner_->extents_changed(true, false); | |
272 | owner_->row_item_appearance_changed(true, false); | |
273 | } | |
274 | } | |
275 | ||
276 | void Trace::on_color_changed(const QColor &color) | |
277 | { | |
278 | /* This event handler is called by SignalBase when the color was changed there */ | |
279 | (void)color; | |
280 | ||
281 | if (owner_) | |
282 | owner_->row_item_appearance_changed(true, true); | |
283 | } | |
284 | ||
285 | void Trace::on_popup_closed() | |
286 | { | |
287 | popup_ = nullptr; | |
288 | popup_form_ = nullptr; | |
289 | } | |
290 | ||
291 | void Trace::on_nameedit_changed(const QString &name) | |
292 | { | |
293 | /* This event handler notifies SignalBase that the name changed */ | |
294 | set_name(name); | |
295 | } | |
296 | ||
297 | void Trace::on_coloredit_changed(const QColor &color) | |
298 | { | |
299 | /* This event handler notifies SignalBase that the color changed */ | |
300 | set_color(color); | |
301 | } | |
302 | ||
303 | } // namespace trace | |
304 | } // namespace views | |
305 | } // namespace pv |