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