NCL Composer  0.1.5
 All Classes Functions Variables Pages
PropertyEditor.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 "PropertyEditor.h"
19 
20 #include <QMap>
21 #include <QSpinBox>
22 #include <QPushButton>
23 #include <QTableWidget>
24 #include <QHBoxLayout>
25 #include <QWidget>
26 #include <QFileDialog>
27 #include <QRect>
28 
29 #include "PropertyButton.h"
30 #include "NCLStructure.h"
31 
32 using namespace composer::language;
33 
35  QWidget(parent)
36 {
37  ui = new Ui::PropertyEditorWidget();
38  ui->setupUi(this);
39 
40  connect(ui->tableWidget,
41  SIGNAL(itemChanged(QTableWidgetItem *)),
42  this,
43  SLOT(updateWithItemChanges(QTableWidgetItem *)),
44  Qt::DirectConnection);
45 
46  connect(ui->filterLineEdit,
47  SIGNAL(filterTextChanged(const QString&)),
48  this,
49  SLOT(filterProperties(const QString&)));
50 
51  internalPropertyChange = false;
52 }
53 
55 {
56 
57 }
58 
59 void PropertyEditor::setTagname(QString tagname, QString name)
60 {
61  this->currentTagname = tagname;
62  //this->currentFilterString = "";
63 
64  //Clear previous items
65  propertyToLine.clear();
66  propertyToValue.clear();
67 
68  while(ui->tableWidget->rowCount())
69  ui->tableWidget->removeRow(0);
70 
71  setCurrentName(name);
72 
73  // add the new ones
74  map <QString, bool> *attrs =
75  NCLStructure::getInstance()->getAttributes(currentTagname);
76 
77  if(attrs != NULL)
78  {
79  map <QString, bool>::iterator it;
80 
81  int i;
82  for(i=0,it = attrs->begin(); it != attrs->end(); ++it, i++)
83  {
84  QString currentAttr = (*it).first;
85  propertyToValue[currentAttr] = "";
86  }
87  }
88 
89  filterProperties(this->currentFilterString);
90 }
91 
93 {
94  this->currentName = name;
95  ui->label->setText(currentTagname + ":" + currentName);
96 }
97 
98 void PropertyEditor::setErrorMessage(QString errorMessage)
99 {
100  if(!errorMessage.isEmpty())
101  ui->label_ErrorMessage->setText(" (" + errorMessage + ")");
102  else
103  ui->label_ErrorMessage->setText("");
104 
105  if(!errorMessage.isNull() && !errorMessage.isEmpty())
106  {
107  ui->frame_Name->setStyleSheet("color: red;");
108  }
109  else
110  ui->frame_Name->setStyleSheet("color: black;");
111 
112  // ui->frame_Name->adjustSize();
113 }
114 
115 void PropertyEditor::setAttributeValue(QString property, QString value)
116 {
117  // Set the attibute just if this property is a valid property of the current
118  // tagname.
119  // Also, if propertyToLine does not contains property it is not been showed by
120  // the filter.
121  if(propertyToValue.contains(property) && propertyToLine.contains(property))
122  {
123  int line = propertyToLine.value(property);
124 
125  // \todo This must be improved to use NCLStructure
126  if(isURL(currentTagname, property))
127  {
128  PropertyButtons *item = dynamic_cast <PropertyButtons*>
129  (ui->tableWidget->cellWidget(line, 1));
130  if(item)
131  {
132  // Try to update if the values are not equal
133  if(item->text() != value)
134  {
135 // internalPropertyChange = true;
136  item->setText(value);
137  propertyToValue[property] = value;
138  }
139  }
140  }
141  else
142  {
143  QTableWidgetItem *item = ui->tableWidget->item(line, 1);
144  if(item)
145  {
146  // Try to update if the values are not equal
147  if(item->text() != value)
148  {
149  internalPropertyChange = true;
150  item->setText(value);
151  propertyToValue[property] = value;
152  }
153  }
154  }
155  }
156 }
157 
158 void PropertyEditor::updateWithItemChanges(QTableWidgetItem *item)
159 {
160 // qDebug() << "updateWithItemChanges " << internalPropertyChange;
161  int row = ui->tableWidget->row(item);
162  int column = ui->tableWidget->column(item);
163 
164  if(column == 0) return; //not for me!
165 
166  if(internalPropertyChange)
167  {
168  internalPropertyChange = false;
169  return;
170  }
171 
172  QTableWidgetItem *leftItem = ui->tableWidget->item(row, column-1);
173  QString name = "", value = "";
174 
175  if(item != NULL)
176  {
177  name = leftItem->text();
178  }
179  else return; //property with empty name isn't usefull
180 
181  if(item != NULL)
182  {
183  value = item->text();
184  }
185 
186  propertyToValue[name] = value; //update internal map
187  emit propertyChanged(name, value);
188 }
189 
190 void PropertyEditor::filterProperties(const QString& text)
191 {
192  this->currentFilterString = text;
193  while(ui->tableWidget->rowCount())
194  ui->tableWidget->removeRow(0);
195 
196  propertyToLine.clear();
197 
198  QString attr;
199  foreach( attr, propertyToValue.keys() )
200  {
201  if(attr.toLower().startsWith(text.toLower()))
202  {
203  QTableWidgetItem *item = new QTableWidgetItem(attr);
204  QTableWidgetItem *itemValue = new QTableWidgetItem(propertyToValue[attr]);
205 
206  ui->tableWidget->insertRow(ui->tableWidget->rowCount());
207  internalPropertyChange = true;
208  ui->tableWidget->setItem(ui->tableWidget->rowCount()-1, 0, item);
209  propertyToLine.insert(attr, ui->tableWidget->rowCount()-1);
210 
211  internalPropertyChange = true;
212  ui->tableWidget->setItem(ui->tableWidget->rowCount()-1, 1, itemValue);
213 
214  if(isURL(currentTagname, attr))
215  {
216  PropertyButtons *control = new PropertyButtons( attr );
217  ui->tableWidget->setCellWidget(ui->tableWidget->rowCount()-1, 1,
218  control);
219 
220  control->setText(propertyToValue[attr]);
221  control->show();
222 
223  QObject::connect(control,SIGNAL(newValue(QString,QString)),
224  SIGNAL(propertyChanged(QString,QString)));
225  }
226  }
227  }
228 }
229 
230 
231 bool PropertyEditor::isURL(const QString &tagname, const QString &attr)
232 {
233  // \todo This must be improved to use NCLStructure
234  return (attr == "src" ||
235  attr == "focusSrc" ||
236  attr == "focusSelSrc" ||
237  attr == "documentURI");
238 }