]> sigrok.org Git - pulseview.git/blame - pv/view/header.cpp
RowItemOwner: Added depth() method
[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
8b4802fb
JH
87void Header::show_popup(const shared_ptr<RowItem> &item)
88{
89 using pv::widgets::Popup;
90
91 Popup *const p = item->create_popup(&_view);
92 if (!p)
93 return;
94
be9e7b4b 95 const QPoint pt(width() - BaselineOffset, item->get_visual_y());
8b4802fb
JH
96 p->set_position(mapToGlobal(pt), Popup::Right);
97 p->show();
98}
99
e314eca4 100void Header::paintEvent(QPaintEvent*)
1d8dca91 101{
512bfc56
JS
102 // The trace labels are not drawn with the arrows exactly on the
103 // left edge of the widget, because then the selection shadow
104 // would be clipped away.
105 const int w = width() - BaselineOffset;
68b21a71 106
aa59d5c2
JH
107 vector< shared_ptr<RowItem> > row_items(
108 _view.begin(), _view.end());
109
68b21a71
JH
110 stable_sort(row_items.begin(), row_items.end(),
111 [](const shared_ptr<RowItem> &a, const shared_ptr<RowItem> &b) {
be9e7b4b 112 return a->visual_v_offset() < b->visual_v_offset(); });
1d8dca91
JH
113
114 QPainter painter(this);
115 painter.setRenderHint(QPainter::Antialiasing);
116
eae6e30a 117 for (const shared_ptr<RowItem> r : row_items)
1d8dca91 118 {
eae6e30a 119 assert(r);
1d8dca91 120
0dda6fe5 121 const bool highlight = !_dragging &&
eae6e30a
JH
122 r->label_rect(w).contains(_mouse_point);
123 r->paint_label(painter, w, highlight);
1d8dca91
JH
124 }
125
126 painter.end();
127}
128
e57ee799
JH
129void Header::mouseLeftPressEvent(QMouseEvent *event)
130{
f0c0b38f
JH
131 (void)event;
132
e57ee799
JH
133 const bool ctrl_pressed =
134 QApplication::keyboardModifiers() & Qt::ControlModifier;
135
136 // Clear selection if control is not pressed and this item is unselected
f0c0b38f
JH
137 if ((!_mouse_down_item || !_mouse_down_item->selected()) &&
138 !ctrl_pressed)
e57ee799
JH
139 for (shared_ptr<RowItem> r : _view)
140 r->select(false);
141
142 // Set the signal selection state if the item has been clicked
f0c0b38f 143 if (_mouse_down_item) {
e57ee799 144 if (ctrl_pressed)
f0c0b38f 145 _mouse_down_item->select(!_mouse_down_item->selected());
e57ee799 146 else
f0c0b38f 147 _mouse_down_item->select(true);
e57ee799
JH
148 }
149
150 // Save the offsets of any signals which will be dragged
e57ee799
JH
151 for (const shared_ptr<RowItem> r : _view)
152 if (r->selected())
0dda6fe5 153 r->drag();
e57ee799
JH
154
155 selection_changed();
156 update();
157}
158
e3374498
JH
159void Header::mousePressEvent(QMouseEvent *event)
160{
161 assert(event);
f0c0b38f
JH
162
163 _mouse_down_point = event->pos();
164 _mouse_down_item = get_mouse_over_row_item(event->pos());
165
e57ee799
JH
166 if (event->button() & Qt::LeftButton)
167 mouseLeftPressEvent(event);
168}
e3374498 169
e57ee799
JH
170void Header::mouseLeftReleaseEvent(QMouseEvent *event)
171{
172 assert(event);
54401bbb 173
e57ee799
JH
174 const bool ctrl_pressed =
175 QApplication::keyboardModifiers() & Qt::ControlModifier;
da2bebfb 176
e57ee799
JH
177 // Unselect everything if control is not pressed
178 const shared_ptr<RowItem> mouse_over =
eae6e30a 179 get_mouse_over_row_item(event->pos());
54401bbb 180
0dda6fe5
JH
181 for (auto &r : _view)
182 r->drag_release();
183
4f82e4f6 184 if (!_dragging)
e57ee799
JH
185 {
186 if (!ctrl_pressed) {
187 for (shared_ptr<RowItem> r : _view)
f0c0b38f 188 if (_mouse_down_item != r)
e57ee799
JH
189 r->select(false);
190
f0c0b38f
JH
191 if (_mouse_down_item)
192 show_popup(_mouse_down_item);
e57ee799 193 }
07204819
JH
194 }
195
e57ee799 196 _dragging = false;
e3374498
JH
197}
198
54401bbb
JH
199void Header::mouseReleaseEvent(QMouseEvent *event)
200{
201 assert(event);
e57ee799
JH
202 if (event->button() & Qt::LeftButton)
203 mouseLeftReleaseEvent(event);
f0c0b38f
JH
204
205 _mouse_down_item = nullptr;
54401bbb
JH
206}
207
a29bb7fb
JH
208void Header::mouseMoveEvent(QMouseEvent *event)
209{
210 assert(event);
211 _mouse_point = event->pos();
54401bbb 212
728fcafc
JH
213 if (!(event->buttons() & Qt::LeftButton))
214 return;
215
216 if ((event->pos() - _mouse_down_point).manhattanLength() <
217 QApplication::startDragDistance())
218 return;
219
ee5f2a54 220 // Check all the drag items share a common owner
0dda6fe5
JH
221 RowItemOwner *item_owner = nullptr;
222 for (shared_ptr<RowItem> r : _view)
223 if (r->dragging()) {
224 if (!item_owner)
225 item_owner = r->owner();
226 else if(item_owner != r->owner())
227 return;
228 }
229
230 if (!item_owner)
231 return;
da2bebfb 232
ee5f2a54
JH
233 // Do the drag
234 _dragging = true;
54401bbb 235
ee5f2a54
JH
236 const int delta = event->pos().y() - _mouse_down_point.y();
237
0dda6fe5
JH
238 for (std::shared_ptr<RowItem> r : _view)
239 if (r->dragging()) {
be9e7b4b 240 r->force_to_v_offset(r->drag_point().y() + delta);
ee5f2a54
JH
241
242 // Ensure the trace is selected
0dda6fe5 243 r->select();
ee5f2a54 244 }
54401bbb 245
ee5f2a54
JH
246 signals_moved();
247
a29bb7fb
JH
248 update();
249}
250
e314eca4 251void Header::leaveEvent(QEvent*)
a29bb7fb
JH
252{
253 _mouse_point = QPoint(-1, -1);
254 update();
255}
256
49f8ff3f
JH
257void Header::contextMenuEvent(QContextMenuEvent *event)
258{
eae6e30a 259 const shared_ptr<RowItem> r = get_mouse_over_row_item(_mouse_point);
a28878f4
JH
260 if (!r)
261 return;
262
263 QMenu *const menu = r->create_context_menu(this);
264 if (!menu)
265 return;
49f8ff3f 266
a28878f4 267 menu->exec(event->globalPos());
b3b57abc
JH
268}
269
5ed1adf5
JH
270void Header::keyPressEvent(QKeyEvent *e)
271{
272 assert(e);
273
274 switch (e->key())
275 {
276 case Qt::Key_Delete:
277 {
aa59d5c2 278 for (const shared_ptr<RowItem> r : _view)
eae6e30a
JH
279 if (r->selected())
280 r->delete_pressed();
5ed1adf5
JH
281 break;
282 }
283 }
284}
285
07204819
JH
286void Header::on_signals_moved()
287{
288 update();
289}
290
1d8dca91
JH
291} // namespace view
292} // namespace pv