]> sigrok.org Git - pulseview.git/blame - pv/view/header.cpp
Fixed Header to respect the minimum drag distance
[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
29#include <boost/foreach.hpp>
30
e3374498 31#include <QApplication>
49f8ff3f 32#include <QMenu>
a29bb7fb 33#include <QMouseEvent>
1d8dca91
JH
34#include <QPainter>
35#include <QRect>
36
37using namespace boost;
38using namespace std;
39
40namespace pv {
41namespace view {
42
43Header::Header(View &parent) :
728fcafc
JH
44 MarginWidget(parent),
45 _dragging(false)
1d8dca91 46{
a29bb7fb 47 setMouseTracking(true);
49f8ff3f 48
9e40e83d
JH
49 connect(&_view.session(), SIGNAL(signals_changed()),
50 this, SLOT(on_signals_changed()));
51
07204819
JH
52 connect(&_view, SIGNAL(signals_moved()),
53 this, SLOT(on_signals_moved()));
1d8dca91
JH
54}
55
38eeddea 56shared_ptr<Trace> Header::get_mouse_over_trace(const QPoint &pt)
e3374498
JH
57{
58 const int w = width();
38eeddea 59 const vector< shared_ptr<Trace> > traces(_view.get_traces());
e3374498 60
38eeddea 61 BOOST_FOREACH(const shared_ptr<Trace> t, traces)
e3374498 62 {
38eeddea 63 assert(t);
01fd3263 64 if (t->pt_in_label_rect(0, w, pt))
38eeddea 65 return t;
e3374498
JH
66 }
67
38eeddea 68 return shared_ptr<Trace>();
e3374498
JH
69}
70
a2ae0205
JH
71void Header::clear_selection()
72{
38eeddea
JH
73 const vector< shared_ptr<Trace> > traces(_view.get_traces());
74 BOOST_FOREACH(const shared_ptr<Trace> t, traces) {
75 assert(t);
76 t->select(false);
a2ae0205
JH
77 }
78
79 update();
80}
81
e314eca4 82void Header::paintEvent(QPaintEvent*)
1d8dca91
JH
83{
84 const int w = width();
38eeddea 85 const vector< shared_ptr<Trace> > traces(_view.get_traces());
1d8dca91
JH
86
87 QPainter painter(this);
88 painter.setRenderHint(QPainter::Antialiasing);
89
38eeddea
JH
90 const bool dragging = !_drag_traces.empty();
91 BOOST_FOREACH(const shared_ptr<Trace> t, traces)
1d8dca91 92 {
38eeddea 93 assert(t);
1d8dca91 94
38eeddea 95 const bool highlight = !dragging && t->pt_in_label_rect(
01fd3263
JH
96 0, w, _mouse_point);
97 t->paint_label(painter, w, highlight);
1d8dca91
JH
98 }
99
100 painter.end();
101}
102
e3374498
JH
103void Header::mousePressEvent(QMouseEvent *event)
104{
105 assert(event);
106
38eeddea 107 const vector< shared_ptr<Trace> > traces(_view.get_traces());
e3374498 108
333d5bbc 109 if (event->button() & Qt::LeftButton) {
54401bbb
JH
110 _mouse_down_point = event->pos();
111
da2bebfb 112 // Save the offsets of any signals which will be dragged
38eeddea
JH
113 BOOST_FOREACH(const shared_ptr<Trace> t, traces)
114 if (t->selected())
115 _drag_traces.push_back(
116 make_pair(t, t->get_v_offset()));
da2bebfb
JH
117 }
118
119 // Select the signal if it has been clicked
38eeddea
JH
120 const shared_ptr<Trace> mouse_over_trace =
121 get_mouse_over_trace(event->pos());
122 if (mouse_over_trace) {
123 if (mouse_over_trace->selected())
124 mouse_over_trace->select(false);
da2bebfb 125 else {
38eeddea 126 mouse_over_trace->select(true);
da2bebfb 127
333d5bbc 128 if (~QApplication::keyboardModifiers() &
07204819 129 Qt::ControlModifier)
38eeddea 130 _drag_traces.clear();
07204819 131
da2bebfb 132 // Add the signal to the drag list
333d5bbc 133 if (event->button() & Qt::LeftButton)
38eeddea
JH
134 _drag_traces.push_back(
135 make_pair(mouse_over_trace,
136 mouse_over_trace->get_v_offset()));
da2bebfb 137 }
54401bbb
JH
138 }
139
333d5bbc 140 if (~QApplication::keyboardModifiers() & Qt::ControlModifier) {
07204819
JH
141 // Unselect all other signals because the Ctrl is not
142 // pressed
38eeddea
JH
143 BOOST_FOREACH(const shared_ptr<Trace> t, traces)
144 if (t != mouse_over_trace)
145 t->select(false);
07204819
JH
146 }
147
b2a53645 148 selection_changed();
e3374498
JH
149 update();
150}
151
54401bbb
JH
152void Header::mouseReleaseEvent(QMouseEvent *event)
153{
154 assert(event);
333d5bbc 155 if (event->button() == Qt::LeftButton) {
728fcafc 156 _dragging = false;
38eeddea 157 _drag_traces.clear();
4b192962
JH
158 _view.normalize_layout();
159 }
54401bbb
JH
160}
161
a29bb7fb
JH
162void Header::mouseMoveEvent(QMouseEvent *event)
163{
164 assert(event);
165 _mouse_point = event->pos();
54401bbb 166
728fcafc
JH
167 if (!(event->buttons() & Qt::LeftButton))
168 return;
169
170 if ((event->pos() - _mouse_down_point).manhattanLength() <
171 QApplication::startDragDistance())
172 return;
173
54401bbb 174 // Move the signals if we are dragging
728fcafc
JH
175 if (!_drag_traces.empty())
176 {
177 _dragging = true;
178
54401bbb
JH
179 const int delta = event->pos().y() - _mouse_down_point.y();
180
38eeddea
JH
181 for (std::list<std::pair<boost::weak_ptr<Trace>,
182 int> >::iterator i = _drag_traces.begin();
183 i != _drag_traces.end(); i++) {
184 const boost::shared_ptr<Trace> trace((*i).first);
185 if (trace) {
da2bebfb 186 const int y = (*i).second + delta;
49153683
JH
187 const int y_snap =
188 ((y + View::SignalSnapGridSize / 2) /
189 View::SignalSnapGridSize) *
190 View::SignalSnapGridSize;
38eeddea 191 trace->set_v_offset(y_snap);
da2bebfb 192
38eeddea
JH
193 // Ensure the trace is selected
194 trace->select();
49153683 195 }
da2bebfb
JH
196
197 }
54401bbb
JH
198
199 signals_moved();
200 }
201
a29bb7fb
JH
202 update();
203}
204
e314eca4 205void Header::leaveEvent(QEvent*)
a29bb7fb
JH
206{
207 _mouse_point = QPoint(-1, -1);
208 update();
209}
210
49f8ff3f
JH
211void Header::contextMenuEvent(QContextMenuEvent *event)
212{
38eeddea 213 const shared_ptr<Trace> t = get_mouse_over_trace(_mouse_point);
49f8ff3f 214
9b6378f1
JH
215 if (t)
216 t->create_context_menu(this)->exec(event->globalPos());
b3b57abc
JH
217}
218
9e40e83d
JH
219void Header::on_signals_changed()
220{
38eeddea
JH
221 const vector< shared_ptr<Trace> > traces(_view.get_traces());
222 BOOST_FOREACH(shared_ptr<Trace> t, traces) {
223 assert(t);
224 connect(t.get(), SIGNAL(text_changed()), this, SLOT(update()));
9e40e83d
JH
225 }
226}
227
07204819
JH
228void Header::on_signals_moved()
229{
230 update();
231}
232
233
1d8dca91
JH
234} // namespace view
235} // namespace pv