Implement "fill logic signal high areas" feature
[pulseview.git] / pv / views / trace / tracetreeitemowner.cpp
1 /*
2  * This file is part of the PulseView project.
3  *
4  * Copyright (C) 2014 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, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include <cassert>
21
22 #include "tracetreeitem.hpp"
23 #include "trace.hpp"
24 #include "tracetreeitemowner.hpp"
25
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;
34
35 namespace pv {
36 namespace views {
37 namespace trace {
38
39 const ViewItemOwner::item_list& TraceTreeItemOwner::child_items() const
40 {
41         return items_;
42 }
43
44 vector< shared_ptr<TraceTreeItem> >
45 TraceTreeItemOwner::trace_tree_child_items() const
46 {
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);
53         }
54
55         return items;
56 }
57
58 void TraceTreeItemOwner::clear_child_items()
59 {
60         for (auto &t : trace_tree_child_items()) {
61                 assert(t->owner() == this);
62                 t->set_owner(nullptr);
63         }
64         items_.clear();
65 }
66
67 void TraceTreeItemOwner::add_child_item(shared_ptr<TraceTreeItem> item)
68 {
69         assert(!item->owner());
70         item->set_owner(this);
71         items_.push_back(item);
72
73         extents_changed(true, true);
74 }
75
76 void TraceTreeItemOwner::remove_child_item(shared_ptr<TraceTreeItem> item)
77 {
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);
83
84         extents_changed(true, true);
85 }
86
87 pair<int, int> TraceTreeItemOwner::v_extents() const
88 {
89         bool has_children = false;
90
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;
96
97                 has_children = true;
98
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);
105         }
106
107         if (!has_children)
108                 extents = make_pair(0, 0);
109
110         return extents;
111 }
112
113 void TraceTreeItemOwner::restack_items()
114 {
115         vector<shared_ptr<TraceTreeItem>> items(trace_tree_child_items());
116
117         // Sort by the centre 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;
126                 });
127
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;
133
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;
138
139                 if (!r->dragging())
140                         r->set_layout_v_offset(total_offset);
141
142                 if (r->enabled())
143                         total_offset += extents.second;
144         }
145 }
146
147 } // namespace trace
148 } // namespace views
149 } // namespace pv