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