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