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