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) :
46 setMouseTracking(true);
48 connect(&_view.session(), SIGNAL(signals_changed()),
49 this, SLOT(on_signals_changed()));
51 connect(&_view, SIGNAL(signals_moved()),
52 this, SLOT(on_signals_moved()));
55 shared_ptr<Trace> Header::get_mouse_over_trace(const QPoint &pt)
57 const int w = width();
58 const vector< shared_ptr<Trace> > traces(_view.get_traces());
60 BOOST_FOREACH(const shared_ptr<Trace> t, traces)
63 if (t->pt_in_label_rect(0, w, pt))
67 return shared_ptr<Trace>();
70 void Header::clear_selection()
72 const vector< shared_ptr<Trace> > traces(_view.get_traces());
73 BOOST_FOREACH(const shared_ptr<Trace> t, traces) {
81 void Header::paintEvent(QPaintEvent*)
83 const int w = width();
84 const vector< shared_ptr<Trace> > traces(_view.get_traces());
86 QPainter painter(this);
87 painter.setRenderHint(QPainter::Antialiasing);
89 const bool dragging = !_drag_traces.empty();
90 BOOST_FOREACH(const shared_ptr<Trace> t, traces)
94 const bool highlight = !dragging && t->pt_in_label_rect(
96 t->paint_label(painter, w, highlight);
102 void Header::mousePressEvent(QMouseEvent *event)
106 const vector< shared_ptr<Trace> > traces(_view.get_traces());
108 if (event->button() & Qt::LeftButton) {
109 _mouse_down_point = event->pos();
111 // Save the offsets of any signals which will be dragged
112 BOOST_FOREACH(const shared_ptr<Trace> t, traces)
114 _drag_traces.push_back(
115 make_pair(t, t->get_v_offset()));
118 // Select the signal if it has been clicked
119 const shared_ptr<Trace> mouse_over_trace =
120 get_mouse_over_trace(event->pos());
121 if (mouse_over_trace) {
122 if (mouse_over_trace->selected())
123 mouse_over_trace->select(false);
125 mouse_over_trace->select(true);
127 if (~QApplication::keyboardModifiers() &
129 _drag_traces.clear();
131 // Add the signal to the drag list
132 if (event->button() & Qt::LeftButton)
133 _drag_traces.push_back(
134 make_pair(mouse_over_trace,
135 mouse_over_trace->get_v_offset()));
139 if (~QApplication::keyboardModifiers() & Qt::ControlModifier) {
140 // Unselect all other signals because the Ctrl is not
142 BOOST_FOREACH(const shared_ptr<Trace> t, traces)
143 if (t != mouse_over_trace)
151 void Header::mouseReleaseEvent(QMouseEvent *event)
154 if (event->button() == Qt::LeftButton) {
155 _drag_traces.clear();
156 _view.normalize_layout();
160 void Header::mouseMoveEvent(QMouseEvent *event)
163 _mouse_point = event->pos();
165 // Move the signals if we are dragging
166 if (!_drag_traces.empty()) {
167 const int delta = event->pos().y() - _mouse_down_point.y();
169 for (std::list<std::pair<boost::weak_ptr<Trace>,
170 int> >::iterator i = _drag_traces.begin();
171 i != _drag_traces.end(); i++) {
172 const boost::shared_ptr<Trace> trace((*i).first);
174 const int y = (*i).second + delta;
176 ((y + View::SignalSnapGridSize / 2) /
177 View::SignalSnapGridSize) *
178 View::SignalSnapGridSize;
179 trace->set_v_offset(y_snap);
181 // Ensure the trace is selected
193 void Header::leaveEvent(QEvent*)
195 _mouse_point = QPoint(-1, -1);
199 void Header::contextMenuEvent(QContextMenuEvent *event)
201 const shared_ptr<Trace> t = get_mouse_over_trace(_mouse_point);
204 t->create_context_menu(this)->exec(event->globalPos());
207 void Header::on_signals_changed()
209 const vector< shared_ptr<Trace> > traces(_view.get_traces());
210 BOOST_FOREACH(shared_ptr<Trace> t, traces) {
212 connect(t.get(), SIGNAL(text_changed()), this, SLOT(update()));
216 void Header::on_signals_moved()