]> sigrok.org Git - pulseview.git/blame - pv/views/trace/tracegroup.cpp
Session: Fix issue #67 by improving error handling
[pulseview.git] / pv / views / trace / tracegroup.cpp
CommitLineData
722930c1
JH
1/*
2 * This file is part of the PulseView project.
3 *
4 * Copyright (C) 2013 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
efdec55a 17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
722930c1
JH
18 */
19
1dc835a4 20#include <extdef.h>
722930c1
JH
21
22#include <algorithm>
aca9aa83 23#include <cassert>
722930c1 24
0a51d1c1 25#include <QMenu>
1dc835a4 26#include <QPainter>
0a51d1c1 27
2acdb232 28#include "tracegroup.hpp"
722930c1 29
6f925ba9 30using std::any_of;
a5d93c27 31using std::pair;
722930c1 32using std::shared_ptr;
0a51d1c1 33using std::vector;
722930c1
JH
34
35namespace pv {
f4e57597 36namespace views {
1573bf16 37namespace trace {
722930c1 38
b781f806
JH
39const int TraceGroup::Padding = 8;
40const int TraceGroup::Width = 12;
1dc835a4 41const int TraceGroup::LineThickness = 5;
641574bc 42const QColor TraceGroup::LineColor(QColor(0x55, 0x57, 0x53));
b781f806 43
722930c1
JH
44TraceGroup::~TraceGroup()
45{
8dbbc7f0 46 owner_ = nullptr;
722930c1
JH
47 clear_child_items();
48}
49
50bool TraceGroup::enabled() const
51{
6f925ba9 52 return any_of(child_items().begin(), child_items().end(),
c373f828 53 [](const shared_ptr<ViewItem> &r) { return r->enabled(); });
722930c1
JH
54}
55
2b81ae46 56pv::Session& TraceGroup::session()
722930c1 57{
8dbbc7f0
JH
58 assert(owner_);
59 return owner_->session();
722930c1
JH
60}
61
2b81ae46 62const pv::Session& TraceGroup::session() const
722930c1 63{
8dbbc7f0
JH
64 assert(owner_);
65 return owner_->session();
722930c1
JH
66}
67
f4e57597 68View* TraceGroup::view()
722930c1 69{
8dbbc7f0
JH
70 assert(owner_);
71 return owner_->view();
722930c1
JH
72}
73
f4e57597 74const View* TraceGroup::view() const
722930c1 75{
8dbbc7f0
JH
76 assert(owner_);
77 return owner_->view();
722930c1
JH
78}
79
a5d93c27
JH
80pair<int, int> TraceGroup::v_extents() const
81{
af503b10 82 return TraceTreeItemOwner::v_extents();
a5d93c27
JH
83}
84
b3f44329 85void TraceGroup::paint_label(QPainter &p, const QRect &rect, bool hover)
722930c1 86{
b3f44329 87 const QRectF r = label_rect(rect).adjusted(
c3b911b5
UH
88 LineThickness / 2.0, LineThickness / 2.0,
89 -LineThickness / 2.0, -LineThickness / 2.0);
1dc835a4
JH
90
91 // Paint the label
92 const QPointF points[] = {
93 r.topRight(),
94 r.topLeft(),
95 r.bottomLeft(),
96 r.bottomRight()
97 };
98
99 if (selected()) {
100 const QPen pen(highlight_pen());
101 p.setPen(QPen(pen.brush(), pen.width() + LineThickness,
102 Qt::SolidLine, Qt::SquareCap, Qt::RoundJoin));
103 p.setBrush(Qt::transparent);
104 p.drawPolyline(points, countof(points));
105 }
106
641574bc 107 p.setPen(QPen(QBrush(LineColor.darker()), LineThickness,
1dc835a4
JH
108 Qt::SolidLine, Qt::SquareCap, Qt::RoundJoin));
109 p.drawPolyline(points, countof(points));
641574bc 110 p.setPen(QPen(QBrush(hover ? LineColor.lighter() : LineColor),
1dc835a4
JH
111 LineThickness - 2, Qt::SolidLine, Qt::SquareCap,
112 Qt::RoundJoin));
113 p.drawPolyline(points, countof(points));
722930c1
JH
114}
115
b3f44329 116QRectF TraceGroup::label_rect(const QRectF &rect) const
722930c1 117{
b3f44329 118 QRectF child_rect;
f4ab4b5c 119 for (const shared_ptr<ViewItem>& r : child_items())
7ff0145f 120 if (r && r->enabled())
b3f44329 121 child_rect = child_rect.united(r->label_rect(rect));
b781f806 122
b3f44329
JH
123 return QRectF(child_rect.x() - Width - Padding, child_rect.y(),
124 Width, child_rect.height());
722930c1
JH
125}
126
127bool TraceGroup::pt_in_label_rect(int left, int right, const QPoint &point)
128{
129 (void)left;
130 (void)right;
131 (void)point;
132
133 return false;
134}
135
9e773fec 136QMenu* TraceGroup::create_header_context_menu(QWidget *parent)
722930c1 137{
0a51d1c1 138 QMenu *const menu = new QMenu(parent);
722930c1 139
0a51d1c1 140 QAction *const ungroup = new QAction(tr("Ungroup"), this);
1ed73ebd
VPP
141#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
142 ungroup->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_U));
143#else
0a51d1c1 144 ungroup->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_U));
1ed73ebd 145#endif
0a51d1c1
JH
146 connect(ungroup, SIGNAL(triggered()), this, SLOT(on_ungroup()));
147 menu->addAction(ungroup);
148
149 return menu;
722930c1
JH
150}
151
152pv::widgets::Popup* TraceGroup::create_popup(QWidget *parent)
153{
154 (void)parent;
4c60462b 155 return nullptr;
722930c1
JH
156}
157
7ff0145f 158int TraceGroup::owner_visual_v_offset() const
722930c1 159{
8dbbc7f0 160 return owner_ ? visual_v_offset() + owner_->owner_visual_v_offset() : 0;
7ff0145f
JH
161}
162
3e769a37
JH
163unsigned int TraceGroup::depth() const
164{
8dbbc7f0 165 return owner_ ? owner_->depth() + 1 : 0;
3e769a37
JH
166}
167
5d6b95f9 168void TraceGroup::ungroup()
0a51d1c1 169{
c373f828 170 const vector<shared_ptr<TraceTreeItem>> items(trace_tree_child_items());
0a51d1c1
JH
171 clear_child_items();
172
f4ab4b5c 173 for (const shared_ptr<TraceTreeItem>& r : items)
8dbbc7f0 174 owner_->add_child_item(r);
0a51d1c1 175
8dbbc7f0 176 owner_->remove_child_item(shared_from_this());
32218d3e
JH
177}
178
5d6b95f9
JH
179void TraceGroup::on_ungroup()
180{
181 ungroup();
182}
183
6e2c3c85 184void TraceGroup::row_item_appearance_changed(bool label, bool content)
32218d3e 185{
8dbbc7f0 186 if (owner_)
6e2c3c85 187 owner_->row_item_appearance_changed(label, content);
32218d3e
JH
188}
189
190void TraceGroup::extents_changed(bool horz, bool vert)
191{
8dbbc7f0
JH
192 if (owner_)
193 owner_->extents_changed(horz, vert);
0a51d1c1
JH
194}
195
1573bf16 196} // namespace trace
f4e57597 197} // namespace views
722930c1 198} // namespace pv