MathSignal: Add config dialog
[pulseview.git] / pv / views / trace / tracetreeitemowner.cpp
blob95ea83732c7bb4743b0abfb6c1afa70f2384e50a
1 /*
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/>.
20 #include <cassert>
22 #include "tracetreeitem.hpp"
23 #include "trace.hpp"
24 #include "tracetreeitemowner.hpp"
26 using std::find;
27 using std::make_pair;
28 using std::max;
29 using std::min;
30 using std::pair;
31 using std::shared_ptr;
32 using std::static_pointer_cast;
33 using std::vector;
35 namespace pv {
36 namespace views {
37 namespace trace {
39 const ViewItemOwner::item_list& TraceTreeItemOwner::child_items() const
41 return items_;
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));
52 items.push_back(t);
55 return items;
58 void TraceTreeItemOwner::clear_child_items()
60 for (auto &t : trace_tree_child_items()) {
61 assert(t->owner() == this);
62 t->set_owner(nullptr);
64 items_.clear();
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());
82 items_.erase(iter);
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()) {
93 assert(t);
94 if (!t->enabled())
95 continue;
97 has_children = true;
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,
102 extents.first);
103 extents.second = max(child_extents.second + child_offset,
104 extents.second);
107 if (!has_children)
108 extents = make_pair(0, 0);
110 return extents;
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)
132 continue;
134 // We position disabled traces, so that they are close to the
135 // animation target positon should they be re-enabled
136 if (r->enabled())
137 total_offset += -extents.first;
139 if (!r->dragging())
140 r->set_layout_v_offset(total_offset);
142 if (r->enabled())
143 total_offset += extents.second;
147 } // namespace trace
148 } // namespace views
149 } // namespace pv