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 const int Header::Padding = 12;
47 Header::Header(View &parent) :
51 setFocusPolicy(Qt::ClickFocus);
52 setMouseTracking(true);
54 connect(&_view.session(), SIGNAL(signals_changed()),
55 this, SLOT(on_signals_changed()));
57 connect(&_view, SIGNAL(signals_moved()),
58 this, SLOT(on_signals_moved()));
61 QSize Header::sizeHint() const
65 const vector< shared_ptr<Trace> > traces(_view.get_traces());
66 BOOST_FOREACH(shared_ptr<Trace> t, traces) {
68 max_width = max(max_width, (int)t->get_label_rect(0).width());
71 return QSize(max_width + Padding, 0);
74 shared_ptr<Trace> Header::get_mouse_over_trace(const QPoint &pt)
76 const int w = width();
77 const vector< shared_ptr<Trace> > traces(_view.get_traces());
79 BOOST_FOREACH(const shared_ptr<Trace> t, traces)
82 if (t->pt_in_label_rect(0, w, pt))
86 return shared_ptr<Trace>();
89 void Header::clear_selection()
91 const vector< shared_ptr<Trace> > traces(_view.get_traces());
92 BOOST_FOREACH(const shared_ptr<Trace> t, traces) {
100 void Header::paintEvent(QPaintEvent*)
102 const int w = width();
103 const vector< shared_ptr<Trace> > traces(_view.get_traces());
105 QPainter painter(this);
106 painter.setRenderHint(QPainter::Antialiasing);
108 const bool dragging = !_drag_traces.empty();
109 BOOST_FOREACH(const shared_ptr<Trace> t, traces)
113 const bool highlight = !dragging && t->pt_in_label_rect(
115 t->paint_label(painter, w, highlight);
121 void Header::mousePressEvent(QMouseEvent *event)
125 const vector< shared_ptr<Trace> > traces(_view.get_traces());
127 if (event->button() & Qt::LeftButton) {
128 _mouse_down_point = event->pos();
130 // Save the offsets of any signals which will be dragged
131 BOOST_FOREACH(const shared_ptr<Trace> t, traces)
133 _drag_traces.push_back(
134 make_pair(t, t->get_v_offset()));
137 // Select the signal if it has been clicked
138 const shared_ptr<Trace> mouse_over_trace =
139 get_mouse_over_trace(event->pos());
140 if (mouse_over_trace) {
141 if (mouse_over_trace->selected())
142 mouse_over_trace->select(false);
144 mouse_over_trace->select(true);
146 if (~QApplication::keyboardModifiers() &
148 _drag_traces.clear();
150 // Add the signal to the drag list
151 if (event->button() & Qt::LeftButton)
152 _drag_traces.push_back(
153 make_pair(mouse_over_trace,
154 mouse_over_trace->get_v_offset()));
158 if (~QApplication::keyboardModifiers() & Qt::ControlModifier) {
159 // Unselect all other signals because the Ctrl is not
161 BOOST_FOREACH(const shared_ptr<Trace> t, traces)
162 if (t != mouse_over_trace)
170 void Header::mouseReleaseEvent(QMouseEvent *event)
172 using pv::widgets::Popup;
175 if (event->button() == Qt::LeftButton) {
177 _view.normalize_layout();
180 const shared_ptr<Trace> mouse_over_trace =
181 get_mouse_over_trace(event->pos());
182 if (mouse_over_trace) {
184 mouse_over_trace->create_popup(&_view);
185 p->set_position(mapToGlobal(QPoint(width(),
186 mouse_over_trace->get_y())),
193 _drag_traces.clear();
197 void Header::mouseMoveEvent(QMouseEvent *event)
200 _mouse_point = event->pos();
202 if (!(event->buttons() & Qt::LeftButton))
205 if ((event->pos() - _mouse_down_point).manhattanLength() <
206 QApplication::startDragDistance())
209 // Move the signals if we are dragging
210 if (!_drag_traces.empty())
214 const int delta = event->pos().y() - _mouse_down_point.y();
216 for (std::list<std::pair<boost::weak_ptr<Trace>,
217 int> >::iterator i = _drag_traces.begin();
218 i != _drag_traces.end(); i++) {
219 const boost::shared_ptr<Trace> trace((*i).first);
221 const int y = (*i).second + delta;
223 ((y + View::SignalSnapGridSize / 2) /
224 View::SignalSnapGridSize) *
225 View::SignalSnapGridSize;
226 trace->set_v_offset(y_snap);
228 // Ensure the trace is selected
240 void Header::leaveEvent(QEvent*)
242 _mouse_point = QPoint(-1, -1);
246 void Header::contextMenuEvent(QContextMenuEvent *event)
248 const shared_ptr<Trace> t = get_mouse_over_trace(_mouse_point);
251 t->create_context_menu(this)->exec(event->globalPos());
254 void Header::keyPressEvent(QKeyEvent *e)
262 const vector< shared_ptr<Trace> > traces(_view.get_traces());
263 BOOST_FOREACH(const shared_ptr<Trace> t, traces)
271 void Header::on_signals_changed()
273 const vector< shared_ptr<Trace> > traces(_view.get_traces());
274 BOOST_FOREACH(shared_ptr<Trace> t, traces) {
276 connect(t.get(), SIGNAL(visibility_changed()),
277 this, SLOT(update()));
278 connect(t.get(), SIGNAL(text_changed()),
279 this, SLOT(on_trace_text_changed()));
280 connect(t.get(), SIGNAL(colour_changed()),
281 this, SLOT(update()));
285 void Header::on_signals_moved()
290 void Header::on_trace_text_changed()