]> sigrok.org Git - pulseview.git/blame - pv/view/header.cpp
Added Signal::populate_popup_form
[pulseview.git] / pv / view / header.cpp
CommitLineData
1d8dca91 1/*
b3f22de0 2 * This file is part of the PulseView project.
1d8dca91
JH
3 *
4 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
5 *
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.
10 *
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.
15 *
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
19 */
20
21#include "header.h"
22#include "view.h"
23
8d634081 24#include "signal.h"
51e77110 25#include "../sigsession.h"
1d8dca91
JH
26
27#include <assert.h>
28
29#include <boost/foreach.hpp>
30
e3374498 31#include <QApplication>
49f8ff3f 32#include <QMenu>
a29bb7fb 33#include <QMouseEvent>
1d8dca91
JH
34#include <QPainter>
35#include <QRect>
36
569d1e41
JH
37#include <pv/widgets/popup.h>
38
1d8dca91
JH
39using namespace boost;
40using namespace std;
41
42namespace pv {
43namespace view {
44
45Header::Header(View &parent) :
728fcafc
JH
46 MarginWidget(parent),
47 _dragging(false)
1d8dca91 48{
a29bb7fb 49 setMouseTracking(true);
49f8ff3f 50
9e40e83d
JH
51 connect(&_view.session(), SIGNAL(signals_changed()),
52 this, SLOT(on_signals_changed()));
53
07204819
JH
54 connect(&_view, SIGNAL(signals_moved()),
55 this, SLOT(on_signals_moved()));
1d8dca91
JH
56}
57
38eeddea 58shared_ptr<Trace> Header::get_mouse_over_trace(const QPoint &pt)
e3374498
JH
59{
60 const int w = width();
38eeddea 61 const vector< shared_ptr<Trace> > traces(_view.get_traces());
e3374498 62
38eeddea 63 BOOST_FOREACH(const shared_ptr<Trace> t, traces)
e3374498 64 {
38eeddea 65 assert(t);
01fd3263 66 if (t->pt_in_label_rect(0, w, pt))
38eeddea 67 return t;
e3374498
JH
68 }
69
38eeddea 70 return shared_ptr<Trace>();
e3374498
JH
71}
72
a2ae0205
JH
73void Header::clear_selection()
74{
38eeddea
JH
75 const vector< shared_ptr<Trace> > traces(_view.get_traces());
76 BOOST_FOREACH(const shared_ptr<Trace> t, traces) {
77 assert(t);
78 t->select(false);
a2ae0205
JH
79 }
80
81 update();
82}
83
e314eca4 84void Header::paintEvent(QPaintEvent*)
1d8dca91
JH
85{
86 const int w = width();
38eeddea 87 const vector< shared_ptr<Trace> > traces(_view.get_traces());
1d8dca91
JH
88
89 QPainter painter(this);
90 painter.setRenderHint(QPainter::Antialiasing);
91
38eeddea
JH
92 const bool dragging = !_drag_traces.empty();
93 BOOST_FOREACH(const shared_ptr<Trace> t, traces)
1d8dca91 94 {
38eeddea 95 assert(t);
1d8dca91 96
38eeddea 97 const bool highlight = !dragging && t->pt_in_label_rect(
01fd3263
JH
98 0, w, _mouse_point);
99 t->paint_label(painter, w, highlight);
1d8dca91
JH
100 }
101
102 painter.end();
103}
104
e3374498
JH
105void Header::mousePressEvent(QMouseEvent *event)
106{
107 assert(event);
108
38eeddea 109 const vector< shared_ptr<Trace> > traces(_view.get_traces());
e3374498 110
333d5bbc 111 if (event->button() & Qt::LeftButton) {
54401bbb
JH
112 _mouse_down_point = event->pos();
113
da2bebfb 114 // Save the offsets of any signals which will be dragged
38eeddea
JH
115 BOOST_FOREACH(const shared_ptr<Trace> t, traces)
116 if (t->selected())
117 _drag_traces.push_back(
118 make_pair(t, t->get_v_offset()));
da2bebfb
JH
119 }
120
121 // Select the signal if it has been clicked
38eeddea
JH
122 const shared_ptr<Trace> mouse_over_trace =
123 get_mouse_over_trace(event->pos());
124 if (mouse_over_trace) {
125 if (mouse_over_trace->selected())
126 mouse_over_trace->select(false);
da2bebfb 127 else {
38eeddea 128 mouse_over_trace->select(true);
da2bebfb 129
333d5bbc 130 if (~QApplication::keyboardModifiers() &
07204819 131 Qt::ControlModifier)
38eeddea 132 _drag_traces.clear();
07204819 133
da2bebfb 134 // Add the signal to the drag list
333d5bbc 135 if (event->button() & Qt::LeftButton)
38eeddea
JH
136 _drag_traces.push_back(
137 make_pair(mouse_over_trace,
138 mouse_over_trace->get_v_offset()));
da2bebfb 139 }
54401bbb
JH
140 }
141
333d5bbc 142 if (~QApplication::keyboardModifiers() & Qt::ControlModifier) {
07204819
JH
143 // Unselect all other signals because the Ctrl is not
144 // pressed
38eeddea
JH
145 BOOST_FOREACH(const shared_ptr<Trace> t, traces)
146 if (t != mouse_over_trace)
147 t->select(false);
07204819
JH
148 }
149
b2a53645 150 selection_changed();
e3374498
JH
151 update();
152}
153
54401bbb
JH
154void Header::mouseReleaseEvent(QMouseEvent *event)
155{
569d1e41
JH
156 using pv::widgets::Popup;
157
54401bbb 158 assert(event);
333d5bbc 159 if (event->button() == Qt::LeftButton) {
569d1e41
JH
160 if (_dragging)
161 _view.normalize_layout();
162 else
163 {
164 const shared_ptr<Trace> mouse_over_trace =
165 get_mouse_over_trace(event->pos());
166 if (mouse_over_trace) {
167 Popup *const p =
168 mouse_over_trace->create_popup(&_view);
169 p->set_position(mapToGlobal(QPoint(width(),
170 mouse_over_trace->get_y())),
171 Popup::Right);
172 p->show();
173 }
174 }
175
728fcafc 176 _dragging = false;
38eeddea 177 _drag_traces.clear();
4b192962 178 }
54401bbb
JH
179}
180
a29bb7fb
JH
181void Header::mouseMoveEvent(QMouseEvent *event)
182{
183 assert(event);
184 _mouse_point = event->pos();
54401bbb 185
728fcafc
JH
186 if (!(event->buttons() & Qt::LeftButton))
187 return;
188
189 if ((event->pos() - _mouse_down_point).manhattanLength() <
190 QApplication::startDragDistance())
191 return;
192
54401bbb 193 // Move the signals if we are dragging
728fcafc
JH
194 if (!_drag_traces.empty())
195 {
196 _dragging = true;
197
54401bbb
JH
198 const int delta = event->pos().y() - _mouse_down_point.y();
199
38eeddea
JH
200 for (std::list<std::pair<boost::weak_ptr<Trace>,
201 int> >::iterator i = _drag_traces.begin();
202 i != _drag_traces.end(); i++) {
203 const boost::shared_ptr<Trace> trace((*i).first);
204 if (trace) {
da2bebfb 205 const int y = (*i).second + delta;
49153683
JH
206 const int y_snap =
207 ((y + View::SignalSnapGridSize / 2) /
208 View::SignalSnapGridSize) *
209 View::SignalSnapGridSize;
38eeddea 210 trace->set_v_offset(y_snap);
da2bebfb 211
38eeddea
JH
212 // Ensure the trace is selected
213 trace->select();
49153683 214 }
da2bebfb
JH
215
216 }
54401bbb
JH
217
218 signals_moved();
219 }
220
a29bb7fb
JH
221 update();
222}
223
e314eca4 224void Header::leaveEvent(QEvent*)
a29bb7fb
JH
225{
226 _mouse_point = QPoint(-1, -1);
227 update();
228}
229
49f8ff3f
JH
230void Header::contextMenuEvent(QContextMenuEvent *event)
231{
38eeddea 232 const shared_ptr<Trace> t = get_mouse_over_trace(_mouse_point);
49f8ff3f 233
9b6378f1
JH
234 if (t)
235 t->create_context_menu(this)->exec(event->globalPos());
b3b57abc
JH
236}
237
9e40e83d
JH
238void Header::on_signals_changed()
239{
38eeddea
JH
240 const vector< shared_ptr<Trace> > traces(_view.get_traces());
241 BOOST_FOREACH(shared_ptr<Trace> t, traces) {
242 assert(t);
243 connect(t.get(), SIGNAL(text_changed()), this, SLOT(update()));
9e40e83d
JH
244 }
245}
246
07204819
JH
247void Header::on_signals_moved()
248{
249 update();
250}
251
252
1d8dca91
JH
253} // namespace view
254} // namespace pv