2010-04-24 18:39:15 +00:00
|
|
|
/*
|
2020-06-15 17:42:37 +02:00
|
|
|
Copyright 2006-2020 The QElectroTech Team
|
2010-04-24 18:39:15 +00:00
|
|
|
This file is part of QElectroTech.
|
|
|
|
|
|
|
|
QElectroTech is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
QElectroTech is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with QElectroTech. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2010-04-18 17:59:54 +00:00
|
|
|
#include "conductortextitem.h"
|
|
|
|
#include "conductor.h"
|
2010-05-08 21:24:43 +00:00
|
|
|
#include "diagramcommands.h"
|
2014-07-21 20:44:32 +00:00
|
|
|
#include "diagram.h"
|
2010-04-18 17:59:54 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
Constructeur
|
|
|
|
@param parent_conductor Conducteur auquel ce texte est rattache
|
|
|
|
*/
|
2014-12-14 13:06:21 +00:00
|
|
|
ConductorTextItem::ConductorTextItem(Conductor *parent_conductor) :
|
|
|
|
DiagramTextItem(parent_conductor),
|
2010-05-08 21:24:43 +00:00
|
|
|
parent_conductor_(parent_conductor),
|
2010-05-09 00:30:41 +00:00
|
|
|
moved_by_user_(false),
|
2014-10-18 12:25:16 +00:00
|
|
|
rotate_by_user_(false)
|
2014-11-16 14:15:32 +00:00
|
|
|
{
|
2015-03-02 20:14:56 +00:00
|
|
|
setAcceptHoverEvents(true);
|
2014-11-16 14:15:32 +00:00
|
|
|
}
|
2010-04-18 17:59:54 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
Constructeur
|
|
|
|
@param text Le texte affiche par le champ de texte
|
|
|
|
@param parent_conductor Conducteur auquel ce texte est rattache
|
|
|
|
*/
|
2020-08-16 14:24:51 +02:00
|
|
|
ConductorTextItem::ConductorTextItem(const QString &text,
|
|
|
|
Conductor *parent_conductor) :
|
2014-12-14 13:06:21 +00:00
|
|
|
DiagramTextItem(text, parent_conductor),
|
2010-05-08 21:24:43 +00:00
|
|
|
parent_conductor_(parent_conductor),
|
2010-05-09 00:30:41 +00:00
|
|
|
moved_by_user_(false),
|
2014-10-18 12:25:16 +00:00
|
|
|
rotate_by_user_(false)
|
|
|
|
{}
|
2010-04-18 17:59:54 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
Destructeur
|
|
|
|
*/
|
|
|
|
ConductorTextItem::~ConductorTextItem() {
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
@return le conducteur parent de ce champ de texte, ou 0 si celui-ci n'en a
|
|
|
|
pas
|
|
|
|
*/
|
|
|
|
Conductor *ConductorTextItem::parentConductor() const {
|
|
|
|
return(parent_conductor_);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief ConductorTextItem::fromXml
|
|
|
|
Read the properties stored in the xml element given in parameter
|
|
|
|
@param e
|
|
|
|
*/
|
2010-04-18 17:59:54 +00:00
|
|
|
void ConductorTextItem::fromXml(const QDomElement &e) {
|
2014-11-14 13:06:24 +00:00
|
|
|
if (e.hasAttribute("userx")) {
|
|
|
|
setPos(e.attribute("userx").toDouble(),
|
|
|
|
e.attribute("usery").toDouble());
|
|
|
|
moved_by_user_ = true;
|
|
|
|
}
|
|
|
|
if (e.hasAttribute("rotation")) {
|
|
|
|
setRotation(e.attribute("rotation").toDouble());
|
|
|
|
rotate_by_user_ = true;
|
2010-05-08 21:24:43 +00:00
|
|
|
}
|
2010-04-18 17:59:54 +00:00
|
|
|
}
|
|
|
|
|
2010-05-08 21:24:43 +00:00
|
|
|
/**
|
|
|
|
@return true si ce champ de texte a ete explictement deplace par
|
|
|
|
l'utilisateur, false sinon
|
|
|
|
*/
|
|
|
|
bool ConductorTextItem::wasMovedByUser() const {
|
|
|
|
return(moved_by_user_);
|
|
|
|
}
|
|
|
|
|
2013-06-09 19:09:27 +00:00
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief ConductorTextItem::wasRotateByUser
|
|
|
|
@return true if text was explicit moved by user else false
|
|
|
|
*/
|
2013-06-09 19:09:27 +00:00
|
|
|
bool ConductorTextItem::wasRotateByUser() const {
|
|
|
|
return(rotate_by_user_);
|
|
|
|
}
|
|
|
|
|
2010-05-08 21:24:43 +00:00
|
|
|
/**
|
|
|
|
@param moved_by_user true pour que la position du texte soit consideree
|
|
|
|
comme ayant ete definie par l'utilisateur (et donc soit sauvegardee), false
|
|
|
|
pour remettre le texte a sa position originelle
|
|
|
|
*/
|
|
|
|
void ConductorTextItem::forceMovedByUser(bool moved_by_user) {
|
|
|
|
if (moved_by_user == moved_by_user_) return;
|
|
|
|
|
|
|
|
moved_by_user_ = moved_by_user;
|
|
|
|
if (!moved_by_user && parent_conductor_) {
|
2014-10-07 19:51:26 +00:00
|
|
|
parent_conductor_ -> calculateTextItemPosition();
|
2010-05-08 21:24:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-06-09 19:09:27 +00:00
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief ConductorTextItem::forceRotateByUser
|
|
|
|
@param rotate_by_user true pour que la rotation du texte soit consideree
|
2013-06-09 19:09:27 +00:00
|
|
|
comme ayant ete definie par l'utilisateur (et donc soit sauvegardee), false
|
|
|
|
pour remettre le texte a sont angle originelle
|
2020-08-16 11:19:36 +02:00
|
|
|
*/
|
2013-06-09 19:09:27 +00:00
|
|
|
void ConductorTextItem::forceRotateByUser(bool rotate_by_user) {
|
|
|
|
if (rotate_by_user == rotate_by_user_) return;
|
|
|
|
|
|
|
|
rotate_by_user_ = rotate_by_user;
|
|
|
|
if (!rotate_by_user && parent_conductor_) {
|
2014-10-07 19:51:26 +00:00
|
|
|
parent_conductor_ -> calculateTextItemPosition();
|
2017-08-03 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief ConductorTextItem::setPos
|
|
|
|
@param pos
|
|
|
|
*/
|
2017-08-03 17:04:08 +00:00
|
|
|
void ConductorTextItem::setPos(const QPointF &pos)
|
|
|
|
{
|
2020-07-15 20:20:07 +02:00
|
|
|
/*
|
|
|
|
* In some condition the conductor text item is outside the border of folio in the left.
|
|
|
|
* They cause a margin on the left of folio and in most case this margin is unwanted and annoying the user.
|
|
|
|
* If the text is empty and the scene position is outside the border (left and top),
|
|
|
|
* we can say that this position, is unwanted by user.
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
DiagramTextItem::setPos(pos);
|
|
|
|
if(toPlainText().isEmpty() && (scenePos().x() < 0 || scenePos().y() < 0))
|
|
|
|
{
|
|
|
|
Conductor *cond = parentConductor();
|
|
|
|
if(cond)
|
|
|
|
DiagramTextItem::setPos(cond->boundingRect().topLeft());
|
|
|
|
else
|
|
|
|
DiagramTextItem::setPos(0,0);
|
|
|
|
}
|
2017-08-03 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief ConductorTextItem::setPos
|
|
|
|
@param x
|
|
|
|
@param y
|
|
|
|
*/
|
2017-08-03 17:04:08 +00:00
|
|
|
void ConductorTextItem::setPos(qreal x, qreal y)
|
|
|
|
{
|
|
|
|
QPointF p(x,y);
|
|
|
|
setPos(p);
|
2013-06-09 19:09:27 +00:00
|
|
|
}
|
|
|
|
|
2010-05-08 21:24:43 +00:00
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief ConductorTextItem::mousePressEvent
|
|
|
|
@param event
|
|
|
|
*/
|
2014-10-18 12:25:16 +00:00
|
|
|
void ConductorTextItem::mousePressEvent(QGraphicsSceneMouseEvent *event) {
|
|
|
|
before_mov_pos_ = pos();
|
|
|
|
DiagramTextItem::mousePressEvent(event);
|
2010-05-08 21:24:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief ConductorTextItem::mouseMoveEvent
|
|
|
|
@param event
|
|
|
|
*/
|
2014-10-18 12:25:16 +00:00
|
|
|
void ConductorTextItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
|
|
|
|
if (textInteractionFlags() & Qt::TextEditable) QGraphicsTextItem::mouseMoveEvent(event);
|
|
|
|
|
|
|
|
else if ((flags() & QGraphicsItem::ItemIsMovable) && (event -> buttons() & Qt::LeftButton)) {
|
|
|
|
|
|
|
|
QPointF intended_pos = event ->scenePos() + m_mouse_to_origin_movement;
|
|
|
|
|
2010-05-08 21:24:43 +00:00
|
|
|
if (parent_conductor_) {
|
2015-09-15 08:20:39 +00:00
|
|
|
if (parent_conductor_->nearShape().contains(intended_pos)) {
|
2014-10-18 12:25:16 +00:00
|
|
|
event->modifiers() == Qt::ControlModifier ? setPos(intended_pos) : setPos(Diagram::snapToGrid(intended_pos));
|
2010-05-09 00:30:41 +00:00
|
|
|
parent_conductor_ -> setHighlighted(Conductor::Normal);
|
|
|
|
} else {
|
|
|
|
parent_conductor_ -> setHighlighted(Conductor::Alert);
|
2010-05-08 21:24:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-09 00:30:41 +00:00
|
|
|
}
|
2014-10-18 12:25:16 +00:00
|
|
|
|
|
|
|
else event -> ignore();
|
2010-05-08 21:24:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief ConductorTextItem::mouseReleaseEvent
|
|
|
|
@param e
|
|
|
|
*/
|
2010-05-08 21:24:43 +00:00
|
|
|
void ConductorTextItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *e) {
|
|
|
|
if (flags() & QGraphicsItem::ItemIsMovable) {
|
2014-10-18 12:25:16 +00:00
|
|
|
|
2010-05-08 21:24:43 +00:00
|
|
|
if (Diagram *diagram_ptr = diagram()) {
|
|
|
|
QPointF applied_movement = pos() - before_mov_pos_;
|
|
|
|
|
|
|
|
if (!applied_movement.isNull()) {
|
2014-10-18 12:25:16 +00:00
|
|
|
//Create an undo object
|
2018-07-30 15:24:29 +00:00
|
|
|
MoveConductorsTextsCommand *undo_object = new MoveConductorsTextsCommand(diagram_ptr);
|
2010-05-08 21:24:43 +00:00
|
|
|
undo_object -> addTextMovement(this, before_mov_pos_, pos(), moved_by_user_);
|
2014-10-18 12:25:16 +00:00
|
|
|
|
2010-05-08 21:24:43 +00:00
|
|
|
moved_by_user_ = true;
|
|
|
|
|
|
|
|
diagram_ptr -> undoStack().push(undo_object);
|
|
|
|
}
|
2010-05-09 00:30:41 +00:00
|
|
|
|
|
|
|
if (parent_conductor_) {
|
|
|
|
parent_conductor_ -> setHighlighted(Conductor::None);
|
|
|
|
}
|
2010-05-08 21:24:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!(e -> modifiers() & Qt::ControlModifier)) {
|
|
|
|
QGraphicsTextItem::mouseReleaseEvent(e);
|
|
|
|
}
|
|
|
|
}
|
2014-11-16 14:15:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
When mouse over element
|
2015-03-02 20:14:56 +00:00
|
|
|
change m_mouse_hover to true (used in paint() function )
|
2014-11-16 14:15:32 +00:00
|
|
|
@param e QGraphicsSceneHoverEvent
|
|
|
|
*/
|
|
|
|
void ConductorTextItem::hoverEnterEvent(QGraphicsSceneHoverEvent *e) {
|
|
|
|
Q_UNUSED(e);
|
|
|
|
|
2015-03-02 20:14:56 +00:00
|
|
|
m_mouse_hover = true;
|
2014-11-16 14:15:32 +00:00
|
|
|
QString str_ToolTip = toPlainText();
|
|
|
|
setToolTip( str_ToolTip );
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
When mouse over element leave the position
|
2015-03-02 20:14:56 +00:00
|
|
|
change m_mouse_hover to false(used in paint() function )
|
2014-11-16 14:15:32 +00:00
|
|
|
@param e QGraphicsSceneHoverEvent
|
|
|
|
*/
|
2020-09-04 23:00:32 +02:00
|
|
|
void ConductorTextItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *e)
|
|
|
|
{
|
2014-11-16 14:15:32 +00:00
|
|
|
Q_UNUSED(e);
|
2020-09-04 23:00:32 +02:00
|
|
|
qDebug() << "Leave mouse over";
|
2015-03-02 20:14:56 +00:00
|
|
|
m_mouse_hover = false;
|
2014-11-16 14:15:32 +00:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Do nothing default function .
|
|
|
|
@param e QGraphicsSceneHoverEvent
|
|
|
|
*/
|
2020-09-04 23:00:32 +02:00
|
|
|
void ConductorTextItem::hoverMoveEvent(QGraphicsSceneHoverEvent *e)
|
|
|
|
{
|
|
|
|
QGraphicsTextItem::hoverMoveEvent(e);
|
2014-11-16 14:15:32 +00:00
|
|
|
}
|