All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
PropertyBrowser.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2001-2014 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 /*!
21  \file PropertyBrowser.cpp
22 
23  \brief
24 
25  \ingroup layout
26 */
27 
28 // TerraLib
29 #include "PropertyBrowser.h"
30 #include "Properties.h"
31 
32 // Qt
33 #include <QRegExpValidator>
34 #include <QRegExp>
35 #include <QWidget>
36 #include "../../../../../../third-party/qt/propertybrowser/qtvariantproperty.h"
37 #include "../../../../../../third-party/qt/propertybrowser/qteditorfactory.h"
38 #include <QVariant>
39 
40 // STL
41 #include <algorithm> // std::find
42 
44  QObject(parent),
45  m_propertyEditor(0),
46  m_variantPropertyEditorManager(0),
47  m_strDlgManager(0),
48  m_hasGridWindows(false)
49 {
50  createManager();
51 }
52 
54 {
55  if(m_propertyEditor)
56  {
57  delete m_propertyEditor;
58  m_propertyEditor = 0;
59  }
60 
61  if(m_variantPropertyEditorManager)
62  {
63  delete m_variantPropertyEditorManager;
64  m_variantPropertyEditorManager = 0;
65  }
66 
67  if(m_strDlgManager)
68  {
69  delete m_strDlgManager;
70  m_strDlgManager = 0;
71  }
72 }
73 
75 {
76  //Qt - The Property Browser
77  m_propertyEditor = new QtTreePropertyBrowser;
78 
79  m_variantPropertyEditorManager = new QtVariantPropertyManager;
80  connect(m_variantPropertyEditorManager, SIGNAL(valueChanged(QtProperty*, const QVariant &)),
81  this, SLOT(propertyEditorValueChanged(QtProperty *, const QVariant &)));
82 
83  m_strDlgManager = new QtStringPropertyManager();
84 
85  QtVariantEditorFactory* variantPropertyEditorFactory = new QtVariantEditorFactory;
86 
87  QtDlgEditorFactory* dlgFactory = new QtDlgEditorFactory();
88 
89  connect(dlgFactory, SIGNAL(internalDlg(QWidget *, QtProperty *)), this, SLOT(onSetDlg(QWidget *, QtProperty *)));
90 
91  m_propertyEditor->setFactoryForManager(m_strDlgManager, dlgFactory);
92  m_propertyEditor->setFactoryForManager(m_variantPropertyEditorManager, variantPropertyEditorFactory);
93  m_propertyEditor->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
94  m_propertyEditor->setResizeMode(QtTreePropertyBrowser::ResizeToContents);
95 }
96 
97 void te::layout::PropertyBrowser::propertyEditorValueChanged( QtProperty *property, const QVariant &value )
98 {
99  QList<QtBrowserItem *> list = m_propertyEditor->items(property);
100  changePropertyValue(property, list);
101 
102  Property prop = getProperty(property->propertyName().toStdString());
103  changePropertyValue(prop);
104 }
105 
107 {
108  QList<QtBrowserItem *> list = m_propertyEditor->topLevelItems();
109  QListIterator<QtBrowserItem *> it(list);
110  while (it.hasNext()) {
111  QtBrowserItem *item = it.next();
112  QtProperty *prop = item->property();
113  m_idToExpanded[m_propertyToId[prop]] = m_propertyEditor->isExpanded(item);
114  }
115 }
116 
118 {
119  updateExpandState();
120 
121  QMap<QtProperty *, QString>::ConstIterator itProp = m_propertyToId.constBegin();
122  while (itProp != m_propertyToId.constEnd()) {
123  delete itProp.key();
124  itProp++;
125  }
126  m_propertyToId.clear();
127  m_idToProperty.clear();
128 }
129 
130 void te::layout::PropertyBrowser::addPropertyItem(QtProperty *property, const QString &id)
131 {
132  m_propertyToId[property] = id;
133  m_idToProperty[id] = property;
134  QtBrowserItem *item = m_propertyEditor->addProperty(property);
135  if (m_idToExpanded.contains(id))
136  m_propertyEditor->setExpanded(item, m_idToExpanded[id]);
137 }
138 
139 void te::layout::PropertyBrowser::onChangeFilter( const QString& filter )
140 {
141  QRegExp rx;
142  QString search_text = filter;
143  bool doesContain = false;
144  search_text.replace(" ","|"); // to make it possible to look up all words given for the search
145  rx.setPattern(search_text);
146  rx.setCaseSensitivity(Qt::CaseInsensitive);
147 
148  QList<QtProperty*> list = m_propertyEditor->properties();
149  foreach( QtProperty* prop, list)
150  {
151  doesContain = false;
152  if(prop)
153  {
154  doesContain = prop->propertyName().contains(rx);
155  changeVisibility(m_propertyEditor->items(prop), doesContain);
156  }
157  }
158 }
159 
160 void te::layout::PropertyBrowser::changeVisibility( QList<QtBrowserItem*> items, bool visible )
161 {
162  foreach(QtBrowserItem* item, items)
163  {
164  if(item)
165  {
166  m_propertyEditor->setItemVisible(item, visible);
167  }
168  }
169 }
170 
172 {
173  return m_propertyEditor;
174 }
175 
177 {
178  return m_variantPropertyEditorManager;
179 }
180 
182 {
183  QtVariantProperty* vproperty = 0;
184 
185  te::color::RGBAColor color;
186  QColor qcolor;
187 
188  switch(property.getType())
189  {
190  case DataTypeString:
191  vproperty = m_variantPropertyEditorManager->addProperty(QVariant::String, tr(property.getName().c_str()));
192  vproperty->setValue(property.getValue().toString().c_str());
193  break;
194  case DataTypeStringList:
195  /* The type of property is enum, and so a combobox appears.
196  The type of the property value is int, as is the position in which the attribute is in the list of Enum. */
197  vproperty = m_variantPropertyEditorManager->addProperty(QtVariantPropertyManager::enumTypeId(), tr(property.getName().c_str()));
198  addAttribute(vproperty, property);
199  vproperty->setValue(property.getValue().toString().c_str());
200  break;
201  case DataTypeDouble:
202  vproperty = m_variantPropertyEditorManager->addProperty(QVariant::Double, tr(property.getName().c_str()));
203  vproperty->setValue(property.getValue().toDouble());
204  break;
205  case DataTypeInt:
206  vproperty = m_variantPropertyEditorManager->addProperty(QVariant::Int, tr(property.getName().c_str()));
207  vproperty->setValue(property.getValue().toInt());
208  break;
209  case DataTypeBool:
210  vproperty = m_variantPropertyEditorManager->addProperty(QVariant::Bool, tr(property.getName().c_str()));
211  vproperty->setValue(property.getValue().toBool());
212  break;
213  case DataTypeColor:
214  vproperty = m_variantPropertyEditorManager->addProperty(QVariant::Color, tr(property.getName().c_str()));
215  color = property.getValue().toColor();
216  qcolor.setRed(color.getRed());
217  qcolor.setGreen(color.getGreen());
218  qcolor.setBlue(color.getBlue());
219  vproperty->setValue(qcolor);
220  break;
221  default:
222  vproperty = 0;
223  }
224 
225  if(vproperty)
226  {
227  addPropertyItem(vproperty, QLatin1String(property.getName().c_str()));
228  return true;
229  }
230  return false;
231 }
232 
233 void te::layout::PropertyBrowser::addAttribute( QtVariantProperty* vproperty, Property property )
234 {
235  std::vector<Variant> vrt = property.getOptionChoices();
236  QStringList strList;
237  foreach( Variant v, vrt)
238  {
239  strList.push_back(v.toString().c_str());
240  }
241 
242  /* "enumNames" is a name used by default in
243  QtVariantProperty class for properties with lists */
244  vproperty->setAttribute("enumNames", strList);
245 }
246 
248 {
249  return true;
250 }
251 
253 {
254  Property prop;
255  prop.setName(name);
256 
257  QVariant variant = findPropertyValue(name);
258  QtProperty* property = findProperty(name);
259  LayoutPropertyDataType type = getLayoutType(variant.type(), name);
260 
261  QtVariantProperty* vproperty = 0;
262  if(property)
263  {
264  vproperty = dynamic_cast<QtVariantProperty*>(property);
265  }
266 
267  QColor qcolor;
268  te::color::RGBAColor color;
269  Variant v;
270  QStringList list;
271  std::string value;
272 
273  switch(type)
274  {
275  case DataTypeString:
276  prop.setValue(variant.toString().toStdString(), type);
277  break;
278  case DataTypeStringList:
279 
280  prop.setValue(variant.toString().toStdString(), type);
281  if(vproperty)
282  {
283  list = variant.toStringList();
284  value = list.value(vproperty->value().toInt()).toStdString();
285 
286  foreach(QString s, list)
287  {
288  v.clear();
289  v.setValue(s.toStdString(), DataTypeString);
290  prop.addOption(v);
291  if(value.compare(s.toStdString()) == 0)
292  {
293  prop.setOptionChoice(v);
294  }
295  }
296  }
297  break;
298  case DataTypeDouble:
299  prop.setValue(variant.toDouble(), type);
300  break;
301  case DataTypeInt:
302  prop.setValue(variant.toInt(), type);
303  break;
304  case DataTypeBool:
305  prop.setValue(variant.toBool(), type);
306  break;
308  prop.setValue(variant.toString().toStdString(), type);
309  break;
310  case DataTypeColor:
311  qcolor = variant.value<QColor>();
312  if(qcolor.isValid())
313  {
314  color.setColor(qcolor.red(), qcolor.green(), qcolor.blue(), 255);
315  prop.setValue(color, type);
316  }
317  default:
318  prop.setValue(0, DataTypeNone);
319  }
320 
321  return prop;
322 }
323 
325 {
326  QVariant variant;
327  QtVariantProperty* vproperty = 0;
328  QtProperty* prop = m_idToProperty[name.c_str()];
329 
330  if(prop)
331  {
332  vproperty = dynamic_cast<QtVariantProperty*>(prop);
333  if(vproperty)
334  {
335  variant = checkComplexType(vproperty);
336  }
337  else
338  {
339  variant.setValue(prop->valueText());
340  }
341  }
342 
343  return variant;
344 }
345 
346 QtProperty* te::layout::PropertyBrowser::findProperty( std::string name )
347 {
348  QtProperty* prop = m_idToProperty[name.c_str()];
349  return prop;
350 }
351 
352 QVariant te::layout::PropertyBrowser::checkComplexType( QtVariantProperty* property )
353 {
354  QVariant variant;
355 
356  if(!property)
357  return variant;
358 
359  variant = property->value();
360 
361  if(QtVariantPropertyManager::enumTypeId() == property->propertyType())
362  {
363  variant = property->attributeValue("enumNames");
364  QStringList list = variant.toStringList();
365  /*QString attr = list.value(property->value().toInt());*/
366  variant = QVariant(list);
367  }
368 
369  return variant;
370 }
371 
373 {
374  Properties* properties = new Properties("");
375 
376  QList<QtProperty*> props = m_propertyEditor->properties();
377  foreach( QtProperty* prop, props)
378  {
379  Property property = getProperty(prop->propertyName().toStdString());
380  properties->addProperty(property);
381  }
382 
383  return properties;
384 }
385 
387 {
389  QVariant variant;
390  QtVariantProperty* vproperty = 0;
391  int i = 0;
392 
393  switch(type)
394  {
395  case QVariant::String:
396  {
397  dataType = DataTypeString;
398 
399  //Custom types: Dialog Window Type
400  if(name.compare("") != 0)
401  {
402  QVariant variant = m_strDlgManager->property(name.c_str());
403  if(!variant.isNull())
404  {
405  if(name.compare(m_propGridSettingsName) == 0)
406  {
407  dataType = DataTypeGridSettings;
408  }
409  }
410  }
411  }
412  break;
413  case QVariant::StringList:
414  vproperty = dynamic_cast<QtVariantProperty*>(m_idToProperty[name.c_str()]);
415  if(vproperty)
416  {
417  if(QtVariantPropertyManager::enumTypeId() == vproperty->propertyType())
418  {
419  dataType = DataTypeStringList;
420  }
421  }
422  break;
423  case QVariant::Double:
424  dataType = DataTypeDouble;
425  break;
426  case QVariant::Int:
427  {
428  dataType = DataTypeInt;
429  }
430  break;
431  case QVariant::Bool:
432  dataType = DataTypeBool;
433  break;
434  case QVariant::Color:
435  dataType = DataTypeColor;
436  break;
437  default:
438  dataType = DataTypeNone;
439  }
440 
441  return dataType;
442 }
443 
445 {
446  QVariant::Type type = QVariant::Invalid;
447  switch(dataType)
448  {
449  case DataTypeString:
450  type = QVariant::String;
451  break;
452  case DataTypeStringList:
453  type = QVariant::Int;
454  break;
455  case DataTypeDouble:
456  type = QVariant::Double;
457  break;
458  case DataTypeInt:
459  type = QVariant::Int;
460  break;
461  case DataTypeBool:
462  type = QVariant::Bool;
463  break;
465  type = QVariant::String;
466  break;
467  case DataTypeColor:
468  type = QVariant::Color;
469  break;
470  default:
471  type = QVariant::Invalid;
472  }
473 
474  return type;
475 }
476 
478 {
479  return m_propGridSettingsName;
480 }
481 
483 {
484  m_hasGridWindows = hasWindows;
485  blockOpenGridWindows(!hasWindows);
486 }
487 
489 {
490 
491 }
std::string getName()
Definition: Property.cpp:49
virtual void addAttribute(QtVariantProperty *vproperty, Property property)
virtual QVariant::Type getVariantType(LayoutPropertyDataType dataType)
SimpleData< std::string, STRING_TYPE > String
Definition: SimpleData.h:229
virtual std::string getPropGridSettingsName()
virtual bool addProperty(Property property)
Definition: Properties.h:89
Variant getValue()
Definition: Property.cpp:74
virtual LayoutPropertyDataType getLayoutType(QVariant::Type type, std::string name="")
void setColor(const std::string &hexColor)
It sets the color using a two hexadecimal RGB-encoded color.
Definition: RGBAColor.h:329
int getRed() const
It returns the red component color value (a value from 0 to 255).
Definition: RGBAColor.h:295
void addOption(Variant variant)
Definition: Property.cpp:79
virtual QVariant findPropertyValue(std::string name)
QtTreePropertyBrowser * getPropertyEditor()
QtVariantPropertyManager * getVariantPropertyManager()
virtual void blockOpenGridWindows(bool block)
int getBlue() const
It returns the blue component color value (a value from 0 to 255).
Definition: RGBAColor.h:305
int getGreen() const
It returns the green component color value (a value from 0 to 255).
Definition: RGBAColor.h:300
virtual QtProperty * findProperty(std::string name)
void onChangeFilter(const QString &filter)
virtual Properties * getProperties()
void setValue(typename ValueType value, LayoutPropertyDataType type)
Definition: Variant.h:107
void setValue(typename ValueType value, LayoutPropertyDataType type)
Definition: Property.h:106
virtual bool removeProperty(Property property)
virtual bool addProperty(Property property)
SimpleData< double, DOUBLE_TYPE > Double
Definition: SimpleData.h:227
A helper class for 32-bit RGBA (Red-Green-Blue-Alpha channel) color.
Definition: RGBAColor.h:57
virtual void changeVisibility(QList< QtBrowserItem * > items, bool visible)
virtual QVariant checkComplexType(QtVariantProperty *property)
virtual void addPropertyItem(QtProperty *property, const QString &id)
virtual Property getProperty(std::string name)
LayoutPropertyDataType getType()
Definition: Property.cpp:59
PropertyBrowser(QObject *parent=0)
std::string toString()
Definition: Variant.cpp:237
virtual void setHasGridWindows(bool hasWindows=false)
void propertyEditorValueChanged(QtProperty *property, const QVariant &value)
void setName(std::string name)
Definition: Property.cpp:54
LayoutPropertyDataType
Enum LayoutPropertyDataType.
Definition: AbstractType.h:131
void setOptionChoice(Variant variant)
Definition: Property.cpp:101