]> sigrok.org Git - pulseview.git/blame - pv/view/header.cpp
View: Keep a list of owned traces in RowItemOwner
[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
e314eca4 111void Header::paintEvent(QPaintEvent*)
1d8dca91 112{
512bfc56
JS
113 // The trace labels are not drawn with the arrows exactly on the
114 // left edge of the widget, because then the selection shadow
115 // would be clipped away.
116 const int w = width() - BaselineOffset;
68b21a71
JH
117
118 vector< shared_ptr<RowItem> > row_items(_view.child_items());
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
e3374498
JH
139void Header::mousePressEvent(QMouseEvent *event)
140{
141 assert(event);
142
eae6e30a 143 const vector< shared_ptr<RowItem> > row_items(_view.child_items());
e3374498 144
333d5bbc 145 if (event->button() & Qt::LeftButton) {
54401bbb
JH
146 _mouse_down_point = event->pos();
147
da2bebfb 148 // Save the offsets of any signals which will be dragged
eae6e30a
JH
149 for (const shared_ptr<RowItem> r : row_items)
150 if (r->selected())
151 _drag_row_items.push_back(
152 make_pair(r, r->v_offset()));
da2bebfb
JH
153 }
154
155 // Select the signal if it has been clicked
eae6e30a
JH
156 const shared_ptr<RowItem> mouse_over_row_item =
157 get_mouse_over_row_item(event->pos());
158 if (mouse_over_row_item) {
159 if (mouse_over_row_item->selected())
160 mouse_over_row_item->select(false);
da2bebfb 161 else {
eae6e30a 162 mouse_over_row_item->select(true);
da2bebfb 163
333d5bbc 164 if (~QApplication::keyboardModifiers() &
07204819 165 Qt::ControlModifier)
eae6e30a 166 _drag_row_items.clear();
07204819 167
da2bebfb 168 // Add the signal to the drag list
333d5bbc 169 if (event->button() & Qt::LeftButton)
eae6e30a
JH
170 _drag_row_items.push_back(
171 make_pair(mouse_over_row_item,
172 mouse_over_row_item->v_offset()));
da2bebfb 173 }
54401bbb
JH
174 }
175
333d5bbc 176 if (~QApplication::keyboardModifiers() & Qt::ControlModifier) {
07204819
JH
177 // Unselect all other signals because the Ctrl is not
178 // pressed
eae6e30a
JH
179 for (const shared_ptr<RowItem> r : row_items)
180 if (r != mouse_over_row_item)
181 r->select(false);
07204819
JH
182 }
183
b2a53645 184 selection_changed();
e3374498
JH
185 update();
186}
187
54401bbb
JH
188void Header::mouseReleaseEvent(QMouseEvent *event)
189{
569d1e41
JH
190 using pv::widgets::Popup;
191
54401bbb 192 assert(event);
333d5bbc 193 if (event->button() == Qt::LeftButton) {
569d1e41
JH
194 if (_dragging)
195 _view.normalize_layout();
196 else
197 {
eae6e30a
JH
198 const shared_ptr<RowItem> mouse_over_row_item =
199 get_mouse_over_row_item(event->pos());
200 if (mouse_over_row_item) {
512bfc56 201 const int w = width() - BaselineOffset;
569d1e41 202 Popup *const p =
eae6e30a 203 mouse_over_row_item->create_popup(&_view);
512bfc56 204 p->set_position(mapToGlobal(QPoint(w,
eae6e30a 205 mouse_over_row_item->get_y())),
569d1e41
JH
206 Popup::Right);
207 p->show();
208 }
209 }
210
728fcafc 211 _dragging = false;
eae6e30a 212 _drag_row_items.clear();
4b192962 213 }
54401bbb
JH
214}
215
a29bb7fb
JH
216void Header::mouseMoveEvent(QMouseEvent *event)
217{
218 assert(event);
219 _mouse_point = event->pos();
54401bbb 220
728fcafc
JH
221 if (!(event->buttons() & Qt::LeftButton))
222 return;
223
224 if ((event->pos() - _mouse_down_point).manhattanLength() <
225 QApplication::startDragDistance())
226 return;
227
54401bbb 228 // Move the signals if we are dragging
eae6e30a 229 if (!_drag_row_items.empty())
728fcafc
JH
230 {
231 _dragging = true;
232
54401bbb
JH
233 const int delta = event->pos().y() - _mouse_down_point.y();
234
eae6e30a
JH
235 for (auto i = _drag_row_items.begin();
236 i != _drag_row_items.end(); i++) {
237 const std::shared_ptr<RowItem> row_item((*i).first);
238 if (row_item) {
da2bebfb 239 const int y = (*i).second + delta;
49153683
JH
240 const int y_snap =
241 ((y + View::SignalSnapGridSize / 2) /
242 View::SignalSnapGridSize) *
243 View::SignalSnapGridSize;
eae6e30a 244 row_item->set_v_offset(y_snap);
da2bebfb 245
38eeddea 246 // Ensure the trace is selected
eae6e30a 247 row_item->select();
49153683 248 }
da2bebfb
JH
249
250 }
54401bbb
JH
251
252 signals_moved();
253 }
254
a29bb7fb
JH
255 update();
256}
257
e314eca4 258void Header::leaveEvent(QEvent*)
a29bb7fb
JH
259{
260 _mouse_point = QPoint(-1, -1);
261 update();
262}
263
49f8ff3f
JH
264void Header::contextMenuEvent(QContextMenuEvent *event)
265{
eae6e30a 266 const shared_ptr<RowItem> r = get_mouse_over_row_item(_mouse_point);
49f8ff3f 267
eae6e30a
JH
268 if (r)
269 r->create_context_menu(this)->exec(event->globalPos());
b3b57abc
JH
270}
271
5ed1adf5
JH
272void Header::keyPressEvent(QKeyEvent *e)
273{
274 assert(e);
275
276 switch (e->key())
277 {
278 case Qt::Key_Delete:
279 {
eae6e30a
JH
280 const vector< shared_ptr<RowItem> > row_items(_view.child_items());
281 for (const shared_ptr<RowItem> r : row_items)
282 if (r->selected())
283 r->delete_pressed();
5ed1adf5
JH
284 break;
285 }
286 }
287}
288
9e40e83d
JH
289void Header::on_signals_changed()
290{
eae6e30a
JH
291 const vector< shared_ptr<RowItem> > row_items(_view.child_items());
292 for (shared_ptr<RowItem> r : row_items) {
293 assert(r);
294 connect(r.get(), SIGNAL(visibility_changed()),
6f98ca4c 295 this, SLOT(on_trace_changed()));
eae6e30a 296 connect(r.get(), SIGNAL(text_changed()),
6f98ca4c 297 this, SLOT(on_trace_changed()));
eae6e30a 298 connect(r.get(), SIGNAL(colour_changed()),
91e8bf08 299 this, SLOT(update()));
9e40e83d
JH
300 }
301}
302
07204819
JH
303void Header::on_signals_moved()
304{
305 update();
306}
307
6f98ca4c 308void Header::on_trace_changed()
d7c0ca4a
JH
309{
310 update();
311 geometry_updated();
312}
07204819 313
1d8dca91
JH
314} // namespace view
315} // namespace pv