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 using namespace boost;
43 Header::Header(View &parent) :
47 setMouseTracking(true);
49 connect(&_view.session(), SIGNAL(signals_changed()),
50 this, SLOT(on_signals_changed()));
52 connect(&_view, SIGNAL(signals_moved()),
53 this, SLOT(on_signals_moved()));
56 shared_ptr<Trace> Header::get_mouse_over_trace(const QPoint &pt)
58 const int w = width();
59 const vector< shared_ptr<Trace> > traces(_view.get_traces());
61 BOOST_FOREACH(const shared_ptr<Trace> t, traces)
64 if (t->pt_in_label_rect(0, w, pt))
68 return shared_ptr<Trace>();
71 void Header::clear_selection()
73 const vector< shared_ptr<Trace> > traces(_view.get_traces());
74 BOOST_FOREACH(const shared_ptr<Trace> t, traces) {
82 void Header::paintEvent(QPaintEvent*)
84 const int w = width();
85 const vector< shared_ptr<Trace> > traces(_view.get_traces());
87 QPainter painter(this);
88 painter.setRenderHint(QPainter::Antialiasing);
90 const bool dragging = !_drag_traces.empty();
91 BOOST_FOREACH(const shared_ptr<Trace> t, traces)
95 const bool highlight = !dragging && t->pt_in_label_rect(
97 t->paint_label(painter, w, highlight);
103 void Header::mousePressEvent(QMouseEvent *event)
107 const vector< shared_ptr<Trace> > traces(_view.get_traces());
109 if (event->button() & Qt::LeftButton) {
110 _mouse_down_point = event->pos();
112 // Save the offsets of any signals which will be dragged
113 BOOST_FOREACH(const shared_ptr<Trace> t, traces)
115 _drag_traces.push_back(
116 make_pair(t, t->get_v_offset()));
119 // Select the signal if it has been clicked
120 const shared_ptr<Trace> mouse_over_trace =
121 get_mouse_over_trace(event->pos());
122 if (mouse_over_trace) {
123 if (mouse_over_trace->selected())
124 mouse_over_trace->select(false);
126 mouse_over_trace->select(true);
128 if (~QApplication::keyboardModifiers() &
130 _drag_traces.clear();
132 // Add the signal to the drag list
133 if (event->button() & Qt::LeftButton)
134 _drag_traces.push_back(
135 make_pair(mouse_over_trace,
136 mouse_over_trace->get_v_offset()));
140 if (~QApplication::keyboardModifiers() & Qt::ControlModifier) {
141 // Unselect all other signals because the Ctrl is not
143 BOOST_FOREACH(const shared_ptr<Trace> t, traces)
144 if (t != mouse_over_trace)
152 void Header::mouseReleaseEvent(QMouseEvent *event)
155 if (event->button() == Qt::LeftButton) {
157 _drag_traces.clear();
158 _view.normalize_layout();
162 void Header::mouseMoveEvent(QMouseEvent *event)
165 _mouse_point = event->pos();
167 if (!(event->buttons() & Qt::LeftButton))
170 if ((event->pos() - _mouse_down_point).manhattanLength() <
171 QApplication::startDragDistance())
174 // Move the signals if we are dragging
175 if (!_drag_traces.empty())
179 const int delta = event->pos().y() - _mouse_down_point.y();
181 for (std::list<std::pair<boost::weak_ptr<Trace>,
182 int> >::iterator i = _drag_traces.begin();
183 i != _drag_traces.end(); i++) {
184 const boost::shared_ptr<Trace> trace((*i).first);
186 const int y = (*i).second + delta;
188 ((y + View::SignalSnapGridSize / 2) /
189 View::SignalSnapGridSize) *
190 View::SignalSnapGridSize;
191 trace->set_v_offset(y_snap);
193 // Ensure the trace is selected
205 void Header::leaveEvent(QEvent*)
207 _mouse_point = QPoint(-1, -1);
211 void Header::contextMenuEvent(QContextMenuEvent *event)
213 const shared_ptr<Trace> t = get_mouse_over_trace(_mouse_point);
216 t->create_context_menu(this)->exec(event->globalPos());
219 void Header::on_signals_changed()
221 const vector< shared_ptr<Trace> > traces(_view.get_traces());
222 BOOST_FOREACH(shared_ptr<Trace> t, traces) {
224 connect(t.get(), SIGNAL(text_changed()), this, SLOT(update()));
228 void Header::on_signals_moved()