2 * This file is part of the PulseView project.
4 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
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.
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.
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
25 #include "../sigsession.h"
29 #include <boost/foreach.hpp>
31 #include <QApplication>
33 #include <QMouseEvent>
37 #include <pv/widgets/popup.h>
39 using boost::shared_ptr;
48 const int Header::Padding = 12;
50 Header::Header(View &parent) :
54 setFocusPolicy(Qt::ClickFocus);
55 setMouseTracking(true);
57 connect(&_view.session(), SIGNAL(signals_changed()),
58 this, SLOT(on_signals_changed()));
60 connect(&_view, SIGNAL(signals_moved()),
61 this, SLOT(on_signals_moved()));
63 // Trigger the initial event manually. The default device has signals
64 // which were created before this object came into being
68 QSize Header::sizeHint() const
72 const vector< shared_ptr<Trace> > traces(_view.get_traces());
73 BOOST_FOREACH(shared_ptr<Trace> t, traces) {
77 max_width = max(max_width, (int)t->get_label_rect(0).width());
81 return QSize(max_width + Padding, 0);
84 shared_ptr<Trace> Header::get_mouse_over_trace(const QPoint &pt)
86 const int w = width();
87 const vector< shared_ptr<Trace> > traces(_view.get_traces());
89 BOOST_FOREACH(const shared_ptr<Trace> t, traces)
92 if (t->pt_in_label_rect(0, w, pt))
96 return shared_ptr<Trace>();
99 void Header::clear_selection()
101 const vector< shared_ptr<Trace> > traces(_view.get_traces());
102 BOOST_FOREACH(const shared_ptr<Trace> t, traces) {
110 void Header::paintEvent(QPaintEvent*)
112 const int w = width();
113 const vector< shared_ptr<Trace> > traces(_view.get_traces());
115 QPainter painter(this);
116 painter.setRenderHint(QPainter::Antialiasing);
118 const bool dragging = !_drag_traces.empty();
119 BOOST_FOREACH(const shared_ptr<Trace> t, traces)
123 const bool highlight = !dragging && t->pt_in_label_rect(
125 t->paint_label(painter, w, highlight);
131 void Header::mousePressEvent(QMouseEvent *event)
135 const vector< shared_ptr<Trace> > traces(_view.get_traces());
137 if (event->button() & Qt::LeftButton) {
138 _mouse_down_point = event->pos();
140 // Save the offsets of any signals which will be dragged
141 BOOST_FOREACH(const shared_ptr<Trace> t, traces)
143 _drag_traces.push_back(
144 make_pair(t, t->get_v_offset()));
147 // Select the signal if it has been clicked
148 const shared_ptr<Trace> mouse_over_trace =
149 get_mouse_over_trace(event->pos());
150 if (mouse_over_trace) {
151 if (mouse_over_trace->selected())
152 mouse_over_trace->select(false);
154 mouse_over_trace->select(true);
156 if (~QApplication::keyboardModifiers() &
158 _drag_traces.clear();
160 // Add the signal to the drag list
161 if (event->button() & Qt::LeftButton)
162 _drag_traces.push_back(
163 make_pair(mouse_over_trace,
164 mouse_over_trace->get_v_offset()));
168 if (~QApplication::keyboardModifiers() & Qt::ControlModifier) {
169 // Unselect all other signals because the Ctrl is not
171 BOOST_FOREACH(const shared_ptr<Trace> t, traces)
172 if (t != mouse_over_trace)
180 void Header::mouseReleaseEvent(QMouseEvent *event)
182 using pv::widgets::Popup;
185 if (event->button() == Qt::LeftButton) {
187 _view.normalize_layout();
190 const shared_ptr<Trace> mouse_over_trace =
191 get_mouse_over_trace(event->pos());
192 if (mouse_over_trace) {
194 mouse_over_trace->create_popup(&_view);
195 p->set_position(mapToGlobal(QPoint(width(),
196 mouse_over_trace->get_y())),
203 _drag_traces.clear();
207 void Header::mouseMoveEvent(QMouseEvent *event)
210 _mouse_point = event->pos();
212 if (!(event->buttons() & Qt::LeftButton))
215 if ((event->pos() - _mouse_down_point).manhattanLength() <
216 QApplication::startDragDistance())
219 // Move the signals if we are dragging
220 if (!_drag_traces.empty())
224 const int delta = event->pos().y() - _mouse_down_point.y();
226 for (auto i = _drag_traces.begin(); i != _drag_traces.end(); i++) {
227 const boost::shared_ptr<Trace> trace((*i).first);
229 const int y = (*i).second + delta;
231 ((y + View::SignalSnapGridSize / 2) /
232 View::SignalSnapGridSize) *
233 View::SignalSnapGridSize;
234 trace->set_v_offset(y_snap);
236 // Ensure the trace is selected
248 void Header::leaveEvent(QEvent*)
250 _mouse_point = QPoint(-1, -1);
254 void Header::contextMenuEvent(QContextMenuEvent *event)
256 const shared_ptr<Trace> t = get_mouse_over_trace(_mouse_point);
259 t->create_context_menu(this)->exec(event->globalPos());
262 void Header::keyPressEvent(QKeyEvent *e)
270 const vector< shared_ptr<Trace> > traces(_view.get_traces());
271 BOOST_FOREACH(const shared_ptr<Trace> t, traces)
279 void Header::on_signals_changed()
281 const vector< shared_ptr<Trace> > traces(_view.get_traces());
282 BOOST_FOREACH(shared_ptr<Trace> t, traces) {
284 connect(t.get(), SIGNAL(visibility_changed()),
285 this, SLOT(on_trace_changed()));
286 connect(t.get(), SIGNAL(text_changed()),
287 this, SLOT(on_trace_changed()));
288 connect(t.get(), SIGNAL(colour_changed()),
289 this, SLOT(update()));
293 void Header::on_signals_moved()
298 void Header::on_trace_changed()