NCL Composer  0.1.5
 All Classes Functions Variables Pages
Project.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/Project.h"
11 
12 #include <QReadWriteLock>
13 
14 namespace composer {
15  namespace core {
16  namespace model {
17 
18 Project::Project(QObject *parent) :
19  Entity(parent)
20 {
21  setType("document");
22  dirty = false;
23  entities[this->getUniqueId()] = this;
24 
25  lockEntities = new QMutex();
26 }
27 
28 Project::Project(QMap<QString,QString> &atts, QObject *parent) :
29  Entity(atts, parent)
30 {
31  setType("document");
32  dirty = false;
33  entities[this->getUniqueId()] = this;
34 
35  lockEntities = new QMutex();
36 }
37 
38 Project::Project( QString uniqueId, QMap<QString,QString> &atts,
39  QObject *parent) :
40  Entity(uniqueId, "document", atts, parent)
41 {
42  dirty = false;
43  entities[this->getUniqueId()] = this;
44 
45  lockEntities = new QMutex();
46 }
47 
49 {
50 // QMutexLocker locker(lockEntities);
51  entities.clear();
52 
53  delete lockEntities;
54 }
55 
57 {
58  return this->projectType;
59 }
60 
61 void Project::setProjectType(LanguageType type)
62 {
63  this->projectType = type;
64 }
65 
67 {
68  QMutexLocker locker(lockEntities);
69  return entities.contains(_id) ? entities[_id] : NULL;
70 }
71 
72 QList<Entity*> Project::getEntitiesbyType(QString _type)
73 {
74  QMutexLocker locker(lockEntities);
75  QMapIterator<QString, Entity*> it(entities);
76  QList<Entity*> listRet;
77  qDebug() << "Project::getEntitiesbyType " << type;
78 
79  while(it.hasNext()){
80  it.next();
81  Entity* ent = it.value();
82  if (ent->getType() == _type)
83  listRet.append(ent);
84  }
85  return listRet;
86 }
87 
89 {
90 // QMutexLocker locker(&lockLocation);
91  return this->projectLocation;
92 }
93 
94 void Project::setLocation(QString location)
95 {
96 // QMutexLocker locker(&lockLocation);
97  this->projectLocation = location;
98 }
99 
100 bool Project::addEntity(Entity* entity, QString parentId)
102 {
103  QMutexLocker locker(lockEntities);
104  if (!entities.contains(parentId))
105  {
106  throw ParentNotFound(entity->getType(), entity->getType(), parentId);
107  return false;
108  }
109  if (entities.contains(entity->getUniqueId()))
110  {
111  throw EntityNotFound(entity->getType(), entity->getUniqueId());
112  return false;
113  }
114 
115  Entity *parent = entities[parentId];
116  parent->addChild(entity);
117  entities[entity->getUniqueId()] = entity;
118 
119  setDirty(true);
120  return true;
121 }
122 
123 bool Project::removeEntity(Entity* entity, bool appendChild)
124  throw (EntityNotFound)
125 {
126  QMutexLocker locker(lockEntities);
127  QString _id = entity->getUniqueId();
128 
129  if (entities.contains(entity->getUniqueId()))
130  {
131  Entity *parent = entity->getParent();
132  if (parent)
133  {
134  /* if (appendChild)
135  parent->removeChildAppendChildren(entity);
136  else */
137  QStack <Entity*> stack;
138  //remove all children
139  stack.push(entity);
140  while(stack.size())
141  {
142  Entity *currentEntity = stack.top();
143  stack.pop();
144  entities.remove(currentEntity->getUniqueId());
145 
146  QVector <Entity *> children = currentEntity->getChildren();
147  for(int i = 0; i < children.size(); i++)
148  stack.push(children.at(i));
149  }
150 
151  // DELETE the entity and its children recursivelly
152  parent->deleteChild(entity);
153  }
154  else
155  { // does not have a parent, so don't append
156  delete entity;
157  entity = NULL;
158  }
159  } else
160  {
161  throw EntityNotFound(entity->getType(), entity->getUniqueId());
162  return false; // entity does not exist in the model
163  }
164 
165  // qDebug() << entities;
166 
167  setDirty(true);
168  return true;
169 }
170 
173 {
174  QString result = "";
175  result += "#COMPOSER_PROJECT name=\"" + this->projectName
176  + "\" version=\"0.1\"#\n";
177 
178  result += "#COMPOSER_MODEL#\n";
179  result += Entity::toString(0);
180  result += "#END_COMPOSER_MODEL#\n";
181 
182  QString key;
183  foreach(key, pluginData.keys())
184  {
185  result += "#COMPOSER_PLUGIN_DATA "+ key + "#\n";
186  result += pluginData[key];
187  result += "\n#END_COMPOSER_PLUGIN_DATA#\n";
188  }
189  return result;
190 }
191 
192 bool Project::setPluginData(QString pluginId, const QByteArray data)
193 {
194  this->pluginData[pluginId] = data;
195 
196  setDirty(true);
197  return true;
198 }
199 
200 QByteArray Project::getPluginData(QString pluginId)
201 {
202  if(pluginData.contains(pluginId))
203  {
204  return this->pluginData[pluginId];
205  }
206  return QByteArray();
207 }
208 
209 bool Project::isDirty()
210 {
211  return dirty;
212 }
213 
214 void Project::setDirty(bool isDirty)
215 {
216 // if(dirty != isDirty) {
217  dirty = isDirty;
218  emit dirtyProject(isDirty);
219 // }
220 }
221 
222 QString Project::generateUniqueNCLId(const QString &tagname)
223 {
224  QList <Entity*> elements = getEntitiesbyType(tagname);
225  QList <QString> currentElementsNCLID;
226  for(int i = 0; i < elements.size(); i++)
227  {
228  if(elements.at(i)->hasAttribute("id"))
229  currentElementsNCLID.push_back(elements.at(i)->getAttribute("id"));
230  }
231 
232  for(int i = 1; ; i++)
233  {
234  QString retNCLID = tagname + QString::number(i);
235  if(!currentElementsNCLID.contains(retNCLID))
236  return retNCLID;
237  }
238 }
239 
240 QList<Entity*> Project::getEntityByAttrId(const QString &id)
241 {
242  QMutexLocker locker(lockEntities);
243  QMapIterator<QString, Entity*> it(entities);
244  QList<Entity*> listRet;
245  qDebug() << "Project::getEntitiesbyType " << type;
246 
247  while(it.hasNext()){
248  it.next();
249  Entity* ent = it.value();
250  if(ent->hasAttribute("id") &&
251  ent->getAttribute("id") == id)
252  listRet.append(ent);
253  }
254  return listRet;
255 }
256 
257 } } } //end namespace