All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
VariantPropertiesBrowser.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
30 #include "../../../core/property/Properties.h"
31 #include "../../../core/enum/Enums.h"
32 
33 // Qt
34 #include <QVariant>
35 #include <QFont>
36 #include <QColor>
37 
40  m_variantPropertyEditorManager(0),
41  m_variantPropertyEditorFactory(0)
42 {
43  createManager();
44 }
45 
47 {
48  if(m_variantPropertyEditorManager)
49  {
50  delete m_variantPropertyEditorManager;
51  m_variantPropertyEditorManager = 0;
52  }
53 
54  if(m_variantPropertyEditorFactory)
55  {
56  delete m_variantPropertyEditorFactory;
57  m_variantPropertyEditorFactory = 0;
58  }
59 }
60 
62 {
63  m_variantPropertyEditorManager = new QtVariantPropertyManager;
64  m_variantPropertyEditorFactory = new QtVariantEditorFactory;
65 }
66 
68 {
69  QtVariantProperty* vproperty = 0;
70 
72  QColor qcolor;
73  QFont qfont;
74  Font font;
75 
76  if(!m_variantPropertyEditorManager)
77  {
78  return vproperty;
79  }
80 
82 
83  if(!dataType)
84  {
85  return vproperty;
86  }
87 
88  int type = getVariantType(property.getType());
89  vproperty = m_variantPropertyEditorManager->addProperty(type, tr(property.getName().c_str()));
90  changeQtVariantPropertyValue(vproperty, property);
91 
92  addPropertyItem(vproperty, QLatin1String(property.getName().c_str()));
93 
94  return vproperty;
95 }
96 
97 void te::layout::VariantPropertiesBrowser::addAttribute( QtVariantProperty* vproperty, Property property )
98 {
99  std::vector<Variant> vrt = property.getOptionChoices();
100  QStringList strList;
101  foreach( Variant v, vrt)
102  {
103  strList.push_back(v.toString().c_str());
104  }
105 
106  /* "enumNames" is a name used by default in
107  QtVariantProperty class for properties with lists */
108  vproperty->setAttribute("enumNames", strList);
109 }
110 
112 {
113  Property prop;
114  prop.setName(name);
115 
116  QVariant variant = findPropertyValue(name);
117 
118  if(variant.isNull() || !variant.isValid())
119  {
120  return prop;
121  }
122 
123  QtProperty* property = findProperty(name);
124  EnumType* type = getLayoutType(variant.type(), name);
125 
126  QtVariantProperty* vproperty = 0;
127  if(property)
128  {
129  vproperty = dynamic_cast<QtVariantProperty*>(property);
130  }
131 
133 
134  QColor qcolor;
135  te::color::RGBAColor color;
136  Variant v;
137  QStringList list;
138  std::string value;
139  Font font;
140  QFont qfont;
141 
142  if(type == dataType->getDataTypeString())
143  {
144  prop.setValue(variant.toString().toStdString(), type);
145  }
146  else if(type == dataType->getDataTypeStringList())
147  {
148  prop.setValue(variant.toString().toStdString(), type);
149  if(vproperty)
150  {
151  list = variant.toStringList();
152  value = list.value(vproperty->value().toInt()).toStdString();
153 
154  foreach(QString s, list)
155  {
156  v.clear();
157  v.setValue(s.toStdString(), dataType->getDataTypeString());
158  prop.addOption(v);
159  if(value.compare(s.toStdString()) == 0)
160  {
161  prop.setOptionChoice(v);
162  }
163  }
164  }
165  }
166  else if(type == dataType->getDataTypeDouble())
167  {
168  prop.setValue(variant.toDouble(), type);
169  }
170  else if(type == dataType->getDataTypeInt())
171  {
172  prop.setValue(variant.toInt(), type);
173  }
174  else if(type == dataType->getDataTypeBool())
175  {
176  prop.setValue(variant.toBool(), type);
177  }
178  else if(type == dataType->getDataTypeColor())
179  {
180  qcolor = variant.value<QColor>();
181  if(qcolor.isValid())
182  {
183  color.setColor(qcolor.red(), qcolor.green(), qcolor.blue(), qcolor.alpha());
184  prop.setValue(color, type);
185  }
186  }
187  else if(type == dataType->getDataTypeFont())
188  {
189  qfont = variant.value<QFont>();
190  font.setFamily(qfont.family().toStdString());
191  font.setPointSize(qfont.pointSize());
192  font.setBold(qfont.bold());
193  font.setItalic(qfont.italic());
194  font.setUnderline(qfont.underline());
195  font.setStrikeout(qfont.strikeOut());
196  font.setKerning(qfont.kerning());
197  prop.setValue(font, type);
198  }
199 
200  return prop;
201 }
202 
204 {
206 
207  EnumType* dataType = dtType->getDataTypeNone();
208  QVariant variant;
209  QtVariantProperty* vproperty = 0;
210  QtProperty* prop = 0;
211 
212  switch(type)
213  {
214  case QVariant::String:
215  {
216  dataType = dtType->getDataTypeString();
217  }
218  break;
219  case QVariant::StringList:
220  prop = findProperty(name);
221  vproperty = dynamic_cast<QtVariantProperty*>(prop);
222  if(vproperty)
223  {
224  if(QtVariantPropertyManager::enumTypeId() == vproperty->propertyType())
225  {
226  dataType = dtType->getDataTypeStringList();
227  }
228  else if(QtVariantPropertyManager::groupTypeId() == vproperty->propertyType())
229  {
230  dataType = dtType->getDataTypeGroup();
231  }
232  }
233  break;
234  case QVariant::Double:
235  dataType = dtType->getDataTypeDouble();
236  break;
237  case QVariant::Int:
238  {
239  dataType = dtType->getDataTypeInt();
240  }
241  break;
242  case QVariant::Bool:
243  dataType = dtType->getDataTypeBool();
244  break;
245  case QVariant::Color:
246  dataType = dtType->getDataTypeColor();
247  break;
248  case QVariant::Font:
249  dataType = dtType->getDataTypeFont();
250  break;
251  default:
252  dataType = dtType->getDataTypeNone();
253  }
254 
255  return dataType;
256 }
257 
259 {
260  int type = QVariant::Invalid;
261 
263 
264  if(!dtType)
265  {
266  return type;
267  }
268 
269  if(dataType == dtType->getDataTypeString())
270  {
271  type = QVariant::String;
272  }
273  else if(dataType == dtType->getDataTypeStringList())
274  {
275  type = QtVariantPropertyManager::enumTypeId();
276  }
277  else if(dataType == dtType->getDataTypeGroup())
278  {
279  type = QtVariantPropertyManager::groupTypeId();
280  }
281  else if(dataType == dtType->getDataTypeDouble())
282  {
283  type = QVariant::Double;
284  }
285  else if(dataType == dtType->getDataTypeInt())
286  {
287  type = QVariant::Int;
288  }
289  else if(dataType == dtType->getDataTypeBool())
290  {
291  type = QVariant::Bool;
292  }
293  else if(dataType == dtType->getDataTypeColor())
294  {
295  type = QVariant::Color;
296  }
297  else if(dataType == dtType->getDataTypeFont())
298  {
299  type = QVariant::Font;
300  }
301 
302  return type;
303 }
304 
306 {
307  bool result = true;
308 
309  QColor qcolor;
310  QFont qfont;
311  te::color::RGBAColor color;
312  Font font;
313 
314  m_changeProperty = true;
315 
316  if(!vproperty)
317  {
318  return false;
319  }
320 
322 
323  if(property.getType() == dataType->getDataTypeString())
324  {
325  vproperty->setValue(property.getValue().toString().c_str());
326  }
327  else if(property.getType() == dataType->getDataTypeStringList())
328  {
329  addAttribute(vproperty, property);
330  vproperty->setValue(property.getOptionByCurrentChoice().toString().c_str());
331  }
332  else if(property.getType() == dataType->getDataTypeGroup())
333  {
334  vproperty->setValue(property.getValue().toString().c_str());
335  }
336  else if(property.getType() == dataType->getDataTypeDouble())
337  {
338  vproperty->setValue(property.getValue().toDouble());
339  }
340  else if(property.getType() == dataType->getDataTypeInt())
341  {
342  vproperty->setValue(property.getValue().toInt());
343  }
344  else if(property.getType() == dataType->getDataTypeBool())
345  {
346  vproperty->setValue(property.getValue().toBool());
347  }
348  else if(property.getType() == dataType->getDataTypeColor())
349  {
350  color = property.getValue().toColor();
351  qcolor.setRed(color.getRed());
352  qcolor.setGreen(color.getGreen());
353  qcolor.setBlue(color.getBlue());
354  qcolor.setAlpha(color.getAlpha());
355  vproperty->setValue(qcolor);
356  }
357  else if(property.getType() == dataType->getDataTypeFont())
358  {
359  font = property.getValue().toFont();
360  qfont.setFamily(font.getFamily().c_str());
361  qfont.setPointSize(font.getPointSize());
362  qfont.setBold(font.isBold());
363  qfont.setItalic(font.isItalic());
364  qfont.setUnderline(font.isUnderline());
365  qfont.setStrikeOut(font.isStrikeout());
366  qfont.setKerning(font.isKerning());
367  vproperty->setValue(qfont);
368  }
369  else
370  {
371  result = false;
372  }
373 
374  m_changeProperty = false;
375 
376  return result;
377 }
378 
380 {
381  return m_variantPropertyEditorFactory;
382 }
383 
385 {
386  return m_variantPropertyEditorManager;
387 }
388 
390 {
391  std::string name = property.getName();
392  QtProperty* qprop = findProperty(name);
393  QColor qcolor;
394  te::color::RGBAColor color;
395  Font font;
396  QFont qfont;
397 
398  QtVariantProperty* vproperty = m_variantPropertyEditorManager->variantProperty(qprop);
399  if(!vproperty)
400  {
401  return false;
402  }
403 
404  changeQtVariantPropertyValue(vproperty, property);
405 
406  return true;
407 }
408 
409 
410 
411 
std::string getName()
Method that returns the name of this property.
Definition: Property.cpp:57
virtual bool updateProperty(Property property)
Class specifies a font.
Definition: Font.h:46
SimpleData< std::string, STRING_TYPE > String
Definition: SimpleData.h:229
bool isBold()
Returns true if font use bold, false otherwise.
Definition: Font.cpp:81
Variant getValue()
Returns stored value.
Definition: Property.cpp:72
virtual EnumDataType * getEnumDataType()
Returns data type enumeration.
Definition: Enums.cpp:52
void setColor(const std::string &hexColor)
It sets the color using a two hexadecimal RGB-encoded color.
Definition: RGBAColor.h:329
bool isStrikeout()
Returns true if font use strikeout, false otherwise.
Definition: Font.cpp:111
void setStrikeout(bool strikeout)
Sets font with strikeout style.
Definition: Font.cpp:106
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:77
virtual EnumType * getDataTypeBool() const
Returns value that represents type bool belonging to enumeration.
virtual void addAttribute(QtVariantProperty *vproperty, Property property)
void setKerning(bool kerning)
Sets font with kerning style.
Definition: Font.cpp:116
virtual EnumType * getDataTypeDouble() const
Returns value that represents type double belonging to enumeration.
virtual EnumType * getDataTypeInt() const
Returns value that represents type integer belonging to enumeration.
int getPointSize()
Returns point size of the font.
Definition: Font.cpp:71
virtual EnumType * getDataTypeNone() const
Returns value that represents type none belonging to enumeration.
int getBlue() const
It returns the blue component color value (a value from 0 to 255).
Definition: RGBAColor.h:305
Variant getOptionByCurrentChoice()
Definition: Property.cpp:104
int getGreen() const
It returns the green component color value (a value from 0 to 255).
Definition: RGBAColor.h:300
virtual EnumType * getLayoutType(QVariant::Type type, std::string name="")
virtual EnumType * getDataTypeGroup() const
Returns value that represents type Group (string) belonging to enumeration.
void setPointSize(int point)
Sets point size of the font.
Definition: Font.cpp:66
virtual Property getProperty(std::string name)
void setBold(bool bold)
Sets font with bold style.
Definition: Font.cpp:76
static Enums & getInstance()
It returns a reference to the singleton instance.
QtVariantPropertyManager * getVariantPropertyManager()
QtVariantEditorFactory * getVariantEditorFactory()
virtual QtVariantProperty * addProperty(Property property)
Class to represent a data type enumeration. Ex.: int, double, bool, te::color::RGBAColor (color)...
Definition: EnumDataType.h:48
virtual void clear()
Reset state of object. Null state.
Definition: Variant.cpp:356
void setFamily(std::string family)
Returns font family name.
Definition: Font.cpp:56
int getAlpha() const
It returns the alpha component color value (a value from 0 to 255).
Definition: RGBAColor.h:310
Class acts like a union for some C++/TerraLib5 data types. Responsible for storing the value...
Definition: Variant.h:80
virtual bool changeQtVariantPropertyValue(QtVariantProperty *vproperty, Property property)
void setUnderline(bool underline)
Sets font with underline style.
Definition: Font.cpp:96
bool isItalic()
Returns true if font use italic, false otherwise.
Definition: Font.cpp:91
virtual EnumType * getDataTypeColor() const
Returns value that represents type te::color::RGBAColor** (color) belonging to enumeration.
bool isUnderline()
Returns true if font use underline, false otherwise.
Definition: Font.cpp:101
double toDouble()
Returns the value of double type. (The setValue method received a double)
Definition: Variant.cpp:316
virtual int getVariantType(EnumType *dataType)
void setValue(ValueType value, EnumType *type)
Stores a copy of value.
Definition: Property.h:298
Class that represents the value of an enumeration. An enumeration is made of "1..n" objects EnumType...
Definition: EnumType.h:48
virtual EnumType * getDataTypeFont() const
Returns value that represents type Font belonging to enumeration.
virtual EnumType * getDataTypeStringList() const
Returns value that represents type StringList (string) belonging to enumeration.
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
void setItalic(bool italic)
Sets font with italic style.
Definition: Font.cpp:86
std::string getFamily()
Sets font family name.
Definition: Font.cpp:61
std::string toString()
Returns the value of string type. (The setValue method received a string)
Definition: Variant.cpp:311
void setName(std::string name)
Sets the name of this property.
Definition: Property.cpp:62
virtual EnumType * getDataTypeString() const
Returns value that represents type string belonging to enumeration.
Manage properties variants values.
EnumType * getType()
Returns the type of this property.
Definition: Property.cpp:67
void setOptionChoice(Variant variant)
Definition: Property.cpp:99
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
int toInt()
Returns the value of int type. (The setValue method received a int)
Definition: Variant.cpp:321
bool isKerning()
Returns true if font use kerning, false otherwise.
Definition: Font.cpp:121
bool toBool()
Returns the value of boolean type. (The setValue method received a boolean)
Definition: Variant.cpp:336
void setValue(ValueType value, EnumType *type)
Stores a copy of value.
Definition: Variant.h:288