All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
ItemModelObservable.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 ItemModelObservable.cpp
22 
23  \brief
24 
25  \ingroup layout
26 */
27 
28 // TerraLib
29 #include "ItemModelObservable.h"
30 #include "Observer.h"
31 #include "Property.h"
32 #include "../../../../../geometry/Envelope.h"
33 #include "../../../../../geometry/Coord2D.h"
34 #include "Properties.h"
35 #include "SharedProperties.h"
36 
38  m_id(0),
39  m_name("unknown"),
40  m_type(TPObjectUnknown),
41  m_sharedProps(0),
42  m_zValue(0),
43  m_border(true)
44 {
45  m_box = te::gm::Envelope(0,0,0,0);
46 
47  m_backgroundColor = te::color::RGBAColor(255,255,255, 0);
48 
50 
52 }
53 
55 {
56  if(m_properties)
57  {
58  delete m_properties;
59  m_properties = 0;
60  }
61 
62  if(m_sharedProps)
63  {
64  delete m_sharedProps;
65  m_sharedProps = 0;
66  }
67 }
68 
70 {
71  std::pair<std::set<Observer*>::iterator,bool> p = m_observers.insert(o);
72 
73  if(p.second == true)
74  return true;
75 
76  return false;
77 }
78 
80 {
81  int num = m_observers.erase(o);
82 
83  if(num == 1)
84  return true;
85 
86  return false;
87 }
88 
90 {
91  std::set<Observer*>::iterator it;
92  for(it = m_observers.begin(); it != m_observers.end(); ++it)
93  {
94  (*it)->updateObserver(context);
95  }
96 }
97 
99 {
100  m_properties->clear();
101 
102  Property pro_name;
103  pro_name.setName(m_sharedProps->getName());
104  pro_name.setId("unknown");
105  pro_name.setValue(m_name, DataTypeString);
106  m_properties->addProperty(pro_name);
107 
108  Property pro_id;
109  pro_id.setName(m_sharedProps->getId());
110  pro_id.setId("unknown");
111  pro_id.setValue(m_id, DataTypeInt);
112  m_properties->addProperty(pro_id);
113 
114  /* Box */
115 
116  double x1 = m_box.getLowerLeftX();
117  double x2 = m_box.getUpperRightX();
118  double y1 = m_box.getLowerLeftY();
119  double y2 = m_box.getUpperRightY();
120 
121  Property pro_x1;
122  pro_x1.setName(m_sharedProps->getX1());
123  pro_x1.setId("unknown");
124  pro_x1.setValue(x1, DataTypeDouble);
125  m_properties->addProperty(pro_x1);
126 
127  Property pro_x2;
128  pro_x2.setName(m_sharedProps->getX2());
129  pro_x2.setId("unknown");
130  pro_x2.setValue(x2, DataTypeDouble);
131  m_properties->addProperty(pro_x2);
132 
133  Property pro_y1;
134  pro_y1.setName(m_sharedProps->getY1());
135  pro_y1.setId("unknown");
136  pro_y1.setValue(y1, DataTypeDouble);
137  m_properties->addProperty(pro_y1);
138 
139  Property pro_y2;
140  pro_y2.setName(m_sharedProps->getY2());
141  pro_y2.setId("unknown");
142  pro_y2.setValue(y2, DataTypeDouble);
143  m_properties->addProperty(pro_y2);
144 
145  /* ---------- */
146 
147  Property pro_zValue;
148  pro_zValue.setName(m_sharedProps->getZValue());
149  pro_zValue.setId("unknown");
150  pro_zValue.setValue(m_zValue, DataTypeInt);
151  m_properties->addProperty(pro_zValue);
152 
153  Property pro_border;
154  pro_border.setName(m_sharedProps->getBorder());
155  pro_border.setId("unknown");
156  pro_border.setValue(m_border, DataTypeBool);
157  m_properties->addProperty(pro_border);
158 
159  m_properties->setTypeObj(m_type);
160  return m_properties;
161 }
162 
164 {
165  return m_box;
166 }
167 
169 {
170  m_box = box;
171 }
172 
174 {
175  return m_backgroundColor;
176 }
177 
179 {
180  m_backgroundColor = color;
181 }
182 
184 {
185  m_borderColor = color;
186 }
187 
189 {
190  return m_borderColor;
191 }
192 
194 {
195  m_name = name;
196  if(m_properties)
197  m_properties->setObjectName(m_name);
198 }
199 
201 {
202  return m_name;
203 }
204 
205 void te::layout::ItemModelObservable::setPosition( const double& x, const double& y )
206 {
207  //Initial point to draw is : x1, y1, that corresponds 0,0 of local coordinate of a item
208  double x1 = x;
209  double x2 = x + m_box.getWidth();
210 
211  double y1 = y;
212  double y2 = y + m_box.getHeight();
213 
214  m_box = te::gm::Envelope(x1, y1, x2, y2);
215 }
216 
218 {
219  te::gm::Envelope env(coord.x, coord.y, coord.x, coord.y);
220 
221  if(env.isValid())
222  return m_box.contains(env);
223 
224  return false;
225 }
226 
228 {
229  Properties* vectorProps = const_cast<Properties*>(properties);
230 
231  Property pro_name = vectorProps->contains(m_sharedProps->getName());
232  if(!pro_name.isNull())
233  {
234  m_name = pro_name.getValue().toString();
235  }
236 
237  Property pro_id = vectorProps->contains(m_sharedProps->getId());
238  if(!pro_id.isNull())
239  {
240  m_id = pro_id.getValue().toInt();
241  }
242 
243  /* Box */
244 
245  Property pro_x1 = vectorProps->contains(m_sharedProps->getX1());
246  if(!pro_x1.isNull())
247  {
248  m_box.m_llx = pro_x1.getValue().toDouble();
249  }
250 
251  Property pro_x2 = vectorProps->contains(m_sharedProps->getX2());
252  if(!pro_x2.isNull())
253  {
254  m_box.m_urx = pro_x2.getValue().toDouble();
255  }
256 
257  Property pro_y1 = vectorProps->contains(m_sharedProps->getY1());
258  if(!pro_y1.isNull())
259  {
260  m_box.m_lly = pro_y1.getValue().toDouble();
261  }
262 
263  Property pro_y2 = vectorProps->contains(m_sharedProps->getY2());
264  if(!pro_y2.isNull())
265  {
266  m_box.m_ury = pro_y2.getValue().toDouble();
267  }
268 
269  Property pro_zValue = vectorProps->contains(m_sharedProps->getZValue());
270  if(!pro_zValue.isNull())
271  {
272  m_zValue = pro_zValue.getValue().toInt();
273  }
274 
275  Property pro_border = vectorProps->contains(m_sharedProps->getBorder());
276  if(!pro_border.isNull())
277  {
278  m_border = pro_border.getValue().toBool();
279  }
280 }
281 
283 {
284  return m_type;
285 }
286 
288 {
289  m_type = type;
290 }
291 
293 {
294  return m_zValue;
295 }
296 
298 {
299  m_zValue = zValue;
300 }
301 
303 {
304  return m_border;
305 }
306 
308 {
309  m_border = value;
310 }
311 
313 {
314  return m_id;
315 }
316 
318 {
319  m_id = id;
320 }
321 
322 void te::layout::ItemModelObservable::resize( double w, double h )
323 {
324  if((w <= 0) || (h <= 0))
325  return;
326 
327  if(w == m_box.getWidth() && h == m_box.getHeight())
328  return;
329 
330  m_box.m_urx = m_box.m_llx + w;
331  m_box.m_ury = m_box.m_lly + h;
332 }
virtual void setPosition(const double &x, const double &y)
Variant getValue()
Definition: Property.cpp:74
double y
y-coordinate.
Definition: Coord2D.h:87
double x
x-coordinate.
Definition: Coord2D.h:86
virtual void setType(LayoutAbstractObjectType type)
An utility struct for representing 2D coordinates.
Definition: Coord2D.h:40
virtual void updateProperties(te::layout::Properties *properties)
virtual void setBorder(bool value)
void setValue(typename ValueType value, LayoutPropertyDataType type)
Definition: Property.h:106
virtual te::color::RGBAColor getBorderColor()
virtual void notifyAll(ContextItem context)
An Envelope defines a 2D rectangular region.
Definition: Envelope.h:51
virtual void setZValue(int zValue)
virtual void setBackgroundColor(te::color::RGBAColor color)
virtual LayoutAbstractObjectType getType()
te::color::RGBAColor m_backgroundColor
virtual void setName(std::string name)
virtual te::color::RGBAColor getBackgroundColor()
LayoutAbstractObjectType
Enum TdkAbstractComponentType. This is the enumeration of the components types.
Definition: AbstractType.h:38
virtual Properties * getProperties() const
A helper class for 32-bit RGBA (Red-Green-Blue-Alpha channel) color.
Definition: RGBAColor.h:57
virtual bool addObserver(Observer *o)
virtual bool clear()
Definition: Properties.h:121
std::string toString()
Definition: Variant.cpp:237
void setName(std::string name)
Definition: Property.cpp:54
virtual te::gm::Envelope getBox()
virtual void resize(double w, double h)
virtual void setBorderColor(te::color::RGBAColor color)
void setId(std::string id)
Definition: Property.cpp:69
virtual bool contains(const te::gm::Coord2D &coord) const
virtual void setBox(te::gm::Envelope box)
virtual bool contains(Property property)
Definition: Properties.h:147
bool isValid() const
It tells if the rectangle is valid or not.
Definition: Envelope.h:438
virtual bool removeObserver(Observer *o)