dRonin  adbada4
dRonin GCS
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Groups Pages
uavobjectmanager.cpp
Go to the documentation of this file.
1 
17 /*
18  * This program is free software; you can redistribute it and/or modify
19  * it under the terms of the GNU General Public License as published by
20  * the Free Software Foundation; either version 3 of the License, or
21  * (at your option) any later version.
22  *
23  * This program is distributed in the hope that it will be useful, but
24  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
25  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
26  * for more details.
27  *
28  * You should have received a copy of the GNU General Public License along
29  * with this program; if not, see <http://www.gnu.org/licenses/>
30  *
31  * Additional note on redistribution: The copyright and license notices above
32  * must be maintained in each individual source file that is a derivative work
33  * of this source file; otherwise redistribution is prohibited.
34  */
35 #include "uavobjectmanager.h"
36 
41 {
42 }
43 
45 {
46 }
47 
57 {
58  // Check if this object type is already in the list
59  quint32 objID = obj->getObjID();
60  if (objects.contains(objID)) // Known object ID
61  {
62  if (objects.value(objID).contains(obj->getInstID())) // Instance already present
63  return false;
64  if (obj->isSingleInstance())
65  return false;
66  if (obj->getInstID() >= MAX_INSTANCES)
67  return false;
68  UAVDataObject *refObj = dynamic_cast<UAVDataObject *>(objects.value(objID).first());
69  if (refObj == NULL) {
70  return false;
71  }
72  UAVMetaObject *mobj = refObj->getMetaObject();
73  if (objects.value(objID).last()->getInstID() < obj->getInstID()) // Space between last
74  // existent instance and
75  // new one, lets fill the
76  // gaps
77  {
78  for (quint32 instidx = objects.value(objID).last()->getInstID() + 1;
79  instidx < obj->getInstID(); ++instidx) {
80  UAVDataObject *cobj = obj->clone(instidx);
81  cobj->initialize(instidx, mobj);
82  QMap<quint32, UAVObject *> ppp;
83  ppp.insert(instidx, cobj);
84  objects[objID].insert(instidx, cobj);
85  getObject(cobj->getObjID())->emitNewInstance(cobj); // TODO??
86  emit newInstance(cobj);
87  }
88  } else if (obj->getInstID() == 0)
89  obj->initialize(objects.value(objID).last()->getObjID() + 1, mobj);
90  else {
91  return false;
92  }
93  // Add the actual object instance in the list
94  objects[objID].insert(obj->getInstID(), obj);
95  getObject(objID)->emitNewInstance(obj);
96  emit newInstance(obj);
97  return true;
98  } else {
99  // If this point is reached then this is the first time this object type (ID) is added in
100  // the list
101  // create a new list of the instances, add in the object collection and create the object's
102  // metaobject
103  // Create metaobject
104  QString mname = obj->getName();
105  mname.append("Meta");
106  UAVMetaObject *mobj = new UAVMetaObject(objID + 1, mname, obj);
107  // Initialize object
108  obj->initialize(0, mobj);
109  // Add to list
110  addObject(obj);
111  addObject(mobj);
112  return true;
113  }
114 }
115 
123 {
124  // Check if this object type is already in the list
125  quint32 objID = obj->getObjID();
126  if (obj->isSingleInstance())
127  return false;
128  quint32 instances = (quint32)objects.value(obj->getObjID()).count();
129  for (quint32 x = obj->getInstID(); x < instances; ++x) {
130  getObject(objects.value(objID).value(x)->getObjID())
131  ->emitInstanceRemoved(objects.value(objID).value(x));
132  emit instanceRemoved(objects.value(objID).value(x));
133  objects[objID].remove(x);
134  }
135  return true;
136 }
137 
138 void UAVObjectManager::addObject(UAVObject *obj)
139 {
140  // Add to list
141  QMap<quint32, UAVObject *> list;
142  list.insert(obj->getInstID(), obj);
143  objects.insert(obj->getObjID(), list);
144 
145  objectsByName.insert(obj->getName(), list);
146 
147  emit newObject(obj);
148 }
149 
154 QVector<QVector<UAVObject *>> UAVObjectManager::getObjectsVector()
155 {
156  QVector<QVector<UAVObject *>> vector;
157  foreach (const ObjectMap &map, objects.values()) {
158  QVector<UAVObject *> vec = map.values().toVector();
159  vector.append(vec);
160  }
161  return vector;
162 }
163 
164 QHash<quint32, QMap<quint32, UAVObject *>> UAVObjectManager::getObjects()
165 {
166  return objects;
167 }
168 
172 QVector<QVector<UAVDataObject *>> UAVObjectManager::getDataObjectsVector()
173 {
174  QVector<QVector<UAVDataObject *>> vector;
175  foreach (const ObjectMap &map, objects.values()) {
176  UAVDataObject *obj = dynamic_cast<UAVDataObject *>(map.first());
177  if (obj != NULL) {
178  QVector<UAVDataObject *> vec;
179  foreach (UAVObject *o, map) {
180  UAVDataObject *dobj = dynamic_cast<UAVDataObject *>(o);
181  if (dobj)
182  vec.append(dobj);
183  }
184  vector.append(vec);
185  }
186  }
187  return vector;
188 }
189 
193 QVector<QVector<UAVMetaObject *>> UAVObjectManager::getMetaObjectsVector()
194 {
195  QVector<QVector<UAVMetaObject *>> vector;
196  foreach (const ObjectMap &map, objects.values()) {
197  UAVMetaObject *obj = dynamic_cast<UAVMetaObject *>(map.first());
198  if (obj != NULL) {
199  QVector<UAVMetaObject *> vec;
200  foreach (UAVObject *o, map) {
201  UAVMetaObject *mobj = dynamic_cast<UAVMetaObject *>(o);
202  if (mobj)
203  vec.append(mobj);
204  }
205  vector.append(vec);
206  }
207  }
208  return vector;
209 }
210 
215 UAVObject *UAVObjectManager::getObject(const QString &name, quint32 instId)
216 {
217  return getObject(name, 0, instId);
218 }
219 
224 UAVObject *UAVObjectManager::getObject(quint32 objId, quint32 instId)
225 {
226  return getObject(NULL, objId, instId);
227 }
228 
232 UAVObject *UAVObjectManager::getObject(const QString &name, quint32 objId, quint32 instId)
233 {
234  if (name != NULL) {
235  if (objectsByName.contains(name)) {
236  return objectsByName.value(name).value(instId);
237  }
238 
239  return NULL;
240  } else if (objects.contains(objId))
241  return objects.value(objId).value(instId);
242  return NULL;
243 }
244 
248 QVector<UAVObject *> UAVObjectManager::getObjectInstancesVector(const QString &name)
249 {
250  return getObjectInstancesVector(&name, 0);
251 }
252 
256 QVector<UAVObject *> UAVObjectManager::getObjectInstancesVector(quint32 objId)
257 {
258  return getObjectInstancesVector(NULL, objId);
259 }
260 
264 QVector<UAVObject *> UAVObjectManager::getObjectInstancesVector(const QString *name, quint32 objId)
265 {
266  if (name != NULL) {
267  foreach (const ObjectMap &map, objects) {
268  if (map.first()->getName().compare(name) == 0)
269  return map.values().toVector();
270  }
271  } else if (objects.contains(objId))
272  return objects.value(objId).values().toVector();
273  return QVector<UAVObject *>();
274 }
275 
279 qint32 UAVObjectManager::getNumInstances(const QString &name)
280 {
281  return getNumInstances(&name, 0);
282 }
283 
288 {
289  return getNumInstances(NULL, objId);
290 }
291 
295 qint32 UAVObjectManager::getNumInstances(const QString *name, quint32 objId)
296 {
297  if (name != NULL) {
298  foreach (const ObjectMap &map, objects) {
299  if (map.first()->getName().compare(name) == 0)
300  return map.count();
301  }
302  } else if (objects.contains(objId))
303  return objects.value(objId).count();
304  return -1;
305 }
306 
307 UAVObjectField *UAVObjectManager::getField(const QString &objName, const QString &fieldName,
308  quint32 instId)
309 {
310  UAVObject *uavo = getObject(objName, instId);
311  Q_ASSERT(uavo);
312  if (!uavo)
313  return nullptr;
314  UAVObjectField *field = uavo->getField(fieldName);
315  Q_ASSERT(field);
316  return field;
317 }
QVector< UAVObject * > getObjectInstancesVector(const QString &name)
QVector< QVector< UAVMetaObject * > > getMetaObjectsVector()
void initialize(quint32 instID, UAVMetaObject *mobj)
QMap< quint32, UAVObject * > ObjectMap
QVector< QVector< UAVObject * > > getObjectsVector()
bool registerObject(UAVDataObject *obj)
void instanceRemoved(UAVObject *obj)
void newInstance(UAVObject *obj)
quint32 getInstID()
Definition: uavobject.cpp:115
UAVMetaObject * getMetaObject()
bool isSingleInstance()
Definition: uavobject.cpp:123
UAVObjectField * getField(const QString &name)
Definition: uavobject.cpp:236
QHash< quint32, QMap< quint32, UAVObject * > > getObjects()
quint32 getObjID()
Definition: uavobject.cpp:107
void emitNewInstance(UAVObject *)
Definition: uavobject.cpp:343
QString getName()
Definition: uavobject.cpp:131
virtual UAVDataObject * clone(quint32 instID=0)=0
x
Definition: OPPlots.m:100
UAVObject * getObject(const QString &name, quint32 instId=0)
UAVObjectField * getField(const QString &objName, const QString &fieldName, quint32 instId=0)
getField Get a UAV Object field Success is asserted so there is no need to do this again in the calle...
QVector< QVector< UAVDataObject * > > getDataObjectsVector()
bool unRegisterObject(UAVDataObject *obj)
unregisters an object instance and all instances bigger than the one passed as argument from the mana...
qint32 getNumInstances(const QString &name)
void newObject(UAVObject *obj)