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