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) {
75 max_width = max(max_width, (int)t->get_label_rect(0).width());
78 return QSize(max_width + Padding, 0);
81 shared_ptr<Trace> Header::get_mouse_over_trace(const QPoint &pt)
83 const int w = width();
84 const vector< shared_ptr<Trace> > traces(_view.get_traces());
86 BOOST_FOREACH(const shared_ptr<Trace> t, traces)
89 if (t->pt_in_label_rect(0, w, pt))
93 return shared_ptr<Trace>();
96 void Header::clear_selection()
98 const vector< shared_ptr<Trace> > traces(_view.get_traces());
99 BOOST_FOREACH(const shared_ptr<Trace> t, traces) {
107 void Header::paintEvent(QPaintEvent*)
109 const int w = width();
110 const vector< shared_ptr<Trace> > traces(_view.get_traces());
112 QPainter painter(this);
113 painter.setRenderHint(QPainter::Antialiasing);
115 const bool dragging = !_drag_traces.empty();
116 BOOST_FOREACH(const shared_ptr<Trace> t, traces)
120 const bool highlight = !dragging && t->pt_in_label_rect(
122 t->paint_label(painter, w, highlight);
128 void Header::mousePressEvent(QMouseEvent *event)
132 const vector< shared_ptr<Trace> > traces(_view.get_traces());
134 if (event->button() & Qt::LeftButton) {
135 _mouse_down_point = event->pos();
137 // Save the offsets of any signals which will be dragged
138 BOOST_FOREACH(const shared_ptr<Trace> t, traces)
140 _drag_traces.push_back(
141 make_pair(t, t->get_v_offset()));
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);
151 mouse_over_trace->select(true);
153 if (~QApplication::keyboardModifiers() &
155 _drag_traces.clear();
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()));
165 if (~QApplication::keyboardModifiers() & Qt::ControlModifier) {
166 // Unselect all other signals because the Ctrl is not
168 BOOST_FOREACH(const shared_ptr<Trace> t, traces)
169 if (t != mouse_over_trace)
177 void Header::mouseReleaseEvent(QMouseEvent *event)
179 using pv::widgets::Popup;
182 if (event->button() == Qt::LeftButton) {
184 _view.normalize_layout();
187 const shared_ptr<Trace> mouse_over_trace =
188 get_mouse_over_trace(event->pos());
189 if (mouse_over_trace) {
191 mouse_over_trace->create_popup(&_view);
192 p->set_position(mapToGlobal(QPoint(width(),
193 mouse_over_trace->get_y())),
200 _drag_traces.clear();
204 void Header::mouseMoveEvent(QMouseEvent *event)
207 _mouse_point = event->pos();
209 if (!(event->buttons() & Qt::LeftButton))
212 if ((event->pos() - _mouse_down_point).manhattanLength() <
213 QApplication::startDragDistance())
216 // Move the signals if we are dragging
217 if (!_drag_traces.empty())
221 const int delta = event->pos().y() - _mouse_down_point.y();
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);
228 const int y = (*i).second + delta;
230 ((y + View::SignalSnapGridSize / 2) /
231 View::SignalSnapGridSize) *
232 View::SignalSnapGridSize;
233 trace->set_v_offset(y_snap);
235 // Ensure the trace is selected
247 void Header::leaveEvent(QEvent*)
249 _mouse_point = QPoint(-1, -1);
253 void Header::contextMenuEvent(QContextMenuEvent *event)
255 const shared_ptr<Trace> t = get_mouse_over_trace(_mouse_point);
258 t->create_context_menu(this)->exec(event->globalPos());
261 void Header::keyPressEvent(QKeyEvent *e)
269 const vector< shared_ptr<Trace> > traces(_view.get_traces());
270 BOOST_FOREACH(const shared_ptr<Trace> t, traces)
278 void Header::on_signals_changed()
280 const vector< shared_ptr<Trace> > traces(_view.get_traces());
281 BOOST_FOREACH(shared_ptr<Trace> t, traces) {
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()));
292 void Header::on_signals_moved()
297 void Header::on_trace_text_changed()