]>
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<TraceTreeItem> r) | |
58 | { | |
59 | return r->selected(); | |
60 | } | |
61 | ||
62 | Header::Header(View &parent) : | |
63 | MarginWidget(parent) | |
64 | { | |
65 | } | |
66 | ||
67 | QSize Header::sizeHint() const | |
68 | { | |
69 | QRectF max_rect(-Padding, 0, Padding, 0); | |
70 | const vector<shared_ptr<TraceTreeItem>> items( | |
71 | view_.list_by_type<TraceTreeItem>()); | |
72 | for (auto &i : items) | |
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 | const vector<shared_ptr<TraceTreeItem>> items( | |
86 | view_.list_by_type<TraceTreeItem>()); | |
87 | return vector< shared_ptr<ViewItem> >(items.begin(), items.end()); | |
88 | } | |
89 | ||
90 | shared_ptr<ViewItem> Header::get_mouse_over_item(const QPoint &pt) | |
91 | { | |
92 | const QRect r(0, 0, width() - BaselineOffset, height()); | |
93 | const vector<shared_ptr<TraceTreeItem>> items( | |
94 | view_.list_by_type<TraceTreeItem>()); | |
95 | for (auto i = items.rbegin(); i != items.rend(); i++) | |
96 | if ((*i)->enabled() && (*i)->label_rect(r).contains(pt)) | |
97 | return *i; | |
98 | return shared_ptr<TraceTreeItem>(); | |
99 | } | |
100 | ||
101 | void Header::paintEvent(QPaintEvent*) | |
102 | { | |
103 | // The trace labels are not drawn with the arrows exactly on the | |
104 | // left edge of the widget, because then the selection shadow | |
105 | // would be clipped away. | |
106 | const QRect rect(0, 0, width() - BaselineOffset, height()); | |
107 | ||
108 | vector< shared_ptr<RowItem> > items( | |
109 | view_.list_by_type<RowItem>()); | |
110 | ||
111 | stable_sort(items.begin(), items.end(), | |
112 | [](const shared_ptr<RowItem> &a, const shared_ptr<RowItem> &b) { | |
113 | return a->point(QRect()).y() < b->point(QRect()).y(); }); | |
114 | ||
115 | QPainter painter(this); | |
116 | painter.setRenderHint(QPainter::Antialiasing); | |
117 | ||
118 | for (const shared_ptr<RowItem> r : items) { | |
119 | assert(r); | |
120 | ||
121 | const bool highlight = !item_dragging_ && | |
122 | r->label_rect(rect).contains(mouse_point_); | |
123 | r->paint_label(painter, rect, highlight); | |
124 | } | |
125 | ||
126 | painter.end(); | |
127 | } | |
128 | ||
129 | void Header::contextMenuEvent(QContextMenuEvent *event) | |
130 | { | |
131 | const shared_ptr<ViewItem> r = get_mouse_over_item(mouse_point_); | |
132 | if (!r) | |
133 | return; | |
134 | ||
135 | QMenu *menu = r->create_context_menu(this); | |
136 | if (!menu) | |
137 | menu = new QMenu(this); | |
138 | ||
139 | const vector< shared_ptr<TraceTreeItem> > items( | |
140 | view_.list_by_type<TraceTreeItem>()); | |
141 | if (std::count_if(items.begin(), items.end(), item_selected) > 1) | |
142 | { | |
143 | menu->addSeparator(); | |
144 | ||
145 | QAction *const group = new QAction(tr("Group"), this); | |
146 | QList<QKeySequence> shortcuts; | |
147 | shortcuts.append(QKeySequence(Qt::ControlModifier | Qt::Key_G)); | |
148 | group->setShortcuts(shortcuts); | |
149 | connect(group, SIGNAL(triggered()), this, SLOT(on_group())); | |
150 | menu->addAction(group); | |
151 | } | |
152 | ||
153 | menu->exec(event->globalPos()); | |
154 | } | |
155 | ||
156 | void Header::keyPressEvent(QKeyEvent *e) | |
157 | { | |
158 | assert(e); | |
159 | ||
160 | MarginWidget::keyPressEvent(e); | |
161 | ||
162 | if (e->key() == Qt::Key_G && e->modifiers() == Qt::ControlModifier) | |
163 | on_group(); | |
164 | else if (e->key() == Qt::Key_U && e->modifiers() == Qt::ControlModifier) | |
165 | on_ungroup(); | |
166 | } | |
167 | ||
168 | void Header::on_group() | |
169 | { | |
170 | const vector< shared_ptr<TraceTreeItem> > items( | |
171 | view_.list_by_type<TraceTreeItem>()); | |
172 | vector< shared_ptr<TraceTreeItem> > selected_items( | |
173 | make_filter_iterator(item_selected, items.begin(), items.end()), | |
174 | make_filter_iterator(item_selected, items.end(), items.end())); | |
175 | stable_sort(selected_items.begin(), selected_items.end(), | |
176 | [](const shared_ptr<TraceTreeItem> &a, const shared_ptr<TraceTreeItem> &b) { | |
177 | return a->visual_v_offset() < b->visual_v_offset(); }); | |
178 | ||
179 | shared_ptr<TraceGroup> group(new TraceGroup()); | |
180 | shared_ptr<TraceTreeItem> mouse_down_item( | |
181 | std::dynamic_pointer_cast<TraceTreeItem>(mouse_down_item_)); | |
182 | shared_ptr<TraceTreeItem> focus_item( | |
183 | mouse_down_item ? mouse_down_item : selected_items.front()); | |
184 | ||
185 | assert(focus_item); | |
186 | assert(focus_item->owner()); | |
187 | focus_item->owner()->add_child_item(group); | |
188 | ||
189 | // Set the group v_offset here before reparenting | |
190 | group->force_to_v_offset(focus_item->layout_v_offset() + | |
191 | focus_item->v_extents().first); | |
192 | ||
193 | for (size_t i = 0; i < selected_items.size(); i++) { | |
194 | const shared_ptr<TraceTreeItem> &r = selected_items[i]; | |
195 | assert(r->owner()); | |
196 | r->owner()->remove_child_item(r); | |
197 | group->add_child_item(r); | |
198 | ||
199 | // Put the items at 1-pixel offsets, so that restack will | |
200 | // stack them in the right order | |
201 | r->set_layout_v_offset(i); | |
202 | } | |
203 | } | |
204 | ||
205 | void Header::on_ungroup() | |
206 | { | |
207 | bool restart; | |
208 | do { | |
209 | restart = false; | |
210 | const vector< shared_ptr<TraceGroup> > groups( | |
211 | view_.list_by_type<TraceGroup>()); | |
212 | for (const shared_ptr<TraceGroup> tg : groups) | |
213 | if (tg->selected()) { | |
214 | tg->ungroup(); | |
215 | restart = true; | |
216 | break; | |
217 | } | |
218 | } while (restart); | |
219 | } | |
220 | ||
221 | } // namespace view | |
222 | } // namespace pv |