All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
MoveCommand.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 MoveCommand.cpp
22 
23  \brief
24 
25  \ingroup layout
26 */
27 
28 // TerraLib
29 #include "MoveCommand.h"
30 #include "../../../../core/pattern/mvc/ItemObserver.h"
31 #include "../../../../core/enum/EnumType.h"
32 #include "../../../../core/pattern/mvc/Observable.h"
33 #include "../../Scene.h"
34 #include "../../../../core/pattern/singleton/Context.h"
35 
36 // Qt
37 #include <QGraphicsItem>
38 
39 te::layout::MoveCommand::MoveCommand( QGraphicsItem* item, const QPointF oldPos,
40  QUndoCommand *parent /*= 0*/ ) :
41  QUndoCommand(parent),
42  m_item(item)
43 {
44  m_newPos = m_item->scenePos();
45  m_oldPos = oldPos;
46 }
47 
48 te::layout::MoveCommand::MoveCommand( std::map<QGraphicsItem*, QPointF> items,
49  QUndoCommand *parent /*= 0*/ ) :
50  QUndoCommand(parent),
51  m_item(0),
52  m_moveItems(items)
53 {
54  std::map<QGraphicsItem*, QPointF>::iterator it;
55  for (it = m_moveItems.begin(); it != m_moveItems.end(); ++it)
56  {
57  QGraphicsItem* item = it->first;
58  QPointF ptNew = item->scenePos();
59  m_itemsPoints.push_back(ptNew);
60  }
61 }
62 
64 {
65 
66 }
67 
69 {
70  if(m_item)
71  {
72  m_item->setPos(m_oldPos);
73  m_item->scene()->update();
74  setText(QObject::tr("Move %1")
75  .arg(createCommandString(m_item, m_oldPos)));
76  }
77 
78  if(m_moveItems.empty())
79  return;
80 
81  int size = m_moveItems.size();
82 
83  std::map<QGraphicsItem*, QPointF>::iterator it;
84  for (it = m_moveItems.begin(); it != m_moveItems.end(); ++it)
85  {
86  QGraphicsItem* item = it->first;
87  QPointF ptOld = it->second;
88  item->setPos(ptOld);
89  }
90 
91  Scene* sc = dynamic_cast<Scene*>(Context::getInstance().getScene());
92  sc->update();
93  setText(QObject::tr("Move %1").arg(size));
94 }
95 
97 {
98  if(m_item)
99  {
100  m_item->setPos(m_newPos);
101  setText(QObject::tr("Move %1")
102  .arg(createCommandString(m_item, m_newPos)));
103  }
104 
105  if(m_moveItems.empty())
106  return;
107 
108  if(m_moveItems.size() != m_itemsPoints.size())
109  return;
110 
111  Scene* sc = dynamic_cast<Scene*>(Context::getInstance().getScene());
112  if(!sc)
113  return;
114 
115  if(!sc->getUndoStack())
116  return;
117 
118  bool resultFound = false;
119 
120  for(int i = 0 ; i < sc->getUndoStack()->count() ; ++i)
121  {
122  const QUndoCommand* cmd = sc->getUndoStack()->command(i);
123  if(cmd == this)
124  {
125  resultFound = true;
126  }
127  }
128 
129  int size = m_moveItems.size();
130 
131  //no makes redo while the command is not on the stack
132  if(resultFound)
133  {
134  int index = 0;
135 
136  std::map<QGraphicsItem*, QPointF>::iterator it;
137  for (it = m_moveItems.begin(); it != m_moveItems.end(); ++it)
138  {
139  QGraphicsItem* item = it->first;
140  QPointF ptNew = m_itemsPoints[index];
141  item->setPos(ptNew);
142  index++;
143  }
144  }
145 
146  sc->update();
147  setText(QObject::tr("Move %1").arg(size));
148 }
149 
150 QString te::layout::MoveCommand::createCommandString( QGraphicsItem* item, const QPointF &pos )
151 {
152  if(!m_item)
153  return QObject::tr("%1");
154 
155  ItemObserver* obs = dynamic_cast<ItemObserver*>(item);
156 
157  if(!obs)
158  return QObject::tr("%1");
159 
160  return QObject::tr("%1 at (%2, %3)")
161  .arg(obs->getModel()->getType()->getName().c_str())
162  .arg(pos.x()).arg(pos.y());
163 }
std::string getName()
Returns name.
Definition: EnumType.cpp:54
MoveCommand(QGraphicsItem *item, const QPointF oldPos, QUndoCommand *parent=0)
Constructor.
Definition: MoveCommand.cpp:39
Abstract class to represent an observer. "View" part of MVC component. All classes representing the g...
Definition: ItemObserver.h:52
QList< QPointF > m_itemsPoints
Definition: MoveCommand.h:99
virtual EnumType * getType()=0
Returns the type of component Reimplement this function in a Observable subclass to provide the model...
static Context & getInstance()
It returns a reference to the singleton instance.
virtual QUndoStack * getUndoStack()
Method that return stack of Undo/Redo.
Definition: Scene.cpp:328
Undo/Redo for moving components.
QGraphicsItem * m_item
Definition: MoveCommand.h:95
virtual void undo()
Reimplemented from QUndoCommand.
Definition: MoveCommand.cpp:68
virtual QString createCommandString(QGraphicsItem *item, const QPointF &pos)
std::map< QGraphicsItem *, QPointF > m_moveItems
Definition: MoveCommand.h:98
Class representing the scene. This scene is child of QGraphicsScene, part of Graphics View Framework...
Definition: Scene.h:80
virtual void redo()
Reimplemented from QUndoCommand.
Definition: MoveCommand.cpp:96
virtual Observable * getModel()
Returns the "Model" part of the MVC.
AbstractScene * getScene()
Returns abstract scene for QGraphicsScene class, part of Graphics View Framework. ...
Definition: Context.cpp:88
virtual ~MoveCommand()
Destructor.
Definition: MoveCommand.cpp:63