]>
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 JH |
126 | const bool dragging = !_drag_row_items.empty(); |
127 | for (const shared_ptr<RowItem> r : row_items) | |
1d8dca91 | 128 | { |
eae6e30a | 129 | assert(r); |
1d8dca91 | 130 | |
25201e88 | 131 | const bool highlight = !dragging && |
eae6e30a JH |
132 | r->label_rect(w).contains(_mouse_point); |
133 | r->paint_label(painter, w, highlight); | |
1d8dca91 JH |
134 | } |
135 | ||
136 | painter.end(); | |
137 | } | |
138 | ||
e3374498 JH |
139 | void Header::mousePressEvent(QMouseEvent *event) |
140 | { | |
141 | assert(event); | |
142 | ||
333d5bbc | 143 | if (event->button() & Qt::LeftButton) { |
54401bbb JH |
144 | _mouse_down_point = event->pos(); |
145 | ||
da2bebfb | 146 | // Save the offsets of any signals which will be dragged |
aa59d5c2 | 147 | for (const shared_ptr<RowItem> r : _view) |
eae6e30a JH |
148 | if (r->selected()) |
149 | _drag_row_items.push_back( | |
150 | make_pair(r, r->v_offset())); | |
da2bebfb JH |
151 | } |
152 | ||
153 | // Select the signal if it has been clicked | |
eae6e30a JH |
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); | |
da2bebfb | 159 | else { |
eae6e30a | 160 | mouse_over_row_item->select(true); |
da2bebfb | 161 | |
333d5bbc | 162 | if (~QApplication::keyboardModifiers() & |
07204819 | 163 | Qt::ControlModifier) |
eae6e30a | 164 | _drag_row_items.clear(); |
07204819 | 165 | |
da2bebfb | 166 | // Add the signal to the drag list |
333d5bbc | 167 | if (event->button() & Qt::LeftButton) |
eae6e30a JH |
168 | _drag_row_items.push_back( |
169 | make_pair(mouse_over_row_item, | |
170 | mouse_over_row_item->v_offset())); | |
da2bebfb | 171 | } |
54401bbb JH |
172 | } |
173 | ||
333d5bbc | 174 | if (~QApplication::keyboardModifiers() & Qt::ControlModifier) { |
07204819 JH |
175 | // Unselect all other signals because the Ctrl is not |
176 | // pressed | |
aa59d5c2 | 177 | for (const shared_ptr<RowItem> r : _view) |
eae6e30a JH |
178 | if (r != mouse_over_row_item) |
179 | r->select(false); | |
07204819 JH |
180 | } |
181 | ||
b2a53645 | 182 | selection_changed(); |
e3374498 JH |
183 | update(); |
184 | } | |
185 | ||
54401bbb JH |
186 | void Header::mouseReleaseEvent(QMouseEvent *event) |
187 | { | |
188 | assert(event); | |
333d5bbc | 189 | if (event->button() == Qt::LeftButton) { |
569d1e41 JH |
190 | if (_dragging) |
191 | _view.normalize_layout(); | |
192 | else | |
193 | { | |
eae6e30a JH |
194 | const shared_ptr<RowItem> mouse_over_row_item = |
195 | get_mouse_over_row_item(event->pos()); | |
8b4802fb JH |
196 | if (mouse_over_row_item) |
197 | show_popup(mouse_over_row_item); | |
569d1e41 JH |
198 | } |
199 | ||
728fcafc | 200 | _dragging = false; |
eae6e30a | 201 | _drag_row_items.clear(); |
4b192962 | 202 | } |
54401bbb JH |
203 | } |
204 | ||
a29bb7fb JH |
205 | void Header::mouseMoveEvent(QMouseEvent *event) |
206 | { | |
207 | assert(event); | |
208 | _mouse_point = event->pos(); | |
54401bbb | 209 | |
728fcafc JH |
210 | if (!(event->buttons() & Qt::LeftButton)) |
211 | return; | |
212 | ||
213 | if ((event->pos() - _mouse_down_point).manhattanLength() < | |
214 | QApplication::startDragDistance()) | |
215 | return; | |
216 | ||
54401bbb | 217 | // Move the signals if we are dragging |
eae6e30a | 218 | if (!_drag_row_items.empty()) |
728fcafc JH |
219 | { |
220 | _dragging = true; | |
221 | ||
54401bbb JH |
222 | const int delta = event->pos().y() - _mouse_down_point.y(); |
223 | ||
eae6e30a JH |
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) { | |
da2bebfb | 228 | const int y = (*i).second + delta; |
5c47d6c1 | 229 | row_item->set_v_offset(y); |
da2bebfb | 230 | |
38eeddea | 231 | // Ensure the trace is selected |
eae6e30a | 232 | row_item->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 | { | |
eae6e30a | 251 | const shared_ptr<RowItem> r = get_mouse_over_row_item(_mouse_point); |
a28878f4 JH |
252 | if (!r) |
253 | return; | |
254 | ||
255 | QMenu *const menu = r->create_context_menu(this); | |
256 | if (!menu) | |
257 | return; | |
49f8ff3f | 258 | |
a28878f4 | 259 | menu->exec(event->globalPos()); |
b3b57abc JH |
260 | } |
261 | ||
5ed1adf5 JH |
262 | void Header::keyPressEvent(QKeyEvent *e) |
263 | { | |
264 | assert(e); | |
265 | ||
266 | switch (e->key()) | |
267 | { | |
268 | case Qt::Key_Delete: | |
269 | { | |
aa59d5c2 | 270 | for (const shared_ptr<RowItem> r : _view) |
eae6e30a JH |
271 | if (r->selected()) |
272 | r->delete_pressed(); | |
5ed1adf5 JH |
273 | break; |
274 | } | |
275 | } | |
276 | } | |
277 | ||
07204819 JH |
278 | void Header::on_signals_moved() |
279 | { | |
280 | update(); | |
281 | } | |
282 | ||
6f98ca4c | 283 | void Header::on_trace_changed() |
d7c0ca4a JH |
284 | { |
285 | update(); | |
286 | geometry_updated(); | |
287 | } | |
07204819 | 288 | |
1d8dca91 JH |
289 | } // namespace view |
290 | } // namespace pv |