All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
ItemUtils.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 ItemUtils.cpp
22 
23  \brief
24 
25  \ingroup layout
26 */
27 
28 // TerraLib
29 #include "ItemUtils.h"
30 #include "../../core/pattern/mvc/ItemModelObservable.h"
31 #include "../../core/pattern/singleton/Context.h"
32 #include "../../core/enum/Enums.h"
33 #include "../item/MapItem.h"
34 #include "../item/LegendItem.h"
35 #include "../../item/LegendModel.h"
36 #include "../../item/MapModel.h"
37 #include "../item/TextItem.h"
38 #include "../../item/TextModel.h"
39 #include "../item/LegendChildItem.h"
40 #include "../../item/LegendChildModel.h"
41 #include "../../core/pattern/derivativevisitor/VisitorUtils.h"
42 #include "../../item/GridGeodesicModel.h"
43 #include "../../item/GridPlanarModel.h"
44 #include "Scene.h"
45 #include "../item/GridMapItem.h"
46 
47 // STL
48 #include <stddef.h> // defines NULL
49 
50 // Qt
51 #include <QObject>
52 #include <QGraphicsItem>
53 #include <QGraphicsScene>
54 #include <QTextDocument>
55 #include <QFont>
56 
57 te::layout::ItemUtils::ItemUtils( QGraphicsScene* scene ) :
58  m_scene(scene)
59 {
60 
61 }
62 
64 {
65 
66 }
67 
68 std::vector<te::layout::MapItem*> te::layout::ItemUtils::getMapItemList(bool selected)
69 {
70  std::vector<te::layout::MapItem*> list;
71 
72  QList<QGraphicsItem*> graphicsItems = getItems(selected);
73  foreach( QGraphicsItem *item, graphicsItems)
74  {
75  if(!item)
76  continue;
77 
78  te::layout::ItemObserver* lItem = dynamic_cast<te::layout::ItemObserver*>(item);
79  if(!lItem)
80  continue;
81 
82  te::layout::MapItem* mit = dynamic_cast<te::layout::MapItem*>(lItem);
83  if(!mit)
84  continue;
85 
86  list.push_back(mit);
87  }
88 
89  return list;
90 }
91 
93 {
94  te::layout::MapItem* map = 0;
95 
96  QList<QGraphicsItem*> graphicsItems = getItems(false);
97  foreach( QGraphicsItem *item, graphicsItems)
98  {
99  if(!item)
100  continue;
101 
102  te::layout::ItemObserver* lItem = dynamic_cast<te::layout::ItemObserver*>(item);
103  if(!lItem)
104  continue;
105 
106  te::layout::MapItem* mit = dynamic_cast<te::layout::MapItem*>(lItem);
107  if(!mit)
108  continue;
109 
110  if(!mit->getModel())
111  continue;
112 
113  if(mit->getModel()->getName().compare(name) != 0)
114  continue;
115 
116  map = mit;
117  }
118 
119  return map;
120 }
121 
122 std::vector<std::string> te::layout::ItemUtils::mapNameList(bool selected)
123 {
124  std::vector<std::string> strList;
125 
126  QList<QGraphicsItem*> graphicsItems = getItems(selected);
127  foreach( QGraphicsItem *item, graphicsItems)
128  {
129  if(!item)
130  continue;
131 
132  te::layout::ItemObserver* lItem = dynamic_cast<te::layout::ItemObserver*>(item);
133  if(!lItem)
134  continue;
135 
136  te::layout::MapItem* mit = dynamic_cast<te::layout::MapItem*>(lItem);
137  if(!mit)
138  continue;
139 
140  if(!mit->getModel())
141  continue;
142 
143  std::string name = mit->getModel()->getName();
144  strList.push_back(name);
145  }
146 
147  return strList;
148 }
149 
151 {
152  int count = 0;
153 
154  QList<QGraphicsItem*> graphicsItems = getItems();
155  foreach( QGraphicsItem *item, graphicsItems)
156  {
157  if(!item)
158  continue;
159 
160  ItemObserver* obs = dynamic_cast<ItemObserver*>(item);
161  if(!obs)
162  continue;
163 
164  if(obs->getModel()->getType() == type)
165  {
166  count+=1;
167  }
168  }
169 
170  return count;
171 }
172 
174 {
175  int id = -1;
176 
177  QList<QGraphicsItem*> graphicsItems = getItems();
178  foreach( QGraphicsItem *item, graphicsItems)
179  {
180  if(!item)
181  continue;
182 
183  ItemObserver* obs = dynamic_cast<ItemObserver*>(item);
184  if(!obs)
185  continue;
186 
187  ItemModelObservable* model = dynamic_cast<ItemModelObservable*>(obs->getModel());
188  if(!model)
189  continue;
190 
191  if(obs->getModel()->getType() == type)
192  {
193  if(id == -1)
194  {
195  id = model->getId();
196  }
197  else
198  {
199  if(model->getId() > id)
200  {
201  id = model->getId();
202  }
203  }
204  }
205  }
206 
207  return id;
208 }
209 
211 {
212  bool result = false;
213 
216 
217  if(mode == type->getModeMapPan())
218  {
219  result = true;
220  }
221  else if(mode == type->getModeMapZoomIn())
222  {
223  result = true;
224  }
225  else if(mode == type->getModeMapZoomOut())
226  {
227  result = true;
228  }
229  return result;
230 }
231 
232 QList<QGraphicsItem*> te::layout::ItemUtils::getItems( bool selected )
233 {
234  QList<QGraphicsItem*> graphicsItems;
235 
236  if(selected)
237  {
238  graphicsItems = m_scene->selectedItems();
239  }
240  else
241  {
242  graphicsItems = m_scene->items();
243  }
244 
245  return graphicsItems;
246 }
247 
249 {
250  if(!mode)
251  return;
252 
254 
255  if(mode == type->getModeNone())
256  return;
257 
258  if(!isCurrentMapTools())
259  return;
260 
261  QList<QGraphicsItem*> graphicsItems = getItems(true);
262  foreach(QGraphicsItem *item, graphicsItems)
263  {
264  if(!item)
265  continue;
266 
267  te::layout::ItemObserver* lItem = dynamic_cast<te::layout::ItemObserver*>(item);
268  if(!lItem)
269  continue;
270 
271  te::layout::MapItem* mit = dynamic_cast<te::layout::MapItem*>(lItem);
272  if(!mit)
273  continue;
274 
275  mit->changeCurrentTool(mode);
276  }
277 }
278 
280 {
281  //do nothing;
282 }
283 
285 {
286  QGraphicsItem *item = m_scene->selectedItems().first();
287  if(item)
288  {
289  ItemObserver* it = dynamic_cast<ItemObserver*>(item);
290  if(it)
291  {
292  MapItem* mt = dynamic_cast<MapItem*>(it);
293  if(mt)
294  {
295  MapModel* model = dynamic_cast<MapModel*>(mt->getModel());
296  std::map<te::gm::Point*, std::string> map = model->getTextMapAsObjectInfo();
297  createTextItemFromObject(map);
298  }
299  }
300  }
301 }
302 
304 {
305  QGraphicsItem *item = m_scene->selectedItems().first();
306  if(item)
307  {
308  ItemObserver* it = dynamic_cast<ItemObserver*>(item);
309  if(it)
310  {
311  LegendItem* lit = dynamic_cast<LegendItem*>(it);
312  if(lit)
313  {
314  LegendModel* model = dynamic_cast<LegendModel*>(lit->getModel());
315  MapModel* visitable = dynamic_cast<MapModel*>(model->getVisitable());
316 
317  std::map<te::gm::Point*, std::string> coord = model->getCoordChildren();
318  createLegendChildItemFromLegend(coord, visitable);
319  }
320  it->redraw();
321  }
322  }
323 }
324 
325 void te::layout::ItemUtils::createTextItemFromObject( std::map<te::gm::Point*, std::string> map, QFont* ft )
326 {
327  Scene* scne = dynamic_cast<Scene*>(m_scene);
328 
329  if(!scne)
330  return;
331 
333 
334  std::map<te::gm::Point*, std::string>::iterator it;
335 
336  for (it = map.begin(); it != map.end(); ++it)
337  {
338  te::gm::Point* pt = it->first;
339  std::string text = it->second;
340 
342 
343  QGraphicsItem* item = 0;
344 
345  te::gm::Coord2D coord(pt->getX(), pt->getY());
346  item = scne->createItem(coord);
347  if(!item)
348  continue;
349 
350  TextItem* txtItem = dynamic_cast<TextItem*>(item);
351  if(txtItem)
352  {
353  TextModel* model = dynamic_cast<TextModel*>(txtItem->getModel());
354  if(model)
355  {
356  if(ft)
357  {
358  txtItem->setFont(*ft);
359  Font fnt = model->getFont();
360  fnt.setFamily(ft->family().toStdString());
361  fnt.setPointSize(ft->pointSize());
362  model->setFont(fnt);
363  }
364  model->setText(text);
365  txtItem->document()->setPlainText(text.c_str());
366  }
367  }
368  }
369 
371 }
372 
373 void te::layout::ItemUtils::createLegendChildItemFromLegend( std::map<te::gm::Point*, std::string> map, te::layout::MapModel* visitable )
374 {
375  Scene* scne = dynamic_cast<Scene*>(m_scene);
376 
377  if(!scne)
378  return;
379 
380  if(!visitable)
381  return;
382 
384 
385  std::map<te::gm::Point*, std::string>::iterator it;
386 
387  for (it = map.begin(); it != map.end(); ++it)
388  {
389  te::gm::Point* pt = it->first;
390  std::string text = it->second;
391 
393 
394  QGraphicsItem* item = 0;
395  te::gm::Coord2D coord(pt->getX(), pt->getY());
396  item = scne->createItem(coord);
397  if(!item)
398  continue;
399 
400  LegendChildItem* lgItem = dynamic_cast<LegendChildItem*>(item);
401  if(lgItem)
402  {
403  QList<QGraphicsItem*> legends;
404  legends.push_back(lgItem);
405  te::layout::VisitorUtils::getInstance().changeMapVisitable(legends, visitable);
406  }
407  }
408 
410 }
411 
412 std::vector<te::layout::Properties*> te::layout::ItemUtils::getGridMapProperties()
413 {
414  std::vector<te::layout::Properties*> props;
415 
416  std::vector<te::layout::GridMapItem*> gridMapItems = getMapChildren();
417 
418  std::vector<te::layout::GridMapItem*>::iterator it = gridMapItems.begin();
419  for( ; it != gridMapItems.end() ; ++it)
420  {
421  if(!(*it))
422  {
423  continue;
424  }
425 
426  if(!(*it)->getModel())
427  {
428  continue;;
429  }
430 
431  Properties* prop = (*it)->getModel()->getProperties();
432  props.push_back(prop);
433  }
434 
435  return props;
436 }
437 
438 std::vector<te::layout::GridMapItem*> te::layout::ItemUtils::getMapChildren()
439 {
440  std::vector<te::layout::GridMapItem*> gridMapItems;
441 
442  QGraphicsItem *item = m_scene->selectedItems().first();
443  if(!item)
444  {
445  return gridMapItems;
446  }
447 
448  MapItem* map = dynamic_cast<MapItem*>(item);
449  if(!map)
450  {
451  return gridMapItems;
452  }
453 
454  QList<QGraphicsItem*> graphicsItems = map->childItems();
455  foreach(QGraphicsItem *item, graphicsItems)
456  {
457  if(!item)
458  continue;
459 
460  GridMapItem* grid = dynamic_cast<GridMapItem*>(item);
461  if(!grid)
462  {
463  continue;
464  }
465 
466  gridMapItems.push_back(grid);
467  }
468 
469  return gridMapItems;
470 }
471 
473 {
474  QGraphicsItem* intersectionItem = 0;
475 
477 
478  if(!abstScene)
479  {
480  return intersectionItem;
481  }
482 
483  Scene* sc = dynamic_cast<Scene*>(abstScene);
484  if(!sc)
485  {
486  return intersectionItem;
487  }
488 
489  QList<QGraphicsItem*> items = sc->selectedItems();
490 
491  QPointF pt(x, y);
492 
493  bool intersection = false;
494 
495  foreach (QGraphicsItem *item, items)
496  {
497  if(item)
498  {
499  bool intersection = item->contains(pt);
500  if(intersection)
501  {
502  intersectionItem = item;
503  break;
504  }
505  }
506  }
507 
508  return intersectionItem;
509 }
510 
511 
512 
513 
virtual std::map< te::gm::Point *, std::string > getTextMapAsObjectInfo()
Definition: MapModel.cpp:563
virtual void setText(std::string txt)
Definition: TextModel.cpp:99
Class specifies a font.
Definition: Font.h:46
Class that represents a "Model" part of Legend MVC component. Its coordinate system is the same of sc...
Definition: LegendModel.h:62
virtual void createTextMapAsObject()
Vectorizes inside text of selected MapItem. (generates te::layout::TextItem objects) ...
Definition: ItemUtils.cpp:284
virtual std::vector< Properties * > getGridMapProperties()
Get properties of all GridMapItem, children of a MapItem.
Definition: ItemUtils.cpp:412
virtual EnumModeType * getEnumModeType()
Returns mode type enumeration.
Definition: Enums.cpp:92
virtual EnumType * getModeMapZoomIn() const
Returns value that represents map zoom in mode type belonging to enumeration.
virtual EnumType * getModeCreateText() const
Returns value that represents create text mode type belonging to enumeration.
virtual std::vector< GridMapItem * > getMapChildren()
Get all GridMapItem, children of a MapItem.
Definition: ItemUtils.cpp:438
Utility class for manipulating items in the scene and vectorization of text and legend.
virtual EnumType * getModeCreateLegendChild() const
Returns value that represents create legend child mode type belonging to enumeration.
Abstract class to represent an observable. "Model" part of MVC component. All classes representing th...
Class representing the scene. This scene is child of QGraphicsScene, part of Graphics View Framework...
virtual bool isCurrentMapTools()
Indicates whether there is a tool active for object te::layout::MapItem.
Definition: ItemUtils.cpp:210
The Properties class represents a persistent set of properties. The Properties can be saved to a file...
Definition: Properties.h:52
void setPointSize(int point)
Sets point size of the font.
Definition: Font.cpp:66
Abstract class to represent an observer. "View" part of MVC component. All classes representing the g...
Definition: ItemObserver.h:52
virtual std::vector< Property > getProperties()
Returns set of all properties.
Definition: Properties.h:220
virtual std::map< te::gm::Point *, std::string > getCoordChildren()
An utility struct for representing 2D coordinates.
Definition: Coord2D.h:40
virtual void createLegendChildItemFromLegend(std::map< te::gm::Point *, std::string > map, MapModel *visitable)
Vectorizes grid text of selected MapItem. (generates te::layout::TextItem objects) ...
Definition: ItemUtils.cpp:373
ItemUtils(QGraphicsScene *scene)
Constructor.
Definition: ItemUtils.cpp:57
Class that represents a "Model" part of Text MVC component. Its coordinate system is the same of scen...
Definition: TextModel.h:58
virtual void redraw(bool bRefresh=true)
Redraws the graphic component.
Class that represents a graphic legend of a map. Its coordinate system is the same of scene (millimet...
Definition: LegendItem.h:54
virtual QGraphicsItem * intersectionSelectionItem(int x, int y)
Checks whether the coordinated intersects an item and returns.
Definition: ItemUtils.cpp:472
virtual EnumType * getType()=0
Returns the type of component Reimplement this function in a Observable subclass to provide the model...
const double & getY() const
It returns the Point y-coordinate value.
Definition: Point.h:150
static Context & getInstance()
It returns a reference to the singleton instance.
Class to represent a mode type enumeration. Ex.: select, pan, create text, etc. The type of mode is u...
Definition: EnumModeType.h:50
A point with x and y coordinate values.
Definition: Point.h:50
virtual std::vector< MapItem * > getMapItemList(bool selected=false)
MVC components list of type te::layout::MapItem.
Definition: ItemUtils.cpp:68
virtual void createTextItemFromObject(std::map< te::gm::Point *, std::string > map, QFont *ft=0)
Vectorizes grid text of selected MapItem. (generates te::layout::TextItem objects) ...
Definition: ItemUtils.cpp:325
virtual int getId()
Reimplemented from Observable.
virtual EnumType * getModeNone() const
Returns value that represents none mode type belonging to enumeration.
void setFamily(std::string family)
Returns font family name.
Definition: Font.cpp:56
virtual EnumType * getModeMapPan() const
Returns value that represents map pan mode type belonging to enumeration.
EnumType * getMode()
Returns value of the enumeration of mode type.
Definition: Context.cpp:75
virtual ~ItemUtils()
Destructor.
Definition: ItemUtils.cpp:63
Class representing the scene. This scene is child of QGraphicsScene, part of Graphics View Framework...
Definition: Scene.h:80
Class that represents a "Model" part of Map MVC component. Its coordinate system is the same of scene...
Definition: MapModel.h:61
Abstract scene for Scene, a QGraphicsScene class, part of Graphics View Framework.
Definition: AbstractScene.h:48
virtual MapItem * getMapItem(std::string name)
Search for te::layout::MapItem by name.
Definition: ItemUtils.cpp:92
Class that represents a graphic GridMap. Its coordinate system is the same of scene (millimeters)...
Definition: GridMapItem.h:52
virtual void createTextGridAsObject()
Vectorizes grid text of selected MapItem. (generates te::layout::TextItem objects) ...
Definition: ItemUtils.cpp:279
virtual QGraphicsItem * createItem(const te::gm::Coord2D &coord)
Method that create a graphic object and place it in the scene. A name and a position is added...
Definition: Scene.cpp:408
virtual EnumType * getModeMapZoomOut() const
Returns value that represents map zoom out mode type belonging to enumeration.
virtual void changeCurrentTool(EnumType *mode)
Definition: MapItem.cpp:547
Class that represents the value of an enumeration. An enumeration is made of "1..n" objects EnumType...
Definition: EnumType.h:48
virtual Visitable * getVisitable()
Return the object to be visited.
virtual Observable * getModel()
Returns the "Model" part of the MVC.
virtual void setCurrentToolInSelectedMapItems(EnumType *mode)
Changes the active tool of te::layout::MapItem.
Definition: ItemUtils.cpp:248
void setMode(EnumType *mode)
Change value of the enumeration of mode type. Ex.: select, pan, create text, etc. The type of mode is...
Definition: Context.cpp:93
virtual QList< QGraphicsItem * > getItems(bool selected=false)
Returns the list of items added to a scene.
Definition: ItemUtils.cpp:232
AbstractScene * getScene()
Returns abstract scene for QGraphicsScene class, part of Graphics View Framework. ...
Definition: Context.cpp:88
virtual Font getFont()
Definition: TextModel.cpp:109
virtual int countType(te::layout::EnumType *type)
Returns the number of items added to the scene with the type.
Definition: ItemUtils.cpp:150
virtual std::string getName()=0
Method that returns the name of the MVC component. Reimplement this function in a Observable subclass...
virtual std::vector< std::string > mapNameList(bool selected=false)
List of names te::layout::MapItem.
Definition: ItemUtils.cpp:122
virtual void createLegendChildAsObject()
Vectorizes selected LegendItem. (generates te::layout::TextItem objects)
Definition: ItemUtils.cpp:303
const double & getX() const
It returns the Point x-coordinate value.
Definition: Point.h:136
virtual void setFont(Font ft)
Definition: TextModel.cpp:114
This class is a proxy MapDisplay. This makes it possible to add a MapDisplay as item of a scene...
Definition: MapItem.h:74
virtual int maxTypeId(te::layout::EnumType *type)
Search max id for a type.
Definition: ItemUtils.cpp:173
Class that represents text. This object is of type QGraphicsTextItem. He is directly editable via use...
Definition: TextItem.h:70