]>
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 | for (auto &i : _view) | |
74 | if (i->enabled()) | |
75 | max_width = max(max_width, (int)i->label_rect(0).width()); | |
76 | ||
77 | return QSize(max_width + Padding + BaselineOffset, 0); | |
78 | } | |
79 | ||
80 | shared_ptr<RowItem> Header::get_mouse_over_row_item(const QPoint &pt) | |
81 | { | |
82 | const int w = width() - BaselineOffset; | |
83 | for (auto &i : _view) | |
84 | if (i->enabled() && i->label_rect(w).contains(pt)) | |
85 | return i; | |
86 | return shared_ptr<RowItem>(); | |
87 | } | |
88 | ||
89 | void Header::clear_selection() | |
90 | { | |
91 | for (auto &i : _view) | |
92 | i->select(false); | |
93 | update(); | |
94 | } | |
95 | ||
96 | void Header::show_popup(const shared_ptr<RowItem> &item) | |
97 | { | |
98 | using pv::widgets::Popup; | |
99 | ||
100 | Popup *const p = item->create_popup(&_view); | |
101 | if (!p) | |
102 | return; | |
103 | ||
104 | const QPoint pt(width() - BaselineOffset, item->get_y()); | |
105 | p->set_position(mapToGlobal(pt), Popup::Right); | |
106 | p->show(); | |
107 | } | |
108 | ||
109 | void Header::paintEvent(QPaintEvent*) | |
110 | { | |
111 | // The trace labels are not drawn with the arrows exactly on the | |
112 | // left edge of the widget, because then the selection shadow | |
113 | // would be clipped away. | |
114 | const int w = width() - BaselineOffset; | |
115 | ||
116 | vector< shared_ptr<RowItem> > row_items( | |
117 | _view.begin(), _view.end()); | |
118 | ||
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 | if (event->button() & Qt::LeftButton) { | |
144 | _mouse_down_point = event->pos(); | |
145 | ||
146 | // Save the offsets of any signals which will be dragged | |
147 | for (const shared_ptr<RowItem> r : _view) | |
148 | if (r->selected()) | |
149 | _drag_row_items.push_back( | |
150 | make_pair(r, r->v_offset())); | |
151 | } | |
152 | ||
153 | // Select the signal if it has been clicked | |
154 | const shared_ptr<RowItem> mouse_over_row_item = | |
155 | get_mouse_over_row_item(event->pos()); | |
156 | if (mouse_over_row_item) { | |
157 | if (mouse_over_row_item->selected()) | |
158 | mouse_over_row_item->select(false); | |
159 | else { | |
160 | mouse_over_row_item->select(true); | |
161 | ||
162 | if (~QApplication::keyboardModifiers() & | |
163 | Qt::ControlModifier) | |
164 | _drag_row_items.clear(); | |
165 | ||
166 | // Add the signal to the drag list | |
167 | if (event->button() & Qt::LeftButton) | |
168 | _drag_row_items.push_back( | |
169 | make_pair(mouse_over_row_item, | |
170 | mouse_over_row_item->v_offset())); | |
171 | } | |
172 | } | |
173 | ||
174 | if (~QApplication::keyboardModifiers() & Qt::ControlModifier) { | |
175 | // Unselect all other signals because the Ctrl is not | |
176 | // pressed | |
177 | for (const shared_ptr<RowItem> r : _view) | |
178 | if (r != mouse_over_row_item) | |
179 | r->select(false); | |
180 | } | |
181 | ||
182 | selection_changed(); | |
183 | update(); | |
184 | } | |
185 | ||
186 | void Header::mouseReleaseEvent(QMouseEvent *event) | |
187 | { | |
188 | assert(event); | |
189 | if (event->button() == Qt::LeftButton) { | |
190 | if (_dragging) | |
191 | _view.normalize_layout(); | |
192 | else | |
193 | { | |
194 | const shared_ptr<RowItem> mouse_over_row_item = | |
195 | get_mouse_over_row_item(event->pos()); | |
196 | if (mouse_over_row_item) | |
197 | show_popup(mouse_over_row_item); | |
198 | } | |
199 | ||
200 | _dragging = false; | |
201 | _drag_row_items.clear(); | |
202 | } | |
203 | } | |
204 | ||
205 | void Header::mouseMoveEvent(QMouseEvent *event) | |
206 | { | |
207 | assert(event); | |
208 | _mouse_point = event->pos(); | |
209 | ||
210 | if (!(event->buttons() & Qt::LeftButton)) | |
211 | return; | |
212 | ||
213 | if ((event->pos() - _mouse_down_point).manhattanLength() < | |
214 | QApplication::startDragDistance()) | |
215 | return; | |
216 | ||
217 | // Move the signals if we are dragging | |
218 | if (!_drag_row_items.empty()) | |
219 | { | |
220 | _dragging = true; | |
221 | ||
222 | const int delta = event->pos().y() - _mouse_down_point.y(); | |
223 | ||
224 | for (auto i = _drag_row_items.begin(); | |
225 | i != _drag_row_items.end(); i++) { | |
226 | const std::shared_ptr<RowItem> row_item((*i).first); | |
227 | if (row_item) { | |
228 | const int y = (*i).second + delta; | |
229 | const int y_snap = | |
230 | ((y + View::SignalSnapGridSize / 2) / | |
231 | View::SignalSnapGridSize) * | |
232 | View::SignalSnapGridSize; | |
233 | row_item->set_v_offset(y_snap); | |
234 | ||
235 | // Ensure the trace is selected | |
236 | row_item->select(); | |
237 | } | |
238 | ||
239 | } | |
240 | ||
241 | signals_moved(); | |
242 | } | |
243 | ||
244 | update(); | |
245 | } | |
246 | ||
247 | void Header::leaveEvent(QEvent*) | |
248 | { | |
249 | _mouse_point = QPoint(-1, -1); | |
250 | update(); | |
251 | } | |
252 | ||
253 | void Header::contextMenuEvent(QContextMenuEvent *event) | |
254 | { | |
255 | const shared_ptr<RowItem> r = get_mouse_over_row_item(_mouse_point); | |
256 | if (!r) | |
257 | return; | |
258 | ||
259 | QMenu *const menu = r->create_context_menu(this); | |
260 | if (!menu) | |
261 | return; | |
262 | ||
263 | menu->exec(event->globalPos()); | |
264 | } | |
265 | ||
266 | void Header::keyPressEvent(QKeyEvent *e) | |
267 | { | |
268 | assert(e); | |
269 | ||
270 | switch (e->key()) | |
271 | { | |
272 | case Qt::Key_Delete: | |
273 | { | |
274 | for (const shared_ptr<RowItem> r : _view) | |
275 | if (r->selected()) | |
276 | r->delete_pressed(); | |
277 | break; | |
278 | } | |
279 | } | |
280 | } | |
281 | ||
282 | void Header::on_signals_changed() | |
283 | { | |
284 | for (shared_ptr<RowItem> r : _view) { | |
285 | assert(r); | |
286 | connect(r.get(), SIGNAL(visibility_changed()), | |
287 | this, SLOT(on_trace_changed())); | |
288 | connect(r.get(), SIGNAL(text_changed()), | |
289 | this, SLOT(on_trace_changed())); | |
290 | connect(r.get(), SIGNAL(colour_changed()), | |
291 | this, SLOT(update())); | |
292 | } | |
293 | } | |
294 | ||
295 | void Header::on_signals_moved() | |
296 | { | |
297 | update(); | |
298 | } | |
299 | ||
300 | void Header::on_trace_changed() | |
301 | { | |
302 | update(); | |
303 | geometry_updated(); | |
304 | } | |
305 | ||
306 | } // namespace view | |
307 | } // namespace pv |