]> sigrok.org Git - pulseview.git/blame - pv/view/header.cpp
SamplingBar: Handle a failure to list the LIMIT_SAMPLES key
[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 {
52namespace view {
53
d7c0ca4a 54const int Header::Padding = 12;
512bfc56 55const int Header::BaselineOffset = 5;
d7c0ca4a 56
73ba2d70
JH
57static bool item_selected(shared_ptr<RowItem> r)
58{
59 return r->selected();
60}
61
1d8dca91 62Header::Header(View &parent) :
f4433aa9 63 MarginWidget(parent)
1d8dca91
JH
64{
65}
66
d7c0ca4a
JH
67QSize Header::sizeHint() const
68{
b781f806 69 QRectF max_rect(-Padding, 0, Padding, 0);
8dbbc7f0 70 for (auto &i : view_)
aa59d5c2 71 if (i->enabled())
b3f44329 72 max_rect = max_rect.united(i->label_rect(QRect()));
b781f806 73 return QSize(max_rect.width() + Padding + BaselineOffset, 0);
d7c0ca4a
JH
74}
75
819e2e95
JH
76QSize Header::extended_size_hint() const
77{
78 return sizeHint() + QSize(ViewItem::HighlightRadius, 0);
79}
80
3e124bee
JH
81vector< shared_ptr<ViewItem> > Header::items()
82{
83 return vector< shared_ptr<ViewItem> >(view_.begin(), view_.end());
84}
85
bc01bb31 86shared_ptr<ViewItem> Header::get_mouse_over_item(const QPoint &pt)
e3374498 87{
c13447b3 88 const QRect r(0, 0, width() - BaselineOffset, height());
8dbbc7f0 89 for (auto &i : view_)
b3f44329 90 if (i->enabled() && i->label_rect(r).contains(pt))
aa59d5c2 91 return i;
eae6e30a 92 return shared_ptr<RowItem>();
e3374498
JH
93}
94
e314eca4 95void Header::paintEvent(QPaintEvent*)
1d8dca91 96{
512bfc56
JS
97 // The trace labels are not drawn with the arrows exactly on the
98 // left edge of the widget, because then the selection shadow
99 // would be clipped away.
c13447b3 100 const QRect rect(0, 0, width() - BaselineOffset, height());
68b21a71 101
aa59d5c2 102 vector< shared_ptr<RowItem> > row_items(
8dbbc7f0 103 view_.begin(), view_.end());
aa59d5c2 104
68b21a71
JH
105 stable_sort(row_items.begin(), row_items.end(),
106 [](const shared_ptr<RowItem> &a, const shared_ptr<RowItem> &b) {
be9e7b4b 107 return a->visual_v_offset() < b->visual_v_offset(); });
1d8dca91
JH
108
109 QPainter painter(this);
110 painter.setRenderHint(QPainter::Antialiasing);
111
eae6e30a 112 for (const shared_ptr<RowItem> r : row_items)
1d8dca91 113 {
eae6e30a 114 assert(r);
1d8dca91 115
803cdac4 116 const bool highlight = !item_dragging_ &&
b3f44329
JH
117 r->label_rect(rect).contains(mouse_point_);
118 r->paint_label(painter, rect, highlight);
1d8dca91
JH
119 }
120
121 painter.end();
122}
123
49f8ff3f
JH
124void Header::contextMenuEvent(QContextMenuEvent *event)
125{
bc01bb31 126 const shared_ptr<ViewItem> r = get_mouse_over_item(mouse_point_);
a28878f4
JH
127 if (!r)
128 return;
129
73ba2d70 130 QMenu *menu = r->create_context_menu(this);
a28878f4 131 if (!menu)
73ba2d70
JH
132 menu = new QMenu(this);
133
8dbbc7f0 134 if (std::count_if(view_.begin(), view_.end(), item_selected) > 1)
73ba2d70
JH
135 {
136 menu->addSeparator();
137
138 QAction *const group = new QAction(tr("Group"), this);
139 QList<QKeySequence> shortcuts;
140 shortcuts.append(QKeySequence(Qt::ControlModifier | Qt::Key_G));
141 group->setShortcuts(shortcuts);
142 connect(group, SIGNAL(triggered()), this, SLOT(on_group()));
143 menu->addAction(group);
144 }
49f8ff3f 145
a28878f4 146 menu->exec(event->globalPos());
b3b57abc
JH
147}
148
5ed1adf5
JH
149void Header::keyPressEvent(QKeyEvent *e)
150{
151 assert(e);
152
bb4a0e8e
JH
153 MarginWidget::keyPressEvent(e);
154
155 if (e->key() == Qt::Key_G && e->modifiers() == Qt::ControlModifier)
14ca8a8d
JH
156 on_group();
157 else if (e->key() == Qt::Key_U && e->modifiers() == Qt::ControlModifier)
158 on_ungroup();
5ed1adf5
JH
159}
160
73ba2d70
JH
161void Header::on_group()
162{
163 vector< shared_ptr<RowItem> > selected_items(
8dbbc7f0
JH
164 make_filter_iterator(item_selected, view_.begin(), view_.end()),
165 make_filter_iterator(item_selected, view_.end(), view_.end()));
73ba2d70
JH
166 stable_sort(selected_items.begin(), selected_items.end(),
167 [](const shared_ptr<RowItem> &a, const shared_ptr<RowItem> &b) {
168 return a->visual_v_offset() < b->visual_v_offset(); });
169
170 shared_ptr<TraceGroup> group(new TraceGroup());
f052f9cd
JH
171 shared_ptr<RowItem> mouse_down_item(
172 std::dynamic_pointer_cast<RowItem>(mouse_down_item_));
73ba2d70 173 shared_ptr<RowItem> focus_item(
f052f9cd 174 mouse_down_item ? mouse_down_item : selected_items.front());
73ba2d70
JH
175
176 assert(focus_item);
177 assert(focus_item->owner());
178 focus_item->owner()->add_child_item(group);
179
180 // Set the group v_offset here before reparenting
181 group->force_to_v_offset(focus_item->layout_v_offset() +
182 focus_item->v_extents().first);
183
184 for (size_t i = 0; i < selected_items.size(); i++) {
185 const shared_ptr<RowItem> &r = selected_items[i];
186 assert(r->owner());
187 r->owner()->remove_child_item(r);
188 group->add_child_item(r);
189
190 // Put the items at 1-pixel offsets, so that restack will
191 // stack them in the right order
192 r->set_layout_v_offset(i);
193 }
194}
195
bb419fdd
JH
196void Header::on_ungroup()
197{
198 bool restart;
199 do {
200 restart = false;
8dbbc7f0 201 for (const shared_ptr<RowItem> r : view_) {
bb419fdd
JH
202 const shared_ptr<TraceGroup> tg =
203 dynamic_pointer_cast<TraceGroup>(r);
204 if (tg && tg->selected()) {
205 tg->ungroup();
206 restart = true;
207 break;
208 }
209 }
210 } while(restart);
211}
212
1d8dca91
JH
213} // namespace view
214} // namespace pv