All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
ItemController.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 ItemController.cpp
22 
23  \brief
24 
25  \ingroup layout
26 */
27 
28 // TerraLib
29 #include "ItemController.h"
30 #include "ItemModelObservable.h"
31 #include "ItemObserver.h"
32 #include "../../ContextItem.h"
33 #include "../singleton/Context.h"
34 #include "../../../../maptools/Canvas.h"
35 #include "../factory/ItemParamsCreate.h"
36 #include "../factory/AbstractItemFactory.h"
37 #include "Observer.h"
38 
40  m_model(o)
41 {
42  create();
43 }
44 
46 {
47  if(m_model)
48  {
49  ItemModelObservable* model = dynamic_cast<ItemModelObservable*>(m_model);
50  if(model)
51  delete model;
52  m_model = 0;
53  }
54 }
55 
57 {
58  if(!m_model)
59  return;
60 
61  ItemModelObservable* model = dynamic_cast<ItemModelObservable*>(m_model);
62  if(!model)
63  return;
64 
65  ParamsCreate params;
66  double zoomFactor = Context::getInstance().getZoomFactor();
67  ContextItem contxt(params, zoomFactor);
68 
70  Utils* utils = Context::getInstance().getUtils();
71  double dpiX = Context::getInstance().getDpiX();
72  double dpiY = Context::getInstance().getDpiY();
73 
74  contxt.setCanvas(canvas);
75  contxt.setUtils(utils);
76  contxt.setDpiX(dpiX);
77  contxt.setDpiY(dpiY);
78 
79  utils->resetCanvas();
80  model->draw(contxt);
81  canvas->clear();
82 }
83 
85 {
86  return m_model;
87 }
88 
90 {
91  return m_view;
92 }
93 
95 {
96  if(m_model)
97  {
98  ItemModelObservable* model = dynamic_cast<ItemModelObservable*>(m_model);
99  if(model)
100  model->setBox(box);
101  }
102 }
103 
105 {
106  if(m_model)
107  {
108  ItemObserver* iOb = (ItemObserver*)m_view;
109  if(iOb)
110  {
111  iOb->refresh(); /* Update the position of the object */
112  }
113 
114  ItemModelObservable* model = dynamic_cast<ItemModelObservable*>(m_model);
115  if(model)
116  return model->contains(coord);
117  }
118  return false;
119 }
120 
122 {
124  ItemParamsCreate params(this, m_model);
125  m_view = (Observer*)factory->make(m_model->getType(), params);
126 }
127 
128 void te::layout::ItemController::setPosition( const double& x, const double& y )
129 {
130  if(m_model)
131  {
132  ItemModelObservable* model = dynamic_cast<ItemModelObservable*>(m_model);
133  if(model)
134  return model->setPosition(x, y);
135  }
136 }
virtual void setPosition(const double &x, const double &y)
Change coordinate llx,lly of the MVC component.
Abstract class to represent an observable. "Model" part of MVC component.
Definition: Observable.h:56
Class responsible for maintaining the drawing context of a MVC component. It is always used by the "M...
Definition: ContextItem.h:49
Abstract Factory provide an interface for creating families of related or dependent graphic objects (...
Abstract class to represent a controller. "Controller" part of MVC component. All classes representin...
virtual void setCanvas(te::map::Canvas *canvas)
Change canvas.
virtual bool contains(const te::gm::Coord2D &coord) const
Checks if the coordinate is contained within the bounding rectangle.
Abstract class to represent an observer. "View" part of MVC component. All classes representing the g...
const Observer * getView()
Returns the "View" part of the MVC component.
Abstract class to represent an observer. "View" part of MVC component.
Abstract class to represent an observable. "Model" part of MVC component. All classes representing th...
virtual void setUtils(Utils *utils)
virtual void clear()=0
It clears the canvas content and fills with the background color.
Abstract class to represent an observer. "View" part of MVC component. All classes representing the g...
Definition: ItemObserver.h:52
An utility struct for representing 2D coordinates.
Definition: Coord2D.h:40
virtual Observer * make(EnumType *type, ItemParamsCreate params=ItemParamsCreate())=0
Method that builds and returns a new object (MVC component) of the type defined as parameter...
virtual void setPosition(const double &x, const double &y)
Change coordinate llx,lly of the MVC component.
const Observable * getModel()
Returns the "Model" part of the MVC component.
virtual void resetCanvas()
Clears the canvas content and fills with the background color. Sets all width with 1...
Definition: Utils.cpp:585
Abstract class to represent an observer. "View" part of MVC component.
Definition: Observer.h:48
static Context & getInstance()
It returns a reference to the singleton instance.
An Envelope defines a 2D rectangular region.
Definition: Envelope.h:51
Utils * getUtils()
Returns pointer with functions to manipulate the canvas and conversion between projections.
Definition: Context.cpp:153
virtual void refresh(bool pos=true)
Updated model state.
ItemController(Observable *o)
Constructor.
AbstractItemFactory * getItemFactory()
Returns Abstract Factory that provide an interface for creating families of related or dependent grap...
Definition: Context.cpp:113
A canvas is an abstraction of a drawing area.
Definition: Canvas.h:91
Abstract class to represent an observable. "Model" part of MVC component. All classes representing th...
virtual void setDpiX(double dpi)
virtual ~ItemController()
Destructor.
virtual void create()
Call factory to create the "View" part of the MVC component and passes the model and himself as contr...
virtual void setDpiY(double dpi)
double getZoomFactor()
Returns current zoom factor. Ex.: 0.5 (50%)
Definition: Context.cpp:103
Parameters to creation new object. Ex.: "Model" and "Controller" of the new object (MVC component)...
virtual void draw(ContextItem context)
Drawing method of the MVC component.
Utility class with functions to manipulate the canvas and conversion between projections.
Definition: Utils.h:61
virtual bool contains(const te::gm::Coord2D &coord) const
Checks if the coordinate is contained within the bounding rectangle.
virtual void setBox(te::gm::Envelope box)
Change the bounding rectangle.
virtual void redraw()
Redraws the MVC component. Creates the ContextItem object and configures it. Calls the draw method of...
te::map::Canvas * getCanvas()
Returns abstraction of a drawing area.
Definition: Context.cpp:143
virtual void setBox(te::gm::Envelope box)
Change the bounding rectangle.