NCL Composer  0.1.5
 All Classes Functions Variables Pages
PropertiesViewPlugin.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 "PropertiesViewPlugin.h"
19 #include "core/modules/LanguageControl.h"
20 
21 #include <QDockWidget>
22 
24 {
25  window = new PropertyEditor(0);
26  project = NULL;
27  currentEntity = NULL;
28 
29  connect( window, SIGNAL(propertyChanged(QString, QString)),
30  this, SLOT(updateCurrentEntityAttr(QString, QString)) );
31 }
32 
34 {
35  //delete window;
36 }
37 
39 {
40  return window;
41 }
42 
43 void PropertiesViewPlugin::onEntityAdded(QString pluginID, Entity *entity)
44 {
45  QString line = "<" + entity->getType() + "> </" + entity->getType() + ">\n";
46 }
47 
49 {
50  qDebug() << "PropertiesViewPlugin::onEntityAddError(" << error << ")";
51 }
52 
53 void PropertiesViewPlugin::onEntityChanged(QString pluginID, Entity * entity)
54 {
55  QString line = "PLUGIN (" + pluginID + ") changed the Entity (" +
56  entity->getType() + " - " + entity->getUniqueId() +")";
57 
58  if(entity != NULL && currentEntity != NULL)
59  {
60  if(entity->getUniqueId() == currentEntity->getUniqueId())
61  updateCurrentEntity();
62  }
63 }
64 
65 void PropertiesViewPlugin::onEntityRemoved(QString pluginID, QString entityID)
66 {
67  QString line = "PLUGIN (" + pluginID + ") removed Entity (" +
68  entityID + ")";
69 
70  if(entityID == currentEntityId)
71  {
72  currentEntity = NULL;
73  window->setTagname("", "");
74  currentEntityId = "";
75  }
76 }
77 
79 {
80  //TODO: All
81  return true;
82 }
83 
85 {
86  //TODO: All
87  /*
88  QPushButton *refresh = new QPushButton(window);
89  refresh->setIcon(QIcon(":/mainwindow/refreshplugin"));
90  ((QDockWidget*)window->parent())->titleBarWidget()->layout()
91  ->addWidget(refresh);
92  */
93 }
94 
95 void PropertiesViewPlugin::changeSelectedEntity(QString pluginID, void *param)
96 {
97  QString *id = (QString*) param;
98  if(id != NULL && *id != "") {
99  currentEntity = project->getEntityById(*id);
100  currentEntityId = *id;
101  }
102 
103  if(currentEntity != NULL)
104  {
105  window->setTagname(currentEntity->getType(), "");
106  updateCurrentEntity();
107  }
108 
109  emit sendBroadcastMessage("askAllValidationMessages", NULL);
110 }
111 
112 void PropertiesViewPlugin::updateCurrentEntity(QString errorMessage)
113 {
114  QString name;
115  if( currentEntity->hasAttribute("id") )
116  name = currentEntity->getAttribute("id");
117  else if(currentEntity->hasAttribute("name"))
118  name = currentEntity->getAttribute("name");
119  else
120  name = "Unknown";
121 
122  if(currentEntity->getType() != window->getTagname())
123  window->setTagname(currentEntity->getType(), name);
124  else if(window->getCurrentName() != name)
125  window->setCurrentName(name);
126 
127  window->setErrorMessage(errorMessage);
128 
129  QMap <QString, QString>::iterator begin, end, it;
130  currentEntity->getAttributeIterator(begin, end);
131  for (it = begin; it != end; ++it)
132  {
133  window->setAttributeValue(it.key(), it.value());
134  }
135 }
136 
137 void PropertiesViewPlugin::updateCurrentEntityAttr(QString attr, QString value)
138 {
139  if (currentEntity != NULL)
140  {
141  if(currentEntity->hasAttribute(attr) &&
142  currentEntity->getAttribute(attr) == value)
143  {
144  //do nothing
145  return;
146  }
147  else
148  {
149  QMap <QString, QString> attrs;
150  QMap <QString, QString>::iterator begin, end, it;
151  currentEntity->getAttributeIterator(begin, end);
152 
153  for (it = begin; it != end; ++it)
154  {
155  if(it.key() == attr)
156  {
157  if(!value.isNull() && !value.isEmpty())
158  {
159  if(PropertyEditor::isURL(currentEntity->getType(), attr))
160  {
161  try {
162  value = Utilities::relativePath(project->getLocation(), value,
163  true);
164  }
165  catch(...){
166  qDebug() << "Do not changing to a relative path";
167  }
168  }
169  attrs.insert(attr, value);
170  }
171  }
172  else
173  attrs.insert(it.key(), it.value());
174  }
175 
176  if(!attrs.contains(attr))
177  {
178  if(!value.isNull() && !value.isEmpty() && value != "")
179  {
180  if(PropertyEditor::isURL(currentEntity->getType(), attr))
181  {
182  try {
183  value = Utilities::relativePath(project->getLocation(), value,
184  true);
185  }
186  catch(...){
187  qDebug() << "Do not changing to a relative path";
188  }
189  }
190  attrs.insert(attr, value);
191  }
192  }
193 
194  emit setAttributes(currentEntity, attrs, false);
195  }
196  }
197 }
198 
199 void PropertiesViewPlugin::validationError(QString pluginID, void * param)
200 {
201  if (param)
202  {
203  pair <QString , QString> *p = (pair <QString, QString> *) param;
204 
205  QString uid = p->first;
206 
207  if(currentEntity == project->getEntityById(uid))
208  updateCurrentEntity(p->second);
209  }
210 }