QElectroTech  0.70
qgraphicsitemutility.cpp
Go to the documentation of this file.
1 /*
2  Copyright 2006-2019 The QElectroTech Team
3  This file is part of QElectroTech.
4 
5  QElectroTech is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 2 of the License, or
8  (at your option) any later version.
9 
10  QElectroTech 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 General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with QElectroTech. If not, see <http://www.gnu.org/licenses/>.
17 */
18 #include "qgraphicsitemutility.h"
19 #include "element.h"
20 #include "diagram.h"
21 #include <QGraphicsItem>
22 #include <QDebug>
23 
30 bool centerToParentBottom(QGraphicsItem *item) {
31  if (! item->parentItem()) {
32  qDebug() << "Qet::centerToParentBottom : item have not parent";
33  return false;
34  }
35 
36  QPointF p = item -> parentItem() -> boundingRect().center();
37  p.ry() += item -> parentItem() -> boundingRect().height()/2;
38  p.rx() -= (item -> boundingRect().width()/2 + item->boundingRect().left()); //< we add boundingrect.left because this value can be négative
39 
40  item -> setPos(p);
41  return true;
42 }
43 
52 #include "elementtextitemgroup.h"
53 #include "crossrefitem.h"
54 bool centerToBottomDiagram (QGraphicsItem *item_to_center, Element *element_to_follow, qreal offset) {
55  if (! element_to_follow -> diagram()) {
56  qDebug() << "qgraphicsitemutility centerAtBottomDiagram : Element_to_follow have not diagram";
57  return false;
58  }
59 
60  QRectF border = element_to_follow -> diagram() -> border_and_titleblock.insideBorderRect();
61  QPointF point = element_to_follow -> sceneBoundingRect().center();
62 
63  point.setY(border.bottom() - item_to_center -> boundingRect().height() - offset );
64  point.rx() -= (item_to_center -> boundingRect().width()/2);
65 
66  //Apply the difference between the pos() of item and his bounding rect
67  QPointF tl = item_to_center->boundingRect().topLeft();
68  point.rx() -= tl.x();
69  point.ry() -= tl.y();
70 
71  item_to_center -> setPos(0,0); //Due to a weird behavior or bug, before set the new position and rotation,
72  item_to_center -> setRotation(0); //we must to set the position and rotation at 0.
73 
74  item_to_center->setPos(item_to_center->mapFromScene(point));
75 
76  qreal rot = item_to_center->rotation();
77  QGraphicsItem *parent = item_to_center->parentItem();
78  while (parent) {
79  rot += parent->rotation();
80  parent = parent->parentItem();
81  }
82  if(rot != 0)
83  item_to_center->setRotation(item_to_center->rotation() - rot);
84 
85  return true;
86 }
QRectF boundingRect() const override
Definition: element.cpp:205
bool centerToBottomDiagram(QGraphicsItem *item_to_center, Element *element_to_follow, qreal offset)
centerToBottomDiagram Set item pos to the bottom of diagram and centered vertically to element_to_fol...
bool centerToParentBottom(QGraphicsItem *item)
centerToParentBottom Center the item at the bottom of is parent.