All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
BuildGraphicsItem.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2001-2014 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 BuildGraphicsItem.cpp
22 
23  \brief
24 
25  \ingroup layout
26 */
27 
28 // TerraLib
29 #include "BuildGraphicsItem.h"
30 #include "ItemObserver.h"
31 #include "RectangleModel.h"
32 #include "RectangleController.h"
33 #include "RectangleItem.h"
34 #include "MapModel.h"
35 #include "MapController.h"
36 #include "MapItem.h"
37 #include "MapGridModel.h"
38 #include "MapGridController.h"
39 #include "MapGridItem.h"
40 #include "LegendModel.h"
41 #include "LegendController.h"
42 #include "LegendItem.h"
43 #include "PaperModel.h"
44 #include "PaperController.h"
45 #include "PaperItem.h"
46 #include "Context.h"
47 #include "SharedProperties.h"
48 #include "ScaleModel.h"
49 #include "ScaleController.h"
50 #include "ScaleItem.h"
51 #include "Scene.h"
52 #include "ItemUtils.h"
53 #include "ItemGroupModel.h"
54 #include "ItemGroupController.h"
55 #include "ItemGroup.h"
56 
57 // Qt
58 #include <QGraphicsItem>
59 
60 // STL
61 #include <sstream>
62 #include <string>
63 
65  m_zValue(0),
66  m_id(0),
67  m_props(0),
68  m_redraw(true),
69  m_name("Unknown"),
70  m_sharedProps(0),
71  m_paperItem("PAPER_"),
72  m_mapItem("MAP_"),
73  m_mapGridItem("MAPGRID_"),
74  m_textItem("TEXT_"),
75  m_rectangleItem("RECTANGLE_"),
76  m_legendItem("LEGEND_"),
77  m_scaleItem("SCALE_"),
78  m_horizontalRuler("HORIZONTAL_RULER_"),
79  m_verticalRuler("VERTICAL_RULER_"),
80  m_groupItem("ITEM_GROUP_")
81 {
83 }
84 
86 {
87  if(m_sharedProps)
88  {
89  delete m_sharedProps;
90  m_sharedProps = 0;
91  }
92 }
93 
95 {
96  QGraphicsItem* item = 0;
97 
98  if(!props)
99  return item;
100 
101  clear();
102 
103  m_props = props;
104  m_coord = findCoordinate(props);
105  m_zValue = findZValue(props);
106  m_redraw = draw;
107 
109 
110  switch(type)
111  {
112  case TPMapItem:
113  item = createMap();
114  break;
115  case TPMapGridItem:
116  item = createMapGrid();
117  break;
118  case TPText:
119  item = createText();
120  break;
121  case TPRetangleItem:
122  item = createRectangle();
123  break;
124  case TPLegendItem:
125  item = createLegend();
126  break;
127  case TPPaperItem:
128  item = createPaper();
129  break;
130  case TPScaleItem:
131  item = createScale();
132  break;
133  case TPItemGroup:
134  item = createItemGroup();
135  break;
136  default:
137  item = 0;
138  }
139 
140  return item;
141 }
142 
143 QGraphicsItem* te::layout::BuildGraphicsItem::createItem( te::layout::LayoutMode mode, const te::gm::Coord2D& coordinate, bool draw )
144 {
145  QGraphicsItem* item = 0;
146 
147  m_coord = coordinate;
148  clear();
149  m_redraw = draw;
150 
151  switch(mode)
152  {
153  case TypeCreateMap:
154  m_name = nameItem(m_mapItem, TPMapItem);
155  item = createMap();
156  break;
157  case TypeCreateMapGrid:
158  m_name = nameItem(m_mapGridItem, TPMapGridItem);
159  item = createMapGrid();
160  break;
161  case TypeCreateText:
162  m_name = nameItem(m_textItem, TPText);
163  item = createText();
164  break;
165  case TypeCreateRectangle:
166  m_name = nameItem(m_rectangleItem, TPRetangleItem);
167  item = createRectangle();
168  break;
169  case TypeCreateLegend:
170  m_name = nameItem(m_legendItem, TPLegendItem);
171  item = createLegend();
172  break;
173  case TypeCreateScale:
174  m_name = nameItem(m_scaleItem, TPScaleItem);
175  item = createScale();
176  break;
177  case TypeCreateItemGroup:
178  m_name = nameItem(m_groupItem, TPItemGroup);
179  item = createItemGroup();
180  break;
181  default:
182  item = 0;
183  }
184 
185  return item;
186 }
187 
189 {
190  /* Coordinate - x1, y1*/
191 
192  double x1 = 0;
193  double y1 = 0;
194 
195  Property pro_x1 = props->contains(m_sharedProps->getX1());
196 
197  if(!pro_x1.isNull())
198  {
199  x1 = pro_x1.getValue().toDouble();
200  }
201 
202  Property pro_y1 = props->contains(m_sharedProps->getY1());
203 
204  if(!pro_y1.isNull())
205  {
206  y1 = pro_y1.getValue().toDouble();
207  }
208 
209  te::gm::Coord2D coord(x1, y1);
210  return coord;
211 }
212 
214 {
215  int zValue = 0;
216 
217  Property pro_zValue = props->contains(m_sharedProps->getZValue());
218 
219  if(!pro_zValue.isNull())
220  {
221  zValue = pro_zValue.getValue().toInt();
222  }
223 
224  return zValue;
225 }
226 
228 {
229  AbstractScene* abstScene = Context::getInstance()->getScene();
230 
231  QList<QGraphicsItem*> graphicsItems;
232  std::stringstream ss;//create a stringstream
233 
234  if(abstScene)
235  {
236  Scene* sc = dynamic_cast<Scene*>(abstScene);
237  if(sc)
238  {
239  graphicsItems = sc->items();
240  }
241  }
242 
243  m_id = te::layout::maxTypeId(graphicsItems, type);
244 
245  if(m_id < 0)
246  m_id = 0;
247  else
248  m_id+= 1;
249 
250  ss << m_id;
251 
252  name+=ss.str();
253 
254  return name;
255 }
256 
258 {
259  m_id = 0;
260  m_name = "Unknown";
261  m_zValue = 0;
262  m_props = 0;
263  m_redraw = true;
264 }
265 
267 {
268  //Paper
269  QGraphicsItem* item = 0;
270 
271  PaperModel* model = new PaperModel(te::layout::Context::getInstance()->getPaperConfig());
272  if(m_props)
273  {
274  model->updateProperties(m_props);
275  }
276  else
277  {
278  model->setId(m_id);
279  model->setName(m_name);
280  }
281 
282  PaperController* controllerMap = new PaperController(model);
283  ItemObserver* itemObs = (ItemObserver*)controllerMap->getView();
284 
285  PaperItem* view = dynamic_cast<PaperItem*>(itemObs);
286 
287  if(view)
288  {
289  view->setPos(QPointF(m_coord.x, m_coord.y));
290  if(m_props)
291  {
292  view->setZValue(m_zValue);
293  }
294  if(m_redraw)
295  itemObs->redraw();
296  return view;
297  }
298 
299  return item;
300 }
301 
303 {
304  QGraphicsItem* item = 0;
305 
306  MapModel* model = new MapModel();
307  if(m_props)
308  {
309  model->updateProperties(m_props);
310  }
311  else
312  {
313  model->setId(m_id);
314  model->setName(m_name);
315  }
316 
317  MapController* controllerMap = new MapController(model);
318  ItemObserver* itemObs = (ItemObserver*)controllerMap->getView();
319 
320  MapItem* qrectMap = dynamic_cast<MapItem*>(itemObs);
321 
322  if(qrectMap)
323  {
324  qrectMap->setPos(QPointF(m_coord.x, m_coord.y));
325  if(m_props)
326  {
327  qrectMap->setZValue(m_zValue);
328  }
329  if(m_redraw)
330  itemObs->redraw();
331  return qrectMap;
332  }
333 
334  return item;
335 }
336 
338 {
339  QGraphicsItem* item = 0;
340 
341  MapGridModel* model = new MapGridModel();
342  if(m_props)
343  {
344  model->updateProperties(m_props);
345  }
346  else
347  {
348  model->setId(m_id);
349  model->setName(m_name);
350  }
351 
352  MapGridController* controllerMapGrid = new MapGridController(model);
353  ItemObserver* itemObs = (ItemObserver*)controllerMapGrid->getView();
354 
355  MapGridItem* qrectMapGrid = dynamic_cast<MapGridItem*>(itemObs);
356 
357  if(qrectMapGrid)
358  {
359  qrectMapGrid->setPos(QPointF(m_coord.x, m_coord.y));
360  if(m_props)
361  {
362  qrectMapGrid->setZValue(m_zValue);
363  }
364  if(m_redraw)
365  itemObs->redraw();
366  return qrectMapGrid;
367  }
368 
369  return item;
370 }
371 
373 {
374  QGraphicsItem* item = 0;
375 
376  return item;
377 }
378 
380 {
381  QGraphicsItem* item = 0;
382 
383  RectangleModel* model = new RectangleModel();
384  if(m_props)
385  {
386  model->updateProperties(m_props);
387  }
388  else
389  {
390  model->setId(m_id);
391  model->setName(m_name);
392  }
393 
394  RectangleController* controller = new RectangleController(model);
395  ItemObserver* itemObs = (ItemObserver*)controller->getView();
396 
397  RectangleItem* rect = dynamic_cast<RectangleItem*>(itemObs);
398 
399  if(rect)
400  {
401  rect->setPos(QPointF(m_coord.x, m_coord.y));
402  if(m_props)
403  {
404  rect->setZValue(m_zValue);
405  }
406  if(m_redraw)
407  itemObs->redraw();
408  return rect;
409  }
410 
411  return item;
412 }
413 
415 {
416  QGraphicsItem* item = 0;
417 
418  LegendModel* model = new LegendModel();
419  if(m_props)
420  {
421  model->updateProperties(m_props);
422  }
423  else
424  {
425  model->setId(m_id);
426  model->setName(m_name);
427  }
428 
429  LegendController* controller = new LegendController(model);
430  ItemObserver* itemObs = (ItemObserver*)controller->getView();
431 
432  LegendItem* legend = dynamic_cast<LegendItem*>(itemObs);
433 
434  if(legend)
435  {
436  legend->setPos(QPointF(m_coord.x, m_coord.y));
437  if(m_props)
438  {
439  legend->setZValue(m_zValue);
440  }
441  if(m_redraw)
442  itemObs->redraw();
443  return legend;
444  }
445 
446  return item;
447 }
448 
450 {
451  QGraphicsItem* item = 0;
452 
453  ScaleModel* model = new ScaleModel();
454  if(m_props)
455  {
456  model->updateProperties(m_props);
457  }
458  else
459  {
460  model->setId(m_id);
461  model->setName(m_name);
462  }
463 
464  ScaleController* controller = new ScaleController(model);
465  ItemObserver* itemObs = (ItemObserver*)controller->getView();
466 
467  ScaleItem* scale = dynamic_cast<ScaleItem*>(itemObs);
468 
469  if(scale)
470  {
471  scale->setPos(QPointF(m_coord.x, m_coord.y));
472  if(m_props)
473  {
474  scale->setZValue(m_zValue);
475  }
476  if(m_redraw)
477  itemObs->redraw();
478  return scale;
479  }
480 
481  return item;
482 }
483 
485 {
486  QGraphicsItem* item = 0;
487 
488  ItemGroupModel* model = new ItemGroupModel();
489  if(m_props)
490  {
491  model->updateProperties(m_props);
492  }
493  else
494  {
495  model->setId(m_id);
496  model->setName(m_name);
497  }
498 
499  ItemGroupController* controller = new ItemGroupController(model);
500  ItemObserver* itemObs = (ItemObserver*)controller->getView();
501 
502  ItemGroup* view = dynamic_cast<ItemGroup*>(itemObs);
503 
504  if(view)
505  {
506  if(m_props)
507  {
508  view->setZValue(m_zValue);
509  }
510  if(m_redraw)
511  {
512  itemObs->redraw();
513  }
514  return view;
515  }
516 
517  return item;
518 }
std::string nameItem(std::string name, te::layout::LayoutAbstractObjectType type)
Variant getValue()
Definition: Property.cpp:74
SharedProperties * m_sharedProps
const Observer * getView()
virtual void updateProperties(te::layout::Properties *properties)
Definition: ScaleModel.cpp:183
virtual void setPos(const QPointF &pos)
Definition: MapItem.cpp:284
An utility struct for representing 2D coordinates.
Definition: Coord2D.h:40
LayoutMode
Enum LayoutMode. This is the enumeration of the components types.
Definition: EnumMode.h:38
virtual void updateProperties(te::layout::Properties *properties)
void setZValue(qreal z)
Definition: MapItem.cpp:420
virtual void updateProperties(te::layout::Properties *properties)
Definition: MapModel.cpp:61
virtual void updateProperties(te::layout::Properties *properties)
void setZValue(qreal z)
Definition: ObjectItem.cpp:428
QGraphicsItem * rebuildItem(te::layout::Properties *props, bool draw=true)
te::gm::Coord2D findCoordinate(te::layout::Properties *props)
virtual void setName(std::string name)
virtual LayoutAbstractObjectType getTypeObj()
Definition: Properties.h:137
QGraphicsItem * createItem(te::layout::LayoutMode mode, const te::gm::Coord2D &coordinate, bool draw=true)
LayoutAbstractObjectType
Enum TdkAbstractComponentType. This is the enumeration of the components types.
Definition: AbstractType.h:38
virtual void setPos(const QPointF &pos)
Definition: ObjectItem.cpp:79
TELAYOUTEXPORT int maxTypeId(QList< QGraphicsItem * > graphicsItems, te::layout::LayoutAbstractObjectType type)
Definition: ItemUtils.cpp:133
AbstractScene * getScene()
Definition: Context.cpp:61
virtual void redraw(const double &scaleFactor=1.)
static Context * getInstance()
This function is called to create an instance of the class.
Definition: Context.cpp:46
void setZValue(qreal z)
Definition: ItemGroup.cpp:239
int findZValue(te::layout::Properties *props)
virtual bool contains(Property property)
Definition: Properties.h:147
virtual void updateProperties(te::layout::Properties *properties)