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 shared_ptr<Trace> Header::get_mouse_over_trace(const QPoint &pt)
66 const int w = width();
67 const vector< shared_ptr<Trace> > traces(_view.get_traces());
69 const int v_offset = _view.v_offset();
70 BOOST_FOREACH(const shared_ptr<Trace> t, traces)
74 if (t->pt_in_label_rect(t->get_v_offset() - v_offset,
79 return shared_ptr<Trace>();
82 void Header::clear_selection()
84 const vector< shared_ptr<Trace> > traces(_view.get_traces());
85 BOOST_FOREACH(const shared_ptr<Trace> t, traces) {
93 void Header::paintEvent(QPaintEvent*)
95 const int w = width();
96 const vector< shared_ptr<Trace> > traces(_view.get_traces());
98 QPainter painter(this);
99 painter.setRenderHint(QPainter::Antialiasing);
101 const int v_offset = _view.v_offset();
102 const bool dragging = !_drag_traces.empty();
103 BOOST_FOREACH(const shared_ptr<Trace> t, traces)
107 const int y = t->get_v_offset() - v_offset;
108 const bool highlight = !dragging && t->pt_in_label_rect(
109 y, 0, w, _mouse_point);
110 t->paint_label(painter, y, w, highlight);
116 void Header::mousePressEvent(QMouseEvent *event)
120 const vector< shared_ptr<Trace> > traces(_view.get_traces());
122 if (event->button() & Qt::LeftButton) {
123 _mouse_down_point = event->pos();
125 // Save the offsets of any signals which will be dragged
126 BOOST_FOREACH(const shared_ptr<Trace> t, traces)
128 _drag_traces.push_back(
129 make_pair(t, t->get_v_offset()));
132 // Select the signal if it has been clicked
133 const shared_ptr<Trace> mouse_over_trace =
134 get_mouse_over_trace(event->pos());
135 if (mouse_over_trace) {
136 if (mouse_over_trace->selected())
137 mouse_over_trace->select(false);
139 mouse_over_trace->select(true);
141 if (~QApplication::keyboardModifiers() &
143 _drag_traces.clear();
145 // Add the signal to the drag list
146 if (event->button() & Qt::LeftButton)
147 _drag_traces.push_back(
148 make_pair(mouse_over_trace,
149 mouse_over_trace->get_v_offset()));
153 if (~QApplication::keyboardModifiers() & Qt::ControlModifier) {
154 // Unselect all other signals because the Ctrl is not
156 BOOST_FOREACH(const shared_ptr<Trace> t, traces)
157 if (t != mouse_over_trace)
165 void Header::mouseReleaseEvent(QMouseEvent *event)
168 if (event->button() == Qt::LeftButton) {
169 _drag_traces.clear();
170 _view.normalize_layout();
174 void Header::mouseMoveEvent(QMouseEvent *event)
177 _mouse_point = event->pos();
179 // Move the signals if we are dragging
180 if (!_drag_traces.empty()) {
181 const int delta = event->pos().y() - _mouse_down_point.y();
183 for (std::list<std::pair<boost::weak_ptr<Trace>,
184 int> >::iterator i = _drag_traces.begin();
185 i != _drag_traces.end(); i++) {
186 const boost::shared_ptr<Trace> trace((*i).first);
188 const int y = (*i).second + delta;
190 ((y + View::SignalSnapGridSize / 2) /
191 View::SignalSnapGridSize) *
192 View::SignalSnapGridSize;
193 trace->set_v_offset(y_snap);
195 // Ensure the trace is selected
207 void Header::leaveEvent(QEvent*)
209 _mouse_point = QPoint(-1, -1);
213 void Header::contextMenuEvent(QContextMenuEvent *event)
215 const shared_ptr<Trace> t = get_mouse_over_trace(_mouse_point);
221 menu.addAction(_action_set_name);
222 menu.addAction(_action_set_colour);
225 menu.exec(event->globalPos());
226 _context_trace.reset();
229 void Header::on_action_set_name_triggered()
233 shared_ptr<view::Trace> context_trace = _context_trace;
237 const QString new_label = QInputDialog::getText(this, tr("Set Name"),
238 tr("Name"), QLineEdit::Normal, context_trace->get_name(), &ok);
241 context_trace->set_name(new_label);
244 void Header::on_action_set_colour_triggered()
246 shared_ptr<view::Trace> context_trace = _context_trace;
250 const QColor new_colour = QColorDialog::getColor(
251 context_trace->get_colour(), this, tr("Set Colour"));
253 if (new_colour.isValid())
254 context_trace->set_colour(new_colour);
257 void Header::on_signals_changed()
259 const vector< shared_ptr<Trace> > traces(_view.get_traces());
260 BOOST_FOREACH(shared_ptr<Trace> t, traces) {
262 connect(t.get(), SIGNAL(text_changed()), this, SLOT(update()));
266 void Header::on_signals_moved()