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 setFocusPolicy(Qt::ClickFocus);
50 setMouseTracking(true);
52 connect(&_view.session(), SIGNAL(signals_changed()),
53 this, SLOT(on_signals_changed()));
55 connect(&_view, SIGNAL(signals_moved()),
56 this, SLOT(on_signals_moved()));
59 shared_ptr<Trace> Header::get_mouse_over_trace(const QPoint &pt)
61 const int w = width();
62 const vector< shared_ptr<Trace> > traces(_view.get_traces());
64 BOOST_FOREACH(const shared_ptr<Trace> t, traces)
67 if (t->pt_in_label_rect(0, w, pt))
71 return shared_ptr<Trace>();
74 void Header::clear_selection()
76 const vector< shared_ptr<Trace> > traces(_view.get_traces());
77 BOOST_FOREACH(const shared_ptr<Trace> t, traces) {
85 void Header::paintEvent(QPaintEvent*)
87 const int w = width();
88 const vector< shared_ptr<Trace> > traces(_view.get_traces());
90 QPainter painter(this);
91 painter.setRenderHint(QPainter::Antialiasing);
93 const bool dragging = !_drag_traces.empty();
94 BOOST_FOREACH(const shared_ptr<Trace> t, traces)
98 const bool highlight = !dragging && t->pt_in_label_rect(
100 t->paint_label(painter, w, highlight);
106 void Header::mousePressEvent(QMouseEvent *event)
110 const vector< shared_ptr<Trace> > traces(_view.get_traces());
112 if (event->button() & Qt::LeftButton) {
113 _mouse_down_point = event->pos();
115 // Save the offsets of any signals which will be dragged
116 BOOST_FOREACH(const shared_ptr<Trace> t, traces)
118 _drag_traces.push_back(
119 make_pair(t, t->get_v_offset()));
122 // Select the signal if it has been clicked
123 const shared_ptr<Trace> mouse_over_trace =
124 get_mouse_over_trace(event->pos());
125 if (mouse_over_trace) {
126 if (mouse_over_trace->selected())
127 mouse_over_trace->select(false);
129 mouse_over_trace->select(true);
131 if (~QApplication::keyboardModifiers() &
133 _drag_traces.clear();
135 // Add the signal to the drag list
136 if (event->button() & Qt::LeftButton)
137 _drag_traces.push_back(
138 make_pair(mouse_over_trace,
139 mouse_over_trace->get_v_offset()));
143 if (~QApplication::keyboardModifiers() & Qt::ControlModifier) {
144 // Unselect all other signals because the Ctrl is not
146 BOOST_FOREACH(const shared_ptr<Trace> t, traces)
147 if (t != mouse_over_trace)
155 void Header::mouseReleaseEvent(QMouseEvent *event)
157 using pv::widgets::Popup;
160 if (event->button() == Qt::LeftButton) {
162 _view.normalize_layout();
165 const shared_ptr<Trace> mouse_over_trace =
166 get_mouse_over_trace(event->pos());
167 if (mouse_over_trace) {
169 mouse_over_trace->create_popup(&_view);
170 p->set_position(mapToGlobal(QPoint(width(),
171 mouse_over_trace->get_y())),
178 _drag_traces.clear();
182 void Header::mouseMoveEvent(QMouseEvent *event)
185 _mouse_point = event->pos();
187 if (!(event->buttons() & Qt::LeftButton))
190 if ((event->pos() - _mouse_down_point).manhattanLength() <
191 QApplication::startDragDistance())
194 // Move the signals if we are dragging
195 if (!_drag_traces.empty())
199 const int delta = event->pos().y() - _mouse_down_point.y();
201 for (std::list<std::pair<boost::weak_ptr<Trace>,
202 int> >::iterator i = _drag_traces.begin();
203 i != _drag_traces.end(); i++) {
204 const boost::shared_ptr<Trace> trace((*i).first);
206 const int y = (*i).second + delta;
208 ((y + View::SignalSnapGridSize / 2) /
209 View::SignalSnapGridSize) *
210 View::SignalSnapGridSize;
211 trace->set_v_offset(y_snap);
213 // Ensure the trace is selected
225 void Header::leaveEvent(QEvent*)
227 _mouse_point = QPoint(-1, -1);
231 void Header::contextMenuEvent(QContextMenuEvent *event)
233 const shared_ptr<Trace> t = get_mouse_over_trace(_mouse_point);
236 t->create_context_menu(this)->exec(event->globalPos());
239 void Header::keyPressEvent(QKeyEvent *e)
247 const vector< shared_ptr<Trace> > traces(_view.get_traces());
248 BOOST_FOREACH(const shared_ptr<Trace> t, traces)
256 void Header::on_signals_changed()
258 const vector< shared_ptr<Trace> > traces(_view.get_traces());
259 BOOST_FOREACH(shared_ptr<Trace> t, traces) {
261 connect(t.get(), SIGNAL(visibility_changed()),
262 this, SLOT(update()));
263 connect(t.get(), SIGNAL(text_changed()),
264 this, SLOT(update()));
265 connect(t.get(), SIGNAL(colour_changed()),
266 this, SLOT(update()));
270 void Header::on_signals_moved()