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