]> sigrok.org Git - pulseview.git/blame - pv/view/header.cpp
DecodeTrace: Removed set_view
[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
JH
26
27#include <assert.h>
28
e3374498 29#include <QApplication>
49f8ff3f 30#include <QMenu>
a29bb7fb 31#include <QMouseEvent>
1d8dca91
JH
32#include <QPainter>
33#include <QRect>
34
569d1e41
JH
35#include <pv/widgets/popup.h>
36
819f4c25
JH
37using std::max;
38using std::make_pair;
39using std::pair;
f9abf97e 40using std::shared_ptr;
819f4c25 41using std::vector;
1d8dca91
JH
42
43namespace pv {
44namespace view {
45
d7c0ca4a 46const int Header::Padding = 12;
512bfc56 47const int Header::BaselineOffset = 5;
d7c0ca4a 48
1d8dca91 49Header::Header(View &parent) :
728fcafc
JH
50 MarginWidget(parent),
51 _dragging(false)
1d8dca91 52{
5ed1adf5 53 setFocusPolicy(Qt::ClickFocus);
a29bb7fb 54 setMouseTracking(true);
49f8ff3f 55
9e40e83d
JH
56 connect(&_view.session(), SIGNAL(signals_changed()),
57 this, SLOT(on_signals_changed()));
58
07204819
JH
59 connect(&_view, SIGNAL(signals_moved()),
60 this, SLOT(on_signals_moved()));
9f46d905
JH
61
62 // Trigger the initial event manually. The default device has signals
63 // which were created before this object came into being
64 on_signals_changed();
1d8dca91
JH
65}
66
d7c0ca4a
JH
67QSize Header::sizeHint() const
68{
69 int max_width = 0;
70
71 const vector< shared_ptr<Trace> > traces(_view.get_traces());
d9aecf1f 72 for (shared_ptr<Trace> t : traces) {
d7c0ca4a 73 assert(t);
6f98ca4c
JS
74
75 if (t->enabled()) {
1053d05c 76 max_width = max(max_width, (int)t->label_rect(0).width());
6f98ca4c 77 }
d7c0ca4a
JH
78 }
79
512bfc56 80 return QSize(max_width + Padding + BaselineOffset, 0);
d7c0ca4a
JH
81}
82
38eeddea 83shared_ptr<Trace> Header::get_mouse_over_trace(const QPoint &pt)
e3374498 84{
de0b46de 85 const int w = width() - BaselineOffset;
38eeddea 86 const vector< shared_ptr<Trace> > traces(_view.get_traces());
e3374498 87
d9aecf1f 88 for (const shared_ptr<Trace> t : traces)
e3374498 89 {
38eeddea 90 assert(t);
25201e88 91 if (t->enabled() && t->label_rect(w).contains(pt))
38eeddea 92 return t;
e3374498
JH
93 }
94
38eeddea 95 return shared_ptr<Trace>();
e3374498
JH
96}
97
a2ae0205
JH
98void Header::clear_selection()
99{
38eeddea 100 const vector< shared_ptr<Trace> > traces(_view.get_traces());
d9aecf1f 101 for (const shared_ptr<Trace> t : traces) {
38eeddea
JH
102 assert(t);
103 t->select(false);
a2ae0205
JH
104 }
105
106 update();
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;
38eeddea 115 const vector< shared_ptr<Trace> > traces(_view.get_traces());
1d8dca91
JH
116
117 QPainter painter(this);
118 painter.setRenderHint(QPainter::Antialiasing);
119
38eeddea 120 const bool dragging = !_drag_traces.empty();
d9aecf1f 121 for (const shared_ptr<Trace> t : traces)
1d8dca91 122 {
38eeddea 123 assert(t);
1d8dca91 124
25201e88
JH
125 const bool highlight = !dragging &&
126 t->label_rect(w).contains(_mouse_point);
01fd3263 127 t->paint_label(painter, w, highlight);
1d8dca91
JH
128 }
129
130 painter.end();
131}
132
e3374498
JH
133void Header::mousePressEvent(QMouseEvent *event)
134{
135 assert(event);
136
38eeddea 137 const vector< shared_ptr<Trace> > traces(_view.get_traces());
e3374498 138
333d5bbc 139 if (event->button() & Qt::LeftButton) {
54401bbb
JH
140 _mouse_down_point = event->pos();
141
da2bebfb 142 // Save the offsets of any signals which will be dragged
d9aecf1f 143 for (const shared_ptr<Trace> t : traces)
38eeddea
JH
144 if (t->selected())
145 _drag_traces.push_back(
146 make_pair(t, t->get_v_offset()));
da2bebfb
JH
147 }
148
149 // Select the signal if it has been clicked
38eeddea
JH
150 const shared_ptr<Trace> mouse_over_trace =
151 get_mouse_over_trace(event->pos());
152 if (mouse_over_trace) {
153 if (mouse_over_trace->selected())
154 mouse_over_trace->select(false);
da2bebfb 155 else {
38eeddea 156 mouse_over_trace->select(true);
da2bebfb 157
333d5bbc 158 if (~QApplication::keyboardModifiers() &
07204819 159 Qt::ControlModifier)
38eeddea 160 _drag_traces.clear();
07204819 161
da2bebfb 162 // Add the signal to the drag list
333d5bbc 163 if (event->button() & Qt::LeftButton)
38eeddea
JH
164 _drag_traces.push_back(
165 make_pair(mouse_over_trace,
166 mouse_over_trace->get_v_offset()));
da2bebfb 167 }
54401bbb
JH
168 }
169
333d5bbc 170 if (~QApplication::keyboardModifiers() & Qt::ControlModifier) {
07204819
JH
171 // Unselect all other signals because the Ctrl is not
172 // pressed
d9aecf1f 173 for (const shared_ptr<Trace> t : traces)
38eeddea
JH
174 if (t != mouse_over_trace)
175 t->select(false);
07204819
JH
176 }
177
b2a53645 178 selection_changed();
e3374498
JH
179 update();
180}
181
54401bbb
JH
182void Header::mouseReleaseEvent(QMouseEvent *event)
183{
569d1e41
JH
184 using pv::widgets::Popup;
185
54401bbb 186 assert(event);
333d5bbc 187 if (event->button() == Qt::LeftButton) {
569d1e41
JH
188 if (_dragging)
189 _view.normalize_layout();
190 else
191 {
192 const shared_ptr<Trace> mouse_over_trace =
193 get_mouse_over_trace(event->pos());
194 if (mouse_over_trace) {
512bfc56 195 const int w = width() - BaselineOffset;
569d1e41
JH
196 Popup *const p =
197 mouse_over_trace->create_popup(&_view);
512bfc56 198 p->set_position(mapToGlobal(QPoint(w,
569d1e41
JH
199 mouse_over_trace->get_y())),
200 Popup::Right);
201 p->show();
202 }
203 }
204
728fcafc 205 _dragging = false;
38eeddea 206 _drag_traces.clear();
4b192962 207 }
54401bbb
JH
208}
209
a29bb7fb
JH
210void Header::mouseMoveEvent(QMouseEvent *event)
211{
212 assert(event);
213 _mouse_point = event->pos();
54401bbb 214
728fcafc
JH
215 if (!(event->buttons() & Qt::LeftButton))
216 return;
217
218 if ((event->pos() - _mouse_down_point).manhattanLength() <
219 QApplication::startDragDistance())
220 return;
221
54401bbb 222 // Move the signals if we are dragging
728fcafc
JH
223 if (!_drag_traces.empty())
224 {
225 _dragging = true;
226
54401bbb
JH
227 const int delta = event->pos().y() - _mouse_down_point.y();
228
f46e495e 229 for (auto i = _drag_traces.begin(); i != _drag_traces.end(); i++) {
f9abf97e 230 const std::shared_ptr<Trace> trace((*i).first);
38eeddea 231 if (trace) {
da2bebfb 232 const int y = (*i).second + delta;
49153683
JH
233 const int y_snap =
234 ((y + View::SignalSnapGridSize / 2) /
235 View::SignalSnapGridSize) *
236 View::SignalSnapGridSize;
38eeddea 237 trace->set_v_offset(y_snap);
da2bebfb 238
38eeddea
JH
239 // Ensure the trace is selected
240 trace->select();
49153683 241 }
da2bebfb
JH
242
243 }
54401bbb
JH
244
245 signals_moved();
246 }
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{
38eeddea 259 const shared_ptr<Trace> t = get_mouse_over_trace(_mouse_point);
49f8ff3f 260
9b6378f1
JH
261 if (t)
262 t->create_context_menu(this)->exec(event->globalPos());
b3b57abc
JH
263}
264
5ed1adf5
JH
265void Header::keyPressEvent(QKeyEvent *e)
266{
267 assert(e);
268
269 switch (e->key())
270 {
271 case Qt::Key_Delete:
272 {
273 const vector< shared_ptr<Trace> > traces(_view.get_traces());
d9aecf1f 274 for (const shared_ptr<Trace> t : traces)
5ed1adf5
JH
275 if (t->selected())
276 t->delete_pressed();
277 break;
278 }
279 }
280}
281
9e40e83d
JH
282void Header::on_signals_changed()
283{
38eeddea 284 const vector< shared_ptr<Trace> > traces(_view.get_traces());
d9aecf1f 285 for (shared_ptr<Trace> t : traces) {
38eeddea 286 assert(t);
aca00b1e 287 connect(t.get(), SIGNAL(visibility_changed()),
6f98ca4c 288 this, SLOT(on_trace_changed()));
91e8bf08 289 connect(t.get(), SIGNAL(text_changed()),
6f98ca4c 290 this, SLOT(on_trace_changed()));
91e8bf08
JH
291 connect(t.get(), SIGNAL(colour_changed()),
292 this, SLOT(update()));
9e40e83d
JH
293 }
294}
295
07204819
JH
296void Header::on_signals_moved()
297{
298 update();
299}
300
6f98ca4c 301void Header::on_trace_changed()
d7c0ca4a
JH
302{
303 update();
304 geometry_updated();
305}
07204819 306
1d8dca91
JH
307} // namespace view
308} // namespace pv