pulseview_cross.nsi.in: Use Python 3.4 (Windows XP support).
[pulseview.git] / pv / view / 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 "tracetreeitemowner.hpp"
24 #include "trace.hpp"
25
26 using std::dynamic_pointer_cast;
27 using std::max;
28 using std::make_pair;
29 using std::min;
30 using std::pair;
31 using std::set;
32 using std::shared_ptr;
33 using std::static_pointer_cast;
34 using std::vector;
35
36 namespace pv {
37 namespace views {
38 namespace TraceView {
39
40 const ViewItemOwner::item_list& TraceTreeItemOwner::child_items() const
41 {
42         return items_;
43 }
44
45 vector< std::shared_ptr<TraceTreeItem> >
46 TraceTreeItemOwner::trace_tree_child_items() const
47 {
48         vector< shared_ptr<TraceTreeItem> > items;
49         for (auto &i : items_) {
50                 assert(dynamic_pointer_cast<TraceTreeItem>(i));
51                 const shared_ptr<TraceTreeItem> t(
52                         static_pointer_cast<TraceTreeItem>(i));
53                 items.push_back(t);
54         }
55
56         return items;
57 }
58
59 void TraceTreeItemOwner::clear_child_items()
60 {
61         for (auto &t : trace_tree_child_items()) {
62                 assert(t->owner() == this);
63                 t->set_owner(nullptr);
64         }
65         items_.clear();
66 }
67
68 void TraceTreeItemOwner::add_child_item(std::shared_ptr<TraceTreeItem> item)
69 {
70         assert(!item->owner());
71         item->set_owner(this);
72         items_.push_back(item);
73
74         extents_changed(true, true);
75 }
76
77 void TraceTreeItemOwner::remove_child_item(std::shared_ptr<TraceTreeItem> item)
78 {
79         assert(item->owner() == this);
80         item->set_owner(nullptr);
81         auto iter = std::find(items_.begin(), items_.end(), item);
82         assert(iter != items_.end());
83         items_.erase(iter);
84
85         extents_changed(true, true);
86 }
87
88 pair<int, int> TraceTreeItemOwner::v_extents() const
89 {
90         bool has_children = false;
91
92         pair<int, int> extents(INT_MAX, INT_MIN);
93         for (const shared_ptr<TraceTreeItem> t : trace_tree_child_items()) {
94                 assert(t);
95                 if (!t->enabled())
96                         continue;
97
98                 has_children = true;
99
100                 const int child_offset = t->layout_v_offset();
101                 const pair<int, int> child_extents = t->v_extents();
102                 extents.first = min(child_extents.first + child_offset,
103                         extents.first);
104                 extents.second = max(child_extents.second + child_offset,
105                         extents.second);
106         }
107
108         if (!has_children)
109                 extents = make_pair(0, 0);
110
111         return extents;
112 }
113
114 bool TraceTreeItemOwner::reassign_bgcolour_states(bool next_bgcolour_state)
115 {
116         vector< shared_ptr<TraceTreeItem> > items = trace_tree_child_items();
117
118         // Sort items according to vertical position
119         sort(items.begin(), items.end(),
120                 [](const shared_ptr<TraceTreeItem> a, const shared_ptr<TraceTreeItem> b) {
121                 return a->layout_v_offset() > b->layout_v_offset(); });
122
123         for (const shared_ptr<TraceTreeItem> item : items) {
124                 item->set_bgcolour_state(next_bgcolour_state);
125                 next_bgcolour_state = !next_bgcolour_state;
126         }
127
128         return next_bgcolour_state;
129 }
130
131 void TraceTreeItemOwner::restack_items()
132 {
133 }
134
135 } // namespace TraceView
136 } // namespace views
137 } // namespace pv