2 * This file is part of the PulseView project.
4 * Copyright (C) 2014 Joel Holdsworth <joel@airwebreathe.org.uk>
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.
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.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
22 #include "tracetreeitem.hpp"
24 #include "tracetreeitemowner.hpp"
31 using std::shared_ptr
;
32 using std::static_pointer_cast
;
39 const ViewItemOwner::item_list
& TraceTreeItemOwner::child_items() const
44 vector
< shared_ptr
<TraceTreeItem
> >
45 TraceTreeItemOwner::trace_tree_child_items() const
47 vector
< shared_ptr
<TraceTreeItem
> > items
;
48 for (auto &i
: items_
) {
49 assert(dynamic_pointer_cast
<TraceTreeItem
>(i
));
50 const shared_ptr
<TraceTreeItem
> t(
51 static_pointer_cast
<TraceTreeItem
>(i
));
58 void TraceTreeItemOwner::clear_child_items()
60 for (auto &t
: trace_tree_child_items()) {
61 assert(t
->owner() == this);
62 t
->set_owner(nullptr);
67 void TraceTreeItemOwner::add_child_item(shared_ptr
<TraceTreeItem
> item
)
69 assert(!item
->owner());
70 item
->set_owner(this);
71 items_
.push_back(item
);
73 extents_changed(true, true);
76 void TraceTreeItemOwner::remove_child_item(shared_ptr
<TraceTreeItem
> item
)
78 assert(item
->owner() == this);
79 item
->set_owner(nullptr);
80 auto iter
= find(items_
.begin(), items_
.end(), item
);
81 assert(iter
!= items_
.end());
84 extents_changed(true, true);
87 pair
<int, int> TraceTreeItemOwner::v_extents() const
89 bool has_children
= false;
91 pair
<int, int> extents(INT_MAX
, INT_MIN
);
92 for (const shared_ptr
<TraceTreeItem
>& t
: trace_tree_child_items()) {
99 const int child_offset
= t
->layout_v_offset();
100 const pair
<int, int> child_extents
= t
->v_extents();
101 extents
.first
= min(child_extents
.first
+ child_offset
,
103 extents
.second
= max(child_extents
.second
+ child_offset
,
108 extents
= make_pair(0, 0);
113 void TraceTreeItemOwner::restack_items()
115 vector
<shared_ptr
<TraceTreeItem
>> items(trace_tree_child_items());
117 // Sort by the center line of the extents
118 stable_sort(items
.begin(), items
.end(),
119 [](const shared_ptr
<TraceTreeItem
> &a
, const shared_ptr
<TraceTreeItem
> &b
) {
120 const auto aext
= a
->v_extents();
121 const auto bext
= b
->v_extents();
122 return a
->layout_v_offset() +
123 (aext
.first
+ aext
.second
) / 2 <
124 b
->layout_v_offset() +
125 (bext
.first
+ bext
.second
) / 2;
128 int total_offset
= 0;
129 for (shared_ptr
<TraceTreeItem
> r
: items
) {
130 const pair
<int, int> extents
= r
->v_extents();
131 if (extents
.first
== 0 && extents
.second
== 0)
134 // We position disabled traces, so that they are close to the
135 // animation target positon should they be re-enabled
137 total_offset
+= -extents
.first
;
140 r
->set_layout_v_offset(total_offset
);
143 total_offset
+= extents
.second
;