]>
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 | ||
2acdb232 JH |
21 | #include "header.hpp" |
22 | #include "view.hpp" | |
1d8dca91 | 23 | |
2acdb232 JH |
24 | #include "signal.hpp" |
25 | #include "tracegroup.hpp" | |
1d8dca91 | 26 | |
68b21a71 JH |
27 | #include <cassert> |
28 | #include <algorithm> | |
1d8dca91 | 29 | |
73ba2d70 JH |
30 | #include <boost/iterator/filter_iterator.hpp> |
31 | ||
e3374498 | 32 | #include <QApplication> |
49f8ff3f | 33 | #include <QMenu> |
a29bb7fb | 34 | #include <QMouseEvent> |
1d8dca91 JH |
35 | #include <QPainter> |
36 | #include <QRect> | |
37 | ||
f65cd27b | 38 | #include <pv/session.hpp> |
2acdb232 | 39 | #include <pv/widgets/popup.hpp> |
569d1e41 | 40 | |
73ba2d70 | 41 | using boost::make_filter_iterator; |
bb419fdd | 42 | using std::dynamic_pointer_cast; |
819f4c25 JH |
43 | using std::max; |
44 | using std::make_pair; | |
73ba2d70 | 45 | using std::min; |
819f4c25 | 46 | using std::pair; |
f9abf97e | 47 | using std::shared_ptr; |
68b21a71 | 48 | using std::stable_sort; |
819f4c25 | 49 | using std::vector; |
1d8dca91 JH |
50 | |
51 | namespace pv { | |
52 | namespace view { | |
53 | ||
d7c0ca4a | 54 | const int Header::Padding = 12; |
512bfc56 | 55 | const int Header::BaselineOffset = 5; |
d7c0ca4a | 56 | |
73ba2d70 JH |
57 | static bool item_selected(shared_ptr<RowItem> r) |
58 | { | |
59 | return r->selected(); | |
60 | } | |
61 | ||
1d8dca91 | 62 | Header::Header(View &parent) : |
f4433aa9 | 63 | MarginWidget(parent) |
1d8dca91 | 64 | { |
8dbbc7f0 | 65 | connect(&view_, SIGNAL(signals_moved()), |
07204819 | 66 | this, SLOT(on_signals_moved())); |
1d8dca91 JH |
67 | } |
68 | ||
d7c0ca4a JH |
69 | QSize Header::sizeHint() const |
70 | { | |
b781f806 | 71 | QRectF max_rect(-Padding, 0, Padding, 0); |
8dbbc7f0 | 72 | for (auto &i : view_) |
aa59d5c2 | 73 | if (i->enabled()) |
b3f44329 | 74 | max_rect = max_rect.united(i->label_rect(QRect())); |
b781f806 | 75 | return QSize(max_rect.width() + Padding + BaselineOffset, 0); |
d7c0ca4a JH |
76 | } |
77 | ||
819e2e95 JH |
78 | QSize Header::extended_size_hint() const |
79 | { | |
80 | return sizeHint() + QSize(ViewItem::HighlightRadius, 0); | |
81 | } | |
82 | ||
3e124bee JH |
83 | vector< shared_ptr<ViewItem> > Header::items() |
84 | { | |
85 | return vector< shared_ptr<ViewItem> >(view_.begin(), view_.end()); | |
86 | } | |
87 | ||
bc01bb31 | 88 | shared_ptr<ViewItem> Header::get_mouse_over_item(const QPoint &pt) |
e3374498 | 89 | { |
c13447b3 | 90 | const QRect r(0, 0, width() - BaselineOffset, height()); |
8dbbc7f0 | 91 | for (auto &i : view_) |
b3f44329 | 92 | if (i->enabled() && i->label_rect(r).contains(pt)) |
aa59d5c2 | 93 | return i; |
eae6e30a | 94 | return shared_ptr<RowItem>(); |
e3374498 JH |
95 | } |
96 | ||
e314eca4 | 97 | void Header::paintEvent(QPaintEvent*) |
1d8dca91 | 98 | { |
512bfc56 JS |
99 | // The trace labels are not drawn with the arrows exactly on the |
100 | // left edge of the widget, because then the selection shadow | |
101 | // would be clipped away. | |
c13447b3 | 102 | const QRect rect(0, 0, width() - BaselineOffset, height()); |
68b21a71 | 103 | |
aa59d5c2 | 104 | vector< shared_ptr<RowItem> > row_items( |
8dbbc7f0 | 105 | view_.begin(), view_.end()); |
aa59d5c2 | 106 | |
68b21a71 JH |
107 | stable_sort(row_items.begin(), row_items.end(), |
108 | [](const shared_ptr<RowItem> &a, const shared_ptr<RowItem> &b) { | |
be9e7b4b | 109 | return a->visual_v_offset() < b->visual_v_offset(); }); |
1d8dca91 JH |
110 | |
111 | QPainter painter(this); | |
112 | painter.setRenderHint(QPainter::Antialiasing); | |
113 | ||
eae6e30a | 114 | for (const shared_ptr<RowItem> r : row_items) |
1d8dca91 | 115 | { |
eae6e30a | 116 | assert(r); |
1d8dca91 | 117 | |
8dbbc7f0 | 118 | const bool highlight = !dragging_ && |
b3f44329 JH |
119 | r->label_rect(rect).contains(mouse_point_); |
120 | r->paint_label(painter, rect, highlight); | |
1d8dca91 JH |
121 | } |
122 | ||
123 | painter.end(); | |
124 | } | |
125 | ||
a29bb7fb JH |
126 | void Header::mouseMoveEvent(QMouseEvent *event) |
127 | { | |
128 | assert(event); | |
8dbbc7f0 | 129 | mouse_point_ = event->pos(); |
54401bbb | 130 | |
728fcafc JH |
131 | if (!(event->buttons() & Qt::LeftButton)) |
132 | return; | |
133 | ||
8dbbc7f0 | 134 | if ((event->pos() - mouse_down_point_).manhattanLength() < |
728fcafc JH |
135 | QApplication::startDragDistance()) |
136 | return; | |
137 | ||
ee5f2a54 | 138 | // Check all the drag items share a common owner |
0dda6fe5 | 139 | RowItemOwner *item_owner = nullptr; |
8dbbc7f0 | 140 | for (shared_ptr<RowItem> r : view_) |
0dda6fe5 JH |
141 | if (r->dragging()) { |
142 | if (!item_owner) | |
143 | item_owner = r->owner(); | |
144 | else if(item_owner != r->owner()) | |
145 | return; | |
146 | } | |
147 | ||
148 | if (!item_owner) | |
149 | return; | |
da2bebfb | 150 | |
ee5f2a54 | 151 | // Do the drag |
8dbbc7f0 | 152 | dragging_ = true; |
54401bbb | 153 | |
23e75650 | 154 | const QPoint delta = event->pos() - mouse_down_point_; |
ee5f2a54 | 155 | |
8dbbc7f0 | 156 | for (std::shared_ptr<RowItem> r : view_) |
0dda6fe5 | 157 | if (r->dragging()) { |
23e75650 | 158 | r->drag_by(delta); |
ee5f2a54 JH |
159 | |
160 | // Ensure the trace is selected | |
0dda6fe5 | 161 | r->select(); |
ee5f2a54 | 162 | } |
54401bbb | 163 | |
7ff0145f JH |
164 | item_owner->restack_items(); |
165 | for (const auto &r : *item_owner) | |
166 | r->animate_to_layout_v_offset(); | |
ee5f2a54 JH |
167 | signals_moved(); |
168 | ||
a29bb7fb JH |
169 | update(); |
170 | } | |
171 | ||
49f8ff3f JH |
172 | void Header::contextMenuEvent(QContextMenuEvent *event) |
173 | { | |
bc01bb31 | 174 | const shared_ptr<ViewItem> r = get_mouse_over_item(mouse_point_); |
a28878f4 JH |
175 | if (!r) |
176 | return; | |
177 | ||
73ba2d70 | 178 | QMenu *menu = r->create_context_menu(this); |
a28878f4 | 179 | if (!menu) |
73ba2d70 JH |
180 | menu = new QMenu(this); |
181 | ||
8dbbc7f0 | 182 | if (std::count_if(view_.begin(), view_.end(), item_selected) > 1) |
73ba2d70 JH |
183 | { |
184 | menu->addSeparator(); | |
185 | ||
186 | QAction *const group = new QAction(tr("Group"), this); | |
187 | QList<QKeySequence> shortcuts; | |
188 | shortcuts.append(QKeySequence(Qt::ControlModifier | Qt::Key_G)); | |
189 | group->setShortcuts(shortcuts); | |
190 | connect(group, SIGNAL(triggered()), this, SLOT(on_group())); | |
191 | menu->addAction(group); | |
192 | } | |
49f8ff3f | 193 | |
a28878f4 | 194 | menu->exec(event->globalPos()); |
b3b57abc JH |
195 | } |
196 | ||
5ed1adf5 JH |
197 | void Header::keyPressEvent(QKeyEvent *e) |
198 | { | |
199 | assert(e); | |
200 | ||
bb4a0e8e JH |
201 | MarginWidget::keyPressEvent(e); |
202 | ||
203 | if (e->key() == Qt::Key_G && e->modifiers() == Qt::ControlModifier) | |
14ca8a8d JH |
204 | on_group(); |
205 | else if (e->key() == Qt::Key_U && e->modifiers() == Qt::ControlModifier) | |
206 | on_ungroup(); | |
5ed1adf5 JH |
207 | } |
208 | ||
07204819 JH |
209 | void Header::on_signals_moved() |
210 | { | |
211 | update(); | |
212 | } | |
213 | ||
73ba2d70 JH |
214 | void Header::on_group() |
215 | { | |
216 | vector< shared_ptr<RowItem> > selected_items( | |
8dbbc7f0 JH |
217 | make_filter_iterator(item_selected, view_.begin(), view_.end()), |
218 | make_filter_iterator(item_selected, view_.end(), view_.end())); | |
73ba2d70 JH |
219 | stable_sort(selected_items.begin(), selected_items.end(), |
220 | [](const shared_ptr<RowItem> &a, const shared_ptr<RowItem> &b) { | |
221 | return a->visual_v_offset() < b->visual_v_offset(); }); | |
222 | ||
223 | shared_ptr<TraceGroup> group(new TraceGroup()); | |
f052f9cd JH |
224 | shared_ptr<RowItem> mouse_down_item( |
225 | std::dynamic_pointer_cast<RowItem>(mouse_down_item_)); | |
73ba2d70 | 226 | shared_ptr<RowItem> focus_item( |
f052f9cd | 227 | mouse_down_item ? mouse_down_item : selected_items.front()); |
73ba2d70 JH |
228 | |
229 | assert(focus_item); | |
230 | assert(focus_item->owner()); | |
231 | focus_item->owner()->add_child_item(group); | |
232 | ||
233 | // Set the group v_offset here before reparenting | |
234 | group->force_to_v_offset(focus_item->layout_v_offset() + | |
235 | focus_item->v_extents().first); | |
236 | ||
237 | for (size_t i = 0; i < selected_items.size(); i++) { | |
238 | const shared_ptr<RowItem> &r = selected_items[i]; | |
239 | assert(r->owner()); | |
240 | r->owner()->remove_child_item(r); | |
241 | group->add_child_item(r); | |
242 | ||
243 | // Put the items at 1-pixel offsets, so that restack will | |
244 | // stack them in the right order | |
245 | r->set_layout_v_offset(i); | |
246 | } | |
247 | } | |
248 | ||
bb419fdd JH |
249 | void Header::on_ungroup() |
250 | { | |
251 | bool restart; | |
252 | do { | |
253 | restart = false; | |
8dbbc7f0 | 254 | for (const shared_ptr<RowItem> r : view_) { |
bb419fdd JH |
255 | const shared_ptr<TraceGroup> tg = |
256 | dynamic_pointer_cast<TraceGroup>(r); | |
257 | if (tg && tg->selected()) { | |
258 | tg->ungroup(); | |
259 | restart = true; | |
260 | break; | |
261 | } | |
262 | } | |
263 | } while(restart); | |
264 | } | |
265 | ||
1d8dca91 JH |
266 | } // namespace view |
267 | } // namespace pv |