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