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