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 <QApplication>
31 #include <QMouseEvent>
35 #include <pv/widgets/popup.h>
40 using std::shared_ptr;
46 const int Header::Padding = 12;
48 Header::Header(View &parent) :
52 setFocusPolicy(Qt::ClickFocus);
53 setMouseTracking(true);
55 connect(&_view.session(), SIGNAL(signals_changed()),
56 this, SLOT(on_signals_changed()));
58 connect(&_view, SIGNAL(signals_moved()),
59 this, SLOT(on_signals_moved()));
61 // Trigger the initial event manually. The default device has signals
62 // which were created before this object came into being
66 QSize Header::sizeHint() const
70 const vector< shared_ptr<Trace> > traces(_view.get_traces());
71 for (shared_ptr<Trace> t : traces) {
75 max_width = max(max_width, (int)t->get_label_rect(0).width());
79 return QSize(max_width + Padding, 0);
82 shared_ptr<Trace> Header::get_mouse_over_trace(const QPoint &pt)
84 const int w = width();
85 const vector< shared_ptr<Trace> > traces(_view.get_traces());
87 for (const shared_ptr<Trace> t : traces)
90 if (t->pt_in_label_rect(0, w, pt))
94 return shared_ptr<Trace>();
97 void Header::clear_selection()
99 const vector< shared_ptr<Trace> > traces(_view.get_traces());
100 for (const shared_ptr<Trace> t : traces) {
108 void Header::paintEvent(QPaintEvent*)
110 const int w = width();
111 const vector< shared_ptr<Trace> > traces(_view.get_traces());
113 QPainter painter(this);
114 painter.setRenderHint(QPainter::Antialiasing);
116 const bool dragging = !_drag_traces.empty();
117 for (const shared_ptr<Trace> t : traces)
121 const bool highlight = !dragging && t->pt_in_label_rect(
123 t->paint_label(painter, w, highlight);
129 void Header::mousePressEvent(QMouseEvent *event)
133 const vector< shared_ptr<Trace> > traces(_view.get_traces());
135 if (event->button() & Qt::LeftButton) {
136 _mouse_down_point = event->pos();
138 // Save the offsets of any signals which will be dragged
139 for (const shared_ptr<Trace> t : traces)
141 _drag_traces.push_back(
142 make_pair(t, t->get_v_offset()));
145 // Select the signal if it has been clicked
146 const shared_ptr<Trace> mouse_over_trace =
147 get_mouse_over_trace(event->pos());
148 if (mouse_over_trace) {
149 if (mouse_over_trace->selected())
150 mouse_over_trace->select(false);
152 mouse_over_trace->select(true);
154 if (~QApplication::keyboardModifiers() &
156 _drag_traces.clear();
158 // Add the signal to the drag list
159 if (event->button() & Qt::LeftButton)
160 _drag_traces.push_back(
161 make_pair(mouse_over_trace,
162 mouse_over_trace->get_v_offset()));
166 if (~QApplication::keyboardModifiers() & Qt::ControlModifier) {
167 // Unselect all other signals because the Ctrl is not
169 for (const shared_ptr<Trace> t : traces)
170 if (t != mouse_over_trace)
178 void Header::mouseReleaseEvent(QMouseEvent *event)
180 using pv::widgets::Popup;
183 if (event->button() == Qt::LeftButton) {
185 _view.normalize_layout();
188 const shared_ptr<Trace> mouse_over_trace =
189 get_mouse_over_trace(event->pos());
190 if (mouse_over_trace) {
192 mouse_over_trace->create_popup(&_view);
193 p->set_position(mapToGlobal(QPoint(width(),
194 mouse_over_trace->get_y())),
201 _drag_traces.clear();
205 void Header::mouseMoveEvent(QMouseEvent *event)
208 _mouse_point = event->pos();
210 if (!(event->buttons() & Qt::LeftButton))
213 if ((event->pos() - _mouse_down_point).manhattanLength() <
214 QApplication::startDragDistance())
217 // Move the signals if we are dragging
218 if (!_drag_traces.empty())
222 const int delta = event->pos().y() - _mouse_down_point.y();
224 for (auto i = _drag_traces.begin(); i != _drag_traces.end(); i++) {
225 const std::shared_ptr<Trace> trace((*i).first);
227 const int y = (*i).second + delta;
229 ((y + View::SignalSnapGridSize / 2) /
230 View::SignalSnapGridSize) *
231 View::SignalSnapGridSize;
232 trace->set_v_offset(y_snap);
234 // Ensure the trace is selected
246 void Header::leaveEvent(QEvent*)
248 _mouse_point = QPoint(-1, -1);
252 void Header::contextMenuEvent(QContextMenuEvent *event)
254 const shared_ptr<Trace> t = get_mouse_over_trace(_mouse_point);
257 t->create_context_menu(this)->exec(event->globalPos());
260 void Header::keyPressEvent(QKeyEvent *e)
268 const vector< shared_ptr<Trace> > traces(_view.get_traces());
269 for (const shared_ptr<Trace> t : traces)
277 void Header::on_signals_changed()
279 const vector< shared_ptr<Trace> > traces(_view.get_traces());
280 for (shared_ptr<Trace> t : traces) {
282 connect(t.get(), SIGNAL(visibility_changed()),
283 this, SLOT(on_trace_changed()));
284 connect(t.get(), SIGNAL(text_changed()),
285 this, SLOT(on_trace_changed()));
286 connect(t.get(), SIGNAL(colour_changed()),
287 this, SLOT(update()));
291 void Header::on_signals_moved()
296 void Header::on_trace_changed()