]> sigrok.org Git - pulseview.git/blame - pv/view/header.cpp
Header: Allow RowItems to have no popup
[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
9e40e83d
JH
58 connect(&_view.session(), SIGNAL(signals_changed()),
59 this, SLOT(on_signals_changed()));
60
07204819
JH
61 connect(&_view, SIGNAL(signals_moved()),
62 this, SLOT(on_signals_moved()));
9f46d905
JH
63
64 // Trigger the initial event manually. The default device has signals
65 // which were created before this object came into being
66 on_signals_changed();
1d8dca91
JH
67}
68
d7c0ca4a
JH
69QSize Header::sizeHint() const
70{
71 int max_width = 0;
72
eae6e30a
JH
73 const vector< shared_ptr<RowItem> > row_items(_view.child_items());
74 for (shared_ptr<RowItem> r : row_items) {
75 assert(r);
6f98ca4c 76
eae6e30a
JH
77 if (r->enabled()) {
78 max_width = max(max_width, (int)r->label_rect(0).width());
6f98ca4c 79 }
d7c0ca4a
JH
80 }
81
512bfc56 82 return QSize(max_width + Padding + BaselineOffset, 0);
d7c0ca4a
JH
83}
84
eae6e30a 85shared_ptr<RowItem> Header::get_mouse_over_row_item(const QPoint &pt)
e3374498 86{
de0b46de 87 const int w = width() - BaselineOffset;
eae6e30a 88 const vector< shared_ptr<RowItem> > row_items(_view.child_items());
e3374498 89
eae6e30a 90 for (const shared_ptr<RowItem> r : row_items)
e3374498 91 {
eae6e30a
JH
92 assert(r);
93 if (r->enabled() && r->label_rect(w).contains(pt))
94 return r;
e3374498
JH
95 }
96
eae6e30a 97 return shared_ptr<RowItem>();
e3374498
JH
98}
99
a2ae0205
JH
100void Header::clear_selection()
101{
eae6e30a
JH
102 const vector< shared_ptr<RowItem> > row_items(_view.child_items());
103 for (const shared_ptr<RowItem> r : row_items) {
104 assert(r);
105 r->select(false);
a2ae0205
JH
106 }
107
108 update();
109}
110
8b4802fb
JH
111void Header::show_popup(const shared_ptr<RowItem> &item)
112{
113 using pv::widgets::Popup;
114
115 Popup *const p = item->create_popup(&_view);
116 if (!p)
117 return;
118
119 const QPoint pt(width() - BaselineOffset, item->get_y());
120 p->set_position(mapToGlobal(pt), Popup::Right);
121 p->show();
122}
123
e314eca4 124void Header::paintEvent(QPaintEvent*)
1d8dca91 125{
512bfc56
JS
126 // The trace labels are not drawn with the arrows exactly on the
127 // left edge of the widget, because then the selection shadow
128 // would be clipped away.
129 const int w = width() - BaselineOffset;
68b21a71
JH
130
131 vector< shared_ptr<RowItem> > row_items(_view.child_items());
132 stable_sort(row_items.begin(), row_items.end(),
133 [](const shared_ptr<RowItem> &a, const shared_ptr<RowItem> &b) {
134 return a->v_offset() < b->v_offset(); });
1d8dca91
JH
135
136 QPainter painter(this);
137 painter.setRenderHint(QPainter::Antialiasing);
138
eae6e30a
JH
139 const bool dragging = !_drag_row_items.empty();
140 for (const shared_ptr<RowItem> r : row_items)
1d8dca91 141 {
eae6e30a 142 assert(r);
1d8dca91 143
25201e88 144 const bool highlight = !dragging &&
eae6e30a
JH
145 r->label_rect(w).contains(_mouse_point);
146 r->paint_label(painter, w, highlight);
1d8dca91
JH
147 }
148
149 painter.end();
150}
151
e3374498
JH
152void Header::mousePressEvent(QMouseEvent *event)
153{
154 assert(event);
155
eae6e30a 156 const vector< shared_ptr<RowItem> > row_items(_view.child_items());
e3374498 157
333d5bbc 158 if (event->button() & Qt::LeftButton) {
54401bbb
JH
159 _mouse_down_point = event->pos();
160
da2bebfb 161 // Save the offsets of any signals which will be dragged
eae6e30a
JH
162 for (const shared_ptr<RowItem> r : row_items)
163 if (r->selected())
164 _drag_row_items.push_back(
165 make_pair(r, r->v_offset()));
da2bebfb
JH
166 }
167
168 // Select the signal if it has been clicked
eae6e30a
JH
169 const shared_ptr<RowItem> mouse_over_row_item =
170 get_mouse_over_row_item(event->pos());
171 if (mouse_over_row_item) {
172 if (mouse_over_row_item->selected())
173 mouse_over_row_item->select(false);
da2bebfb 174 else {
eae6e30a 175 mouse_over_row_item->select(true);
da2bebfb 176
333d5bbc 177 if (~QApplication::keyboardModifiers() &
07204819 178 Qt::ControlModifier)
eae6e30a 179 _drag_row_items.clear();
07204819 180
da2bebfb 181 // Add the signal to the drag list
333d5bbc 182 if (event->button() & Qt::LeftButton)
eae6e30a
JH
183 _drag_row_items.push_back(
184 make_pair(mouse_over_row_item,
185 mouse_over_row_item->v_offset()));
da2bebfb 186 }
54401bbb
JH
187 }
188
333d5bbc 189 if (~QApplication::keyboardModifiers() & Qt::ControlModifier) {
07204819
JH
190 // Unselect all other signals because the Ctrl is not
191 // pressed
eae6e30a
JH
192 for (const shared_ptr<RowItem> r : row_items)
193 if (r != mouse_over_row_item)
194 r->select(false);
07204819
JH
195 }
196
b2a53645 197 selection_changed();
e3374498
JH
198 update();
199}
200
54401bbb
JH
201void Header::mouseReleaseEvent(QMouseEvent *event)
202{
203 assert(event);
333d5bbc 204 if (event->button() == Qt::LeftButton) {
569d1e41
JH
205 if (_dragging)
206 _view.normalize_layout();
207 else
208 {
eae6e30a
JH
209 const shared_ptr<RowItem> mouse_over_row_item =
210 get_mouse_over_row_item(event->pos());
8b4802fb
JH
211 if (mouse_over_row_item)
212 show_popup(mouse_over_row_item);
569d1e41
JH
213 }
214
728fcafc 215 _dragging = false;
eae6e30a 216 _drag_row_items.clear();
4b192962 217 }
54401bbb
JH
218}
219
a29bb7fb
JH
220void Header::mouseMoveEvent(QMouseEvent *event)
221{
222 assert(event);
223 _mouse_point = event->pos();
54401bbb 224
728fcafc
JH
225 if (!(event->buttons() & Qt::LeftButton))
226 return;
227
228 if ((event->pos() - _mouse_down_point).manhattanLength() <
229 QApplication::startDragDistance())
230 return;
231
54401bbb 232 // Move the signals if we are dragging
eae6e30a 233 if (!_drag_row_items.empty())
728fcafc
JH
234 {
235 _dragging = true;
236
54401bbb
JH
237 const int delta = event->pos().y() - _mouse_down_point.y();
238
eae6e30a
JH
239 for (auto i = _drag_row_items.begin();
240 i != _drag_row_items.end(); i++) {
241 const std::shared_ptr<RowItem> row_item((*i).first);
242 if (row_item) {
da2bebfb 243 const int y = (*i).second + delta;
49153683
JH
244 const int y_snap =
245 ((y + View::SignalSnapGridSize / 2) /
246 View::SignalSnapGridSize) *
247 View::SignalSnapGridSize;
eae6e30a 248 row_item->set_v_offset(y_snap);
da2bebfb 249
38eeddea 250 // Ensure the trace is selected
eae6e30a 251 row_item->select();
49153683 252 }
da2bebfb
JH
253
254 }
54401bbb
JH
255
256 signals_moved();
257 }
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);
49f8ff3f 271
eae6e30a
JH
272 if (r)
273 r->create_context_menu(this)->exec(event->globalPos());
b3b57abc
JH
274}
275
5ed1adf5
JH
276void Header::keyPressEvent(QKeyEvent *e)
277{
278 assert(e);
279
280 switch (e->key())
281 {
282 case Qt::Key_Delete:
283 {
eae6e30a
JH
284 const vector< shared_ptr<RowItem> > row_items(_view.child_items());
285 for (const shared_ptr<RowItem> r : row_items)
286 if (r->selected())
287 r->delete_pressed();
5ed1adf5
JH
288 break;
289 }
290 }
291}
292
9e40e83d
JH
293void Header::on_signals_changed()
294{
eae6e30a
JH
295 const vector< shared_ptr<RowItem> > row_items(_view.child_items());
296 for (shared_ptr<RowItem> r : row_items) {
297 assert(r);
298 connect(r.get(), SIGNAL(visibility_changed()),
6f98ca4c 299 this, SLOT(on_trace_changed()));
eae6e30a 300 connect(r.get(), SIGNAL(text_changed()),
6f98ca4c 301 this, SLOT(on_trace_changed()));
eae6e30a 302 connect(r.get(), SIGNAL(colour_changed()),
91e8bf08 303 this, SLOT(update()));
9e40e83d
JH
304 }
305}
306
07204819
JH
307void Header::on_signals_moved()
308{
309 update();
310}
311
6f98ca4c 312void Header::on_trace_changed()
d7c0ca4a
JH
313{
314 update();
315 geometry_updated();
316}
07204819 317
1d8dca91
JH
318} // namespace view
319} // namespace pv