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