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>
32 #include <QColorDialog>
33 #include <QInputDialog>
35 #include <QMouseEvent>
39 using namespace boost;
45 Header::Header(View &parent) :
47 _action_set_name(new QAction(tr("Set &Name..."), this)),
48 _action_set_colour(new QAction(tr("Set &Colour..."), this))
50 setMouseTracking(true);
52 connect(_action_set_name, SIGNAL(triggered()),
53 this, SLOT(on_action_set_name_triggered()));
54 connect(_action_set_colour, SIGNAL(triggered()),
55 this, SLOT(on_action_set_colour_triggered()));
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()));
64 boost::shared_ptr<pv::view::Signal> Header::get_mouse_over_signal(
67 const int w = width();
68 const vector< shared_ptr<Signal> > sigs(
69 _view.session().get_signals());
71 const int v_offset = _view.v_offset();
72 BOOST_FOREACH(const shared_ptr<Signal> s, sigs)
76 if (s->pt_in_label_rect(s->get_v_offset() - v_offset,
81 return shared_ptr<Signal>();
84 void Header::clear_selection()
86 const vector< shared_ptr<Signal> > sigs(
87 _view.session().get_signals());
88 BOOST_FOREACH(const shared_ptr<Signal> s, sigs) {
96 void Header::paintEvent(QPaintEvent*)
98 const int w = width();
99 const vector< shared_ptr<Signal> > sigs(
100 _view.session().get_signals());
102 QPainter painter(this);
103 painter.setRenderHint(QPainter::Antialiasing);
105 const int v_offset = _view.v_offset();
106 const bool dragging = !_drag_sigs.empty();
107 BOOST_FOREACH(const shared_ptr<Signal> s, sigs)
111 const int y = s->get_v_offset() - v_offset;
112 const bool highlight = !dragging && s->pt_in_label_rect(
113 y, 0, w, _mouse_point);
114 s->paint_label(painter, y, w, highlight);
120 void Header::mousePressEvent(QMouseEvent *event)
124 const vector< shared_ptr<Signal> > sigs(
125 _view.session().get_signals());
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<Signal> s, sigs)
133 _drag_sigs.push_back(
134 make_pair(s, s->get_v_offset()));
137 // Select the signal if it has been clicked
138 const shared_ptr<Signal> mouse_over_signal =
139 get_mouse_over_signal(event->pos());
140 if (mouse_over_signal) {
141 if (mouse_over_signal->selected())
142 mouse_over_signal->select(false);
144 mouse_over_signal->select(true);
146 if (~QApplication::keyboardModifiers() &
150 // Add the signal to the drag list
151 if (event->button() & Qt::LeftButton)
152 _drag_sigs.push_back(
153 make_pair(mouse_over_signal,
154 mouse_over_signal->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<Signal> s, sigs)
162 if (s != mouse_over_signal)
170 void Header::mouseReleaseEvent(QMouseEvent *event)
173 if (event->button() == Qt::LeftButton) {
175 _view.normalize_layout();
179 void Header::mouseMoveEvent(QMouseEvent *event)
182 _mouse_point = event->pos();
184 // Move the signals if we are dragging
185 if (!_drag_sigs.empty()) {
186 const int delta = event->pos().y() - _mouse_down_point.y();
188 for (std::list<std::pair<boost::weak_ptr<Signal>,
189 int> >::iterator i = _drag_sigs.begin();
190 i != _drag_sigs.end(); i++) {
191 const boost::shared_ptr<Signal> sig((*i).first);
193 const int y = (*i).second + delta;
195 ((y + View::SignalSnapGridSize / 2) /
196 View::SignalSnapGridSize) *
197 View::SignalSnapGridSize;
198 sig->set_v_offset(y_snap);
200 // Ensure the signal is selected
212 void Header::leaveEvent(QEvent*)
214 _mouse_point = QPoint(-1, -1);
218 void Header::contextMenuEvent(QContextMenuEvent *event)
220 const shared_ptr<Signal> s = get_mouse_over_signal(_mouse_point);
226 menu.addAction(_action_set_name);
227 menu.addAction(_action_set_colour);
230 menu.exec(event->globalPos());
231 _context_signal.reset();
234 void Header::on_action_set_name_triggered()
238 shared_ptr<view::Signal> context_signal = _context_signal;
242 const QString new_label = QInputDialog::getText(this, tr("Set Name"),
243 tr("Name"), QLineEdit::Normal, context_signal->get_name(), &ok);
246 context_signal->set_name(new_label);
249 void Header::on_action_set_colour_triggered()
251 shared_ptr<view::Signal> context_signal = _context_signal;
255 const QColor new_colour = QColorDialog::getColor(
256 context_signal->get_colour(), this, tr("Set Colour"));
258 if (new_colour.isValid())
259 context_signal->set_colour(new_colour);
262 void Header::on_signals_changed()
264 const vector< shared_ptr<Signal> > sigs(_view.session().get_signals());
265 BOOST_FOREACH(shared_ptr<Signal> s, sigs) {
267 connect(s.get(), SIGNAL(text_changed()), this, SLOT(update()));
271 void Header::on_signals_moved()