]> sigrok.org Git - pulseview.git/blame - pv/view/header.cpp
MainWindow: Fix session error slot declaration
[pulseview.git] / pv / view / header.cpp
CommitLineData
1d8dca91 1/*
b3f22de0 2 * This file is part of the PulseView project.
1d8dca91
JH
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
2acdb232
JH
21#include "header.hpp"
22#include "view.hpp"
1d8dca91 23
2acdb232
JH
24#include "signal.hpp"
25#include "tracegroup.hpp"
1d8dca91 26
68b21a71
JH
27#include <cassert>
28#include <algorithm>
1d8dca91 29
73ba2d70
JH
30#include <boost/iterator/filter_iterator.hpp>
31
e3374498 32#include <QApplication>
49f8ff3f 33#include <QMenu>
a29bb7fb 34#include <QMouseEvent>
1d8dca91
JH
35#include <QPainter>
36#include <QRect>
37
f65cd27b 38#include <pv/session.hpp>
2acdb232 39#include <pv/widgets/popup.hpp>
569d1e41 40
73ba2d70 41using boost::make_filter_iterator;
bb419fdd 42using std::dynamic_pointer_cast;
819f4c25
JH
43using std::max;
44using std::make_pair;
73ba2d70 45using std::min;
819f4c25 46using std::pair;
f9abf97e 47using std::shared_ptr;
68b21a71 48using std::stable_sort;
819f4c25 49using std::vector;
1d8dca91
JH
50
51namespace pv {
f4e57597
SA
52namespace views {
53namespace TraceView {
1d8dca91 54
d7c0ca4a 55const int Header::Padding = 12;
512bfc56 56const int Header::BaselineOffset = 5;
d7c0ca4a 57
af503b10 58static bool item_selected(shared_ptr<TraceTreeItem> r)
73ba2d70
JH
59{
60 return r->selected();
61}
62
1d8dca91 63Header::Header(View &parent) :
f4433aa9 64 MarginWidget(parent)
1d8dca91
JH
65{
66}
67
d7c0ca4a
JH
68QSize Header::sizeHint() const
69{
b781f806 70 QRectF max_rect(-Padding, 0, Padding, 0);
cc88566c
JH
71 const vector<shared_ptr<TraceTreeItem>> items(
72 view_.list_by_type<TraceTreeItem>());
73 for (auto &i : items)
aa59d5c2 74 if (i->enabled())
b3f44329 75 max_rect = max_rect.united(i->label_rect(QRect()));
b781f806 76 return QSize(max_rect.width() + Padding + BaselineOffset, 0);
d7c0ca4a
JH
77}
78
819e2e95
JH
79QSize Header::extended_size_hint() const
80{
81 return sizeHint() + QSize(ViewItem::HighlightRadius, 0);
82}
83
3e124bee
JH
84vector< shared_ptr<ViewItem> > Header::items()
85{
cc88566c
JH
86 const vector<shared_ptr<TraceTreeItem>> items(
87 view_.list_by_type<TraceTreeItem>());
88 return vector< shared_ptr<ViewItem> >(items.begin(), items.end());
3e124bee
JH
89}
90
bc01bb31 91shared_ptr<ViewItem> Header::get_mouse_over_item(const QPoint &pt)
e3374498 92{
c13447b3 93 const QRect r(0, 0, width() - BaselineOffset, height());
cc88566c
JH
94 const vector<shared_ptr<TraceTreeItem>> items(
95 view_.list_by_type<TraceTreeItem>());
adf3aaf4
JH
96 for (auto i = items.rbegin(); i != items.rend(); i++)
97 if ((*i)->enabled() && (*i)->label_rect(r).contains(pt))
98 return *i;
af503b10 99 return shared_ptr<TraceTreeItem>();
e3374498
JH
100}
101
e314eca4 102void Header::paintEvent(QPaintEvent*)
1d8dca91 103{
512bfc56
JS
104 // The trace labels are not drawn with the arrows exactly on the
105 // left edge of the widget, because then the selection shadow
106 // would be clipped away.
c13447b3 107 const QRect rect(0, 0, width() - BaselineOffset, height());
68b21a71 108
83d8efb5
JH
109 vector< shared_ptr<RowItem> > items(
110 view_.list_by_type<RowItem>());
aa59d5c2 111
af503b10 112 stable_sort(items.begin(), items.end(),
83d8efb5
JH
113 [](const shared_ptr<RowItem> &a, const shared_ptr<RowItem> &b) {
114 return a->point(QRect()).y() < b->point(QRect()).y(); });
1d8dca91
JH
115
116 QPainter painter(this);
117 painter.setRenderHint(QPainter::Antialiasing);
118
2ad82c2e 119 for (const shared_ptr<RowItem> r : items) {
eae6e30a 120 assert(r);
1d8dca91 121
803cdac4 122 const bool highlight = !item_dragging_ &&
b3f44329
JH
123 r->label_rect(rect).contains(mouse_point_);
124 r->paint_label(painter, rect, highlight);
1d8dca91
JH
125 }
126
127 painter.end();
128}
129
49f8ff3f
JH
130void Header::contextMenuEvent(QContextMenuEvent *event)
131{
bc01bb31 132 const shared_ptr<ViewItem> r = get_mouse_over_item(mouse_point_);
a28878f4
JH
133 if (!r)
134 return;
135
73ba2d70 136 QMenu *menu = r->create_context_menu(this);
a28878f4 137 if (!menu)
73ba2d70
JH
138 menu = new QMenu(this);
139
cc88566c
JH
140 const vector< shared_ptr<TraceTreeItem> > items(
141 view_.list_by_type<TraceTreeItem>());
142 if (std::count_if(items.begin(), items.end(), item_selected) > 1)
73ba2d70
JH
143 {
144 menu->addSeparator();
145
146 QAction *const group = new QAction(tr("Group"), this);
147 QList<QKeySequence> shortcuts;
148 shortcuts.append(QKeySequence(Qt::ControlModifier | Qt::Key_G));
149 group->setShortcuts(shortcuts);
150 connect(group, SIGNAL(triggered()), this, SLOT(on_group()));
151 menu->addAction(group);
152 }
49f8ff3f 153
a28878f4 154 menu->exec(event->globalPos());
b3b57abc
JH
155}
156
d9ea9628 157void Header::keyPressEvent(QKeyEvent *event)
5ed1adf5 158{
d9ea9628 159 assert(event);
5ed1adf5 160
d9ea9628 161 MarginWidget::keyPressEvent(event);
bb4a0e8e 162
d9ea9628 163 if (event->key() == Qt::Key_G && event->modifiers() == Qt::ControlModifier)
14ca8a8d 164 on_group();
d9ea9628 165 else if (event->key() == Qt::Key_U && event->modifiers() == Qt::ControlModifier)
14ca8a8d 166 on_ungroup();
5ed1adf5
JH
167}
168
73ba2d70
JH
169void Header::on_group()
170{
cc88566c
JH
171 const vector< shared_ptr<TraceTreeItem> > items(
172 view_.list_by_type<TraceTreeItem>());
af503b10 173 vector< shared_ptr<TraceTreeItem> > selected_items(
cc88566c
JH
174 make_filter_iterator(item_selected, items.begin(), items.end()),
175 make_filter_iterator(item_selected, items.end(), items.end()));
73ba2d70 176 stable_sort(selected_items.begin(), selected_items.end(),
af503b10 177 [](const shared_ptr<TraceTreeItem> &a, const shared_ptr<TraceTreeItem> &b) {
73ba2d70
JH
178 return a->visual_v_offset() < b->visual_v_offset(); });
179
180 shared_ptr<TraceGroup> group(new TraceGroup());
af503b10
JH
181 shared_ptr<TraceTreeItem> mouse_down_item(
182 std::dynamic_pointer_cast<TraceTreeItem>(mouse_down_item_));
183 shared_ptr<TraceTreeItem> focus_item(
f052f9cd 184 mouse_down_item ? mouse_down_item : selected_items.front());
73ba2d70
JH
185
186 assert(focus_item);
187 assert(focus_item->owner());
188 focus_item->owner()->add_child_item(group);
189
190 // Set the group v_offset here before reparenting
191 group->force_to_v_offset(focus_item->layout_v_offset() +
192 focus_item->v_extents().first);
193
194 for (size_t i = 0; i < selected_items.size(); i++) {
af503b10 195 const shared_ptr<TraceTreeItem> &r = selected_items[i];
73ba2d70
JH
196 assert(r->owner());
197 r->owner()->remove_child_item(r);
198 group->add_child_item(r);
199
200 // Put the items at 1-pixel offsets, so that restack will
201 // stack them in the right order
202 r->set_layout_v_offset(i);
203 }
204}
205
bb419fdd
JH
206void Header::on_ungroup()
207{
208 bool restart;
209 do {
210 restart = false;
698568bc
JH
211 const vector< shared_ptr<TraceGroup> > groups(
212 view_.list_by_type<TraceGroup>());
213 for (const shared_ptr<TraceGroup> tg : groups)
214 if (tg->selected()) {
bb419fdd
JH
215 tg->ungroup();
216 restart = true;
217 break;
218 }
f3290553 219 } while (restart);
bb419fdd
JH
220}
221
f4e57597
SA
222} // namespace TraceView
223} // namespace views
1d8dca91 224} // namespace pv