All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
GridMapItem.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 GridMapItem.cpp
22 
23  \brief
24 
25  \ingroup layout
26 */
27 
28 // TerraLib
29 #include "GridMapItem.h"
30 #include "../../core/pattern/mvc/ItemController.h"
31 #include "../../core/AbstractScene.h"
32 #include "../../core/pattern/mvc/Observable.h"
33 #include "../../../color/RGBAColor.h"
34 #include "../../../qt/widgets/Utils.h"
35 #include "../../../geometry/Envelope.h"
36 #include "../../../common/STLUtils.h"
37 #include "../../item/GridMapModel.h"
38 #include "MapItem.h"
39 
40 //Qt
41 #include <QStyleOptionGraphicsItem>
42 #include "../../core/WorldTransformer.h"
43 
45  ObjectItem(controller, o),
46  m_maxWidthTextMM(0),
47  m_maxHeigthTextMM(0),
48  m_onePointMM(0.3527777778),
49  m_changeSize(false)
50 {
51  m_nameClass = std::string(this->metaObject()->className());
52 }
53 
55 {
56 
57 }
58 
59 void te::layout::GridMapItem::paint( QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget /*= 0 */ )
60 {
61  ObjectItem::paint(painter, option, widget);
62 
63  Q_UNUSED( option );
64  Q_UNUSED( widget );
65  if ( !painter )
66  {
67  return;
68  }
69 
70  drawBackground(painter);
71 
72  drawGrid(painter);
73 
74  //Draw Selection
75  if (option->state & QStyle::State_Selected)
76  {
77  drawSelection(painter);
78  }
79 }
80 
81 void te::layout::GridMapItem::drawGrid( QPainter* painter )
82 {
83  GridMapModel* model = dynamic_cast<GridMapModel*>(m_model);
84  if(!model)
85  {
86  return;
87  }
88 
89  painter->save();
90 
91  QRectF parentBound = boundingRect();
92 
93  if(parentItem())
94  {
95  parentBound = parentItem()->boundingRect();
96  }
97 
98  QPainterPath gridMapPath;
99  gridMapPath.setFillRule(Qt::WindingFill);
100 
101  int heightRect = (int)parentBound.height();
102  int widgetRect = (int)parentBound.width();
103 
104  te::color::RGBAColor rgbColor = model->getLineColor();
105  QColor cLine(rgbColor.getRed(), rgbColor.getGreen(), rgbColor.getBlue(), rgbColor.getAlpha());
106 
107  painter->setPen(QPen(cLine, 0, Qt::SolidLine));
108 
109  QFont ft(model->getFontFamily().c_str(), model->getTextPointSize());
110 
111  painter->setFont(ft);
112 
113  m_maxHeigthTextMM = m_onePointMM * ft.pointSize();
114 
115  //QString text = "A";
116 
117  for (int i = 0; i <= heightRect; i+=10)
118  {
119  QLineF lineOne = QLineF(parentBound.topLeft().x(), parentBound.topLeft().y() + i, parentBound.topRight().x(), parentBound.topRight().y() + i);
120 
121  QPointF pointInit(parentBound.topLeft().x(), parentBound.topLeft().y() + i - (m_maxHeigthTextMM/2)); //left
122  //drawText(pointInit, painter, text.toStdString(), true);
123  QPointF pointFinal(parentBound.topRight().x(), parentBound.topRight().y() + i - (m_maxHeigthTextMM/2)); //right
124  //drawText(pointFinal, painter, text.toStdString());
125 
126  painter->drawLine(lineOne);
127 
128  for (int j = 0; j <= widgetRect; j+=10)
129  {
130  QLineF lineTwo = QLineF(parentBound.topLeft().x() + j, parentBound.topLeft().y(), parentBound.bottomLeft().x() + j, parentBound.bottomLeft().y());
131 
132  QPointF pointInit(parentBound.topLeft().x() + j + (m_maxWidthTextMM/2), boundingRect().topLeft().y()); //upper
133  //drawText(pointInit, painter, text.toStdString(), true);
134  QPointF pointFinal(parentBound.bottomLeft().x() + j - (m_maxWidthTextMM/2), parentBound.bottomLeft().y()); //lower
135  //drawText(pointFinal, painter, text.toStdString());
136 
137  painter->drawLine(lineTwo);
138  }
139  }
140 
141  painter->restore();
142 }
143 
144 void te::layout::GridMapItem::drawText( QPointF point, QPainter* painter, std::string text, bool displacementLeft /*= false*/, bool displacementRight /*= false*/ )
145 {
146  painter->save();
147 
148  QTransform t = painter->transform();
149  QPointF p = t.map(point);
150 
151  double zoomFactor = Context::getInstance().getZoomFactor();
152 
153  QFont ft = painter->font();
154  ft.setPointSize(ft.pointSize() * zoomFactor);
155  painter->setFont(ft);
156 
157  QFontMetrics fm(ft);
158  int width = fm.width(text.c_str());
159 
160  QPointF newPoint (p);
161 
162  if(displacementLeft)
163  {
164  newPoint.setX(newPoint.x() - width);
165  }
166 
167  if(displacementRight)
168  {
169  newPoint.setX(newPoint.x() + width);
170  }
171 
172  QTransform copyT = painter->transform().inverted();
173  QPointF copyP = copyT.map(newPoint);
174  double widthMM = point.x() - copyP.x();
175 
176  if(widthMM > m_maxWidthTextMM)
177  {
178  m_maxWidthTextMM = widthMM;
179  m_changeSize = true;
180  }
181 
182  //Keeps the size of the text.(Aspect)
183  painter->setMatrixEnabled(false);
184  painter->drawText(newPoint, text.c_str());
185  painter->setMatrixEnabled(true);
186 
187  painter->restore();
188 }
189 
191 {
192  if(parentItem())
193  {
194  return parentItem()->boundingRect();
195  }
196  return m_rect;
197 }
198 
200 {
201  if(!m_changeSize)
202  return;
203 
204  if(parentItem())
205  {
206  QRectF parentBoundRect = parentItem()->boundingRect();
207  if(parentBoundRect.isValid())
208  {
209  QRectF boundRect = boundingRect();
210  double w = parentBoundRect.width() + (m_maxWidthTextMM*2);
211  double h = parentBoundRect.height() + (m_maxHeigthTextMM*2);
212  if(boundRect.width() != w || boundRect.height() != h)
213  {
214  prepareGeometryChange();
215  QRectF rect(0., 0., w, h);
216  setRect(rect);
217 
218  //update model
219  te::gm::Envelope box(m_model->getBox());
220  box.m_urx = box.m_llx + w;
221  box.m_ury = box.m_lly + h;
222  m_controller->setBox(box);
223  }
224  }
225  }
226  m_changeSize = false;
227 }
228 
229 QVariant te::layout::GridMapItem::itemChange( QGraphicsItem::GraphicsItemChange change, const QVariant & value )
230 {
231  if(change == QGraphicsItem::ItemParentHasChanged)
232  {
233  GridMapModel* model = dynamic_cast<GridMapModel*>(m_model);
234  if(model)
235  {
236  if(parentItem())
237  {
238  MapItem* mapItem = dynamic_cast<MapItem*>(parentItem());
239  if(mapItem)
240  {
241  if(mapItem->getModel())
242  {
243  model->setMapName(mapItem->getModel()->getName());
244  }
245  }
246  }
247  }
248  }
249  return QGraphicsItem::itemChange(change, value);
250 }
251 
252 
253 
254 
255 
256 
257 
258 
259 
260 
261 
262 
263 
264 
265 
266 
267 
268 
Abstract class to represent an observable. "Model" part of MVC component.
Definition: Observable.h:56
virtual te::color::RGBAColor getLineColor()
virtual int getTextPointSize()
Class that represents a "Model" part of GridMap MVC component. Its coordinate system is the same of s...
Definition: GridMapModel.h:67
int getRed() const
It returns the red component color value (a value from 0 to 255).
Definition: RGBAColor.h:295
GridMapItem(ItemController *controller, Observable *o)
Constructor.
Definition: GridMapItem.cpp:44
virtual void drawGrid(QPainter *painter)
Definition: GridMapItem.cpp:81
int getBlue() const
It returns the blue component color value (a value from 0 to 255).
Definition: RGBAColor.h:305
double m_urx
Upper right corner x-coordinate.
Definition: Envelope.h:346
int getGreen() const
It returns the green component color value (a value from 0 to 255).
Definition: RGBAColor.h:300
virtual QRectF boundingRect() const
Reimplemented from ParentItem.
Abstract class to represent a controller. "Controller" part of MVC component. All classes representin...
virtual ~GridMapItem()
Destructor.
Definition: GridMapItem.cpp:54
virtual QVariant itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant &value)
Reimplemented from QGraphicsItem.
Abstract class that represents a graphic item. This object is of type QGraphicsObject.
Definition: ObjectItem.h:61
static Context & getInstance()
It returns a reference to the singleton instance.
An Envelope defines a 2D rectangular region.
Definition: Envelope.h:51
int getAlpha() const
It returns the alpha component color value (a value from 0 to 255).
Definition: RGBAColor.h:310
std::string m_nameClass
Class name.
Definition: ItemObserver.h:201
Class that represents a graphic GridMap. Its coordinate system is the same of scene (millimeters)...
virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget=0)
Reimplemented from QGraphicsItem.
virtual void recalculateBoundingRect()
The default implementation does nothing.
virtual void drawText(QPointF point, QPainter *painter, std::string text, bool displacementLeft=false, bool displacementRight=false)
virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget=0)
Reimplemented from QGraphicsItem.
Definition: GridMapItem.cpp:59
A helper class for 32-bit RGBA (Red-Green-Blue-Alpha channel) color.
Definition: RGBAColor.h:57
virtual Observable * getModel()
Returns the "Model" part of the MVC.
virtual std::string getFontFamily()
virtual void setMapName(std::string name)
double getZoomFactor()
Returns current zoom factor. Ex.: 0.5 (50%)
Definition: Context.cpp:103
virtual std::string getName()=0
Method that returns the name of the MVC component. Reimplement this function in a Observable subclass...
This class is a proxy MapDisplay. This makes it possible to add a MapDisplay as item of a scene...
Definition: MapItem.h:74