]>
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 | 26 | |
68b21a71 JH |
27 | #include <cassert> |
28 | #include <algorithm> | |
1d8dca91 | 29 | |
e3374498 | 30 | #include <QApplication> |
49f8ff3f | 31 | #include <QMenu> |
a29bb7fb | 32 | #include <QMouseEvent> |
1d8dca91 JH |
33 | #include <QPainter> |
34 | #include <QRect> | |
35 | ||
569d1e41 JH |
36 | #include <pv/widgets/popup.h> |
37 | ||
819f4c25 JH |
38 | using std::max; |
39 | using std::make_pair; | |
40 | using std::pair; | |
f9abf97e | 41 | using std::shared_ptr; |
68b21a71 | 42 | using std::stable_sort; |
819f4c25 | 43 | using std::vector; |
1d8dca91 JH |
44 | |
45 | namespace pv { | |
46 | namespace view { | |
47 | ||
d7c0ca4a | 48 | const int Header::Padding = 12; |
512bfc56 | 49 | const int Header::BaselineOffset = 5; |
d7c0ca4a | 50 | |
1d8dca91 | 51 | Header::Header(View &parent) : |
728fcafc JH |
52 | MarginWidget(parent), |
53 | _dragging(false) | |
1d8dca91 | 54 | { |
5ed1adf5 | 55 | setFocusPolicy(Qt::ClickFocus); |
a29bb7fb | 56 | setMouseTracking(true); |
49f8ff3f | 57 | |
07204819 JH |
58 | connect(&_view, SIGNAL(signals_moved()), |
59 | this, SLOT(on_signals_moved())); | |
1d8dca91 JH |
60 | } |
61 | ||
d7c0ca4a JH |
62 | QSize Header::sizeHint() const |
63 | { | |
b781f806 | 64 | QRectF max_rect(-Padding, 0, Padding, 0); |
aa59d5c2 JH |
65 | for (auto &i : _view) |
66 | if (i->enabled()) | |
b781f806 JH |
67 | max_rect = max_rect.united(i->label_rect(0)); |
68 | return QSize(max_rect.width() + Padding + BaselineOffset, 0); | |
d7c0ca4a JH |
69 | } |
70 | ||
eae6e30a | 71 | shared_ptr<RowItem> Header::get_mouse_over_row_item(const QPoint &pt) |
e3374498 | 72 | { |
de0b46de | 73 | const int w = width() - BaselineOffset; |
aa59d5c2 JH |
74 | for (auto &i : _view) |
75 | if (i->enabled() && i->label_rect(w).contains(pt)) | |
76 | return i; | |
eae6e30a | 77 | return shared_ptr<RowItem>(); |
e3374498 JH |
78 | } |
79 | ||
a2ae0205 JH |
80 | void Header::clear_selection() |
81 | { | |
aa59d5c2 JH |
82 | for (auto &i : _view) |
83 | i->select(false); | |
a2ae0205 JH |
84 | update(); |
85 | } | |
86 | ||
14009012 JH |
87 | void Header::signals_updated() |
88 | { | |
89 | for (shared_ptr<RowItem> r : _view) { | |
90 | assert(r); | |
91 | connect(r.get(), SIGNAL(appearance_changed()), | |
92 | this, SLOT(on_trace_changed())); | |
93 | } | |
94 | } | |
95 | ||
8b4802fb JH |
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 | ||
e314eca4 | 109 | void Header::paintEvent(QPaintEvent*) |
1d8dca91 | 110 | { |
512bfc56 JS |
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; | |
68b21a71 | 115 | |
aa59d5c2 JH |
116 | vector< shared_ptr<RowItem> > row_items( |
117 | _view.begin(), _view.end()); | |
118 | ||
68b21a71 JH |
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(); }); | |
1d8dca91 JH |
122 | |
123 | QPainter painter(this); | |
124 | painter.setRenderHint(QPainter::Antialiasing); | |
125 | ||
eae6e30a | 126 | for (const shared_ptr<RowItem> r : row_items) |
1d8dca91 | 127 | { |
eae6e30a | 128 | assert(r); |
1d8dca91 | 129 | |
0dda6fe5 | 130 | const bool highlight = !_dragging && |
eae6e30a JH |
131 | r->label_rect(w).contains(_mouse_point); |
132 | r->paint_label(painter, w, highlight); | |
1d8dca91 JH |
133 | } |
134 | ||
135 | painter.end(); | |
136 | } | |
137 | ||
e57ee799 JH |
138 | void Header::mouseLeftPressEvent(QMouseEvent *event) |
139 | { | |
f0c0b38f JH |
140 | (void)event; |
141 | ||
e57ee799 JH |
142 | const bool ctrl_pressed = |
143 | QApplication::keyboardModifiers() & Qt::ControlModifier; | |
144 | ||
145 | // Clear selection if control is not pressed and this item is unselected | |
f0c0b38f JH |
146 | if ((!_mouse_down_item || !_mouse_down_item->selected()) && |
147 | !ctrl_pressed) | |
e57ee799 JH |
148 | for (shared_ptr<RowItem> r : _view) |
149 | r->select(false); | |
150 | ||
151 | // Set the signal selection state if the item has been clicked | |
f0c0b38f | 152 | if (_mouse_down_item) { |
e57ee799 | 153 | if (ctrl_pressed) |
f0c0b38f | 154 | _mouse_down_item->select(!_mouse_down_item->selected()); |
e57ee799 | 155 | else |
f0c0b38f | 156 | _mouse_down_item->select(true); |
e57ee799 JH |
157 | } |
158 | ||
159 | // Save the offsets of any signals which will be dragged | |
e57ee799 JH |
160 | for (const shared_ptr<RowItem> r : _view) |
161 | if (r->selected()) | |
0dda6fe5 | 162 | r->drag(); |
e57ee799 JH |
163 | |
164 | selection_changed(); | |
165 | update(); | |
166 | } | |
167 | ||
e3374498 JH |
168 | void Header::mousePressEvent(QMouseEvent *event) |
169 | { | |
170 | assert(event); | |
f0c0b38f JH |
171 | |
172 | _mouse_down_point = event->pos(); | |
173 | _mouse_down_item = get_mouse_over_row_item(event->pos()); | |
174 | ||
e57ee799 JH |
175 | if (event->button() & Qt::LeftButton) |
176 | mouseLeftPressEvent(event); | |
177 | } | |
e3374498 | 178 | |
e57ee799 JH |
179 | void Header::mouseLeftReleaseEvent(QMouseEvent *event) |
180 | { | |
181 | assert(event); | |
54401bbb | 182 | |
e57ee799 JH |
183 | const bool ctrl_pressed = |
184 | QApplication::keyboardModifiers() & Qt::ControlModifier; | |
da2bebfb | 185 | |
e57ee799 JH |
186 | // Unselect everything if control is not pressed |
187 | const shared_ptr<RowItem> mouse_over = | |
eae6e30a | 188 | get_mouse_over_row_item(event->pos()); |
54401bbb | 189 | |
0dda6fe5 JH |
190 | for (auto &r : _view) |
191 | r->drag_release(); | |
192 | ||
4f82e4f6 | 193 | if (!_dragging) |
e57ee799 JH |
194 | { |
195 | if (!ctrl_pressed) { | |
196 | for (shared_ptr<RowItem> r : _view) | |
f0c0b38f | 197 | if (_mouse_down_item != r) |
e57ee799 JH |
198 | r->select(false); |
199 | ||
f0c0b38f JH |
200 | if (_mouse_down_item) |
201 | show_popup(_mouse_down_item); | |
e57ee799 | 202 | } |
07204819 JH |
203 | } |
204 | ||
e57ee799 | 205 | _dragging = false; |
e3374498 JH |
206 | } |
207 | ||
54401bbb JH |
208 | void Header::mouseReleaseEvent(QMouseEvent *event) |
209 | { | |
210 | assert(event); | |
e57ee799 JH |
211 | if (event->button() & Qt::LeftButton) |
212 | mouseLeftReleaseEvent(event); | |
f0c0b38f JH |
213 | |
214 | _mouse_down_item = nullptr; | |
54401bbb JH |
215 | } |
216 | ||
a29bb7fb JH |
217 | void Header::mouseMoveEvent(QMouseEvent *event) |
218 | { | |
219 | assert(event); | |
220 | _mouse_point = event->pos(); | |
54401bbb | 221 | |
728fcafc JH |
222 | if (!(event->buttons() & Qt::LeftButton)) |
223 | return; | |
224 | ||
225 | if ((event->pos() - _mouse_down_point).manhattanLength() < | |
226 | QApplication::startDragDistance()) | |
227 | return; | |
228 | ||
ee5f2a54 | 229 | // Check all the drag items share a common owner |
0dda6fe5 JH |
230 | RowItemOwner *item_owner = nullptr; |
231 | for (shared_ptr<RowItem> r : _view) | |
232 | if (r->dragging()) { | |
233 | if (!item_owner) | |
234 | item_owner = r->owner(); | |
235 | else if(item_owner != r->owner()) | |
236 | return; | |
237 | } | |
238 | ||
239 | if (!item_owner) | |
240 | return; | |
da2bebfb | 241 | |
ee5f2a54 JH |
242 | // Do the drag |
243 | _dragging = true; | |
54401bbb | 244 | |
ee5f2a54 JH |
245 | const int delta = event->pos().y() - _mouse_down_point.y(); |
246 | ||
0dda6fe5 JH |
247 | for (std::shared_ptr<RowItem> r : _view) |
248 | if (r->dragging()) { | |
249 | r->set_v_offset(r->drag_point().y() + delta); | |
ee5f2a54 JH |
250 | |
251 | // Ensure the trace is selected | |
0dda6fe5 | 252 | r->select(); |
ee5f2a54 | 253 | } |
54401bbb | 254 | |
ee5f2a54 JH |
255 | signals_moved(); |
256 | ||
a29bb7fb JH |
257 | update(); |
258 | } | |
259 | ||
e314eca4 | 260 | void Header::leaveEvent(QEvent*) |
a29bb7fb JH |
261 | { |
262 | _mouse_point = QPoint(-1, -1); | |
263 | update(); | |
264 | } | |
265 | ||
49f8ff3f JH |
266 | void Header::contextMenuEvent(QContextMenuEvent *event) |
267 | { | |
eae6e30a | 268 | const shared_ptr<RowItem> r = get_mouse_over_row_item(_mouse_point); |
a28878f4 JH |
269 | if (!r) |
270 | return; | |
271 | ||
272 | QMenu *const menu = r->create_context_menu(this); | |
273 | if (!menu) | |
274 | return; | |
49f8ff3f | 275 | |
a28878f4 | 276 | menu->exec(event->globalPos()); |
b3b57abc JH |
277 | } |
278 | ||
5ed1adf5 JH |
279 | void Header::keyPressEvent(QKeyEvent *e) |
280 | { | |
281 | assert(e); | |
282 | ||
283 | switch (e->key()) | |
284 | { | |
285 | case Qt::Key_Delete: | |
286 | { | |
aa59d5c2 | 287 | for (const shared_ptr<RowItem> r : _view) |
eae6e30a JH |
288 | if (r->selected()) |
289 | r->delete_pressed(); | |
5ed1adf5 JH |
290 | break; |
291 | } | |
292 | } | |
293 | } | |
294 | ||
07204819 JH |
295 | void Header::on_signals_moved() |
296 | { | |
297 | update(); | |
298 | } | |
299 | ||
6f98ca4c | 300 | void Header::on_trace_changed() |
d7c0ca4a JH |
301 | { |
302 | update(); | |
303 | geometry_updated(); | |
304 | } | |
07204819 | 305 | |
1d8dca91 JH |
306 | } // namespace view |
307 | } // namespace pv |