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