All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
ChangePropertyCommand.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 ChangePropertyCommand.cpp
22 
23  \brief
24 
25  \ingroup layout
26 */
27 
28 // TerraLib
29 #include "ChangePropertyCommand.h"
30 #include "../../../../core/pattern/mvc/ItemObserver.h"
31 #include "../../../../core/pattern/mvc/Observable.h"
32 #include "../../../../core/pattern/mvc/ItemModelObservable.h"
33 #include "../../../outside/PropertiesOutside.h"
34 #include "../../../../core/property/Properties.h"
35 
36 // Qt
37 #include <QGraphicsItem>
38 #include <QPointF>
39 
41  Properties* newProperties, PropertiesOutside* outside /*= 0*/, QUndoCommand *parent /*= 0 */ ) :
42  QUndoCommand(parent),
43  m_item(item),
44  m_oldProperties(oldProperties),
45  m_newProperties(newProperties),
46  m_outside(outside)
47 {
48 
49 }
50 
51 te::layout::ChangePropertyCommand::ChangePropertyCommand( std::vector<QGraphicsItem*> items, std::vector<Properties*> allOld,
52  std::vector<Properties*> allNew, PropertiesOutside* outside /*= 0*/, QUndoCommand *parent /*= 0 */ ) :
53  QUndoCommand(parent),
54  m_item(0),
55  m_oldProperties(0),
56  m_newProperties(0),
57  m_outside(outside),
58  m_items(items),
59  m_allOldProperties(allOld),
60  m_allNewProperties(allNew)
61 {
62 
63 }
64 
66 {
67  if(m_oldProperties)
68  {
69  delete m_oldProperties;
70  m_oldProperties = 0;
71  }
72 
73  if(m_newProperties)
74  {
75  delete m_newProperties;
76  m_newProperties = 0;
77  }
78 
79  if(!m_allOldProperties.empty())
80  {
81  std::vector<Properties*>::iterator ito;
82  for(ito = m_allOldProperties.begin() ; ito != m_allOldProperties.end() ; ++ito)
83  {
84  if(*ito)
85  {
86  Properties* props = *ito;
87  if(props)
88  {
89  delete props;
90  props = 0;
91  }
92  }
93  }
94  m_allOldProperties.clear();
95  }
96 
97  if(!m_allNewProperties.empty())
98  {
99  std::vector<Properties*>::iterator itn;
100  for(itn = m_allNewProperties.begin() ; itn != m_allNewProperties.end() ; ++itn)
101  {
102  if(*itn)
103  {
104  Properties* props = *itn;
105  if(props)
106  {
107  delete props;
108  props = 0;
109  }
110  }
111  }
112  m_allNewProperties.clear();
113  }
114 }
115 
117 {
118  if(m_item)
119  {
120  if(!checkItem(m_item, m_oldProperties))
121  {
122  return;
123  }
124  }
125 
126  if(!checkVectors())
127  {
128  return;
129  }
130 
131  for(unsigned int i = 0 ; i < m_items.size() ; ++i)
132  {
133  QGraphicsItem* item = m_items[i];
134  Properties* props = m_allOldProperties[i];
135  if (item)
136  {
137  checkItem(item, props);
138  }
139  }
140 
141  if(m_outside)
142  {
143  m_outside->refreshOutside();
144  }
145 
146  setText(QObject::tr("Change Properties %1")
147  .arg(createCommandString(m_item)));
148 }
149 
151 {
152  if(m_item)
153  {
154  if(!checkItem(m_item, m_newProperties))
155  {
156  return;
157  }
158  }
159 
160  if(!checkVectors())
161  {
162  return;
163  }
164 
165  for(unsigned int i = 0 ; i < m_items.size() ; ++i)
166  {
167  QGraphicsItem* item = m_items[i];
168  Properties* props = m_allNewProperties[i];
169  if (item)
170  {
171  checkItem(item, props);
172  }
173  }
174 
175  if(m_outside)
176  {
177  m_outside->refreshOutside();
178  }
179 
180  setText(QObject::tr("Change Properties %1")
181  .arg(createCommandString(m_item)));
182 }
183 
185 {
186  if(!m_item)
187  {
188  if(!m_items.empty())
189  {
190  return QObject::tr("%1 %2").arg(m_items.size()).arg("items");
191  }
192  return QObject::tr("%1");
193  }
194 
195  ItemObserver* obs = dynamic_cast<ItemObserver*>(item);
196 
197  if(!obs)
198  return QObject::tr("%1");
199 
200  QPointF pos = m_item->scenePos();
201 
202  return QObject::tr("%1 at (%2, %3)")
203  .arg(obs->getModel()->getType()->getName().c_str())
204  .arg(pos.x()).arg(pos.y());
205 }
206 
208 {
209  bool result = true;
210 
211  if(props1->getProperties().size() != props2->getProperties().size())
212  return false;
213 
214  std::vector<Property> prop1 = props1->getProperties();
215  std::vector<Property> prop2 = props2->getProperties();
216 
217  for(unsigned int i = 0 ; i < props1->getProperties().size() ; ++i)
218  {
219  Property proper1 = prop1[i];
220  Property proper2 = prop2[i];
221 
222  if(proper1 == proper2)
223  {
224  if(proper1.getValue() == proper2.getValue())
225  {
226  continue;
227  }
228  else
229  {
230  result = false;
231  break;
232  }
233  }
234  }
235  return result;
236 }
237 
238 bool te::layout::ChangePropertyCommand::checkItem( QGraphicsItem* item, Properties* props )
239 {
240  if(!item)
241  return false;
242 
243  ItemObserver* obs = dynamic_cast<ItemObserver*>(item);
244 
245  if(!obs)
246  return false;
247 
248  ItemModelObservable* model = dynamic_cast<ItemModelObservable*>(obs->getModel());
249 
250  if(!model)
251  return false;
252 
253  Properties* propsModel = model->getProperties();
254  if(equals(props, propsModel))
255  return false;
256 
257  model->updateProperties(props);
258  obs->redraw();
259 
260  return true;
261 }
262 
264 {
265  if(m_allNewProperties.size() != m_items.size() ||
266  m_allOldProperties.size() != m_items.size())
267  {
268  return false;
269  }
270 
271  return true;
272 }
Variant getValue()
Returns stored value.
Definition: Property.cpp:72
virtual void redo()
Reimplemented from QUndoCommand.
Properties tree for any item, MVC component, using Qt for presentation and editing.
std::string getName()
Returns name.
Definition: EnumType.cpp:54
Abstract class to represent an observable. "Model" part of MVC component. All classes representing th...
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 updateProperties(te::layout::Properties *properties)
Reimplemented from Observable.
virtual void redraw(bool bRefresh=true)
Redraws the graphic component.
virtual bool equals(Properties *props1, Properties *props2)
ChangePropertyCommand(QGraphicsItem *item, Properties *oldProperties, Properties *newProperties, PropertiesOutside *outside=0, QUndoCommand *parent=0)
Constructor.
virtual EnumType * getType()=0
Returns the type of component Reimplement this function in a Observable subclass to provide the model...
virtual ~ChangePropertyCommand()
Destructor. Delete all properties.
virtual bool checkItem(QGraphicsItem *item, Properties *props)
virtual Properties * getProperties() const
Reimplemented from Observable.
virtual void undo()
Reimplemented from QUndoCommand.
virtual Observable * getModel()
Returns the "Model" part of the MVC.
virtual bool clear()
Clear set of properties of this object.
Definition: Properties.h:225
virtual QString createCommandString(QGraphicsItem *item)
Undo/Redo for changes in component properties.
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