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 namespace boost;
45 Header::Header(View &parent) :
49 setMouseTracking(true);
51 connect(&_view.session(), SIGNAL(signals_changed()),
52 this, SLOT(on_signals_changed()));
54 connect(&_view, SIGNAL(signals_moved()),
55 this, SLOT(on_signals_moved()));
58 shared_ptr<Trace> Header::get_mouse_over_trace(const QPoint &pt)
60 const int w = width();
61 const vector< shared_ptr<Trace> > traces(_view.get_traces());
63 BOOST_FOREACH(const shared_ptr<Trace> t, traces)
66 if (t->pt_in_label_rect(0, w, pt))
70 return shared_ptr<Trace>();
73 void Header::clear_selection()
75 const vector< shared_ptr<Trace> > traces(_view.get_traces());
76 BOOST_FOREACH(const shared_ptr<Trace> t, traces) {
84 void Header::paintEvent(QPaintEvent*)
86 const int w = width();
87 const vector< shared_ptr<Trace> > traces(_view.get_traces());
89 QPainter painter(this);
90 painter.setRenderHint(QPainter::Antialiasing);
92 const bool dragging = !_drag_traces.empty();
93 BOOST_FOREACH(const shared_ptr<Trace> t, traces)
97 const bool highlight = !dragging && t->pt_in_label_rect(
99 t->paint_label(painter, w, highlight);
105 void Header::mousePressEvent(QMouseEvent *event)
109 const vector< shared_ptr<Trace> > traces(_view.get_traces());
111 if (event->button() & Qt::LeftButton) {
112 _mouse_down_point = event->pos();
114 // Save the offsets of any signals which will be dragged
115 BOOST_FOREACH(const shared_ptr<Trace> t, traces)
117 _drag_traces.push_back(
118 make_pair(t, t->get_v_offset()));
121 // Select the signal if it has been clicked
122 const shared_ptr<Trace> mouse_over_trace =
123 get_mouse_over_trace(event->pos());
124 if (mouse_over_trace) {
125 if (mouse_over_trace->selected())
126 mouse_over_trace->select(false);
128 mouse_over_trace->select(true);
130 if (~QApplication::keyboardModifiers() &
132 _drag_traces.clear();
134 // Add the signal to the drag list
135 if (event->button() & Qt::LeftButton)
136 _drag_traces.push_back(
137 make_pair(mouse_over_trace,
138 mouse_over_trace->get_v_offset()));
142 if (~QApplication::keyboardModifiers() & Qt::ControlModifier) {
143 // Unselect all other signals because the Ctrl is not
145 BOOST_FOREACH(const shared_ptr<Trace> t, traces)
146 if (t != mouse_over_trace)
154 void Header::mouseReleaseEvent(QMouseEvent *event)
156 using pv::widgets::Popup;
159 if (event->button() == Qt::LeftButton) {
161 _view.normalize_layout();
164 const shared_ptr<Trace> mouse_over_trace =
165 get_mouse_over_trace(event->pos());
166 if (mouse_over_trace) {
168 mouse_over_trace->create_popup(&_view);
169 p->set_position(mapToGlobal(QPoint(width(),
170 mouse_over_trace->get_y())),
177 _drag_traces.clear();
181 void Header::mouseMoveEvent(QMouseEvent *event)
184 _mouse_point = event->pos();
186 if (!(event->buttons() & Qt::LeftButton))
189 if ((event->pos() - _mouse_down_point).manhattanLength() <
190 QApplication::startDragDistance())
193 // Move the signals if we are dragging
194 if (!_drag_traces.empty())
198 const int delta = event->pos().y() - _mouse_down_point.y();
200 for (std::list<std::pair<boost::weak_ptr<Trace>,
201 int> >::iterator i = _drag_traces.begin();
202 i != _drag_traces.end(); i++) {
203 const boost::shared_ptr<Trace> trace((*i).first);
205 const int y = (*i).second + delta;
207 ((y + View::SignalSnapGridSize / 2) /
208 View::SignalSnapGridSize) *
209 View::SignalSnapGridSize;
210 trace->set_v_offset(y_snap);
212 // Ensure the trace is selected
224 void Header::leaveEvent(QEvent*)
226 _mouse_point = QPoint(-1, -1);
230 void Header::contextMenuEvent(QContextMenuEvent *event)
232 const shared_ptr<Trace> t = get_mouse_over_trace(_mouse_point);
235 t->create_context_menu(this)->exec(event->globalPos());
238 void Header::on_signals_changed()
240 const vector< shared_ptr<Trace> > traces(_view.get_traces());
241 BOOST_FOREACH(shared_ptr<Trace> t, traces) {
243 connect(t.get(), SIGNAL(text_changed()),
244 this, SLOT(update()));
245 connect(t.get(), SIGNAL(colour_changed()),
246 this, SLOT(update()));
250 void Header::on_signals_moved()