NCL Composer  0.1.5
 All Classes Functions Variables Pages
Entity.cpp
1 /* Copyright (c) 2011 Telemidia/PUC-Rio.
2  * All rights reserved. This program and the accompanying materials
3  * are made available under the terms of the Eclipse Public License v1.0
4  * which accompanies this distribution, and is available at
5  * http://www.eclipse.org/legal/epl-v10.html
6  *
7  * Contributors:
8  * Telemidia/PUC-Rio - initial API and implementation
9  */
10 #include "model/Entity.h"
11 
12 namespace composer {
13  namespace core {
14  namespace model {
15 
16 Entity::Entity(QObject *parent) :
17  QObject(parent)
18 {
19  setUniqueID(QUuid::createUuid().toString());
20 // QMutexLocker locker(&lockParent);
21  this->parent = (Entity*)parent;
22  this->deleteChildren = true;
23 }
24 
25 Entity::Entity(QMap<QString,QString> &atts, QObject *parent) :
26  QObject(parent)
27 {
28 // QMutexLocker locker(&lockAtts);
29  this->atts = atts;
30  setUniqueID(QUuid::createUuid().toString());
31  QMutexLocker lo(&lockParent);
32  this->parent = (Entity*)parent;
33  this->deleteChildren = true;
34 }
35 
36 Entity::Entity(QString uniqueId, QString type, QMap<QString, QString> &atts,
37  QObject *parent) :
38  QObject(parent)
39 {
40 // QMutexLocker locker(&lockAtts);
41  this->atts = atts;
42  this->_id = uniqueId;
43  QMutexLocker lo(&lockParent);
44  this->parent = (Entity*)parent;
45  this->deleteChildren = true;
46  this->setType(type);
47 }
48 
50 // QMutexLocker locker(&lockChildren);
51  if (deleteChildren) {
52  while(children.size())
53  {
54  Entity *child = children.at(0);
55  delete child;
56  child = NULL;
57  children.pop_front();
58  }
59  }
60 }
61 
62 void Entity::setAttribute(QString name, QString value)
63 {
64 // QMutexLocker locker(&lockAtts);
65  atts[name] = value;
66 }
67 
68 void Entity::setAtrributes(QMap<QString,QString> &newatts)
69 {
70 // QMutexLocker locker(&lockAtts);
71  this->atts.clear(); // Should it??!
72  for ( QMap<QString,QString>::iterator it = newatts.begin();
73  it != newatts.end(); ++it)
74  {
75  this->atts[it.key()] = it.value();
76  }
77 }
78 
79 void Entity::setType(QString type)
80 {
81 // QMutexLocker locker(&lockType);
82  this->type = type;
83 }
84 
85 void Entity::setUniqueID(QString uniqueId)
86 {
87 // QMutexLocker locker(&lockID);
88  this->_id = uniqueId;
89 }
90 
92 {
93  this->parent = parent;
94 }
95 
96 bool Entity::addChild(Entity *entity)
97 {
98  QMutexLocker locker(&lockChildren);
99  QString _id = entity->getUniqueId();
100  // Check if the entity is already children of this entity
101  // TODO: THIS CAN BE IMPROVED!! Maybe checking if the parentUniqueID.
102  for(int i = 0; i < children.size(); i++)
103  {
104  if(children.at(i)->getUniqueId() == _id )
105  return false;
106  }
107  children.push_back(entity);
108  entity->setParent(this);
109  return true;
110 }
111 
114 {
115 // QMutexLocker locker(&lockChildren);
116  entity->setDeleteChildren(true);
117  for(int i = 0; i < children.size(); i++)
118  {
119  if(children.at(i) == entity)
120  {
121  children.remove(i);
122  }
123  }
124  delete entity;
125  // entity = NULL;
126 
127  return true;
128 }
129 
130 QString Entity::getAttribute(QString name)
131 {
132  QMutexLocker locker(&lockAtts);
133  return atts.contains(name) ? atts[name] : "";
134 }
135 
136 void Entity::getAttributeIterator (QMap<QString,QString>::iterator &begin,
137  QMap<QString,QString>::iterator &end)
138 {
139 // QMutexLocker locker(&lockAtts);
140  begin = this->atts.begin();
141  end = this->atts.end();
142 }
143 
144 bool Entity::hasAttribute(const QString &name)
145 {
146 // QMutexLocker locker(&lockAtts);
147  return this->atts.contains(name);
148 }
149 
150 QString Entity::getUniqueId()
151 {
152  return this->_id;
153 }
154 
155 QString Entity::getType()
156 {
157 // QMutexLocker locker(&lockType);
158  return this->type;
159 }
160 
161 Entity* Entity::getParent()
162 {
163 // QMutexLocker locker(&lockParent);
164  return parent;
165 }
166 
167 QString Entity::getParentUniqueId()
168 {
169 // QMutexLocker loecker(&lockParent);
170  return parent->getUniqueId();
171 }
172 
173 void Entity::setDeleteChildren(bool _delete)
174 {
175  this->deleteChildren = _delete;
176 }
177 
178 QVector <Entity *> Entity::getChildren()
179 {
180  return this->children;
181 }
182 
184 // Is it useful ??
186 {
187  for(int i= 0; i < children.size(); i++)
188  {
189  Entity *child = children.at(i);
190  child->setParent(this);
191  }
192 
193  entity->setDeleteChildren(false);
194  // delete entity;
195  // entity = NULL;
196 
197  return true;
198 }
199 
202 {
203  for (int i = 0; i < children.size(); i++)
204  {
205  Entity *child = children.at(i);
206  child->print();
207  }
208 }
209 
210 QString Entity::toString(int ntab, bool writeuid)
211 {
212  QString out = "";
213  for(int i = 0; i < ntab; i++)
214  out += "\t";
215 
216  out += "<";
217  out.append(getType().toAscii());
218  foreach(QString attr, atts.keys()){
219  out += " ";
220  out.append(attr);
221  out += "=\"";
222  out += atts.value(attr);
223  out += "\"";
224  }
225  if(writeuid)
226  {
227  out += " uniqueEntityId=\"";
228  out += getUniqueId();
229  out += "\"";
230  }
231  out += ">\n";
232 
233  for (int i = 0; i < children.size(); i++)
234  {
235  Entity *child = children.at(i);
236  out += child->toString(ntab+1, writeuid);
237  }
238  for(int i = 0; i < ntab; i++)
239  out += "\t";
240  out += "</";
241  out += getType();
242  out += ">\n";
243  return out;
244 }
245 
247 {
248  // QMap <QString, QString>::iterator begin, end, it;
249  // QMap <QString, QString> attrs;
250  //getAttributeIterator(begin, end);
251  //for (it = begin; it != end; ++it)
252  // attrs[it.key()] = it.value();
253 
254  return new Entity(getUniqueId(), getType(), this->atts);
255 }
256 
257 }}} //end namespace