Add a tooltip for the "Stack decoder" button/dropdown.
[pulseview.git] / pv / view / header.cpp
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, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include "header.hpp"
21 #include "view.hpp"
22
23 #include "signal.hpp"
24 #include "tracegroup.hpp"
25
26 #include <algorithm>
27 #include <cassert>
28
29 #include <boost/iterator/filter_iterator.hpp>
30
31 #include <QApplication>
32 #include <QMenu>
33 #include <QMouseEvent>
34 #include <QPainter>
35 #include <QRect>
36
37 #include <pv/session.hpp>
38 #include <pv/widgets/popup.hpp>
39
40 using boost::make_filter_iterator;
41
42 using std::count_if;
43 using std::dynamic_pointer_cast;
44 using std::shared_ptr;
45 using std::stable_sort;
46 using std::vector;
47
48 namespace pv {
49 namespace views {
50 namespace TraceView {
51
52 const int Header::Padding = 12;
53 const int Header::BaselineOffset = 5;
54
55 static bool item_selected(shared_ptr<TraceTreeItem> r)
56 {
57         return r->selected();
58 }
59
60 Header::Header(View &parent) :
61         MarginWidget(parent)
62 {
63 }
64
65 QSize Header::sizeHint() const
66 {
67         QRectF max_rect(-Padding, 0, Padding, 0);
68         const vector<shared_ptr<TraceTreeItem>> items(
69                 view_.list_by_type<TraceTreeItem>());
70         for (auto &i : items)
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         const vector<shared_ptr<TraceTreeItem>> items(
84                 view_.list_by_type<TraceTreeItem>());
85         return vector< shared_ptr<ViewItem> >(items.begin(), items.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         const vector<shared_ptr<TraceTreeItem>> items(
92                 view_.list_by_type<TraceTreeItem>());
93         for (auto i = items.rbegin(); i != items.rend(); i++)
94                 if ((*i)->enabled() && (*i)->label_rect(r).contains(pt))
95                         return *i;
96         return shared_ptr<TraceTreeItem>();
97 }
98
99 void Header::paintEvent(QPaintEvent*)
100 {
101         // The trace labels are not drawn with the arrows exactly on the
102         // left edge of the widget, because then the selection shadow
103         // would be clipped away.
104         const QRect rect(0, 0, width() - BaselineOffset, height());
105
106         vector< shared_ptr<RowItem> > items(view_.list_by_type<RowItem>());
107
108         stable_sort(items.begin(), items.end(),
109                 [](const shared_ptr<RowItem> &a, const shared_ptr<RowItem> &b) {
110                         return a->point(QRect()).y() < b->point(QRect()).y(); });
111
112         QPainter painter(this);
113         painter.setRenderHint(QPainter::Antialiasing);
114
115         for (const shared_ptr<RowItem> r : items) {
116                 assert(r);
117
118                 const bool highlight = !item_dragging_ &&
119                         r->label_rect(rect).contains(mouse_point_);
120                 r->paint_label(painter, rect, highlight);
121         }
122
123         painter.end();
124 }
125
126 void Header::contextMenuEvent(QContextMenuEvent *event)
127 {
128         const shared_ptr<ViewItem> r = get_mouse_over_item(mouse_point_);
129         if (!r)
130                 return;
131
132         QMenu *menu = r->create_context_menu(this);
133         if (!menu)
134                 menu = new QMenu(this);
135
136         const vector< shared_ptr<TraceTreeItem> > items(
137                 view_.list_by_type<TraceTreeItem>());
138         if (count_if(items.begin(), items.end(), item_selected) > 1) {
139                 menu->addSeparator();
140
141                 QAction *const group = new QAction(tr("Group"), this);
142                 QList<QKeySequence> shortcuts;
143                 shortcuts.append(QKeySequence(Qt::ControlModifier | Qt::Key_G));
144                 group->setShortcuts(shortcuts);
145                 connect(group, SIGNAL(triggered()), this, SLOT(on_group()));
146                 menu->addAction(group);
147         }
148
149         menu->exec(event->globalPos());
150 }
151
152 void Header::keyPressEvent(QKeyEvent *event)
153 {
154         assert(event);
155
156         MarginWidget::keyPressEvent(event);
157
158         if (event->key() == Qt::Key_G && event->modifiers() == Qt::ControlModifier)
159                 on_group();
160         else if (event->key() == Qt::Key_U && event->modifiers() == Qt::ControlModifier)
161                 on_ungroup();
162 }
163
164 void Header::on_group()
165 {
166         const vector< shared_ptr<TraceTreeItem> > items(
167                 view_.list_by_type<TraceTreeItem>());
168         vector< shared_ptr<TraceTreeItem> > selected_items(
169                 make_filter_iterator(item_selected, items.begin(), items.end()),
170                 make_filter_iterator(item_selected, items.end(), items.end()));
171         stable_sort(selected_items.begin(), selected_items.end(),
172                 [](const shared_ptr<TraceTreeItem> &a, const shared_ptr<TraceTreeItem> &b) {
173                         return a->visual_v_offset() < b->visual_v_offset(); });
174
175         shared_ptr<TraceGroup> group(new TraceGroup());
176         shared_ptr<TraceTreeItem> mouse_down_item(
177                 dynamic_pointer_cast<TraceTreeItem>(mouse_down_item_));
178         shared_ptr<TraceTreeItem> focus_item(
179                 mouse_down_item ? mouse_down_item : selected_items.front());
180
181         assert(focus_item);
182         assert(focus_item->owner());
183         focus_item->owner()->add_child_item(group);
184
185         // Set the group v_offset here before reparenting
186         group->force_to_v_offset(focus_item->layout_v_offset() +
187                 focus_item->v_extents().first);
188
189         for (size_t i = 0; i < selected_items.size(); i++) {
190                 const shared_ptr<TraceTreeItem> &r = selected_items[i];
191                 assert(r->owner());
192                 r->owner()->remove_child_item(r);
193                 group->add_child_item(r);
194
195                 // Put the items at 1-pixel offsets, so that restack will
196                 // stack them in the right order
197                 r->set_layout_v_offset(i);
198         }
199 }
200
201 void Header::on_ungroup()
202 {
203         bool restart;
204         do {
205                 restart = false;
206                 const vector< shared_ptr<TraceGroup> > groups(
207                         view_.list_by_type<TraceGroup>());
208                 for (const shared_ptr<TraceGroup> tg : groups)
209                         if (tg->selected()) {
210                                 tg->ungroup();
211                                 restart = true;
212                                 break;
213                         }
214         } while (restart);
215 }
216
217 } // namespace TraceView
218 } // namespace views
219 } // namespace pv