All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
OutsideModelObservable.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 OutsideModelObservable.cpp
22 
23  \brief
24 
25  \ingroup layout
26 */
27 
28 // TerraLib
29 #include "OutsideModelObservable.h"
30 #include "Observer.h"
31 #include "../../property/Property.h"
32 #include "../../property/Properties.h"
33 #include "../singleton/Context.h"
34 #include "../../enum/Enums.h"
35 
36 // STL
37 #include <ctime>
38 #include <iostream>
39 
41  m_color(0),
42  m_publicProperties(0),
43  m_type(0),
44  m_id(0),
45  m_resizable(true),
46  m_hashCode(0),
47  m_name("unknown")
48 {
49  EnumObjectType* type = Enums::getInstance().getEnumObjectType();
50  m_type = type->getObjectUnknown();
51 
53 
56 
58 }
59 
61 {
62  if(m_properties)
63  {
64  delete m_properties;
65  m_properties = 0;
66  }
67 
68  if(m_publicProperties)
69  {
70  delete m_publicProperties;
71  m_publicProperties = 0;
72  }
73 }
74 
76 {
77  std::pair<std::set<Observer*>::iterator,bool> p = m_observers.insert(o);
78 
79  if(p.second == true)
80  return true;
81 
82  return false;
83 }
84 
86 {
87  int num = m_observers.erase(o);
88 
89  if(num == 1)
90  return true;
91 
92  return false;
93 }
94 
96 {
97  std::set<Observer*>::iterator it;
98  for(it = m_observers.begin(); it != m_observers.end(); ++it)
99  {
100  (*it)->updateObserver(context);
101  }
102 }
103 
105 {
106  return m_box;
107 }
108 
110 {
111  m_box = box;
112 }
113 
115 {
116  return m_color;
117 }
118 
120 {
121  m_color = color;
122 }
123 
124 void te::layout::OutsideModelObservable::setPosition( const double& x, const double& y )
125 {
126  //Initial point to draw is : x1, y1, that corresponds 0,0 of local coordinate of a item
127  double x1 = x;
128  double x2 = x + m_box.getWidth();
129 
130  double y1 = y;
131  double y2 = y + m_box.getHeight();
132 
133  m_box = te::gm::Envelope(x1, y1, x2, y2);
134 }
135 
137 {
138  m_properties->clear();
139 
140  Property pro_name(m_hashCode);
141  pro_name.setName(m_name);
142 
143  m_properties->addProperty(pro_name);
144 
145  m_properties->setTypeObj(m_type);
146  return m_properties;
147 }
148 
150 {
151  return m_type;
152 }
153 
155 {
156  m_type = type;
157 }
158 
160 {
161  return m_zValue;
162 }
163 
165 {
166  m_zValue = zValue;
167 }
168 
170 {
171  Properties* vectorProps = const_cast<Properties*>(properties);
172 
173  std::vector<Property> vProps = vectorProps->getProperties();
174  Property pro_name = vProps[0];
175  //m_name = pro_name.getName();
176 }
177 
179 {
180  return m_name;
181 }
182 
184 {
185  return m_id;
186 }
187 
189 {
190  m_id = id;
191 }
192 
194 {
195  m_resizable = resize;
196 }
197 
199 {
200  return m_resizable;
201 }
202 
204 {
205  return m_hashCode;
206 }
207 
209 {
210  int nameLength = m_name.length();
211  int id = m_id;
212  int type = 0;
213  if(m_type)
214  {
215  type = m_type->getId();
216  }
217 
218  int propertiesLength = 0;
219 
220  if(m_properties)
221  {
222  Properties* props = getProperties();
223 
224  if(props)
225  {
226  propertiesLength = getProperties()->getProperties().size();
227  }
228  }
229 
230  int hashcode = (nameLength + id + type + propertiesLength) * 5;
231 
232  // current date/time based on current system
233  std::time_t now = std::time(0);
234 
235  std::tm *ltm = localtime(&now);
236 
237  if(!ltm)
238  return hashcode;
239 
240  // print various components of tm structure.
241  int year = 1900 + ltm->tm_year;
242  int month = 1 + ltm->tm_mon;
243  int day = ltm->tm_mday;
244  int hour = 1 + ltm->tm_hour;
245  int numberSecond = 1 + ltm->tm_min;
246  int numberMilliseconds = 1 + ltm->tm_sec;
247 
248  hashcode *= year + month + day;
249  hashcode *= hour + numberSecond + numberMilliseconds;
250 
251  return hashcode;
252 }
253 
255 {
256  if(!m_properties || m_publicProperties)
257  {
258  return 0;
259  }
260 
261  m_publicProperties->clear();
262 
263  std::vector<Property>::iterator it = m_properties->getProperties().begin();
264 
265  for( ; it != m_properties->getProperties().end() ; ++it )
266  {
267  if((*it).isPublic())
268  {
269  m_publicProperties->addProperty(*it);
270  }
271  }
272 
273  m_publicProperties->setTypeObj(m_type);
274 
275  return m_publicProperties;
276 }
277 
278 
279 
280 
281 
282 
virtual void setZValue(int zValue)
Reimplemented from Observable.
virtual void updateProperties(te::layout::Properties *properties)
Reimplemented from Observable.
Class responsible for maintaining the drawing context of a MVC component. It is always used by the "M...
Definition: ContextItem.h:49
virtual void setId(int id)
Reimplemented from Observable.
virtual bool removeObserver(Observer *o)
Reimplemented from Observable.
Class to represent a graphic object (MVC component) and widget object (MVC widget) type enumeration...
virtual EnumType * getType()
Reimplemented from Observable.
std::string m_name
name of the MVC widget
EnumType * m_type
type of the MVC widget
virtual void setHashCode(int hashCode)
Sets the hashcode of a MVC component.
Definition: Properties.h:295
Abstract class to represent an observer. "View" part of MVC component.
virtual bool isResizable()
Reimplemented from Observable.
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 EnumType * getObjectUnknown() const
Returns value that represents unknown type belonging to enumeration.
virtual Properties * getProperties() const
Reimplemented from Observable.
Abstract class to represent an observer. "View" part of MVC component.
Definition: Observer.h:48
virtual void setColor(int color)
Change the background color of the MVC widget.
static Enums & getInstance()
It returns a reference to the singleton instance.
An Envelope defines a 2D rectangular region.
Definition: Envelope.h:51
virtual void setPosition(const double &x, const double &y)
Change coordinate llx,lly of the MVC widget.
virtual te::gm::Envelope getBox()
Reimplemented from Observable.
virtual void setBox(te::gm::Envelope box)
Change the bounding rectangle.
virtual int getColor()
Returns the background color of the MVC widget.
virtual int getId()
Reimplemented from Observable.
virtual te::layout::Properties * getPublicProperties() const
Reimplemented from Observable.
Abstract class to represent an observable. "Model" part of MVC widget. All classes representing the m...
virtual int calculateHashCode()
Returns a new hashcode.
Class that represents the value of an enumeration. An enumeration is made of "1..n" objects EnumType...
Definition: EnumType.h:48
virtual void notifyAll(ContextItem context)
Reimplemented from Observable.
virtual bool clear()
Clear set of properties of this object.
Definition: Properties.h:225
Properties * m_publicProperties
public properties
virtual int getZValue()
Reimplemented from Observable.
void setName(std::string name)
Sets the name of this property.
Definition: Property.cpp:62
virtual void setResizable(bool resize)
Reimplemented from Observable.
virtual void setType(EnumType *type)
Reimplemented from Observable.
virtual bool addObserver(Observer *o)
Reimplemented from Observable.
virtual std::string getName()
Reimplemented from Observable.
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
virtual int getHashCode()
Reimplemented from Observable.