PulseView  0.3.0
A Qt-based sigrok GUI
tracetreeitemowner.cpp
Go to the documentation of this file.
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, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #include <cassert>
22 
23 #include "tracetreeitem.hpp"
24 #include "tracetreeitemowner.hpp"
25 #include "trace.hpp"
26 
27 using std::dynamic_pointer_cast;
28 using std::max;
29 using std::make_pair;
30 using std::min;
31 using std::pair;
32 using std::set;
33 using std::shared_ptr;
34 using std::static_pointer_cast;
35 using std::vector;
36 
37 namespace pv {
38 namespace view {
39 
41 {
42  return items_;
43 }
44 
45 vector< std::shared_ptr<TraceTreeItem> >
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 
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  pair<int, int> extents(INT_MAX, INT_MIN);
91 
92  for (const shared_ptr<TraceTreeItem> t : trace_tree_child_items()) {
93  assert(t);
94  if (!t->enabled())
95  continue;
96 
97  const int child_offset = t->layout_v_offset();
98  const pair<int, int> child_extents = t->v_extents();
99  extents.first = min(child_extents.first + child_offset,
100  extents.first);
101  extents.second = max(child_extents.second + child_offset,
102  extents.second);
103  }
104 
105  return extents;
106 }
107 
108 bool TraceTreeItemOwner::reassign_bgcolour_states(bool next_bgcolour_state)
109 {
110  vector< shared_ptr<TraceTreeItem> > items = trace_tree_child_items();
111 
112  // Sort items according to vertical position
113  sort(items.begin(), items.end(),
114  [](const shared_ptr<TraceTreeItem> a, const shared_ptr<TraceTreeItem> b) {
115  return a->layout_v_offset() > b->layout_v_offset(); });
116 
117  for (const shared_ptr<TraceTreeItem> item : items) {
118  item->set_bgcolour_state(next_bgcolour_state);
119  next_bgcolour_state = !next_bgcolour_state;
120  }
121 
122  return next_bgcolour_state;
123 }
124 
126 {
127 }
128 
129 } // view
130 } // pv
std::vector< std::shared_ptr< ViewItem > > item_list
std::vector< std::shared_ptr< TraceTreeItem > > trace_tree_child_items() const
std::pair< int, int > v_extents() const
virtual void extents_changed(bool horz, bool vert)=0
virtual const item_list & child_items() const
void add_child_item(std::shared_ptr< TraceTreeItem > item)
void remove_child_item(std::shared_ptr< TraceTreeItem > item)
bool reassign_bgcolour_states(bool next_bgcolour_state)