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