All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
AlignItems.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 AlignItems.cpp
22 
23  \brief
24 
25  \ingroup layout
26 */
27 
28 // TerraLib
29 #include "AlignItems.h"
30 #include "../../core/pattern/mvc/ItemObserver.h"
31 
32 // STL
33 #include <sstream>
34 
35 // Qt
36 #include <QGraphicsScene>
37 #include <QGraphicsItem>
38 
39 te::layout::AlignItems::AlignItems( QGraphicsScene* scene, PaperConfig* config ):
40  m_scene(scene),
41  m_config(config)
42 {
43 
44 }
45 
47 {
48 
49 }
50 
52 {
53  if (m_scene->selectedItems().isEmpty())
54  return;
55 
56  QGraphicsItem* selectedItem = m_scene->selectedItems().first();
57  QList<QGraphicsItem *> overlapItems = selectedItem->collidingItems();
58 
59  qreal maxZValue = selectedItem->zValue();
60  QGraphicsItem* itemMaxZValue = selectedItem;
61 
62  qreal zValue = selectedItem->zValue();
63  foreach (QGraphicsItem *item, overlapItems)
64  {
65  if(item)
66  {
67  ItemObserver* it = dynamic_cast<ItemObserver*>(item);
68  if(it)
69  {
70  if((item->zValue() >= zValue) && (it->isCanChangeGraphicOrder()))
71  {
72  maxZValue = item->zValue();
73  itemMaxZValue = item;
74  }
75  }
76  }
77  }
78  selectedItem->setZValue(maxZValue);
79  if(itemMaxZValue)
80  {
81  itemMaxZValue->setZValue(zValue);
82  }
83 }
84 
86 {
87  if (m_scene->selectedItems().isEmpty())
88  return;
89 
90  QGraphicsItem *selectedItem = m_scene->selectedItems().first();
91  QList<QGraphicsItem *> overlapItems = selectedItem->collidingItems();
92 
93  qreal minimumZValue = selectedItem->zValue();
94  QGraphicsItem* itemMinimumZValue = selectedItem;
95 
96  qreal zValue = selectedItem->zValue();
97  foreach (QGraphicsItem *item, overlapItems)
98  {
99  if(item)
100  {
101  ItemObserver* it = dynamic_cast<ItemObserver*>(item);
102  if(it)
103  {
104  if (item->zValue() <= zValue && (it->isCanChangeGraphicOrder()))
105  {
106  minimumZValue = item->zValue();
107  itemMinimumZValue = item;
108  }
109  }
110  }
111  }
112  selectedItem->setZValue(minimumZValue);
113  if(itemMinimumZValue)
114  {
115  itemMinimumZValue->setZValue(zValue);
116  }
117 }
118 
120 {
121  QRectF sourceRect = getSelectionItemsBoundingBox();
122 
123  if(!sourceRect.isValid())
124  return;
125 
126  QList<QGraphicsItem *> items = m_scene->selectedItems();
127  double dbLeft = 0;
128 
129  double ww = 0;
130  double hh = 0;
131 
132  m_config->getPaperSize(ww, hh);
133 
134  if(items.count() == 1)
135  {
136  te::gm::Envelope ppbx(0, 0, ww, hh);
137  dbLeft = ppbx.getLowerLeftX();
138  QPointF pot(dbLeft, items.first()->sceneBoundingRect().y());
139  items.first()->setPos(pot);
140  return;
141  }
142 
143  dbLeft = sourceRect.left();
144 
145  foreach (QGraphicsItem *item, items)
146  {
147  if(item)
148  {
149  QPointF pt(dbLeft, item->scenePos().y());
150  item->setPos(pt);
151  }
152  }
153 }
154 
156 {
157  QRectF sourceRect = getSelectionItemsBoundingBox();
158 
159  if(!sourceRect.isValid())
160  return;
161 
162  QList<QGraphicsItem *> items = m_scene->selectedItems();
163 
164  double dbRight = 0;
165  double w = 0;
166 
167  double ww = 0;
168  double hh = 0;
169 
170  m_config->getPaperSize(ww, hh);
171 
172  if(items.count() == 1)
173  {
174  te::gm::Envelope ppbx(0, 0, ww, hh);
175  dbRight = ppbx.getUpperRightX();
176  w = dbRight - items.first()->sceneBoundingRect().width();
177  QPointF pot(w, items.first()->sceneBoundingRect().y());
178  items.first()->setPos(pot);
179  return;
180  }
181 
182  dbRight = sourceRect.right();
183 
184  foreach (QGraphicsItem *item, items)
185  {
186  if(item)
187  {
188  w = dbRight - item->sceneBoundingRect().width();
189  QPointF pt(w, item->scenePos().y());
190  item->setPos(pt);
191  }
192  }
193 }
194 
196 {
197  QRectF sourceRect = getSelectionItemsBoundingBox();
198 
199  if(!sourceRect.isValid())
200  return;
201 
202  QList<QGraphicsItem *> items = m_scene->selectedItems();
203  double dbBottom = 0;
204  double h = 0;
205 
206  double ww = 0;
207  double hh = 0;
208 
209  m_config->getPaperSize(ww, hh);
210 
211  if(items.count() == 1)
212  {
213  te::gm::Envelope ppbx(0, 0, ww, hh);
214  dbBottom = ppbx.getUpperRightY();
215  h = dbBottom - items.first()->sceneBoundingRect().height();
216  QPointF pot(items.first()->sceneBoundingRect().x(), h);
217  items.first()->setPos(pot);
218  return;
219  }
220 
221  dbBottom = sourceRect.bottom();
222 
223  foreach (QGraphicsItem *item, items)
224  {
225  if(item)
226  {
227  h = dbBottom - item->sceneBoundingRect().height();
228  QPointF pt(item->scenePos().x(), h);
229  item->setPos(pt);
230  }
231  }
232 }
233 
235 {
236  QRectF sourceRect = getSelectionItemsBoundingBox();
237 
238  if(!sourceRect.isValid())
239  return;
240 
241  QList<QGraphicsItem *> items = m_scene->selectedItems();
242  double dbTop = 0;
243 
244  double ww = 0;
245  double hh = 0;
246 
247  m_config->getPaperSize(ww, hh);
248 
249  if(items.count() == 1)
250  {
251  te::gm::Envelope ppbx(0, 0, ww, hh);
252  dbTop = ppbx.getLowerLeftY();
253  QPointF pot(items.first()->sceneBoundingRect().x(), dbTop);
254  items.first()->setPos(pot);
255  return;
256  }
257 
258  dbTop = sourceRect.top();
259 
260  foreach (QGraphicsItem *item, items)
261  {
262  if(item)
263  {
264  QPointF pt(item->scenePos().x(), dbTop);
265  item->setPos(pt);
266  }
267  }
268 }
269 
271 {
272  QRectF sourceRect = getSelectionItemsBoundingBox();
273 
274  if(!sourceRect.isValid())
275  return;
276 
277  QList<QGraphicsItem *> items = m_scene->selectedItems();
278  double dbCenterHrz = 0;
279  double w = 0;
280 
281  double ww = 0;
282  double hh = 0;
283 
284  m_config->getPaperSize(ww, hh);
285 
286  if(items.count() == 1)
287  {
288  te::gm::Envelope ppbx(0, 0, ww, hh);
289  dbCenterHrz = ppbx.getCenter().x;
290  w = items.first()->sceneBoundingRect().width() / 2.;
291  QPointF pot(dbCenterHrz - w, items.first()->sceneBoundingRect().y());
292  items.first()->setPos(pot);
293  return;
294  }
295 
296  dbCenterHrz = sourceRect.center().x();
297 
298  foreach (QGraphicsItem *item, items)
299  {
300  if(item)
301  {
302  w = item->sceneBoundingRect().width() / 2.;
303 
304  QPointF pt(dbCenterHrz - w, item->scenePos().y());
305  item->setPos(pt);
306  }
307  }
308 }
309 
311 {
312  QRectF sourceRect = getSelectionItemsBoundingBox();
313 
314  if(!sourceRect.isValid())
315  return;
316 
317  QList<QGraphicsItem *> items = m_scene->selectedItems();
318  double dbCenterVrt = 0;
319  double h = 0;
320 
321  double ww = 0;
322  double hh = 0;
323 
324  m_config->getPaperSize(ww, hh);
325 
326  if(items.count() == 1)
327  {
328  te::gm::Envelope ppbx(0, 0, ww, hh);
329  dbCenterVrt = ppbx.getCenter().y;
330  h = items.first()->sceneBoundingRect().height() / 2.;
331  QPointF pot(items.first()->sceneBoundingRect().x(), dbCenterVrt - h);
332  items.first()->setPos(pot);
333  return;
334  }
335 
336  dbCenterVrt = sourceRect.center().y();
337 
338  foreach (QGraphicsItem *item, items)
339  {
340  if(item)
341  {
342  h = item->sceneBoundingRect().height() / 2.;
343 
344  QPointF pt(item->scenePos().x(), dbCenterVrt - h);
345  item->setPos(pt);
346  }
347  }
348 }
349 
351 {
352  QRectF sourceRect;
353  QList<QGraphicsItem *> items = m_scene->selectedItems();
354 
355  foreach(QGraphicsItem *item, items)
356  {
357  sourceRect = sourceRect.united(item->sceneBoundingRect());
358  }
359 
360  return sourceRect;
361 }
Class responsible for paper setting. Size, orientation, custom size, etc.
Definition: PaperConfig.h:45
double y
y-coordinate.
Definition: Coord2D.h:114
AlignItems(QGraphicsScene *scene, PaperConfig *config)
Definition: AlignItems.cpp:39
double x
x-coordinate.
Definition: Coord2D.h:113
virtual void alignRight()
Definition: AlignItems.cpp:155
const double & getUpperRightX() const
It returns a constant refernce to the x coordinate of the upper right corner.
Definition: Envelope.h:410
const double & getLowerLeftY() const
It returns a constant refernce to the y coordinate of the lower left corner.
Definition: Envelope.h:400
Class applying the alignment of one or more objects. Ex .: send to back, bring to front...
Abstract class to represent an observer. "View" part of MVC component. All classes representing the g...
Definition: ItemObserver.h:52
const double & getUpperRightY() const
It returns a constant refernce to the x coordinate of the upper right corner.
Definition: Envelope.h:420
virtual void bringToFront()
Definition: AlignItems.cpp:51
virtual void sendToBack()
Definition: AlignItems.cpp:85
Coord2D getCenter() const
It returns the rectangle's center coordinate.
Definition: Envelope.cpp:51
An Envelope defines a 2D rectangular region.
Definition: Envelope.h:51
virtual bool isCanChangeGraphicOrder()
Returns whether the graphic component to be or not send to back or bring to front.
virtual void alignBottom()
Definition: AlignItems.cpp:234
virtual void alignCenterHorizontal()
Definition: AlignItems.cpp:270
virtual void alignTop()
Definition: AlignItems.cpp:195
const double & getLowerLeftX() const
It returns a constant reference to the x coordinate of the lower left corner.
Definition: Envelope.h:390
virtual void alignLeft()
Definition: AlignItems.cpp:119
virtual QRectF getSelectionItemsBoundingBox()
Definition: AlignItems.cpp:350
virtual void alignCenterVertical()
Definition: AlignItems.cpp:310