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) 2008 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 "../../../core/property/Properties.h"
31 #include "../../../core/enum/Enums.h"
33 
34 // Qt
35 #include <QRegExpValidator>
36 #include <QRegExp>
37 #include <QWidget>
38 #include <QVariant>
39 #include <QFont>
40 #include <QColor>
41 
42 // QtPropertyBrowser
43 #include <QtPropertyBrowser/QtVariantPropertyManager>
44 #include <QtPropertyBrowser/QtTreePropertyBrowser>
45 #include <QtPropertyBrowser/qteditorfactory.h>
46 
47 // STL
48 #include <algorithm> // std::find
51 
53  QObject(parent),
54  m_propertyEditor(0),
55  m_variantPropertiesBrowser(0),
56  m_dialogPropertiesBrowser(0),
57  m_hasWindows(false),
58  m_changeQtPropertyVariantValue(false)
59 {
60  createManager();
61 }
62 
64 {
65  clearAll();
66 
67  if(m_variantPropertiesBrowser)
68  {
69  delete m_variantPropertiesBrowser;
70  m_variantPropertiesBrowser = 0;
71  }
72 
73  if(m_dialogPropertiesBrowser)
74  {
75  delete m_dialogPropertiesBrowser;
76  m_dialogPropertiesBrowser = 0;
77  }
78 
79  if(m_propertyEditor)
80  {
81  delete m_propertyEditor;
82  m_propertyEditor = 0;
83  }
84 }
85 
87 {
88  //Qt - The Property Browser
89  m_propertyEditor = new QtTreePropertyBrowser;
90 
91  m_variantPropertiesBrowser = new VariantPropertiesBrowser;
92 
93  connect(m_variantPropertiesBrowser->getVariantPropertyManager(), SIGNAL(valueChanged(QtProperty*, const QVariant &)),
94  this, SLOT(propertyEditorValueChanged(QtProperty *, const QVariant &)));
95 
96  m_dialogPropertiesBrowser = new DialogPropertiesBrowser;
97 
98  connect(m_dialogPropertiesBrowser, SIGNAL(changeDlgProperty(Property)), this, SLOT(onChangeDlgProperty(Property)));
99 
100  m_propertyEditor->setFactoryForManager(m_dialogPropertiesBrowser->getStringPropertyManager(), m_dialogPropertiesBrowser->getDlgEditorFactory());
101  m_propertyEditor->setFactoryForManager(m_variantPropertiesBrowser->getVariantPropertyManager(), m_variantPropertiesBrowser->getVariantEditorFactory());
102  m_propertyEditor->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
103  m_propertyEditor->setResizeMode(QtTreePropertyBrowser::ResizeToContents);
104 }
105 
106 void te::layout::PropertyBrowser::propertyEditorValueChanged( QtProperty *property, const QVariant &value )
107 {
108  if(m_changeQtPropertyVariantValue)
109  {
110  return;
111  }
112 
113  QList<QtBrowserItem *> list = m_propertyEditor->items(property);
114  changePropertyValue(property, list);
115 
116  Property prop = m_variantPropertiesBrowser->getProperty(property->propertyName().toStdString());
117 
118  if(prop.isNull())
119  {
120  prop = m_dialogPropertiesBrowser->getProperty(property->propertyName().toStdString());
121  }
122 
123  changePropertyValue(prop);
124 }
125 
127 {
128  if(m_changeQtPropertyVariantValue)
129  {
130  return;
131  }
132  changePropertyValue(property);
133 }
134 
136 {
137  QList<QtBrowserItem *> list = m_propertyEditor->topLevelItems();
138  QListIterator<QtBrowserItem *> it(list);
139  while (it.hasNext()) {
140  QtBrowserItem *item = it.next();
141  QtProperty *prop = item->property();
142  m_idToExpanded[m_propertyToId[prop]] = m_propertyEditor->isExpanded(item);
143  }
144 }
145 
147 {
148  m_dialogPropertiesBrowser->getDlgProps().clear();
149 
150  updateExpandState();
151 
152  QMap<QtProperty *, QString>::ConstIterator itProp = m_propertyToId.constBegin();
153  while (itProp != m_propertyToId.constEnd()) {
154  delete itProp.key();
155  itProp++;
156  }
157 
158  m_propertyToId.clear();
159  m_idToProperty.clear();
160 
161  m_variantPropertiesBrowser->clearAll();
162  m_dialogPropertiesBrowser->clearAll();
163 }
164 
165 void te::layout::PropertyBrowser::addPropertyItem(QtProperty *property, const QString &id)
166 {
167  m_propertyToId[property] = id;
168  m_idToProperty[id] = property;
169  QtBrowserItem *item = m_propertyEditor->addProperty(property);
170  if (m_idToExpanded.contains(id))
171  m_propertyEditor->setExpanded(item, m_idToExpanded[id]);
172 }
173 
174 void te::layout::PropertyBrowser::onChangeFilter( const QString& filter )
175 {
176  QRegExp rx;
177  QString search_text = filter;
178  bool doesContain = false;
179  search_text.replace(" ","|"); // to make it possible to look up all words given for the search
180  rx.setPattern(search_text);
181  rx.setCaseSensitivity(Qt::CaseInsensitive);
182 
183  QList<QtProperty*> list = m_propertyEditor->properties();
184  foreach( QtProperty* prop, list)
185  {
186  doesContain = false;
187  if(prop)
188  {
189  doesContain = prop->propertyName().contains(rx);
190  changeVisibility(m_propertyEditor->items(prop), doesContain);
191  }
192  }
193 }
194 
195 void te::layout::PropertyBrowser::changeVisibility( QList<QtBrowserItem*> items, bool visible )
196 {
197  foreach(QtBrowserItem* item, items)
198  {
199  if(item)
200  {
201  m_propertyEditor->setItemVisible(item, visible);
202  }
203  }
204 }
205 
207 {
208  return m_propertyEditor;
209 }
210 
212 {
213  te::color::RGBAColor color;
214  QColor qcolor;
215  QFont qfont;
216  Font font;
217 
219 
220  if(!dataType)
221  {
222  return 0;
223  }
224 
225  m_changeQtPropertyVariantValue = true;
226 
227  QtVariantProperty* vproperty = 0;
228  vproperty = m_variantPropertiesBrowser->addProperty(property);
229  if(vproperty)
230  {
231  bool is_readOnly = !property.isEditable();
232  vproperty->setAttribute(QLatin1String("readOnly"), is_readOnly);
233  addPropertyItem(vproperty, QLatin1String(property.getName().c_str()));
234  }
235  else
236  {
237  QtProperty* pproperty = 0;
238  pproperty = m_dialogPropertiesBrowser->addProperty(property);
239  if(pproperty)
240  {
241  addPropertyItem(pproperty, QLatin1String(property.getName().c_str()));
242  }
243  else
244  {
245  return pproperty;
246  }
247  }
248  m_changeQtPropertyVariantValue = false;
249  return vproperty;
250 }
251 
253 {
254  QtProperty* removeProp = 0;
255  QList<QtProperty*> list = m_propertyEditor->properties();
256  foreach( QtProperty* prop, list)
257  {
258  if(property.getName().compare(prop->propertyName().toStdString()) == 0)
259  {
260  removeProp = prop;
261  }
262  }
263 
264  if(!removeProp)
265  return false;
266 
267  m_propertyToId.remove(removeProp);
268  m_idToProperty.remove(removeProp->propertyName());
269 
270  m_variantPropertiesBrowser->removeProperty(removeProp);
271  m_dialogPropertiesBrowser->removeProperty(removeProp);
272 
273  m_propertyEditor->removeProperty(removeProp);
274 
275  if(removeProp)
276  {
277  delete removeProp;
278  removeProp = 0;
279  }
280 
281  return true;
282 }
283 
285 {
286  m_hasWindows = hasWindows;
287  blockOpenWindows(!hasWindows);
288 }
289 
291 {
292 
293 }
294 
296 {
297  QList<QtProperty*> props = m_propertyEditor->properties();
298  foreach( QtProperty* prop, props)
299  {
300  if(prop)
301  {
302  if(name.compare(prop->propertyName().toStdString()) == 0)
303  {
304  QList<QtBrowserItem *> list = m_propertyEditor->items(prop);
305  QtBrowserItem* item = list.first();
306  if(item)
307  {
308  m_propertyEditor->setCurrentItem(item);
309  }
310  }
311  }
312  }
313 }
314 
316 {
317  m_changeQtPropertyVariantValue = true;
318 
319  bool result = m_variantPropertiesBrowser->updateProperty(property);
320 
321  m_changeQtPropertyVariantValue = false;
322 
323  return result;
324 }
325 
327 {
328  foreach( Property prop, props->getProperties())
329  {
330  updateProperty(prop);
331  }
332 }
333 
335 {
336  return m_variantPropertiesBrowser;
337 }
338 
340 {
341  return m_dialogPropertiesBrowser;
342 }
343 
345 {
346  m_dialogPropertiesBrowser->closeAllWindows();
347 }
348 
350 {
351  Properties* properties = new Properties("");
352 
353  QList<QtProperty*> props = m_propertyEditor->properties();
354  foreach( QtProperty* prop, props)
355  {
356  Property property = m_variantPropertiesBrowser->getProperty(prop->propertyName().toStdString());
357 
358  if(property.isNull())
359  {
360  property = m_dialogPropertiesBrowser->getProperty(prop->propertyName().toStdString());
361  }
362 
363  properties->addProperty(property);
364  }
365 
366  return properties;
367 }
368 
369 QtProperty* te::layout::PropertyBrowser::findProperty( std::string name )
370 {
371  QtProperty* prop = 0;
372 
373  prop = m_variantPropertiesBrowser->findProperty(name);
374  if(!prop)
375  {
376  prop = m_dialogPropertiesBrowser->findProperty(name);
377  }
378  return prop;
379 }
380 
381 bool te::layout::PropertyBrowser::addSubProperty( QtProperty* prop, QtProperty* subProp )
382 {
383  if(!prop)
384  {
385  return false;
386  }
387 
388  m_propertyEditor->removeProperty(subProp);
389  prop->addSubProperty(subProp);
390  return true;
391 }
392 
394 {
395  if(prop.isNull() || subProp.isNull())
396  {
397  return false;
398  }
399 
400  QtProperty* addProp = addProperty(prop);
401  QtProperty* addSubProp = addProperty(subProp);
402 
403  return addSubProperty(addProp, addSubProp);
404 }
405 
406 
407 
408 
409 
410 
411 
412 
413 
std::string getName()
Method that returns the name of this property.
Definition: Property.cpp:57
Class specifies a font.
Definition: Font.h:46
virtual bool addProperty(Property property)
Adds the specified property to the set of properties for this object.
Definition: Properties.h:193
virtual EnumDataType * getEnumDataType()
Returns data type enumeration.
Definition: Enums.cpp:52
Manage properties variants values.
virtual void selectProperty(std::string name)
QtTreePropertyBrowser * getPropertyEditor()
bool isNull()
Returns true if no value has been set, false otherwise.
Definition: Property.cpp:146
The Properties class represents a persistent set of properties. The Properties can be saved to a file...
Definition: Properties.h:52
virtual std::vector< Property > getProperties()
Returns set of all properties.
Definition: Properties.h:220
virtual QtProperty * findProperty(std::string name)
VariantPropertiesBrowser * getVariantPropertiesBrowser()
Manage properties dialogs values.
virtual void setHasWindows(bool hasWindows=false)
void onChangeFilter(const QString &filter)
virtual Properties * getProperties()
static Enums & getInstance()
It returns a reference to the singleton instance.
Class to represent a data type enumeration. Ex.: int, double, bool, te::color::RGBAColor (color)...
Definition: EnumDataType.h:48
virtual bool removeProperty(Property property)
virtual bool addSubProperty(QtProperty *prop, QtProperty *subProp)
virtual bool updateProperty(Property property)
Manage properties dialogs values.
Manage properties variants values.
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 void addPropertyItem(QtProperty *property, const QString &id)
virtual void blockOpenWindows(bool block)
virtual void updateProperties(Properties *props)
PropertyBrowser(QObject *parent=0)
void propertyEditorValueChanged(QtProperty *property, const QVariant &value)
virtual void onChangeDlgProperty(Property property)
A property acts like a attribute member of a object and stores the state of this attribute. A set of properties stores the state of an object. Any data type, not included in the convertValue method in the class te::layout::Variant, it will be by default "std::string" value.
Definition: Property.h:47
DialogPropertiesBrowser * getDialogPropertiesBrowser()
virtual QtProperty * addProperty(Property property)