dRonin  adbada4
dRonin GCS
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Groups Pages
treeitem.cpp
Go to the documentation of this file.
1 
16 /*
17  * This program is free software; you can redistribute it and/or modify
18  * it under the terms of the GNU General Public License as published by
19  * the Free Software Foundation; either version 3 of the License, or
20  * (at your option) any later version.
21  *
22  * This program is distributed in the hope that it will be useful, but
23  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
24  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
25  * for more details.
26  *
27  * You should have received a copy of the GNU General Public License along
28  * with this program; if not, see <http://www.gnu.org/licenses/>
29  *
30  * Additional note on redistribution: The copyright and license notices above
31  * must be maintained in each individual source file that is a derivative work
32  * of this source file; otherwise redistribution is prohibited.
33  */
34 
35 #include "treeitem.h"
36 #include "fieldtreeitem.h"
37 #include <math.h>
38 
39 /* Constructor */
40 HighLightManager::HighLightManager(long checkingInterval)
41 {
42  // Start the timer and connect it to the callback
43  m_expirationTimer.start(checkingInterval);
44  connect(&m_expirationTimer, &QTimer::timeout, this, &HighLightManager::checkItemsExpired);
45 }
46 
47 /*
48  * Called to add item to list. Item is only added if absent.
49  * Returns true if item was added, otherwise false.
50  */
52 {
53  if (m_itemsToUnhighlight.remove(itemToAdd)) {
54  m_itemsWaiting.insert(itemToAdd);
55  }
56 
57  if (m_itemsWaiting.contains(itemToAdd)) {
58  return false;
59  }
60 
61  if (m_itemsToHighlight.contains(itemToAdd)) {
62  return false;
63  }
64 
65  m_itemsToHighlight.insert(itemToAdd);
66 
67  return true;
68 }
69 
70 /*
71  * Callback called periodically by the timer.
72  * This method checks for expired highlights and
73  * removes them if they are expired.
74  * Expired highlights are restored.
75  */
76 void HighLightManager::checkItemsExpired()
77 {
78  for (QSet<TreeItem *>::const_iterator i = m_itemsToUnhighlight.begin();
79  i != m_itemsToUnhighlight.end(); ++i) {
80  TreeItem *item = *i;
81 
82  item->removeHighlight();
83  emit updateHighlight(item);
84  }
85 
86  m_itemsToUnhighlight.clear();
87  m_itemsToUnhighlight.swap(m_itemsWaiting);
88 
89  for (QSet<TreeItem *>::const_iterator i = m_itemsToHighlight.begin();
90  i != m_itemsToHighlight.end(); ++i) {
91  TreeItem *item = *i;
92 
93  emit updateHighlight(item);
94  }
95 
96  m_itemsWaiting.swap(m_itemsToHighlight);
97 }
98 
100  : QObject(nullptr)
101  , isPresentOnHardware(true)
102  , m_data(data)
103  , m_parent(parent)
104  , m_highlight(false)
105  , m_changed(false)
106  , m_updated(false)
107 {
108 }
109 
110 TreeItem::TreeItem(const QVariant &data, TreeItem *parent)
111  : QObject(nullptr)
112  , isPresentOnHardware(true)
113  , m_parent(parent)
114  , m_highlight(false)
115  , m_changed(false)
116  , m_updated(false)
117 {
118  m_data << data << ""
119  << "";
120 }
121 
123 {
124  qDeleteAll(m_children);
125 }
126 
128 {
129  m_children.append(child);
130  child->setParentTree(this);
131 }
132 
134 {
135  m_children.removeAll(child);
136 }
137 
139 {
140  int index = nameIndex(child->data(0).toString());
141  m_children.insert(index, child);
142  child->setParentTree(this);
143 }
144 
146 {
147  return m_children.value(index);
148 }
149 
151 {
152  return m_children.count();
153 }
154 
155 int TreeItem::row() const
156 {
157  if (m_parent)
158  return m_parent->m_children.indexOf(const_cast<TreeItem *>(this));
159 
160  return 0;
161 }
162 
164 {
165  return m_data.count();
166 }
167 
168 QVariant TreeItem::data(int column) const
169 {
170  return m_data.value(column);
171 }
172 
173 void TreeItem::setData(QVariant value, int column)
174 {
175  m_data.replace(column, value);
176 }
177 
179 {
180  foreach (TreeItem *child, treeChildren())
181  child->update();
182 }
183 
185 {
186  foreach (TreeItem *child, treeChildren())
187  child->apply();
188 }
189 
190 /*
191  * Called after a value has changed to trigger highlightning of tree item.
192  */
194 {
195  m_highlight = true;
196  m_changed = false;
197  // Add to highlightmanager
198 
199  m_highlightManager->add(this);
200 
201  // If we have a parent, call recursively to update highlight status of parents.
202  // This will ensure that the root of a leaf that is changed also is highlighted.
203  // Only updates that really changes values will trigger highlight of parents.
204  if (m_parent) {
205  m_parent->setHighlight();
206  }
207 }
208 
209 void TreeItem::setUpdatedOnly(bool updated)
210 {
211  if (this->changed() && updated) {
212  m_updated = updated;
213  m_parent->setUpdatedOnlyParent();
214  } else if (!updated)
215  m_updated = false;
216  foreach (TreeItem *item, this->treeChildren()) {
217  item->setUpdatedOnly(updated);
218  }
219 }
220 
222 {
223  FieldTreeItem *field = dynamic_cast<FieldTreeItem *>(this);
224  TopTreeItem *top = dynamic_cast<TopTreeItem *>(this);
225  if (!field && !top) {
226  m_updated = true;
227  m_parent->setUpdatedOnlyParent();
228  }
229 }
230 
232 {
233  m_highlight = false;
234 }
235 
237 {
238  m_highlightManager = mgr;
239 }
240 
242 {
243  return m_metaObjectTreeItemsPerObjectIds.values();
244 }
245 
247 {
248  return m_objectTreeItemsPerObjectIds.values();
249 }
TreeItem(const QList< QVariant > &data, TreeItem *parent=nullptr)
Definition: treeitem.cpp:99
void updateHighlight(TreeItem *)
void insertChild(TreeItem *child)
Definition: treeitem.cpp:138
bool add(TreeItem *itemToAdd)
Definition: treeitem.cpp:51
int columnCount() const
Definition: treeitem.cpp:163
QList< MetaObjectTreeItem * > getMetaObjectItems()
Definition: treeitem.cpp:241
for i
Definition: OPPlots.m:140
int row() const
Definition: treeitem.cpp:155
DataFields data
virtual void removeChild(TreeItem *child)
Definition: treeitem.cpp:133
virtual void apply()
Definition: treeitem.cpp:184
virtual void removeHighlight()
Definition: treeitem.cpp:231
void setParentTree(TreeItem *parent)
Definition: treeitem.h:133
QList< TreeItem * > treeChildren() const
Definition: treeitem.h:112
virtual void setHighlightManager(HighLightManager *mgr)
Definition: treeitem.cpp:236
void setUpdatedOnlyParent()
Definition: treeitem.cpp:221
QList< DataObjectTreeItem * > getDataObjectItems()
Definition: treeitem.cpp:246
virtual void appendChild(TreeItem *child)
Definition: treeitem.cpp:127
virtual void setData(QVariant value, int column=1)
Definition: treeitem.cpp:173
TreeItem * getChild(int index)
Definition: treeitem.cpp:145
QVariant data(int column=1) const
Definition: treeitem.cpp:168
virtual void update()
Definition: treeitem.cpp:178
void setHighlight()
Definition: treeitem.cpp:193
int childCount() const
Definition: treeitem.cpp:150
void setUpdatedOnly(bool updated)
Definition: treeitem.cpp:209
int nameIndex(QString name)
Definition: treeitem.h:158
virtual ~TreeItem()
Definition: treeitem.cpp:122
bool changed()
Definition: treeitem.h:141
HighLightManager(long checkingInterval)
Definition: treeitem.cpp:40