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