All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
AddCommand.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 AddCommand.cpp
22 
23  \brief
24 
25  \ingroup layout
26 */
27 
28 // TerraLib
29 #include "AddCommand.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 
35 // Qt
36 #include <QGraphicsScene>
37 #include <QGraphicsItem>
38 
39 te::layout::AddCommand::AddCommand( QGraphicsItem* item, QUndoCommand *parent /*= 0*/ ) :
40  QUndoCommand(parent),
41  m_scene(0),
42  m_item(item)
43 {
44  if(!m_item)
45  return;
46 
47  m_scene = item->scene();
48 
49  m_initialPosition = m_item->scenePos();
50  setText(QObject::tr("Add %1").arg(createCommandString(m_item, m_initialPosition)));
51 }
52 
54 {
55 
56 }
57 
59 {
60  Scene* scene = dynamic_cast<Scene*>(m_scene);
61 
62  if(m_item->scene() != scene)
63  return;
64 
65  m_scene->removeItem(m_item);
66  scene->addItemStackWithoutScene(m_item);
67  m_scene->update();
68 }
69 
71 {
72  Scene* scene = dynamic_cast<Scene*>(m_scene);
73 
74  if(!scene || !m_item)
75  return;
76 
77  /* Checks if the item is already
78  added to the scene */
79  if(m_item->scene() == m_scene)
80  return;
81 
82  scene->insertItem(m_item);
83  scene->removeItemStackWithoutScene(m_item);
84 
85  m_item->setPos(m_initialPosition);
86  m_scene->clearSelection();
87  m_scene->update();
88 }
89 
90 QString te::layout::AddCommand::createCommandString( QGraphicsItem* item, const QPointF &pos )
91 {
92  if(!m_item)
93  return QObject::tr("%1");
94 
95  ItemObserver* obs = dynamic_cast<ItemObserver*>(item);
96 
97  if(!obs)
98  return QObject::tr("%1");
99 
100  return QObject::tr("%1 at (%2, %3)")
101  .arg(obs->getModel()->getType()->getName().c_str())
102  .arg(pos.x()).arg(pos.y());
103 }
104 
105 
106 
QGraphicsScene * m_scene
Definition: AddCommand.h:82
std::string getName()
Returns name.
Definition: EnumType.cpp:54
virtual void redo()
Reimplemented from QUndoCommand.
Definition: AddCommand.cpp:70
virtual ~AddCommand()
Destructor.
Definition: AddCommand.cpp:53
Abstract class to represent an observer. "View" part of MVC component. All classes representing the g...
Definition: ItemObserver.h:52
virtual EnumType * getType()=0
Returns the type of component Reimplement this function in a Observable subclass to provide the model...
AddCommand(QGraphicsItem *item, QUndoCommand *parent=0)
Constructor.
Definition: AddCommand.cpp:39
virtual void undo()
Reimplemented from QUndoCommand.
Definition: AddCommand.cpp:58
virtual QString createCommandString(QGraphicsItem *item, const QPointF &pos)
Definition: AddCommand.cpp:90
virtual bool addItemStackWithoutScene(QGraphicsItem *item)
Definition: Scene.cpp:810
Class representing the scene. This scene is child of QGraphicsScene, part of Graphics View Framework...
Definition: Scene.h:80
virtual bool removeItemStackWithoutScene(QGraphicsItem *item)
Definition: Scene.cpp:832
QGraphicsItem * m_item
Definition: AddCommand.h:83
virtual Observable * getModel()
Returns the "Model" part of the MVC.
Undo/Redo for add one components.
virtual void insertItem(ItemObserver *item)
Method that inserts a graphic object in the scene. Inverts the matrix of the object if necessary...
Definition: Scene.cpp:116