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