]>
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 | ||
2acdb232 JH |
21 | #include "header.hpp" |
22 | #include "view.hpp" | |
1d8dca91 | 23 | |
2acdb232 JH |
24 | #include "signal.hpp" |
25 | #include "tracegroup.hpp" | |
1d8dca91 | 26 | |
68b21a71 JH |
27 | #include <cassert> |
28 | #include <algorithm> | |
1d8dca91 | 29 | |
73ba2d70 JH |
30 | #include <boost/iterator/filter_iterator.hpp> |
31 | ||
e3374498 | 32 | #include <QApplication> |
49f8ff3f | 33 | #include <QMenu> |
a29bb7fb | 34 | #include <QMouseEvent> |
1d8dca91 JH |
35 | #include <QPainter> |
36 | #include <QRect> | |
37 | ||
f65cd27b | 38 | #include <pv/session.hpp> |
2acdb232 | 39 | #include <pv/widgets/popup.hpp> |
569d1e41 | 40 | |
73ba2d70 | 41 | using boost::make_filter_iterator; |
bb419fdd | 42 | using std::dynamic_pointer_cast; |
819f4c25 JH |
43 | using std::max; |
44 | using std::make_pair; | |
73ba2d70 | 45 | using std::min; |
819f4c25 | 46 | using std::pair; |
f9abf97e | 47 | using std::shared_ptr; |
68b21a71 | 48 | using std::stable_sort; |
819f4c25 | 49 | using std::vector; |
1d8dca91 JH |
50 | |
51 | namespace pv { | |
52 | namespace view { | |
53 | ||
d7c0ca4a | 54 | const int Header::Padding = 12; |
512bfc56 | 55 | const int Header::BaselineOffset = 5; |
d7c0ca4a | 56 | |
73ba2d70 JH |
57 | static bool item_selected(shared_ptr<RowItem> r) |
58 | { | |
59 | return r->selected(); | |
60 | } | |
61 | ||
1d8dca91 | 62 | Header::Header(View &parent) : |
f4433aa9 | 63 | MarginWidget(parent) |
1d8dca91 | 64 | { |
8dbbc7f0 | 65 | connect(&view_, SIGNAL(signals_moved()), |
07204819 | 66 | this, SLOT(on_signals_moved())); |
1d8dca91 JH |
67 | } |
68 | ||
d7c0ca4a JH |
69 | QSize Header::sizeHint() const |
70 | { | |
b781f806 | 71 | QRectF max_rect(-Padding, 0, Padding, 0); |
8dbbc7f0 | 72 | for (auto &i : view_) |
aa59d5c2 | 73 | if (i->enabled()) |
b3f44329 | 74 | max_rect = max_rect.united(i->label_rect(QRect())); |
b781f806 | 75 | return QSize(max_rect.width() + Padding + BaselineOffset, 0); |
d7c0ca4a JH |
76 | } |
77 | ||
819e2e95 JH |
78 | QSize Header::extended_size_hint() const |
79 | { | |
80 | return sizeHint() + QSize(ViewItem::HighlightRadius, 0); | |
81 | } | |
82 | ||
eae6e30a | 83 | shared_ptr<RowItem> Header::get_mouse_over_row_item(const QPoint &pt) |
e3374498 | 84 | { |
c13447b3 | 85 | const QRect r(0, 0, width() - BaselineOffset, height()); |
8dbbc7f0 | 86 | for (auto &i : view_) |
b3f44329 | 87 | if (i->enabled() && i->label_rect(r).contains(pt)) |
aa59d5c2 | 88 | return i; |
eae6e30a | 89 | return shared_ptr<RowItem>(); |
e3374498 JH |
90 | } |
91 | ||
a2ae0205 JH |
92 | void Header::clear_selection() |
93 | { | |
8dbbc7f0 | 94 | for (auto &i : view_) |
aa59d5c2 | 95 | i->select(false); |
a2ae0205 JH |
96 | update(); |
97 | } | |
98 | ||
8b4802fb JH |
99 | void Header::show_popup(const shared_ptr<RowItem> &item) |
100 | { | |
101 | using pv::widgets::Popup; | |
102 | ||
8dbbc7f0 | 103 | Popup *const p = item->create_popup(&view_); |
8b4802fb JH |
104 | if (!p) |
105 | return; | |
106 | ||
be9e7b4b | 107 | const QPoint pt(width() - BaselineOffset, item->get_visual_y()); |
8b4802fb JH |
108 | p->set_position(mapToGlobal(pt), Popup::Right); |
109 | p->show(); | |
110 | } | |
111 | ||
e314eca4 | 112 | void Header::paintEvent(QPaintEvent*) |
1d8dca91 | 113 | { |
512bfc56 JS |
114 | // The trace labels are not drawn with the arrows exactly on the |
115 | // left edge of the widget, because then the selection shadow | |
116 | // would be clipped away. | |
c13447b3 | 117 | const QRect rect(0, 0, width() - BaselineOffset, height()); |
68b21a71 | 118 | |
aa59d5c2 | 119 | vector< shared_ptr<RowItem> > row_items( |
8dbbc7f0 | 120 | view_.begin(), view_.end()); |
aa59d5c2 | 121 | |
68b21a71 JH |
122 | stable_sort(row_items.begin(), row_items.end(), |
123 | [](const shared_ptr<RowItem> &a, const shared_ptr<RowItem> &b) { | |
be9e7b4b | 124 | return a->visual_v_offset() < b->visual_v_offset(); }); |
1d8dca91 JH |
125 | |
126 | QPainter painter(this); | |
127 | painter.setRenderHint(QPainter::Antialiasing); | |
128 | ||
eae6e30a | 129 | for (const shared_ptr<RowItem> r : row_items) |
1d8dca91 | 130 | { |
eae6e30a | 131 | assert(r); |
1d8dca91 | 132 | |
8dbbc7f0 | 133 | const bool highlight = !dragging_ && |
b3f44329 JH |
134 | r->label_rect(rect).contains(mouse_point_); |
135 | r->paint_label(painter, rect, highlight); | |
1d8dca91 JH |
136 | } |
137 | ||
138 | painter.end(); | |
139 | } | |
140 | ||
e57ee799 JH |
141 | void Header::mouseLeftPressEvent(QMouseEvent *event) |
142 | { | |
f0c0b38f JH |
143 | (void)event; |
144 | ||
e57ee799 JH |
145 | const bool ctrl_pressed = |
146 | QApplication::keyboardModifiers() & Qt::ControlModifier; | |
147 | ||
148 | // Clear selection if control is not pressed and this item is unselected | |
8dbbc7f0 | 149 | if ((!mouse_down_item_ || !mouse_down_item_->selected()) && |
f0c0b38f | 150 | !ctrl_pressed) |
8dbbc7f0 | 151 | for (shared_ptr<RowItem> r : view_) |
e57ee799 JH |
152 | r->select(false); |
153 | ||
154 | // Set the signal selection state if the item has been clicked | |
8dbbc7f0 | 155 | if (mouse_down_item_) { |
e57ee799 | 156 | if (ctrl_pressed) |
8dbbc7f0 | 157 | mouse_down_item_->select(!mouse_down_item_->selected()); |
e57ee799 | 158 | else |
8dbbc7f0 | 159 | mouse_down_item_->select(true); |
e57ee799 JH |
160 | } |
161 | ||
162 | // Save the offsets of any signals which will be dragged | |
8dbbc7f0 | 163 | for (const shared_ptr<RowItem> r : view_) |
e57ee799 | 164 | if (r->selected()) |
0dda6fe5 | 165 | r->drag(); |
e57ee799 JH |
166 | |
167 | selection_changed(); | |
168 | update(); | |
169 | } | |
170 | ||
e3374498 JH |
171 | void Header::mousePressEvent(QMouseEvent *event) |
172 | { | |
173 | assert(event); | |
f0c0b38f | 174 | |
8dbbc7f0 JH |
175 | mouse_down_point_ = event->pos(); |
176 | mouse_down_item_ = get_mouse_over_row_item(event->pos()); | |
f0c0b38f | 177 | |
e57ee799 JH |
178 | if (event->button() & Qt::LeftButton) |
179 | mouseLeftPressEvent(event); | |
180 | } | |
e3374498 | 181 | |
e57ee799 JH |
182 | void Header::mouseLeftReleaseEvent(QMouseEvent *event) |
183 | { | |
184 | assert(event); | |
54401bbb | 185 | |
e57ee799 JH |
186 | const bool ctrl_pressed = |
187 | QApplication::keyboardModifiers() & Qt::ControlModifier; | |
da2bebfb | 188 | |
e57ee799 JH |
189 | // Unselect everything if control is not pressed |
190 | const shared_ptr<RowItem> mouse_over = | |
eae6e30a | 191 | get_mouse_over_row_item(event->pos()); |
54401bbb | 192 | |
8dbbc7f0 | 193 | for (auto &r : view_) |
0dda6fe5 JH |
194 | r->drag_release(); |
195 | ||
8dbbc7f0 JH |
196 | if (dragging_) |
197 | view_.restack_all_row_items(); | |
7ff0145f | 198 | else |
e57ee799 JH |
199 | { |
200 | if (!ctrl_pressed) { | |
8dbbc7f0 JH |
201 | for (shared_ptr<RowItem> r : view_) |
202 | if (mouse_down_item_ != r) | |
e57ee799 JH |
203 | r->select(false); |
204 | ||
8dbbc7f0 JH |
205 | if (mouse_down_item_) |
206 | show_popup(mouse_down_item_); | |
e57ee799 | 207 | } |
07204819 JH |
208 | } |
209 | ||
8dbbc7f0 | 210 | dragging_ = false; |
e3374498 JH |
211 | } |
212 | ||
54401bbb JH |
213 | void Header::mouseReleaseEvent(QMouseEvent *event) |
214 | { | |
215 | assert(event); | |
e57ee799 JH |
216 | if (event->button() & Qt::LeftButton) |
217 | mouseLeftReleaseEvent(event); | |
f0c0b38f | 218 | |
8dbbc7f0 | 219 | mouse_down_item_ = nullptr; |
54401bbb JH |
220 | } |
221 | ||
a29bb7fb JH |
222 | void Header::mouseMoveEvent(QMouseEvent *event) |
223 | { | |
224 | assert(event); | |
8dbbc7f0 | 225 | mouse_point_ = event->pos(); |
54401bbb | 226 | |
728fcafc JH |
227 | if (!(event->buttons() & Qt::LeftButton)) |
228 | return; | |
229 | ||
8dbbc7f0 | 230 | if ((event->pos() - mouse_down_point_).manhattanLength() < |
728fcafc JH |
231 | QApplication::startDragDistance()) |
232 | return; | |
233 | ||
ee5f2a54 | 234 | // Check all the drag items share a common owner |
0dda6fe5 | 235 | RowItemOwner *item_owner = nullptr; |
8dbbc7f0 | 236 | for (shared_ptr<RowItem> r : view_) |
0dda6fe5 JH |
237 | if (r->dragging()) { |
238 | if (!item_owner) | |
239 | item_owner = r->owner(); | |
240 | else if(item_owner != r->owner()) | |
241 | return; | |
242 | } | |
243 | ||
244 | if (!item_owner) | |
245 | return; | |
da2bebfb | 246 | |
ee5f2a54 | 247 | // Do the drag |
8dbbc7f0 | 248 | dragging_ = true; |
54401bbb | 249 | |
8dbbc7f0 | 250 | const int delta = event->pos().y() - mouse_down_point_.y(); |
ee5f2a54 | 251 | |
8dbbc7f0 | 252 | for (std::shared_ptr<RowItem> r : view_) |
0dda6fe5 | 253 | if (r->dragging()) { |
be9e7b4b | 254 | r->force_to_v_offset(r->drag_point().y() + delta); |
ee5f2a54 JH |
255 | |
256 | // Ensure the trace is selected | |
0dda6fe5 | 257 | r->select(); |
ee5f2a54 | 258 | } |
54401bbb | 259 | |
7ff0145f JH |
260 | item_owner->restack_items(); |
261 | for (const auto &r : *item_owner) | |
262 | r->animate_to_layout_v_offset(); | |
ee5f2a54 JH |
263 | signals_moved(); |
264 | ||
a29bb7fb JH |
265 | update(); |
266 | } | |
267 | ||
e314eca4 | 268 | void Header::leaveEvent(QEvent*) |
a29bb7fb | 269 | { |
8dbbc7f0 | 270 | mouse_point_ = QPoint(-1, -1); |
a29bb7fb JH |
271 | update(); |
272 | } | |
273 | ||
49f8ff3f JH |
274 | void Header::contextMenuEvent(QContextMenuEvent *event) |
275 | { | |
8dbbc7f0 | 276 | const shared_ptr<RowItem> r = get_mouse_over_row_item(mouse_point_); |
a28878f4 JH |
277 | if (!r) |
278 | return; | |
279 | ||
73ba2d70 | 280 | QMenu *menu = r->create_context_menu(this); |
a28878f4 | 281 | if (!menu) |
73ba2d70 JH |
282 | menu = new QMenu(this); |
283 | ||
8dbbc7f0 | 284 | if (std::count_if(view_.begin(), view_.end(), item_selected) > 1) |
73ba2d70 JH |
285 | { |
286 | menu->addSeparator(); | |
287 | ||
288 | QAction *const group = new QAction(tr("Group"), this); | |
289 | QList<QKeySequence> shortcuts; | |
290 | shortcuts.append(QKeySequence(Qt::ControlModifier | Qt::Key_G)); | |
291 | group->setShortcuts(shortcuts); | |
292 | connect(group, SIGNAL(triggered()), this, SLOT(on_group())); | |
293 | menu->addAction(group); | |
294 | } | |
49f8ff3f | 295 | |
a28878f4 | 296 | menu->exec(event->globalPos()); |
b3b57abc JH |
297 | } |
298 | ||
5ed1adf5 JH |
299 | void Header::keyPressEvent(QKeyEvent *e) |
300 | { | |
301 | assert(e); | |
302 | ||
14ca8a8d | 303 | if (e->key() == Qt::Key_Delete) |
5ed1adf5 | 304 | { |
8dbbc7f0 | 305 | for (const shared_ptr<RowItem> r : view_) |
eae6e30a JH |
306 | if (r->selected()) |
307 | r->delete_pressed(); | |
5ed1adf5 | 308 | } |
14ca8a8d JH |
309 | else if (e->key() == Qt::Key_G && e->modifiers() == Qt::ControlModifier) |
310 | on_group(); | |
311 | else if (e->key() == Qt::Key_U && e->modifiers() == Qt::ControlModifier) | |
312 | on_ungroup(); | |
5ed1adf5 JH |
313 | } |
314 | ||
07204819 JH |
315 | void Header::on_signals_moved() |
316 | { | |
317 | update(); | |
318 | } | |
319 | ||
73ba2d70 JH |
320 | void Header::on_group() |
321 | { | |
322 | vector< shared_ptr<RowItem> > selected_items( | |
8dbbc7f0 JH |
323 | make_filter_iterator(item_selected, view_.begin(), view_.end()), |
324 | make_filter_iterator(item_selected, view_.end(), view_.end())); | |
73ba2d70 JH |
325 | stable_sort(selected_items.begin(), selected_items.end(), |
326 | [](const shared_ptr<RowItem> &a, const shared_ptr<RowItem> &b) { | |
327 | return a->visual_v_offset() < b->visual_v_offset(); }); | |
328 | ||
329 | shared_ptr<TraceGroup> group(new TraceGroup()); | |
330 | shared_ptr<RowItem> focus_item( | |
8dbbc7f0 | 331 | mouse_down_item_ ? mouse_down_item_ : selected_items.front()); |
73ba2d70 JH |
332 | |
333 | assert(focus_item); | |
334 | assert(focus_item->owner()); | |
335 | focus_item->owner()->add_child_item(group); | |
336 | ||
337 | // Set the group v_offset here before reparenting | |
338 | group->force_to_v_offset(focus_item->layout_v_offset() + | |
339 | focus_item->v_extents().first); | |
340 | ||
341 | for (size_t i = 0; i < selected_items.size(); i++) { | |
342 | const shared_ptr<RowItem> &r = selected_items[i]; | |
343 | assert(r->owner()); | |
344 | r->owner()->remove_child_item(r); | |
345 | group->add_child_item(r); | |
346 | ||
347 | // Put the items at 1-pixel offsets, so that restack will | |
348 | // stack them in the right order | |
349 | r->set_layout_v_offset(i); | |
350 | } | |
351 | } | |
352 | ||
bb419fdd JH |
353 | void Header::on_ungroup() |
354 | { | |
355 | bool restart; | |
356 | do { | |
357 | restart = false; | |
8dbbc7f0 | 358 | for (const shared_ptr<RowItem> r : view_) { |
bb419fdd JH |
359 | const shared_ptr<TraceGroup> tg = |
360 | dynamic_pointer_cast<TraceGroup>(r); | |
361 | if (tg && tg->selected()) { | |
362 | tg->ungroup(); | |
363 | restart = true; | |
364 | break; | |
365 | } | |
366 | } | |
367 | } while(restart); | |
368 | } | |
369 | ||
1d8dca91 JH |
370 | } // namespace view |
371 | } // namespace pv |