QElectroTech  0.70
conductortextitem.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 "conductortextitem.h"
19 #include "conductor.h"
20 #include "diagramcommands.h"
21 #include "diagram.h"
22 
29  DiagramTextItem(parent_conductor),
30  parent_conductor_(parent_conductor),
31  moved_by_user_(false),
32  rotate_by_user_(false)
33 {
34  setAcceptHoverEvents(true);
35 }
36 
43 ConductorTextItem::ConductorTextItem(const QString &text, Conductor *parent_conductor) :
44  DiagramTextItem(text, parent_conductor),
45  parent_conductor_(parent_conductor),
46  moved_by_user_(false),
47  rotate_by_user_(false)
48 {}
49 
54 }
55 
61  return(parent_conductor_);
62 }
63 
69 void ConductorTextItem::fromXml(const QDomElement &e) {
70  if (e.hasAttribute("userx")) {
71  setPos(e.attribute("userx").toDouble(),
72  e.attribute("usery").toDouble());
73  moved_by_user_ = true;
74  }
75  if (e.hasAttribute("rotation")) {
76  setRotation(e.attribute("rotation").toDouble());
77  rotate_by_user_ = true;
78  }
79 }
80 
86  return(moved_by_user_);
87 }
88 
94  return(rotate_by_user_);
95 }
96 
102 void ConductorTextItem::forceMovedByUser(bool moved_by_user) {
103  if (moved_by_user == moved_by_user_) return;
104 
105  moved_by_user_ = moved_by_user;
106  if (!moved_by_user && parent_conductor_) {
107  parent_conductor_ -> calculateTextItemPosition();
108  }
109 
110 }
111 
118 void ConductorTextItem::forceRotateByUser(bool rotate_by_user) {
119  if (rotate_by_user == rotate_by_user_) return;
120 
121  rotate_by_user_ = rotate_by_user;
122  if (!rotate_by_user && parent_conductor_) {
123  parent_conductor_ -> calculateTextItemPosition();
124  }
125 }
126 
131 void ConductorTextItem::setPos(const QPointF &pos)
132 {
133  /*
134  * In some condition the conductor text item is outside the border of folio in the left.
135  * They cause a margin on the left of folio and in most case this margin is unwanted and annoying the user.
136  * If the text is empty and the scene position is outside the border (left and top),
137  * we can say that this position, is unwanted by user.
138  * So we move this text item to the top left of the bounding rect of parent conductors, because we sure this position is wanted by user.
139  */
140  DiagramTextItem::setPos(pos);
141  if(toPlainText().isEmpty() && (scenePos().x() < 0 || scenePos().y() < 0))
142  {
143  Conductor *cond = parentConductor();
144  if(cond)
145  DiagramTextItem::setPos(cond->boundingRect().topLeft());
146  else
147  DiagramTextItem::setPos(0,0);
148  }
149 }
150 
156 void ConductorTextItem::setPos(qreal x, qreal y)
157 {
158  QPointF p(x,y);
159  setPos(p);
160 }
161 
166 void ConductorTextItem::mousePressEvent(QGraphicsSceneMouseEvent *event) {
167  before_mov_pos_ = pos();
169 }
170 
175 void ConductorTextItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
176  if (textInteractionFlags() & Qt::TextEditable) QGraphicsTextItem::mouseMoveEvent(event);
177 
178  else if ((flags() & QGraphicsItem::ItemIsMovable) && (event -> buttons() & Qt::LeftButton)) {
179 
180  QPointF intended_pos = event ->scenePos() + m_mouse_to_origin_movement;
181 
182  if (parent_conductor_) {
183  if (parent_conductor_->nearShape().contains(intended_pos)) {
184  event->modifiers() == Qt::ControlModifier ? setPos(intended_pos) : setPos(Diagram::snapToGrid(intended_pos));
185  parent_conductor_ -> setHighlighted(Conductor::Normal);
186  } else {
187  parent_conductor_ -> setHighlighted(Conductor::Alert);
188  }
189  }
190 
191  }
192 
193  else event -> ignore();
194 }
195 
200 void ConductorTextItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *e) {
201  if (flags() & QGraphicsItem::ItemIsMovable) {
202 
203  if (Diagram *diagram_ptr = diagram()) {
204  QPointF applied_movement = pos() - before_mov_pos_;
205 
206  if (!applied_movement.isNull()) {
207  //Create an undo object
208  MoveConductorsTextsCommand *undo_object = new MoveConductorsTextsCommand(diagram_ptr);
209  undo_object -> addTextMovement(this, before_mov_pos_, pos(), moved_by_user_);
210 
211  moved_by_user_ = true;
212 
213  diagram_ptr -> undoStack().push(undo_object);
214  }
215 
216  if (parent_conductor_) {
217  parent_conductor_ -> setHighlighted(Conductor::None);
218  }
219  }
220  }
221  if (!(e -> modifiers() & Qt::ControlModifier)) {
222  QGraphicsTextItem::mouseReleaseEvent(e);
223  }
224 }
225 
226 
227 
233 void ConductorTextItem::hoverEnterEvent(QGraphicsSceneHoverEvent *e) {
234  Q_UNUSED(e);
235 
236  m_mouse_hover = true;
237  QString str_ToolTip = toPlainText();
238  setToolTip( str_ToolTip );
239  update();
240 }
241 
247 void ConductorTextItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *e) {
248  Q_UNUSED(e);
249  //qDebug() << "Leave mouse over";
250  m_mouse_hover = false;
251  update();
252 }
253 
258 void ConductorTextItem::hoverMoveEvent(QGraphicsSceneHoverEvent *e) {
259  Q_UNUSED(e);
260  QGraphicsTextItem::hoverMoveEvent(e);
261 }
void hoverLeaveEvent(QGraphicsSceneHoverEvent *) override
virtual bool wasMovedByUser() const
void mousePressEvent(QGraphicsSceneMouseEvent *event) override
ConductorTextItem::mousePressEvent.
virtual void setPos(const QPointF &pos)
ConductorTextItem::setPos.
void mouseMoveEvent(QGraphicsSceneMouseEvent *event) override
ConductorTextItem::mouseMoveEvent.
virtual void forceMovedByUser(bool)
~ConductorTextItem() override
void fromXml(const QDomElement &) override
ConductorTextItem::fromXml Read the properties stored in the xml element given in parameter...
virtual void forceRotateByUser(bool)
ConductorTextItem::forceRotateByUser.
QRectF boundingRect() const override
Conductor::boundingRect.
Definition: conductor.cpp:862
void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override
ConductorTextItem::mouseReleaseEvent.
ConductorTextItem(Conductor *=nullptr)
QPointF m_mouse_to_origin_movement
void mousePressEvent(QGraphicsSceneMouseEvent *event) override
DiagramTextItem::mousePressEvent.
void hoverEnterEvent(QGraphicsSceneHoverEvent *) override
virtual bool wasRotateByUser() const
ConductorTextItem::wasRotateByUser.
static QPointF snapToGrid(const QPointF &p)
Diagram::snapToGrid Return a nearest snap point of p.
Definition: diagram.cpp:1653
void hoverMoveEvent(QGraphicsSceneHoverEvent *) override
Conductor * parentConductor() const
virtual QPainterPath nearShape() const
Conductor::nearShape.
Definition: conductor.cpp:888
Conductor * parent_conductor_
Diagram * diagram() const
DiagramTextItem::diagram.