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