]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * This file is part of the PulseView project. | |
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 | ||
24 | #include "signal.h" | |
25 | #include "../sigsession.h" | |
26 | ||
27 | #include <assert.h> | |
28 | ||
29 | #include <boost/foreach.hpp> | |
30 | ||
31 | #include <QApplication> | |
32 | #include <QMenu> | |
33 | #include <QMouseEvent> | |
34 | #include <QPainter> | |
35 | #include <QRect> | |
36 | ||
37 | #include <pv/widgets/popup.h> | |
38 | ||
39 | using boost::shared_ptr; | |
40 | using std::max; | |
41 | using std::make_pair; | |
42 | using std::pair; | |
43 | using std::vector; | |
44 | ||
45 | namespace pv { | |
46 | namespace view { | |
47 | ||
48 | const int Header::Padding = 12; | |
49 | ||
50 | Header::Header(View &parent) : | |
51 | MarginWidget(parent), | |
52 | _dragging(false) | |
53 | { | |
54 | setFocusPolicy(Qt::ClickFocus); | |
55 | setMouseTracking(true); | |
56 | ||
57 | connect(&_view.session(), SIGNAL(signals_changed()), | |
58 | this, SLOT(on_signals_changed())); | |
59 | ||
60 | connect(&_view, SIGNAL(signals_moved()), | |
61 | this, SLOT(on_signals_moved())); | |
62 | } | |
63 | ||
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 | ||
77 | shared_ptr<Trace> Header::get_mouse_over_trace(const QPoint &pt) | |
78 | { | |
79 | const int w = width(); | |
80 | const vector< shared_ptr<Trace> > traces(_view.get_traces()); | |
81 | ||
82 | BOOST_FOREACH(const shared_ptr<Trace> t, traces) | |
83 | { | |
84 | assert(t); | |
85 | if (t->pt_in_label_rect(0, w, pt)) | |
86 | return t; | |
87 | } | |
88 | ||
89 | return shared_ptr<Trace>(); | |
90 | } | |
91 | ||
92 | void Header::clear_selection() | |
93 | { | |
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); | |
98 | } | |
99 | ||
100 | update(); | |
101 | } | |
102 | ||
103 | void Header::paintEvent(QPaintEvent*) | |
104 | { | |
105 | const int w = width(); | |
106 | const vector< shared_ptr<Trace> > traces(_view.get_traces()); | |
107 | ||
108 | QPainter painter(this); | |
109 | painter.setRenderHint(QPainter::Antialiasing); | |
110 | ||
111 | const bool dragging = !_drag_traces.empty(); | |
112 | BOOST_FOREACH(const shared_ptr<Trace> t, traces) | |
113 | { | |
114 | assert(t); | |
115 | ||
116 | const bool highlight = !dragging && t->pt_in_label_rect( | |
117 | 0, w, _mouse_point); | |
118 | t->paint_label(painter, w, highlight); | |
119 | } | |
120 | ||
121 | painter.end(); | |
122 | } | |
123 | ||
124 | void Header::mousePressEvent(QMouseEvent *event) | |
125 | { | |
126 | assert(event); | |
127 | ||
128 | const vector< shared_ptr<Trace> > traces(_view.get_traces()); | |
129 | ||
130 | if (event->button() & Qt::LeftButton) { | |
131 | _mouse_down_point = event->pos(); | |
132 | ||
133 | // Save the offsets of any signals which will be dragged | |
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())); | |
138 | } | |
139 | ||
140 | // Select the signal if it has been clicked | |
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); | |
146 | else { | |
147 | mouse_over_trace->select(true); | |
148 | ||
149 | if (~QApplication::keyboardModifiers() & | |
150 | Qt::ControlModifier) | |
151 | _drag_traces.clear(); | |
152 | ||
153 | // Add the signal to the drag list | |
154 | if (event->button() & Qt::LeftButton) | |
155 | _drag_traces.push_back( | |
156 | make_pair(mouse_over_trace, | |
157 | mouse_over_trace->get_v_offset())); | |
158 | } | |
159 | } | |
160 | ||
161 | if (~QApplication::keyboardModifiers() & Qt::ControlModifier) { | |
162 | // Unselect all other signals because the Ctrl is not | |
163 | // pressed | |
164 | BOOST_FOREACH(const shared_ptr<Trace> t, traces) | |
165 | if (t != mouse_over_trace) | |
166 | t->select(false); | |
167 | } | |
168 | ||
169 | selection_changed(); | |
170 | update(); | |
171 | } | |
172 | ||
173 | void Header::mouseReleaseEvent(QMouseEvent *event) | |
174 | { | |
175 | using pv::widgets::Popup; | |
176 | ||
177 | assert(event); | |
178 | if (event->button() == Qt::LeftButton) { | |
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 | ||
195 | _dragging = false; | |
196 | _drag_traces.clear(); | |
197 | } | |
198 | } | |
199 | ||
200 | void Header::mouseMoveEvent(QMouseEvent *event) | |
201 | { | |
202 | assert(event); | |
203 | _mouse_point = event->pos(); | |
204 | ||
205 | if (!(event->buttons() & Qt::LeftButton)) | |
206 | return; | |
207 | ||
208 | if ((event->pos() - _mouse_down_point).manhattanLength() < | |
209 | QApplication::startDragDistance()) | |
210 | return; | |
211 | ||
212 | // Move the signals if we are dragging | |
213 | if (!_drag_traces.empty()) | |
214 | { | |
215 | _dragging = true; | |
216 | ||
217 | const int delta = event->pos().y() - _mouse_down_point.y(); | |
218 | ||
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) { | |
224 | const int y = (*i).second + delta; | |
225 | const int y_snap = | |
226 | ((y + View::SignalSnapGridSize / 2) / | |
227 | View::SignalSnapGridSize) * | |
228 | View::SignalSnapGridSize; | |
229 | trace->set_v_offset(y_snap); | |
230 | ||
231 | // Ensure the trace is selected | |
232 | trace->select(); | |
233 | } | |
234 | ||
235 | } | |
236 | ||
237 | signals_moved(); | |
238 | } | |
239 | ||
240 | update(); | |
241 | } | |
242 | ||
243 | void Header::leaveEvent(QEvent*) | |
244 | { | |
245 | _mouse_point = QPoint(-1, -1); | |
246 | update(); | |
247 | } | |
248 | ||
249 | void Header::contextMenuEvent(QContextMenuEvent *event) | |
250 | { | |
251 | const shared_ptr<Trace> t = get_mouse_over_trace(_mouse_point); | |
252 | ||
253 | if (t) | |
254 | t->create_context_menu(this)->exec(event->globalPos()); | |
255 | } | |
256 | ||
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 | ||
274 | void Header::on_signals_changed() | |
275 | { | |
276 | const vector< shared_ptr<Trace> > traces(_view.get_traces()); | |
277 | BOOST_FOREACH(shared_ptr<Trace> t, traces) { | |
278 | assert(t); | |
279 | connect(t.get(), SIGNAL(visibility_changed()), | |
280 | this, SLOT(update())); | |
281 | connect(t.get(), SIGNAL(text_changed()), | |
282 | this, SLOT(on_trace_text_changed())); | |
283 | connect(t.get(), SIGNAL(colour_changed()), | |
284 | this, SLOT(update())); | |
285 | } | |
286 | } | |
287 | ||
288 | void Header::on_signals_moved() | |
289 | { | |
290 | update(); | |
291 | } | |
292 | ||
293 | void Header::on_trace_text_changed() | |
294 | { | |
295 | update(); | |
296 | geometry_updated(); | |
297 | } | |
298 | ||
299 | } // namespace view | |
300 | } // namespace pv |