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 them_initialPosition
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 */
26 
27 // TerraLib
28 #include "AddCommand.h"
29 
31  QUndoCommand(parent),
32  m_display(display),
33  m_layer(layer),
34  m_id(id),
35  m_stack(UndoStackManager::getInstance())
36 {
37 }
38 
40 
42 {
43  if (m_stack.getAddWatches().empty())
44  return;
45 
47 
48  if (m_stack.m_currentIndex < 0)
50 
51  draw();
52 
53  emit commandFeedback();
54 }
55 
57 {
58  bool resultFound = false;
59 
60  if (!m_stack.getUndoStack())
61  return;
62 
63  for (int i = 0; i < m_stack.getUndoStack()->count(); ++i)
64  {
65  const QUndoCommand* cmd = m_stack.getUndoStack()->command(i);
66  if (cmd == this)
67  resultFound = true;
68  }
69 
70  //no makes redo while the command is not on the stack
71  if (resultFound)
72  {
74 
75  if (m_stack.m_currentIndex >= (int)m_stack.getAddWatches().size())
76  {
78  return;
79  }
80 
81  draw();
82 
83  emit commandFeedback();
84  }
85 }
86 
88 {
89  const te::gm::Envelope& env = m_display->getExtent();
90  if (!env.isValid())
91  return;
92 
93  // Clear!
94  QPixmap* draft = m_display->getDraftPixmap();
95  draft->fill(Qt::transparent);
96 
97  // Initialize the renderer
98  Renderer& renderer = Renderer::getInstance();
99  renderer.begin(draft, env, m_display->getSRID());
100 
102 
103  std::size_t pos = (m_stack.m_currentIndex < 0) ? 0 : static_cast<size_t>(m_stack.m_currentIndex);
104 
105  if (m_stack.m_currentIndex > -1)
106  {
107  repo.addFeature(m_layer->getId(), m_stack.getAddWatches()[pos]->clone());
108 
109  if (pos < m_stack.getAddWatches().size() - 1)
110  {
111  if (m_stack.getAddWatches()[pos]->clone()->getId()->getValueAsString() != m_id->getValueAsString())
112  {
113  if (repo.hasIdentify(m_layer->getId(), m_id))
114  repo.removeFeature(m_layer->getId(), m_id);
115 
116  if (static_cast<int>(countFeaturesById(m_id, pos)) > 1 && static_cast<int>(pos) < m_stack.m_currentIndex)
117  repo.addFeature(m_layer->getId(), m_stack.getAddWatches()[pos]->clone());
118  }
119  }
120 
121  }
122  else if (repo.hasIdentify(m_layer->getId(), m_stack.getAddWatches()[pos]->getId()))
123  {
124  repo.removeFeature(m_layer->getId(), m_stack.getAddWatches()[pos]->getId());
125  }
126 
127  // Draw the layer edited geometries
128  renderer.drawRepository(m_layer->getId(), env, m_display->getSRID());
129 
130  if (m_stack.m_currentIndex < 0)
131  {
132  renderer.end();
133  m_display->repaint();
134  return;
135  }
136 
137  // Draw the geometry
138  renderer.draw(m_stack.getAddWatches()[static_cast<size_t>(m_stack.m_currentIndex)]->clone()->getGeometry(), true);
139 
140  renderer.end();
141 
142  m_display->repaint();
143 }
144 
145 std::size_t te::edit::AddCommand::countFeaturesById(te::da::ObjectId* id, std::size_t& lastPos)
146 {
147  std::size_t count = 0;
148  std::vector<std::size_t> vecPos;
149 
150  if (m_stack.getAddWatches().empty())
151  return 0;
152 
153  for (std::size_t i = 0; i <m_stack.getAddWatches().size(); ++i)
154  {
155  if (m_stack.getAddWatches()[i]->isEquals(id))
156  {
157  count++;
158  vecPos.push_back(i);
159  }
160  }
161 
162  if (vecPos.size() > 1)
163  lastPos = vecPos.at(vecPos.size() - 2);
164 
165  return count;
166 }
virtual ~AddCommand()
Destructor.
QUndoStack * getUndoStack()
Method that return stack of Undo/Redo.
te::da::ObjectId * m_id
Definition: AddCommand.h:83
virtual void undo()
Reimplemented from QUndoCommand.
Definition: AddCommand.cpp:41
const std::vector< Feature * > & getAddWatches() const
A widget to control the display of a set of layers.
te::map::AbstractLayerPtr m_layer
Definition: AddCommand.h:82
This is a singleton for managing edit repositories.
static T & getInstance()
It returns a reference to the singleton instance.
Definition: Singleton.h:126
An Envelope defines a 2D rectangular region.
virtual int getSRID() const
It return the Spatial Reference System used by the Map Display.
This class represents an unique id for a data set element.
virtual const te::gm::Envelope & getExtent() const
It returns the world extent showned by the MapDisplay.
void draw(te::gm::Geometry *geom, bool showVertexes=false)
Definition: Renderer.cpp:260
std::string getValueAsString() const
It gets the properties values used to uniquely identify a data set element as string.
te::qt::widgets::MapDisplay * m_display
Definition: AddCommand.h:81
void removeFeature(const std::string &source, te::da::ObjectId *id)
This is a singleton for rendering geometries and features.
Definition: Renderer.h:70
bool hasIdentify(const std::string &source, te::da::ObjectId *id)
virtual QPixmap * getDraftPixmap() const
It returns the map display draft pixmap.
AddCommand(te::qt::widgets::MapDisplay *display, const te::map::AbstractLayerPtr &layer, te::da::ObjectId *id, QUndoCommand *parent=0)
Definition: AddCommand.cpp:30
virtual void redo()
Reimplemented from QUndoCommand.
Definition: AddCommand.cpp:56
void begin(QPaintDevice *device, const te::gm::Envelope &e, int srid)
Definition: Renderer.cpp:59
void drawRepository(const std::string &source, const te::gm::Envelope &e, int srid)
Definition: Renderer.cpp:78
void addFeature(const std::string &source, Feature *f)
std::size_t countFeaturesById(te::da::ObjectId *id, std::size_t &lastPos)
Definition: AddCommand.cpp:145
UndoStackManager & m_stack
Definition: AddCommand.h:84
boost::intrusive_ptr< AbstractLayer > AbstractLayerPtr
bool isValid() const
It tells if the rectangle is valid or not.