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