]> sigrok.org Git - pulseview.git/blame - pv/view/tracegroup.cpp
Use range-based for loops more often.
[pulseview.git] / pv / view / 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
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
1dc835a4 21#include <extdef.h>
722930c1
JH
22#include <assert.h>
23
24#include <algorithm>
25
0a51d1c1 26#include <QMenu>
1dc835a4 27#include <QPainter>
0a51d1c1 28
2acdb232 29#include "tracegroup.hpp"
722930c1 30
a5d93c27 31using std::pair;
722930c1 32using std::shared_ptr;
0a51d1c1 33using std::vector;
722930c1
JH
34
35namespace pv {
36namespace view {
37
b781f806
JH
38const int TraceGroup::Padding = 8;
39const int TraceGroup::Width = 12;
1dc835a4
JH
40const int TraceGroup::LineThickness = 5;
41const QColor TraceGroup::LineColour(QColor(0x55, 0x57, 0x53));
b781f806 42
722930c1
JH
43TraceGroup::~TraceGroup()
44{
8dbbc7f0 45 owner_ = nullptr;
722930c1
JH
46 clear_child_items();
47}
48
49bool TraceGroup::enabled() const
50{
51 return std::any_of(child_items().begin(), child_items().end(),
c373f828 52 [](const shared_ptr<ViewItem> &r) { return r->enabled(); });
722930c1
JH
53}
54
2b81ae46 55pv::Session& TraceGroup::session()
722930c1 56{
8dbbc7f0
JH
57 assert(owner_);
58 return owner_->session();
722930c1
JH
59}
60
2b81ae46 61const pv::Session& TraceGroup::session() const
722930c1 62{
8dbbc7f0
JH
63 assert(owner_);
64 return owner_->session();
722930c1
JH
65}
66
67pv::view::View* TraceGroup::view()
68{
8dbbc7f0
JH
69 assert(owner_);
70 return owner_->view();
722930c1
JH
71}
72
73const pv::view::View* TraceGroup::view() const
74{
8dbbc7f0
JH
75 assert(owner_);
76 return owner_->view();
722930c1
JH
77}
78
a5d93c27
JH
79pair<int, int> TraceGroup::v_extents() const
80{
af503b10 81 return TraceTreeItemOwner::v_extents();
a5d93c27
JH
82}
83
b3f44329 84void TraceGroup::paint_label(QPainter &p, const QRect &rect, bool hover)
722930c1 85{
b3f44329 86 const QRectF r = label_rect(rect).adjusted(
1dc835a4
JH
87 LineThickness / 2, LineThickness / 2,
88 -LineThickness / 2, -LineThickness / 2);
89
90 // Paint the label
91 const QPointF points[] = {
92 r.topRight(),
93 r.topLeft(),
94 r.bottomLeft(),
95 r.bottomRight()
96 };
97
98 if (selected()) {
99 const QPen pen(highlight_pen());
100 p.setPen(QPen(pen.brush(), pen.width() + LineThickness,
101 Qt::SolidLine, Qt::SquareCap, Qt::RoundJoin));
102 p.setBrush(Qt::transparent);
103 p.drawPolyline(points, countof(points));
104 }
105
106 p.setPen(QPen(QBrush(LineColour.darker()), LineThickness,
107 Qt::SolidLine, Qt::SquareCap, Qt::RoundJoin));
108 p.drawPolyline(points, countof(points));
109 p.setPen(QPen(QBrush(hover ? LineColour.lighter() : LineColour),
110 LineThickness - 2, Qt::SolidLine, Qt::SquareCap,
111 Qt::RoundJoin));
112 p.drawPolyline(points, countof(points));
722930c1
JH
113}
114
b3f44329 115QRectF TraceGroup::label_rect(const QRectF &rect) const
722930c1 116{
b3f44329 117 QRectF child_rect;
c373f828 118 for (const shared_ptr<ViewItem> r : child_items())
7ff0145f 119 if (r && r->enabled())
b3f44329 120 child_rect = child_rect.united(r->label_rect(rect));
b781f806 121
b3f44329
JH
122 return QRectF(child_rect.x() - Width - Padding, child_rect.y(),
123 Width, child_rect.height());
722930c1
JH
124}
125
126bool TraceGroup::pt_in_label_rect(int left, int right, const QPoint &point)
127{
128 (void)left;
129 (void)right;
130 (void)point;
131
132 return false;
133}
134
135QMenu* TraceGroup::create_context_menu(QWidget *parent)
136{
0a51d1c1 137 QMenu *const menu = new QMenu(parent);
722930c1 138
0a51d1c1
JH
139 QAction *const ungroup = new QAction(tr("Ungroup"), this);
140 ungroup->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_U));
141 connect(ungroup, SIGNAL(triggered()), this, SLOT(on_ungroup()));
142 menu->addAction(ungroup);
143
144 return menu;
722930c1
JH
145}
146
147pv::widgets::Popup* TraceGroup::create_popup(QWidget *parent)
148{
149 (void)parent;
4c60462b 150 return nullptr;
722930c1
JH
151}
152
7ff0145f 153int TraceGroup::owner_visual_v_offset() const
722930c1 154{
8dbbc7f0 155 return owner_ ? visual_v_offset() + owner_->owner_visual_v_offset() : 0;
7ff0145f
JH
156}
157
158void TraceGroup::restack_items()
159{
c373f828 160 vector<shared_ptr<TraceTreeItem>> items(trace_tree_child_items());
7ff0145f
JH
161
162 // Sort by the centre line of the extents
163 stable_sort(items.begin(), items.end(),
af503b10 164 [](const shared_ptr<TraceTreeItem> &a, const shared_ptr<TraceTreeItem> &b) {
7ff0145f
JH
165 const auto aext = a->v_extents();
166 const auto bext = b->v_extents();
3d79f521 167 return a->layout_v_offset() +
7ff0145f
JH
168 (aext.first + aext.second) / 2 <
169 b->layout_v_offset() +
170 (bext.first + bext.second) / 2;
171 });
172
173 int total_offset = 0;
af503b10 174 for (shared_ptr<TraceTreeItem> r : items) {
7ff0145f
JH
175 const pair<int, int> extents = r->v_extents();
176 if (extents.first == 0 && extents.second == 0)
177 continue;
178
179 // We position disabled traces, so that they are close to the
180 // animation target positon should they be re-enabled
181 if (r->enabled())
182 total_offset += -extents.first;
183
184 if (!r->dragging())
185 r->set_layout_v_offset(total_offset);
186
187 if (r->enabled())
188 total_offset += extents.second;
189 }
722930c1
JH
190}
191
3e769a37
JH
192unsigned int TraceGroup::depth() const
193{
8dbbc7f0 194 return owner_ ? owner_->depth() + 1 : 0;
3e769a37
JH
195}
196
5d6b95f9 197void TraceGroup::ungroup()
0a51d1c1 198{
c373f828 199 const vector<shared_ptr<TraceTreeItem>> items(trace_tree_child_items());
0a51d1c1
JH
200 clear_child_items();
201
af503b10 202 for (shared_ptr<TraceTreeItem> r : items)
8dbbc7f0 203 owner_->add_child_item(r);
0a51d1c1 204
8dbbc7f0 205 owner_->remove_child_item(shared_from_this());
32218d3e
JH
206}
207
5d6b95f9
JH
208void TraceGroup::on_ungroup()
209{
210 ungroup();
211}
212
6e2c3c85 213void TraceGroup::row_item_appearance_changed(bool label, bool content)
32218d3e 214{
8dbbc7f0 215 if (owner_)
6e2c3c85 216 owner_->row_item_appearance_changed(label, content);
32218d3e
JH
217}
218
219void TraceGroup::extents_changed(bool horz, bool vert)
220{
8dbbc7f0
JH
221 if (owner_)
222 owner_->extents_changed(horz, vert);
0a51d1c1
JH
223}
224
722930c1
JH
225} // namespace view
226} // namespace pv