NCL Composer  0.1.5
 All Classes Functions Variables Pages
OutlineViewPlugin.cpp
1 /*
2  * Copyright 2011 TeleMidia/PUC-Rio.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library. If not, see
16  * <http://www.gnu.org/licenses/>.
17  */
18 #include "OutlineViewPlugin.h"
19 
20 #include "core/modules/LanguageControl.h"
21 
23  window(new NCLTreeWidget(0)), windowBuffering(new NCLTreeWidget(0))
24 {
25  project = NULL;
26 
27  connect ( window,
28  SIGNAL( elementAddedByUser ( QString,
29  QString,
30  QMap <QString, QString> &,
31  bool)),
32  this,
33  SLOT( elementAddedByUser( QString,
34  QString,
35  QMap <QString, QString> &,
36  bool)));
37 
38  connect (window, SIGNAL(elementRemovedByUser(QString)), this,
39  SLOT(elementRemovedByUser(QString)));
40 
41  connect(window,
42  SIGNAL(itemSelectionChanged()),
43  this,
44  SLOT(itemSelectionChanged()));
45 
46  selectedId = NULL;
47  isSyncFromTextual = false;
48 }
49 
51 {
52  if(selectedId != NULL)
53  delete selectedId;
54 
55  delete window;
56  delete windowBuffering;
57 }
58 
60 {
61  return window;
62 }
63 
64 void OutlineViewPlugin::onEntityAdded(QString pluginID, Entity *entity)
65 {
66  if(isSyncFromTextual)
67  return;
68 
69  (void) pluginID;
70  qDebug() << "OutlineViewPlugin::onEntityAdded(" << pluginID << entity << endl;
71  // QString line = "<" + entity->getType() + "> </" + entity->getType() + ">\n";
72 
73  QTreeWidgetItem *item;
74  QMap <QString, QString> attrs;
75  QMap <QString, QString>::iterator begin, end, it;
76 
77  entity->getAttributeIterator(begin, end);
78  for (it = begin; it != end; ++it)
79  {
80  attrs[it.key()] = it.value();
81  }
82 
83  if(idToItem.contains(entity->getParentUniqueId()))
84  {
85  item = window->addElement( idToItem[entity->getParentUniqueId()],
86  -1,
87  entity->getType(),
88  entity->getUniqueId(),
89  attrs,
90  0, 0);
91  }
92  else
93  {
94  item = window->addElement( 0,
95  -1,
96  entity->getType(),
97  entity->getUniqueId(),
98  attrs,
99  0, 0);
100  }
101 
102  idToItem[entity->getUniqueId()] = item;
103 
104  if(entity->getType() == "ncl" ||
105  entity->getType() == "body" ||
106  entity->getType() == "link" ||
107  entity->getType() == "media" ||
108  entity->getType() == "context" ||
109  entity->getType() == "switch" ||
110  entity->getType() == "port" ||
111  entity->getType() == "switchPort" ||
112  entity->getType() == "regionBase" ||
113  entity->getType() == "descriptorBase" ||
114  entity->getType() == "connectorBase" ||
115  entity->getType() == "ruleBase" ||
116  entity->getType() == "transitionBase" )
117  {
118  if(!attrs.keys().contains("id"))
119  {
120  attrs.insert("id", project->generateUniqueNCLId(entity->getType()));
121  emit setAttributes(entity, attrs, false);
122  }
123  }
124 
125  // \todo This must be incremental
126  clearErrorMessages();
127  // emit sendBroadcastMessage("askAllValidationMessages", NULL);
128 }
129 
131 {
132  qDebug() << "OutlineViewPlugin::onEntityAddError(" << error << ")";
133 }
134 
135 void OutlineViewPlugin::onEntityChanged(QString pluginID, Entity * entity)
136 {
137  if(isSyncFromTextual)
138  return;
139 
140  QMap <QString, QString> attrs;
141  QMap <QString, QString>::iterator begin, end, it;
142 
143  entity->getAttributeIterator(begin, end);
144  for (it = begin; it != end; ++it)
145  {
146  attrs[it.key()] = it.value();
147  }
148 
149  idToItem[entity->getUniqueId()]->setTextColor(0, Qt::black);
150  idToItem[entity->getUniqueId()]->setToolTip(0, "");
151 
152  window->updateItem(idToItem[entity->getUniqueId()], entity->getType(),
153  attrs);
154 
155  // \todo This must be incremental
156  clearErrorMessages();
157  // emit sendBroadcastMessage("askAllValidationMessages", NULL);
158 }
159 
160 void OutlineViewPlugin::onEntityRemoved(QString pluginID, QString entityID)
161 {
162  if(isSyncFromTextual)
163  return;
164 
165  (void) pluginID;
166  // qDebug() << "OutlineViewPlugin::onEntityRemoved ("<< pluginID << " "
167  // << entityID << ")";
168  // qDebug() << idToItem.contains(entityID);
169 
170  if(idToItem.contains(entityID))
171  {
172  idToItem.remove(entityID);
173  window->removeItem(entityID);
174  if (selectedId != NULL && entityID == *selectedId)
175  {
176  delete selectedId;
177  selectedId = NULL;
178  }
179  }
180 
181  clearErrorMessages();
182 }
183 
184 void OutlineViewPlugin::elementRemovedByUser(QString itemId)
185 {
186  Entity *entity = project->getEntityById(itemId);
187  emit removeEntity(entity, false);
188 }
189 
190 void OutlineViewPlugin::elementAddedByUser( QString type,
191  QString parentId,
192  QMap <QString, QString> & atts,
193  bool force)
194 {
195  /* If there is no parent, put as child of root */
196  if(parentId == "")
197  parentId = project->getUniqueId();
198 
199  emit addEntity(type, parentId, atts, force);
200 }
201 
203 {
204  //TODO: All
205  return true;
206 }
207 
209 {
210  window->hide();
211  window->collapseAll();
212 
213  // \todo This could be a default implementation for updateFromModel
214  window->clear();
215  idToItem.clear();
216 
217  if(project->getChildren().size())
218  {
219  Entity *entity = project;
220  QList <Entity *> entities;
221  entities.push_back(entity);
222  bool first = true;
223 
224  while(entities.size())
225  {
226  entity = entities.front();
227  entities.pop_front();
228 
229  if(!first) //ignore the project root
230  onEntityAdded("xxx", entity);
231  else
232  first = false;
233 
234  QVector<Entity *> children = entity->getChildren();
235  for(int i = 0; i < children.size(); i++)
236  {
237  entities.push_back(children.at(i));
238  }
239  }
240  }
241 
242  window->expandAll();
243  window->show();
244 }
245 
247 {
248  //Clear previous tree
249  QString key;
250  foreach(key, idToItem.keys())
251  {
252  window->removeItem(key);
253  }
254  idToItem.clear();
255 
256  //Draw new tree
257  if(!project->getChildren().size()) return;
258 
259  QTreeWidgetItem *item;
260  QStack <Entity*> stack;
261  Entity *entity = project->getChildren().at(0);
262 
263  QMap <QString, QString> attrs;
264  QMap <QString, QString>::iterator begin, end, it;
265 
266  entity->getAttributeIterator(begin, end);
267  for (it = begin; it != end; ++it)
268  {
269  attrs[it.key()] = it.value();
270  }
271 
272  item = window->addElement( 0,
273  -1,
274  entity->getType(),
275  entity->getUniqueId(),
276  attrs,
277  0, 0);
278 
279  idToItem[entity->getUniqueId()] = item;
280  stack.push(entity);
281 
282  while(stack.size() > 0)
283  {
284  entity = stack.top();
285  stack.pop();
286 
287  QVector <Entity *> children = entity->getChildren();
288  for(int i = 0; i < children.size(); i++)
289  {
290  if(idToItem.contains(children.at(i)->getUniqueId())) continue;
291 
292  attrs.clear();
293  children.at(i)->getAttributeIterator(begin, end);
294  for (it = begin; it != end; ++it)
295  {
296  attrs[it.key()] = it.value();
297  }
298 
299  item = window->addElement( idToItem[entity->getUniqueId()],
300  -1,
301  children.at(i)->getType(),
302  children.at(i)->getUniqueId(),
303  attrs,
304  0, 0);
305 
306  idToItem[children.at(i)->getUniqueId()] = item;
307  stack.push_front(children.at(i));
308  }
309  }
310 }
311 
312 void OutlineViewPlugin::debugHasSendClearAll(QString pluginID, void *param)
313 {
314  (void) pluginID;
315  (void) param;
316 
317  qDebug() << "OutlineViewPlugin::debugHasSendClearAll";
318 }
319 
320 void OutlineViewPlugin::itemSelectionChanged()
321 {
322  if(selectedId != NULL)
323  {
324  delete selectedId;
325  selectedId = NULL;
326  }
327 
328  QList <QTreeWidgetItem*> selecteds = window->selectedItems();
329 
330  if(selecteds.size())
331  {
332  selectedId = new QString(selecteds.at(0)->text(2));
333  emit sendBroadcastMessage("changeSelectedEntity", selectedId);
334  }
335 }
336 
337 void OutlineViewPlugin::changeSelectedEntity(QString pluginID, void *param)
338 {
339  if(isSyncFromTextual)
340  return;
341 
342  if(pluginID != this->pluginInstanceID)
343  {
344  QString *id = (QString*)param;
345  QTreeWidgetItem *item = window->getItemById(*id);
346  if(item != NULL)
347  window->setCurrentItem(item, 0);
348  else
349  qWarning() << "The OutlineViewPlugin receive a message to select an"
350  << " Entity that it doesn't know.";
351  }
352 }
353 
354 void OutlineViewPlugin::textualStartSync(QString, void*)
355 {
356  isSyncFromTextual = true;
357 }
358 
359 void OutlineViewPlugin::textualFinishSync(QString, void*)
360 {
361  isSyncFromTextual = false;
362 
363  updateFromModel();
364 }
365 
366 void OutlineViewPlugin::clearErrorMessages()
367 {
368  if(isSyncFromTextual)
369  return;
370 
371  qDebug() << "OutlineViewPlugin::clearErrorMessages" << endl;
372 
373  foreach (QTreeWidgetItem *item, idToItem.values())
374  {
375  item->setTextColor(0, Qt::black);
376  item->setToolTip(0, "");
377  }
378 }
379 
381 {
382  clearErrorMessages();
383 }
384 
385 void OutlineViewPlugin::validationError(QString pluginID, void * param)
386 {
387  if(isSyncFromTextual)
388  return;
389 
390  if (param)
391  {
392  pair <QString , QString> *p = (pair <QString, QString> *) param;
393 
394  QString uid = p->first;
395 
396  QTreeWidgetItem *item = window->getItemById(uid);
397  if(item != NULL)
398  {
399  item->setTextColor(0, Qt::red);
400  item->setToolTip(0, p->second);
401  }
402  }
403 }