]>
Commit | Line | Data |
---|---|---|
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 | 31 | using std::pair; |
722930c1 | 32 | using std::shared_ptr; |
0a51d1c1 | 33 | using std::vector; |
722930c1 JH |
34 | |
35 | namespace pv { | |
f4e57597 SA |
36 | namespace views { |
37 | namespace TraceView { | |
722930c1 | 38 | |
b781f806 JH |
39 | const int TraceGroup::Padding = 8; |
40 | const int TraceGroup::Width = 12; | |
1dc835a4 JH |
41 | const int TraceGroup::LineThickness = 5; |
42 | const QColor TraceGroup::LineColour(QColor(0x55, 0x57, 0x53)); | |
b781f806 | 43 | |
722930c1 JH |
44 | TraceGroup::~TraceGroup() |
45 | { | |
8dbbc7f0 | 46 | owner_ = nullptr; |
722930c1 JH |
47 | clear_child_items(); |
48 | } | |
49 | ||
50 | bool TraceGroup::enabled() const | |
51 | { | |
52 | return std::any_of(child_items().begin(), child_items().end(), | |
c373f828 | 53 | [](const shared_ptr<ViewItem> &r) { return r->enabled(); }); |
722930c1 JH |
54 | } |
55 | ||
2b81ae46 | 56 | pv::Session& TraceGroup::session() |
722930c1 | 57 | { |
8dbbc7f0 JH |
58 | assert(owner_); |
59 | return owner_->session(); | |
722930c1 JH |
60 | } |
61 | ||
2b81ae46 | 62 | const pv::Session& TraceGroup::session() const |
722930c1 | 63 | { |
8dbbc7f0 JH |
64 | assert(owner_); |
65 | return owner_->session(); | |
722930c1 JH |
66 | } |
67 | ||
f4e57597 | 68 | View* TraceGroup::view() |
722930c1 | 69 | { |
8dbbc7f0 JH |
70 | assert(owner_); |
71 | return owner_->view(); | |
722930c1 JH |
72 | } |
73 | ||
f4e57597 | 74 | const View* TraceGroup::view() const |
722930c1 | 75 | { |
8dbbc7f0 JH |
76 | assert(owner_); |
77 | return owner_->view(); | |
722930c1 JH |
78 | } |
79 | ||
a5d93c27 JH |
80 | pair<int, int> TraceGroup::v_extents() const |
81 | { | |
af503b10 | 82 | return TraceTreeItemOwner::v_extents(); |
a5d93c27 JH |
83 | } |
84 | ||
b3f44329 | 85 | void TraceGroup::paint_label(QPainter &p, const QRect &rect, bool hover) |
722930c1 | 86 | { |
b3f44329 | 87 | const QRectF r = label_rect(rect).adjusted( |
1dc835a4 JH |
88 | LineThickness / 2, LineThickness / 2, |
89 | -LineThickness / 2, -LineThickness / 2); | |
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 | ||
107 | p.setPen(QPen(QBrush(LineColour.darker()), LineThickness, | |
108 | Qt::SolidLine, Qt::SquareCap, Qt::RoundJoin)); | |
109 | p.drawPolyline(points, countof(points)); | |
110 | p.setPen(QPen(QBrush(hover ? LineColour.lighter() : LineColour), | |
111 | LineThickness - 2, Qt::SolidLine, Qt::SquareCap, | |
112 | Qt::RoundJoin)); | |
113 | p.drawPolyline(points, countof(points)); | |
722930c1 JH |
114 | } |
115 | ||
b3f44329 | 116 | QRectF TraceGroup::label_rect(const QRectF &rect) const |
722930c1 | 117 | { |
b3f44329 | 118 | QRectF child_rect; |
c373f828 | 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 | ||
127 | bool 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 | ||
136 | QMenu* TraceGroup::create_context_menu(QWidget *parent) | |
137 | { | |
0a51d1c1 | 138 | QMenu *const menu = new QMenu(parent); |
722930c1 | 139 | |
0a51d1c1 JH |
140 | QAction *const ungroup = new QAction(tr("Ungroup"), this); |
141 | ungroup->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_U)); | |
142 | connect(ungroup, SIGNAL(triggered()), this, SLOT(on_ungroup())); | |
143 | menu->addAction(ungroup); | |
144 | ||
145 | return menu; | |
722930c1 JH |
146 | } |
147 | ||
148 | pv::widgets::Popup* TraceGroup::create_popup(QWidget *parent) | |
149 | { | |
150 | (void)parent; | |
4c60462b | 151 | return nullptr; |
722930c1 JH |
152 | } |
153 | ||
7ff0145f | 154 | int TraceGroup::owner_visual_v_offset() const |
722930c1 | 155 | { |
8dbbc7f0 | 156 | return owner_ ? visual_v_offset() + owner_->owner_visual_v_offset() : 0; |
7ff0145f JH |
157 | } |
158 | ||
159 | void TraceGroup::restack_items() | |
160 | { | |
c373f828 | 161 | vector<shared_ptr<TraceTreeItem>> items(trace_tree_child_items()); |
7ff0145f JH |
162 | |
163 | // Sort by the centre line of the extents | |
164 | stable_sort(items.begin(), items.end(), | |
af503b10 | 165 | [](const shared_ptr<TraceTreeItem> &a, const shared_ptr<TraceTreeItem> &b) { |
7ff0145f JH |
166 | const auto aext = a->v_extents(); |
167 | const auto bext = b->v_extents(); | |
3d79f521 | 168 | return a->layout_v_offset() + |
7ff0145f JH |
169 | (aext.first + aext.second) / 2 < |
170 | b->layout_v_offset() + | |
171 | (bext.first + bext.second) / 2; | |
172 | }); | |
173 | ||
174 | int total_offset = 0; | |
af503b10 | 175 | for (shared_ptr<TraceTreeItem> r : items) { |
7ff0145f JH |
176 | const pair<int, int> extents = r->v_extents(); |
177 | if (extents.first == 0 && extents.second == 0) | |
178 | continue; | |
179 | ||
180 | // We position disabled traces, so that they are close to the | |
181 | // animation target positon should they be re-enabled | |
182 | if (r->enabled()) | |
183 | total_offset += -extents.first; | |
184 | ||
185 | if (!r->dragging()) | |
186 | r->set_layout_v_offset(total_offset); | |
187 | ||
188 | if (r->enabled()) | |
189 | total_offset += extents.second; | |
190 | } | |
722930c1 JH |
191 | } |
192 | ||
3e769a37 JH |
193 | unsigned int TraceGroup::depth() const |
194 | { | |
8dbbc7f0 | 195 | return owner_ ? owner_->depth() + 1 : 0; |
3e769a37 JH |
196 | } |
197 | ||
5d6b95f9 | 198 | void TraceGroup::ungroup() |
0a51d1c1 | 199 | { |
c373f828 | 200 | const vector<shared_ptr<TraceTreeItem>> items(trace_tree_child_items()); |
0a51d1c1 JH |
201 | clear_child_items(); |
202 | ||
af503b10 | 203 | for (shared_ptr<TraceTreeItem> r : items) |
8dbbc7f0 | 204 | owner_->add_child_item(r); |
0a51d1c1 | 205 | |
8dbbc7f0 | 206 | owner_->remove_child_item(shared_from_this()); |
32218d3e JH |
207 | } |
208 | ||
5d6b95f9 JH |
209 | void TraceGroup::on_ungroup() |
210 | { | |
211 | ungroup(); | |
212 | } | |
213 | ||
6e2c3c85 | 214 | void TraceGroup::row_item_appearance_changed(bool label, bool content) |
32218d3e | 215 | { |
8dbbc7f0 | 216 | if (owner_) |
6e2c3c85 | 217 | owner_->row_item_appearance_changed(label, content); |
32218d3e JH |
218 | } |
219 | ||
220 | void TraceGroup::extents_changed(bool horz, bool vert) | |
221 | { | |
8dbbc7f0 JH |
222 | if (owner_) |
223 | owner_->extents_changed(horz, vert); | |
0a51d1c1 JH |
224 | } |
225 | ||
f4e57597 SA |
226 | } // namespace TraceView |
227 | } // namespace views | |
722930c1 | 228 | } // namespace pv |