]> sigrok.org Git - pulseview.git/blame - pv/view/header.cpp
Header: Fixed dragging glitches
[pulseview.git] / pv / view / header.cpp
CommitLineData
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
21#include "header.h"
22#include "view.h"
23
8d634081 24#include "signal.h"
51e77110 25#include "../sigsession.h"
1d8dca91 26
68b21a71
JH
27#include <cassert>
28#include <algorithm>
1d8dca91 29
e3374498 30#include <QApplication>
49f8ff3f 31#include <QMenu>
a29bb7fb 32#include <QMouseEvent>
1d8dca91
JH
33#include <QPainter>
34#include <QRect>
35
569d1e41
JH
36#include <pv/widgets/popup.h>
37
819f4c25
JH
38using std::max;
39using std::make_pair;
40using std::pair;
f9abf97e 41using std::shared_ptr;
68b21a71 42using std::stable_sort;
819f4c25 43using std::vector;
1d8dca91
JH
44
45namespace pv {
46namespace view {
47
d7c0ca4a 48const int Header::Padding = 12;
512bfc56 49const int Header::BaselineOffset = 5;
d7c0ca4a 50
1d8dca91 51Header::Header(View &parent) :
728fcafc
JH
52 MarginWidget(parent),
53 _dragging(false)
1d8dca91 54{
5ed1adf5 55 setFocusPolicy(Qt::ClickFocus);
a29bb7fb 56 setMouseTracking(true);
49f8ff3f 57
07204819
JH
58 connect(&_view, SIGNAL(signals_moved()),
59 this, SLOT(on_signals_moved()));
1d8dca91
JH
60}
61
d7c0ca4a
JH
62QSize Header::sizeHint() const
63{
b781f806 64 QRectF max_rect(-Padding, 0, Padding, 0);
aa59d5c2
JH
65 for (auto &i : _view)
66 if (i->enabled())
b781f806
JH
67 max_rect = max_rect.united(i->label_rect(0));
68 return QSize(max_rect.width() + Padding + BaselineOffset, 0);
d7c0ca4a
JH
69}
70
eae6e30a 71shared_ptr<RowItem> Header::get_mouse_over_row_item(const QPoint &pt)
e3374498 72{
de0b46de 73 const int w = width() - BaselineOffset;
aa59d5c2
JH
74 for (auto &i : _view)
75 if (i->enabled() && i->label_rect(w).contains(pt))
76 return i;
eae6e30a 77 return shared_ptr<RowItem>();
e3374498
JH
78}
79
a2ae0205
JH
80void Header::clear_selection()
81{
aa59d5c2
JH
82 for (auto &i : _view)
83 i->select(false);
a2ae0205
JH
84 update();
85}
86
14009012
JH
87void Header::signals_updated()
88{
89 for (shared_ptr<RowItem> r : _view) {
90 assert(r);
91 connect(r.get(), SIGNAL(appearance_changed()),
92 this, SLOT(on_trace_changed()));
93 }
94}
95
8b4802fb
JH
96void Header::show_popup(const shared_ptr<RowItem> &item)
97{
98 using pv::widgets::Popup;
99
100 Popup *const p = item->create_popup(&_view);
101 if (!p)
102 return;
103
104 const QPoint pt(width() - BaselineOffset, item->get_y());
105 p->set_position(mapToGlobal(pt), Popup::Right);
106 p->show();
107}
108
e314eca4 109void Header::paintEvent(QPaintEvent*)
1d8dca91 110{
512bfc56
JS
111 // The trace labels are not drawn with the arrows exactly on the
112 // left edge of the widget, because then the selection shadow
113 // would be clipped away.
114 const int w = width() - BaselineOffset;
68b21a71 115
aa59d5c2
JH
116 vector< shared_ptr<RowItem> > row_items(
117 _view.begin(), _view.end());
118
68b21a71
JH
119 stable_sort(row_items.begin(), row_items.end(),
120 [](const shared_ptr<RowItem> &a, const shared_ptr<RowItem> &b) {
121 return a->v_offset() < b->v_offset(); });
1d8dca91
JH
122
123 QPainter painter(this);
124 painter.setRenderHint(QPainter::Antialiasing);
125
eae6e30a
JH
126 const bool dragging = !_drag_row_items.empty();
127 for (const shared_ptr<RowItem> r : row_items)
1d8dca91 128 {
eae6e30a 129 assert(r);
1d8dca91 130
25201e88 131 const bool highlight = !dragging &&
eae6e30a
JH
132 r->label_rect(w).contains(_mouse_point);
133 r->paint_label(painter, w, highlight);
1d8dca91
JH
134 }
135
136 painter.end();
137}
138
e57ee799
JH
139void Header::mouseLeftPressEvent(QMouseEvent *event)
140{
141 const bool ctrl_pressed =
142 QApplication::keyboardModifiers() & Qt::ControlModifier;
143
144 // Clear selection if control is not pressed and this item is unselected
145 const shared_ptr<RowItem> mouse_over =
146 get_mouse_over_row_item(event->pos());
147 if (!ctrl_pressed && (!mouse_over || !mouse_over->selected()))
148 for (shared_ptr<RowItem> r : _view)
149 r->select(false);
150
151 // Set the signal selection state if the item has been clicked
152 if (mouse_over) {
153 if (ctrl_pressed)
154 mouse_over->select(!mouse_over->selected());
155 else
156 mouse_over->select(true);
157 }
158
159 // Save the offsets of any signals which will be dragged
160 _mouse_down_point = event->pos();
161 for (const shared_ptr<RowItem> r : _view)
162 if (r->selected())
163 _drag_row_items.push_back(
164 make_pair(r, r->v_offset()));
165
166 selection_changed();
167 update();
168}
169
e3374498
JH
170void Header::mousePressEvent(QMouseEvent *event)
171{
172 assert(event);
e57ee799
JH
173 if (event->button() & Qt::LeftButton)
174 mouseLeftPressEvent(event);
175}
e3374498 176
e57ee799
JH
177void Header::mouseLeftReleaseEvent(QMouseEvent *event)
178{
179 assert(event);
54401bbb 180
e57ee799
JH
181 const bool ctrl_pressed =
182 QApplication::keyboardModifiers() & Qt::ControlModifier;
da2bebfb 183
e57ee799
JH
184 // Unselect everything if control is not pressed
185 const shared_ptr<RowItem> mouse_over =
eae6e30a 186 get_mouse_over_row_item(event->pos());
54401bbb 187
e57ee799
JH
188 if (_dragging)
189 _view.normalize_layout();
190 else
191 {
192 if (!ctrl_pressed) {
193 for (shared_ptr<RowItem> r : _view)
194 if (mouse_over != r)
195 r->select(false);
196
197 if (mouse_over)
198 show_popup(mouse_over);
199 }
07204819
JH
200 }
201
e57ee799
JH
202 _dragging = false;
203 _drag_row_items.clear();
e3374498
JH
204}
205
54401bbb
JH
206void Header::mouseReleaseEvent(QMouseEvent *event)
207{
208 assert(event);
e57ee799
JH
209 if (event->button() & Qt::LeftButton)
210 mouseLeftReleaseEvent(event);
54401bbb
JH
211}
212
a29bb7fb
JH
213void Header::mouseMoveEvent(QMouseEvent *event)
214{
215 assert(event);
216 _mouse_point = event->pos();
54401bbb 217
728fcafc
JH
218 if (!(event->buttons() & Qt::LeftButton))
219 return;
220
221 if ((event->pos() - _mouse_down_point).manhattanLength() <
222 QApplication::startDragDistance())
223 return;
224
ee5f2a54
JH
225 // Check the list of dragging items is not empty
226 if (_drag_row_items.empty())
227 return;
728fcafc 228
ee5f2a54
JH
229 // Check all the drag items share a common owner
230 const shared_ptr<RowItem> first_row_item(
231 _drag_row_items.front().first);
232 for (const auto &r : _drag_row_items) {
233 const shared_ptr<RowItem> row_item(r.first);
234 assert(row_item);
54401bbb 235
ee5f2a54
JH
236 if (row_item->owner() != first_row_item->owner())
237 return;
238 }
da2bebfb 239
ee5f2a54
JH
240 // Do the drag
241 _dragging = true;
54401bbb 242
ee5f2a54
JH
243 const int delta = event->pos().y() - _mouse_down_point.y();
244
245 for (auto i = _drag_row_items.begin();
246 i != _drag_row_items.end(); i++) {
247 const std::shared_ptr<RowItem> row_item((*i).first);
248 if (row_item) {
249 const int y = (*i).second + delta;
250 row_item->set_v_offset(y);
251
252 // Ensure the trace is selected
253 row_item->select();
254 }
54401bbb
JH
255 }
256
ee5f2a54
JH
257 signals_moved();
258
a29bb7fb
JH
259 update();
260}
261
e314eca4 262void Header::leaveEvent(QEvent*)
a29bb7fb
JH
263{
264 _mouse_point = QPoint(-1, -1);
265 update();
266}
267
49f8ff3f
JH
268void Header::contextMenuEvent(QContextMenuEvent *event)
269{
eae6e30a 270 const shared_ptr<RowItem> r = get_mouse_over_row_item(_mouse_point);
a28878f4
JH
271 if (!r)
272 return;
273
274 QMenu *const menu = r->create_context_menu(this);
275 if (!menu)
276 return;
49f8ff3f 277
a28878f4 278 menu->exec(event->globalPos());
b3b57abc
JH
279}
280
5ed1adf5
JH
281void Header::keyPressEvent(QKeyEvent *e)
282{
283 assert(e);
284
285 switch (e->key())
286 {
287 case Qt::Key_Delete:
288 {
aa59d5c2 289 for (const shared_ptr<RowItem> r : _view)
eae6e30a
JH
290 if (r->selected())
291 r->delete_pressed();
5ed1adf5
JH
292 break;
293 }
294 }
295}
296
07204819
JH
297void Header::on_signals_moved()
298{
299 update();
300}
301
6f98ca4c 302void Header::on_trace_changed()
d7c0ca4a
JH
303{
304 update();
305 geometry_updated();
306}
07204819 307
1d8dca91
JH
308} // namespace view
309} // namespace pv