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