]>
Commit | Line | Data |
---|---|---|
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 | ||
819f4c25 JH |
39 | using boost::shared_ptr; |
40 | using std::max; | |
41 | using std::make_pair; | |
42 | using std::pair; | |
43 | using std::vector; | |
1d8dca91 JH |
44 | |
45 | namespace pv { | |
46 | namespace view { | |
47 | ||
d7c0ca4a JH |
48 | const int Header::Padding = 12; |
49 | ||
1d8dca91 | 50 | Header::Header(View &parent) : |
728fcafc JH |
51 | MarginWidget(parent), |
52 | _dragging(false) | |
1d8dca91 | 53 | { |
5ed1adf5 | 54 | setFocusPolicy(Qt::ClickFocus); |
a29bb7fb | 55 | setMouseTracking(true); |
49f8ff3f | 56 | |
9e40e83d JH |
57 | connect(&_view.session(), SIGNAL(signals_changed()), |
58 | this, SLOT(on_signals_changed())); | |
59 | ||
07204819 JH |
60 | connect(&_view, SIGNAL(signals_moved()), |
61 | this, SLOT(on_signals_moved())); | |
9f46d905 JH |
62 | |
63 | // Trigger the initial event manually. The default device has signals | |
64 | // which were created before this object came into being | |
65 | on_signals_changed(); | |
1d8dca91 JH |
66 | } |
67 | ||
d7c0ca4a JH |
68 | QSize Header::sizeHint() const |
69 | { | |
70 | int max_width = 0; | |
71 | ||
72 | const vector< shared_ptr<Trace> > traces(_view.get_traces()); | |
73 | BOOST_FOREACH(shared_ptr<Trace> t, traces) { | |
74 | assert(t); | |
6f98ca4c JS |
75 | |
76 | if (t->enabled()) { | |
77 | max_width = max(max_width, (int)t->get_label_rect(0).width()); | |
78 | } | |
d7c0ca4a JH |
79 | } |
80 | ||
81 | return QSize(max_width + Padding, 0); | |
82 | } | |
83 | ||
38eeddea | 84 | shared_ptr<Trace> Header::get_mouse_over_trace(const QPoint &pt) |
e3374498 JH |
85 | { |
86 | const int w = width(); | |
38eeddea | 87 | const vector< shared_ptr<Trace> > traces(_view.get_traces()); |
e3374498 | 88 | |
38eeddea | 89 | BOOST_FOREACH(const shared_ptr<Trace> t, traces) |
e3374498 | 90 | { |
38eeddea | 91 | assert(t); |
01fd3263 | 92 | if (t->pt_in_label_rect(0, w, pt)) |
38eeddea | 93 | return t; |
e3374498 JH |
94 | } |
95 | ||
38eeddea | 96 | return shared_ptr<Trace>(); |
e3374498 JH |
97 | } |
98 | ||
a2ae0205 JH |
99 | void Header::clear_selection() |
100 | { | |
38eeddea JH |
101 | const vector< shared_ptr<Trace> > traces(_view.get_traces()); |
102 | BOOST_FOREACH(const shared_ptr<Trace> t, traces) { | |
103 | assert(t); | |
104 | t->select(false); | |
a2ae0205 JH |
105 | } |
106 | ||
107 | update(); | |
108 | } | |
109 | ||
e314eca4 | 110 | void Header::paintEvent(QPaintEvent*) |
1d8dca91 JH |
111 | { |
112 | const int w = width(); | |
38eeddea | 113 | const vector< shared_ptr<Trace> > traces(_view.get_traces()); |
1d8dca91 JH |
114 | |
115 | QPainter painter(this); | |
116 | painter.setRenderHint(QPainter::Antialiasing); | |
117 | ||
38eeddea JH |
118 | const bool dragging = !_drag_traces.empty(); |
119 | BOOST_FOREACH(const shared_ptr<Trace> t, traces) | |
1d8dca91 | 120 | { |
38eeddea | 121 | assert(t); |
1d8dca91 | 122 | |
38eeddea | 123 | const bool highlight = !dragging && t->pt_in_label_rect( |
01fd3263 JH |
124 | 0, w, _mouse_point); |
125 | t->paint_label(painter, w, highlight); | |
1d8dca91 JH |
126 | } |
127 | ||
128 | painter.end(); | |
129 | } | |
130 | ||
e3374498 JH |
131 | void Header::mousePressEvent(QMouseEvent *event) |
132 | { | |
133 | assert(event); | |
134 | ||
38eeddea | 135 | const vector< shared_ptr<Trace> > traces(_view.get_traces()); |
e3374498 | 136 | |
333d5bbc | 137 | if (event->button() & Qt::LeftButton) { |
54401bbb JH |
138 | _mouse_down_point = event->pos(); |
139 | ||
da2bebfb | 140 | // Save the offsets of any signals which will be dragged |
38eeddea JH |
141 | BOOST_FOREACH(const shared_ptr<Trace> t, traces) |
142 | if (t->selected()) | |
143 | _drag_traces.push_back( | |
144 | make_pair(t, t->get_v_offset())); | |
da2bebfb JH |
145 | } |
146 | ||
147 | // Select the signal if it has been clicked | |
38eeddea JH |
148 | const shared_ptr<Trace> mouse_over_trace = |
149 | get_mouse_over_trace(event->pos()); | |
150 | if (mouse_over_trace) { | |
151 | if (mouse_over_trace->selected()) | |
152 | mouse_over_trace->select(false); | |
da2bebfb | 153 | else { |
38eeddea | 154 | mouse_over_trace->select(true); |
da2bebfb | 155 | |
333d5bbc | 156 | if (~QApplication::keyboardModifiers() & |
07204819 | 157 | Qt::ControlModifier) |
38eeddea | 158 | _drag_traces.clear(); |
07204819 | 159 | |
da2bebfb | 160 | // Add the signal to the drag list |
333d5bbc | 161 | if (event->button() & Qt::LeftButton) |
38eeddea JH |
162 | _drag_traces.push_back( |
163 | make_pair(mouse_over_trace, | |
164 | mouse_over_trace->get_v_offset())); | |
da2bebfb | 165 | } |
54401bbb JH |
166 | } |
167 | ||
333d5bbc | 168 | if (~QApplication::keyboardModifiers() & Qt::ControlModifier) { |
07204819 JH |
169 | // Unselect all other signals because the Ctrl is not |
170 | // pressed | |
38eeddea JH |
171 | BOOST_FOREACH(const shared_ptr<Trace> t, traces) |
172 | if (t != mouse_over_trace) | |
173 | t->select(false); | |
07204819 JH |
174 | } |
175 | ||
b2a53645 | 176 | selection_changed(); |
e3374498 JH |
177 | update(); |
178 | } | |
179 | ||
54401bbb JH |
180 | void Header::mouseReleaseEvent(QMouseEvent *event) |
181 | { | |
569d1e41 JH |
182 | using pv::widgets::Popup; |
183 | ||
54401bbb | 184 | assert(event); |
333d5bbc | 185 | if (event->button() == Qt::LeftButton) { |
569d1e41 JH |
186 | if (_dragging) |
187 | _view.normalize_layout(); | |
188 | else | |
189 | { | |
190 | const shared_ptr<Trace> mouse_over_trace = | |
191 | get_mouse_over_trace(event->pos()); | |
192 | if (mouse_over_trace) { | |
193 | Popup *const p = | |
194 | mouse_over_trace->create_popup(&_view); | |
195 | p->set_position(mapToGlobal(QPoint(width(), | |
196 | mouse_over_trace->get_y())), | |
197 | Popup::Right); | |
198 | p->show(); | |
199 | } | |
200 | } | |
201 | ||
728fcafc | 202 | _dragging = false; |
38eeddea | 203 | _drag_traces.clear(); |
4b192962 | 204 | } |
54401bbb JH |
205 | } |
206 | ||
a29bb7fb JH |
207 | void Header::mouseMoveEvent(QMouseEvent *event) |
208 | { | |
209 | assert(event); | |
210 | _mouse_point = event->pos(); | |
54401bbb | 211 | |
728fcafc JH |
212 | if (!(event->buttons() & Qt::LeftButton)) |
213 | return; | |
214 | ||
215 | if ((event->pos() - _mouse_down_point).manhattanLength() < | |
216 | QApplication::startDragDistance()) | |
217 | return; | |
218 | ||
54401bbb | 219 | // Move the signals if we are dragging |
728fcafc JH |
220 | if (!_drag_traces.empty()) |
221 | { | |
222 | _dragging = true; | |
223 | ||
54401bbb JH |
224 | const int delta = event->pos().y() - _mouse_down_point.y(); |
225 | ||
f46e495e | 226 | for (auto i = _drag_traces.begin(); i != _drag_traces.end(); i++) { |
38eeddea JH |
227 | const boost::shared_ptr<Trace> trace((*i).first); |
228 | if (trace) { | |
da2bebfb | 229 | const int y = (*i).second + delta; |
49153683 JH |
230 | const int y_snap = |
231 | ((y + View::SignalSnapGridSize / 2) / | |
232 | View::SignalSnapGridSize) * | |
233 | View::SignalSnapGridSize; | |
38eeddea | 234 | trace->set_v_offset(y_snap); |
da2bebfb | 235 | |
38eeddea JH |
236 | // Ensure the trace is selected |
237 | trace->select(); | |
49153683 | 238 | } |
da2bebfb JH |
239 | |
240 | } | |
54401bbb JH |
241 | |
242 | signals_moved(); | |
243 | } | |
244 | ||
a29bb7fb JH |
245 | update(); |
246 | } | |
247 | ||
e314eca4 | 248 | void Header::leaveEvent(QEvent*) |
a29bb7fb JH |
249 | { |
250 | _mouse_point = QPoint(-1, -1); | |
251 | update(); | |
252 | } | |
253 | ||
49f8ff3f JH |
254 | void Header::contextMenuEvent(QContextMenuEvent *event) |
255 | { | |
38eeddea | 256 | const shared_ptr<Trace> t = get_mouse_over_trace(_mouse_point); |
49f8ff3f | 257 | |
9b6378f1 JH |
258 | if (t) |
259 | t->create_context_menu(this)->exec(event->globalPos()); | |
b3b57abc JH |
260 | } |
261 | ||
5ed1adf5 JH |
262 | void Header::keyPressEvent(QKeyEvent *e) |
263 | { | |
264 | assert(e); | |
265 | ||
266 | switch (e->key()) | |
267 | { | |
268 | case Qt::Key_Delete: | |
269 | { | |
270 | const vector< shared_ptr<Trace> > traces(_view.get_traces()); | |
271 | BOOST_FOREACH(const shared_ptr<Trace> t, traces) | |
272 | if (t->selected()) | |
273 | t->delete_pressed(); | |
274 | break; | |
275 | } | |
276 | } | |
277 | } | |
278 | ||
9e40e83d JH |
279 | void Header::on_signals_changed() |
280 | { | |
38eeddea JH |
281 | const vector< shared_ptr<Trace> > traces(_view.get_traces()); |
282 | BOOST_FOREACH(shared_ptr<Trace> t, traces) { | |
283 | assert(t); | |
aca00b1e | 284 | connect(t.get(), SIGNAL(visibility_changed()), |
6f98ca4c | 285 | this, SLOT(on_trace_changed())); |
91e8bf08 | 286 | connect(t.get(), SIGNAL(text_changed()), |
6f98ca4c | 287 | this, SLOT(on_trace_changed())); |
91e8bf08 JH |
288 | connect(t.get(), SIGNAL(colour_changed()), |
289 | this, SLOT(update())); | |
9e40e83d JH |
290 | } |
291 | } | |
292 | ||
07204819 JH |
293 | void Header::on_signals_moved() |
294 | { | |
295 | update(); | |
296 | } | |
297 | ||
6f98ca4c | 298 | void Header::on_trace_changed() |
d7c0ca4a JH |
299 | { |
300 | update(); | |
301 | geometry_updated(); | |
302 | } | |
07204819 | 303 | |
1d8dca91 JH |
304 | } // namespace view |
305 | } // namespace pv |