All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
PropertiesUtils.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 PropertiesUtils.cpp
22 
23  \brief
24 
25  \ingroup layout
26 */
27 
28 // TerraLib
29 #include "PropertiesUtils.h"
30 #include "../../../core/property/Properties.h"
31 #include "../../../core/pattern/mvc/ItemObserver.h"
32 #include "../../../core/property/SharedProperties.h"
33 #include "../../../core/enum/Enums.h"
34 #include "../../../core/pattern/singleton/Context.h"
35 #include "../ItemUtils.h"
36 #include "../Scene.h"
37 #include "../pattern/command/ChangePropertyCommand.h"
38 #include "../../../core/pattern/mvc/Observable.h"
39 
40 // Qt
41 #include <QGraphicsItem>
42 #include <QObject>
43 #include <QUndoCommand>
44 
46 {
47 
48 }
49 
51 {
52 
53 }
54 
55 te::layout::Properties* te::layout::PropertiesUtils::intersection( QList<QGraphicsItem*> graphicsItems, bool& window )
56 {
57  Properties* props = 0;
58 
59  if(graphicsItems.size() == 1)
60  {
61  QGraphicsItem* item = graphicsItems.first();
62  if (item)
63  {
64  ItemObserver* lItem = dynamic_cast<ItemObserver*>(item);
65  if(lItem)
66  {
67  if(lItem->getModel())
68  {
69  props = const_cast<Properties*>(lItem->getModel()->getProperties());
70  window = props->hasWindows();
71  }
72  }
73  }
74  }
75  else
76  {
77  props = sameProperties(graphicsItems, window);
78  }
79 
80  return props;
81 }
82 
83 te::layout::Properties* te::layout::PropertiesUtils::sameProperties( QList<QGraphicsItem*> graphicsItems, bool& window )
84 {
85  Properties* props = 0;
86  std::vector<Properties*> propsVec = getAllProperties(graphicsItems, window);
87 
88  QGraphicsItem* firstItem = graphicsItems.first();
89  ItemObserver* lItem = dynamic_cast<ItemObserver*>(firstItem);
90 
91  if(!lItem)
92  {
93  return props;
94  }
95 
96  Properties* firstProps = 0;
97 
98  if(lItem->getModel())
99  {
100  firstProps = const_cast<Properties*>(lItem->getModel()->getProperties());
101  }
102 
103  if(!firstProps)
104  {
105  return props;
106  }
107 
108  std::vector<Properties*>::iterator it = propsVec.begin();
109  std::vector<Properties*>::iterator itend = propsVec.end();
110  bool result = false;
111  foreach( Property prop, firstProps->getProperties())
112  {
113  contains(itend, it, prop.getName(), result);
114  if(result)
115  {
116  if(!props)
117  {
118  props = new Properties("");
119  }
120  prop.setParentItemHashCode(0);
121  props->addProperty(prop);
122  }
123  }
124 
125  return props;
126 }
127 
128 void te::layout::PropertiesUtils::contains( std::vector<Properties*>::iterator itend, std::vector<Properties*>::iterator it, std::string name, bool& result )
129 {
130  Property prop = (*it)->contains(name);
131  if(prop.isNull())
132  {
133  result = false;
134  return;
135  }
136  else
137  {
138  ++it;
139  result = true;
140  if(it != itend)
141  {
142  contains(itend, it, name, result);
143  }
144  }
145 }
146 
147 std::vector<te::layout::Properties*> te::layout::PropertiesUtils::getAllProperties( QList<QGraphicsItem*> graphicsItems, bool& window )
148 {
149  std::vector<Properties*> propsVec;
150  bool result = true;
151 
152  foreach( QGraphicsItem *item, graphicsItems)
153  {
154  if (item)
155  {
156  ItemObserver* lItem = dynamic_cast<ItemObserver*>(item);
157  if(lItem)
158  {
159  if(!lItem->getModel())
160  {
161  continue;
162  }
163 
164  Properties* propsItem = const_cast<Properties*>(lItem->getModel()->getProperties());
165  if(propsItem)
166  {
167  propsVec.push_back(propsItem);
168  if(result)
169  {
170  result = propsItem->hasWindows();
171  }
172  }
173  }
174  }
175  }
176 
177  window = result;
178  return propsVec;
179 }
180 
181 void te::layout::PropertiesUtils::addDynamicOptions( Property& property, std::vector<std::string> list )
182 {
184 
185  foreach(std::string str, list)
186  {
187  Variant v;
188  v.setValue(str, dataType->getDataTypeString());
189  property.addOption(v);
190  }
191 }
192 
193 void te::layout::PropertiesUtils::checkDynamicProperty( Property& property, QList<QGraphicsItem*> graphicsItems )
194 {
195  SharedProperties* sharedProps = new SharedProperties;
196 
197  if(property.getName().compare(sharedProps->getMapName()) == 0)
198  {
199  mapNameDynamicProperty(property, graphicsItems);
200  }
201 
202  delete sharedProps;
203  sharedProps = 0;
204 }
205 
206 void te::layout::PropertiesUtils::mapNameDynamicProperty( Property& property, QList<QGraphicsItem*> graphicsItems )
207 {
208  std::string currentName = property.getValue().toString();
209 
210  if(currentName.compare("") == 0)
211  {
212  currentName = property.getOptionByCurrentChoice().toString();
213  }
214 
216  std::vector<std::string> strList = iUtils->mapNameList();
217 
218  if(std::find(strList.begin(), strList.end(), currentName) != strList.end())
219  {
220  std::vector<std::string>::iterator it = std::find(strList.begin(), strList.end(), currentName);
221  strList.erase(it);
222  }
223 
224  addDynamicOptions(property, strList);
225 }
226 
227 QGraphicsItem* te::layout::PropertiesUtils::equalsHashCode( Property property, QList<QGraphicsItem*> graphicsItems )
228 {
229  QGraphicsItem *itemSelected = 0;
230 
231  foreach( QGraphicsItem *item, graphicsItems)
232  {
233  if (item)
234  {
235  ItemObserver* lItem = dynamic_cast<ItemObserver*>(item);
236  if(lItem)
237  {
238  if(lItem->getModel())
239  {
240  int hashcode = lItem->getModel()->getHashCode();
241  if(hashcode == property.getParentItemHashCode())
242  {
243  itemSelected = item;
244  }
245  }
246  }
247  }
248  }
249  return itemSelected;
250 }
251 
252 
253 
254 
255 
256 
257 
258 
259 
std::string getName()
Method that returns the name of this property.
Definition: Property.cpp:57
virtual void addDynamicOptions(Property &property, std::vector< std::string > list)
virtual Properties * sameProperties(QList< QGraphicsItem * > graphicsItems, bool &window)
virtual bool addProperty(Property property)
Adds the specified property to the set of properties for this object.
Definition: Properties.h:193
virtual bool hasWindows()
Definition: Properties.h:285
virtual EnumDataType * getEnumDataType()
Returns data type enumeration.
Definition: Enums.cpp:52
bool isNull()
Returns true if no value has been set, false otherwise.
Definition: Property.cpp:146
int getParentItemHashCode()
Returns the hashcode of the object that owns this property.
Definition: Property.cpp:278
virtual Properties * intersection(QList< QGraphicsItem * > graphicsItems, bool &window)
The Properties class represents a persistent set of properties. The Properties can be saved to a file...
Definition: Properties.h:52
Abstract class to represent an observer. "View" part of MVC component. All classes representing the g...
Definition: ItemObserver.h:52
virtual std::vector< Property > getProperties()
Returns set of all properties.
Definition: Properties.h:220
virtual void contains(std::vector< Properties * >::iterator itend, std::vector< Properties * >::iterator it, std::string name, bool &result)
virtual void checkDynamicProperty(Property &property, QList< QGraphicsItem * > graphicsItems)
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
Class acts like a union for some C++/TerraLib5 data types. Responsible for storing the value...
Definition: Variant.h:80
virtual int getHashCode()=0
Returns the hashcode of a MVC component. Reimplement this function in a Observable subclass to provid...
ItemUtils * getItemUtils()
Returns pointer for manipulating items in the scene and vectorization of text and legend...
Definition: Context.cpp:273
virtual QGraphicsItem * equalsHashCode(Property property, QList< QGraphicsItem * > graphicsItems)
virtual te::layout::Properties * getProperties() const =0
Returns the model state as properties. Reimplement this function in a Observable subclass to provide ...
Utility class for manipulating items in the scene and vectorization of text and legend.
Definition: ItemUtils.h:62
virtual void mapNameDynamicProperty(Property &property, QList< QGraphicsItem * > graphicsItems)
Utility class with functions to facilitate handling of qt properties and properties layout module...
virtual Observable * getModel()
Returns the "Model" part of the MVC.
virtual EnumType * getDataTypeString() const
Returns value that represents type string belonging to enumeration.
virtual std::vector< Properties * > getAllProperties(QList< QGraphicsItem * > graphicsItems, bool &window)
virtual std::vector< std::string > mapNameList(bool selected=false)
List of names te::layout::MapItem.
Definition: ItemUtils.cpp:122
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
void setValue(ValueType value, EnumType *type)
Stores a copy of value.
Definition: Variant.h:288
void setParentItemHashCode(int hashCode)
Sets the hashcode of the object that owns this property.
Definition: Property.cpp:273