]>
Commit | Line | Data |
---|---|---|
1d8dca91 | 1 | /* |
b3f22de0 | 2 | * This file is part of the PulseView project. |
1d8dca91 JH |
3 | * |
4 | * Copyright (C) 2012 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 "header.h" | |
22 | #include "view.h" | |
23 | ||
8d634081 | 24 | #include "signal.h" |
51e77110 | 25 | #include "../sigsession.h" |
1d8dca91 | 26 | |
68b21a71 JH |
27 | #include <cassert> |
28 | #include <algorithm> | |
1d8dca91 | 29 | |
e3374498 | 30 | #include <QApplication> |
49f8ff3f | 31 | #include <QMenu> |
a29bb7fb | 32 | #include <QMouseEvent> |
1d8dca91 JH |
33 | #include <QPainter> |
34 | #include <QRect> | |
35 | ||
569d1e41 JH |
36 | #include <pv/widgets/popup.h> |
37 | ||
819f4c25 JH |
38 | using std::max; |
39 | using std::make_pair; | |
40 | using std::pair; | |
f9abf97e | 41 | using std::shared_ptr; |
68b21a71 | 42 | using std::stable_sort; |
819f4c25 | 43 | using std::vector; |
1d8dca91 JH |
44 | |
45 | namespace pv { | |
46 | namespace view { | |
47 | ||
d7c0ca4a | 48 | const int Header::Padding = 12; |
512bfc56 | 49 | const int Header::BaselineOffset = 5; |
d7c0ca4a | 50 | |
1d8dca91 | 51 | Header::Header(View &parent) : |
728fcafc JH |
52 | MarginWidget(parent), |
53 | _dragging(false) | |
1d8dca91 | 54 | { |
5ed1adf5 | 55 | setFocusPolicy(Qt::ClickFocus); |
a29bb7fb | 56 | setMouseTracking(true); |
49f8ff3f | 57 | |
9e40e83d JH |
58 | connect(&_view.session(), SIGNAL(signals_changed()), |
59 | this, SLOT(on_signals_changed())); | |
60 | ||
07204819 JH |
61 | connect(&_view, SIGNAL(signals_moved()), |
62 | this, SLOT(on_signals_moved())); | |
1d8dca91 JH |
63 | } |
64 | ||
d7c0ca4a JH |
65 | QSize Header::sizeHint() const |
66 | { | |
b781f806 | 67 | QRectF max_rect(-Padding, 0, Padding, 0); |
aa59d5c2 JH |
68 | for (auto &i : _view) |
69 | if (i->enabled()) | |
b781f806 JH |
70 | max_rect = max_rect.united(i->label_rect(0)); |
71 | return QSize(max_rect.width() + Padding + BaselineOffset, 0); | |
d7c0ca4a JH |
72 | } |
73 | ||
eae6e30a | 74 | shared_ptr<RowItem> Header::get_mouse_over_row_item(const QPoint &pt) |
e3374498 | 75 | { |
de0b46de | 76 | const int w = width() - BaselineOffset; |
aa59d5c2 JH |
77 | for (auto &i : _view) |
78 | if (i->enabled() && i->label_rect(w).contains(pt)) | |
79 | return i; | |
eae6e30a | 80 | return shared_ptr<RowItem>(); |
e3374498 JH |
81 | } |
82 | ||
a2ae0205 JH |
83 | void Header::clear_selection() |
84 | { | |
aa59d5c2 JH |
85 | for (auto &i : _view) |
86 | i->select(false); | |
a2ae0205 JH |
87 | update(); |
88 | } | |
89 | ||
8b4802fb JH |
90 | void Header::show_popup(const shared_ptr<RowItem> &item) |
91 | { | |
92 | using pv::widgets::Popup; | |
93 | ||
94 | Popup *const p = item->create_popup(&_view); | |
95 | if (!p) | |
96 | return; | |
97 | ||
98 | const QPoint pt(width() - BaselineOffset, item->get_y()); | |
99 | p->set_position(mapToGlobal(pt), Popup::Right); | |
100 | p->show(); | |
101 | } | |
102 | ||
e314eca4 | 103 | void Header::paintEvent(QPaintEvent*) |
1d8dca91 | 104 | { |
512bfc56 JS |
105 | // The trace labels are not drawn with the arrows exactly on the |
106 | // left edge of the widget, because then the selection shadow | |
107 | // would be clipped away. | |
108 | const int w = width() - BaselineOffset; | |
68b21a71 | 109 | |
aa59d5c2 JH |
110 | vector< shared_ptr<RowItem> > row_items( |
111 | _view.begin(), _view.end()); | |
112 | ||
68b21a71 JH |
113 | stable_sort(row_items.begin(), row_items.end(), |
114 | [](const shared_ptr<RowItem> &a, const shared_ptr<RowItem> &b) { | |
115 | return a->v_offset() < b->v_offset(); }); | |
1d8dca91 JH |
116 | |
117 | QPainter painter(this); | |
118 | painter.setRenderHint(QPainter::Antialiasing); | |
119 | ||
eae6e30a JH |
120 | const bool dragging = !_drag_row_items.empty(); |
121 | for (const shared_ptr<RowItem> r : row_items) | |
1d8dca91 | 122 | { |
eae6e30a | 123 | assert(r); |
1d8dca91 | 124 | |
25201e88 | 125 | const bool highlight = !dragging && |
eae6e30a JH |
126 | r->label_rect(w).contains(_mouse_point); |
127 | r->paint_label(painter, w, highlight); | |
1d8dca91 JH |
128 | } |
129 | ||
130 | painter.end(); | |
131 | } | |
132 | ||
e3374498 JH |
133 | void Header::mousePressEvent(QMouseEvent *event) |
134 | { | |
135 | assert(event); | |
136 | ||
333d5bbc | 137 | if (event->button() & Qt::LeftButton) { |
54401bbb JH |
138 | _mouse_down_point = event->pos(); |
139 | ||
da2bebfb | 140 | // Save the offsets of any signals which will be dragged |
aa59d5c2 | 141 | for (const shared_ptr<RowItem> r : _view) |
eae6e30a JH |
142 | if (r->selected()) |
143 | _drag_row_items.push_back( | |
144 | make_pair(r, r->v_offset())); | |
da2bebfb JH |
145 | } |
146 | ||
147 | // Select the signal if it has been clicked | |
eae6e30a JH |
148 | const shared_ptr<RowItem> mouse_over_row_item = |
149 | get_mouse_over_row_item(event->pos()); | |
150 | if (mouse_over_row_item) { | |
151 | if (mouse_over_row_item->selected()) | |
152 | mouse_over_row_item->select(false); | |
da2bebfb | 153 | else { |
eae6e30a | 154 | mouse_over_row_item->select(true); |
da2bebfb | 155 | |
333d5bbc | 156 | if (~QApplication::keyboardModifiers() & |
07204819 | 157 | Qt::ControlModifier) |
eae6e30a | 158 | _drag_row_items.clear(); |
07204819 | 159 | |
da2bebfb | 160 | // Add the signal to the drag list |
333d5bbc | 161 | if (event->button() & Qt::LeftButton) |
eae6e30a JH |
162 | _drag_row_items.push_back( |
163 | make_pair(mouse_over_row_item, | |
164 | mouse_over_row_item->v_offset())); | |
da2bebfb | 165 | } |
54401bbb JH |
166 | } |
167 | ||
333d5bbc | 168 | if (~QApplication::keyboardModifiers() & Qt::ControlModifier) { |
07204819 JH |
169 | // Unselect all other signals because the Ctrl is not |
170 | // pressed | |
aa59d5c2 | 171 | for (const shared_ptr<RowItem> r : _view) |
eae6e30a JH |
172 | if (r != mouse_over_row_item) |
173 | r->select(false); | |
07204819 JH |
174 | } |
175 | ||
b2a53645 | 176 | selection_changed(); |
e3374498 JH |
177 | update(); |
178 | } | |
179 | ||
54401bbb JH |
180 | void Header::mouseReleaseEvent(QMouseEvent *event) |
181 | { | |
182 | assert(event); | |
333d5bbc | 183 | if (event->button() == Qt::LeftButton) { |
569d1e41 JH |
184 | if (_dragging) |
185 | _view.normalize_layout(); | |
186 | else | |
187 | { | |
eae6e30a JH |
188 | const shared_ptr<RowItem> mouse_over_row_item = |
189 | get_mouse_over_row_item(event->pos()); | |
8b4802fb JH |
190 | if (mouse_over_row_item) |
191 | show_popup(mouse_over_row_item); | |
569d1e41 JH |
192 | } |
193 | ||
728fcafc | 194 | _dragging = false; |
eae6e30a | 195 | _drag_row_items.clear(); |
4b192962 | 196 | } |
54401bbb JH |
197 | } |
198 | ||
a29bb7fb JH |
199 | void Header::mouseMoveEvent(QMouseEvent *event) |
200 | { | |
201 | assert(event); | |
202 | _mouse_point = event->pos(); | |
54401bbb | 203 | |
728fcafc JH |
204 | if (!(event->buttons() & Qt::LeftButton)) |
205 | return; | |
206 | ||
207 | if ((event->pos() - _mouse_down_point).manhattanLength() < | |
208 | QApplication::startDragDistance()) | |
209 | return; | |
210 | ||
54401bbb | 211 | // Move the signals if we are dragging |
eae6e30a | 212 | if (!_drag_row_items.empty()) |
728fcafc JH |
213 | { |
214 | _dragging = true; | |
215 | ||
54401bbb JH |
216 | const int delta = event->pos().y() - _mouse_down_point.y(); |
217 | ||
eae6e30a JH |
218 | for (auto i = _drag_row_items.begin(); |
219 | i != _drag_row_items.end(); i++) { | |
220 | const std::shared_ptr<RowItem> row_item((*i).first); | |
221 | if (row_item) { | |
da2bebfb | 222 | const int y = (*i).second + delta; |
49153683 JH |
223 | const int y_snap = |
224 | ((y + View::SignalSnapGridSize / 2) / | |
225 | View::SignalSnapGridSize) * | |
226 | View::SignalSnapGridSize; | |
eae6e30a | 227 | row_item->set_v_offset(y_snap); |
da2bebfb | 228 | |
38eeddea | 229 | // Ensure the trace is selected |
eae6e30a | 230 | row_item->select(); |
49153683 | 231 | } |
da2bebfb JH |
232 | |
233 | } | |
54401bbb JH |
234 | |
235 | signals_moved(); | |
236 | } | |
237 | ||
a29bb7fb JH |
238 | update(); |
239 | } | |
240 | ||
e314eca4 | 241 | void Header::leaveEvent(QEvent*) |
a29bb7fb JH |
242 | { |
243 | _mouse_point = QPoint(-1, -1); | |
244 | update(); | |
245 | } | |
246 | ||
49f8ff3f JH |
247 | void Header::contextMenuEvent(QContextMenuEvent *event) |
248 | { | |
eae6e30a | 249 | const shared_ptr<RowItem> r = get_mouse_over_row_item(_mouse_point); |
a28878f4 JH |
250 | if (!r) |
251 | return; | |
252 | ||
253 | QMenu *const menu = r->create_context_menu(this); | |
254 | if (!menu) | |
255 | return; | |
49f8ff3f | 256 | |
a28878f4 | 257 | menu->exec(event->globalPos()); |
b3b57abc JH |
258 | } |
259 | ||
5ed1adf5 JH |
260 | void Header::keyPressEvent(QKeyEvent *e) |
261 | { | |
262 | assert(e); | |
263 | ||
264 | switch (e->key()) | |
265 | { | |
266 | case Qt::Key_Delete: | |
267 | { | |
aa59d5c2 | 268 | for (const shared_ptr<RowItem> r : _view) |
eae6e30a JH |
269 | if (r->selected()) |
270 | r->delete_pressed(); | |
5ed1adf5 JH |
271 | break; |
272 | } | |
273 | } | |
274 | } | |
275 | ||
9e40e83d JH |
276 | void Header::on_signals_changed() |
277 | { | |
aa59d5c2 | 278 | for (shared_ptr<RowItem> r : _view) { |
eae6e30a JH |
279 | assert(r); |
280 | connect(r.get(), SIGNAL(visibility_changed()), | |
6f98ca4c | 281 | this, SLOT(on_trace_changed())); |
eae6e30a | 282 | connect(r.get(), SIGNAL(text_changed()), |
6f98ca4c | 283 | this, SLOT(on_trace_changed())); |
eae6e30a | 284 | connect(r.get(), SIGNAL(colour_changed()), |
91e8bf08 | 285 | this, SLOT(update())); |
9e40e83d JH |
286 | } |
287 | } | |
288 | ||
07204819 JH |
289 | void Header::on_signals_moved() |
290 | { | |
291 | update(); | |
292 | } | |
293 | ||
6f98ca4c | 294 | void Header::on_trace_changed() |
d7c0ca4a JH |
295 | { |
296 | update(); | |
297 | geometry_updated(); | |
298 | } | |
07204819 | 299 | |
1d8dca91 JH |
300 | } // namespace view |
301 | } // namespace pv |