2013-11-14 10:11:22 +00:00
|
|
|
/*
|
2021-02-06 19:00:48 +01:00
|
|
|
Copyright 2006-2020 The QElectroTech Team
|
2013-11-14 10:11:22 +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/>.
|
|
|
|
*/
|
|
|
|
#include "qetgraphicsitem.h"
|
2020-12-08 19:57:35 +01:00
|
|
|
|
|
|
|
#include "../diagram.h"
|
2013-11-14 10:11:22 +00:00
|
|
|
|
|
|
|
/**
|
2020-08-20 17:44:59 +02:00
|
|
|
@brief QetGraphicsItem::QetGraphicsItem
|
|
|
|
Default constructor
|
|
|
|
@param parent : Parent Item
|
2020-08-16 11:19:36 +02:00
|
|
|
*/
|
2013-11-14 10:11:22 +00:00
|
|
|
QetGraphicsItem::QetGraphicsItem(QGraphicsItem *parent):
|
|
|
|
QGraphicsObject(parent),
|
2013-11-28 21:25:20 +00:00
|
|
|
is_movable_(true),
|
2019-02-04 19:00:46 +00:00
|
|
|
m_first_move(true),
|
2014-03-26 10:41:06 +00:00
|
|
|
snap_to_grid_(true)
|
2014-10-23 08:48:30 +00:00
|
|
|
{}
|
2013-11-14 10:11:22 +00:00
|
|
|
|
|
|
|
QetGraphicsItem::~QetGraphicsItem()
|
|
|
|
{}
|
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief QetGraphicsItem::diagram
|
2013-11-14 10:11:22 +00:00
|
|
|
*return the diagram of this item
|
2020-08-16 11:19:36 +02:00
|
|
|
*/
|
2013-11-14 10:11:22 +00:00
|
|
|
Diagram* QetGraphicsItem::diagram() const{
|
|
|
|
return(qobject_cast<Diagram *>(scene()));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief QetGraphicsItem::setPos
|
2013-11-14 10:11:22 +00:00
|
|
|
*set the position of the item to p
|
2020-08-16 11:19:36 +02:00
|
|
|
@param p the new position of item
|
|
|
|
*/
|
2013-11-14 10:11:22 +00:00
|
|
|
void QetGraphicsItem::setPos(const QPointF &p) {
|
2014-06-27 18:34:18 +00:00
|
|
|
QPointF pp = Diagram::snapToGrid(p);
|
2017-11-27 19:37:39 +00:00
|
|
|
if (pp == pos() || !is_movable_)
|
|
|
|
return;
|
|
|
|
QGraphicsItem::setPos(pp);
|
2013-11-14 10:11:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief QetGraphicsItem::setPos
|
2013-11-14 10:11:22 +00:00
|
|
|
*set the position of the item
|
2020-08-16 11:19:36 +02:00
|
|
|
@param x new abscisse of item
|
|
|
|
@param y new ordonne of item
|
|
|
|
*/
|
2013-11-14 10:11:22 +00:00
|
|
|
void QetGraphicsItem::setPos(qreal x, qreal y) {
|
|
|
|
setPos(QPointF(x, y));
|
|
|
|
}
|
|
|
|
|
2018-06-21 18:02:58 +00:00
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief QetGraphicsItem::state
|
|
|
|
@return the current state of this item
|
|
|
|
*/
|
2020-09-07 22:03:40 +02:00
|
|
|
QET::GraphicsItemState QetGraphicsItem::state() const
|
|
|
|
{
|
2018-06-21 18:02:58 +00:00
|
|
|
return m_state;
|
|
|
|
}
|
|
|
|
|
2013-11-14 10:11:22 +00:00
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief QetGraphicsItem::mousePressEvent
|
2013-11-14 10:11:22 +00:00
|
|
|
*handle the mouse click
|
2020-08-16 11:19:36 +02:00
|
|
|
@param event
|
|
|
|
*/
|
2019-02-04 19:00:46 +00:00
|
|
|
void QetGraphicsItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
|
2015-01-08 18:59:23 +00:00
|
|
|
{
|
2019-02-04 19:00:46 +00:00
|
|
|
if (event->button() == Qt::LeftButton)
|
2015-01-08 18:59:23 +00:00
|
|
|
{
|
2019-02-04 19:00:46 +00:00
|
|
|
m_first_move = true;
|
|
|
|
if (event->modifiers() & Qt::ControlModifier) {
|
2015-01-08 18:59:23 +00:00
|
|
|
setSelected(!isSelected());
|
2019-02-04 19:00:46 +00:00
|
|
|
}
|
2013-11-14 10:11:22 +00:00
|
|
|
}
|
2015-01-08 18:59:23 +00:00
|
|
|
|
2019-02-04 19:00:46 +00:00
|
|
|
QGraphicsItem::mousePressEvent(event);
|
2013-11-14 10:11:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief QetGraphicsItem::mouseDoubleClickEvent
|
2013-11-14 10:11:22 +00:00
|
|
|
*handle the mouse double click
|
2020-08-16 11:19:36 +02:00
|
|
|
@param event
|
|
|
|
*/
|
2019-02-04 19:00:46 +00:00
|
|
|
void QetGraphicsItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
|
|
|
|
{
|
2013-11-14 10:11:22 +00:00
|
|
|
editProperty();
|
2019-02-04 19:00:46 +00:00
|
|
|
event->accept();
|
2013-11-14 10:11:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief QetGraphicsItem::mouseMoveEvent
|
2013-11-14 10:11:22 +00:00
|
|
|
*handle mouse movement
|
2020-08-16 11:19:36 +02:00
|
|
|
@param event
|
|
|
|
*/
|
2019-02-04 19:00:46 +00:00
|
|
|
void QetGraphicsItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
|
|
|
|
{
|
|
|
|
if (isSelected() && event->buttons() & Qt::LeftButton)
|
|
|
|
{
|
|
|
|
//Item is moving
|
|
|
|
if(diagram() && m_first_move) {
|
|
|
|
//It's the first movement, we signal it to parent diagram
|
2018-04-05 09:37:35 +00:00
|
|
|
diagram()->elementsMover().beginMovement(diagram(), this);
|
2013-11-14 10:11:22 +00:00
|
|
|
}
|
|
|
|
|
2019-02-04 19:00:46 +00:00
|
|
|
//we apply the mouse movement
|
2013-11-14 10:11:22 +00:00
|
|
|
QPointF old_pos = pos();
|
2019-02-04 19:00:46 +00:00
|
|
|
if (m_first_move) {
|
|
|
|
m_mouse_to_origin_movement = old_pos - event -> buttonDownScenePos(Qt::LeftButton);
|
2013-11-14 10:11:22 +00:00
|
|
|
}
|
2019-02-04 19:00:46 +00:00
|
|
|
QPointF expected_pos = event->scenePos() + m_mouse_to_origin_movement;
|
2013-11-14 10:11:22 +00:00
|
|
|
setPos(expected_pos); // setPos() will snap the expected position to the grid
|
|
|
|
|
2019-02-04 19:00:46 +00:00
|
|
|
//we calcul the real movement apply by setPos()
|
2013-11-14 10:11:22 +00:00
|
|
|
QPointF effective_movement = pos() - old_pos;
|
|
|
|
if (diagram()) {
|
2019-02-04 19:00:46 +00:00
|
|
|
//we signal the real movement apply to diagram,
|
|
|
|
//who he apply to other selected item
|
2018-04-05 09:37:35 +00:00
|
|
|
diagram()->elementsMover().continueMovement(effective_movement);
|
2013-11-14 10:11:22 +00:00
|
|
|
}
|
2019-02-04 19:00:46 +00:00
|
|
|
event->accept();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
event->ignore();
|
|
|
|
}
|
2013-11-14 10:11:22 +00:00
|
|
|
|
2019-02-04 19:00:46 +00:00
|
|
|
if (m_first_move) {
|
|
|
|
m_first_move = false;
|
|
|
|
}
|
2013-11-14 10:11:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief QetGraphicsItem::mouseReleaseEvent
|
2020-08-20 17:44:59 +02:00
|
|
|
handle mouse release click
|
|
|
|
@param event
|
2020-08-16 11:19:36 +02:00
|
|
|
*/
|
2019-02-04 19:00:46 +00:00
|
|
|
void QetGraphicsItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
|
|
|
|
{
|
|
|
|
if (diagram()) {
|
2018-04-05 09:37:35 +00:00
|
|
|
diagram()->elementsMover().endMovement();
|
2019-02-04 19:00:46 +00:00
|
|
|
event->accept();
|
|
|
|
}
|
2013-11-14 10:11:22 +00:00
|
|
|
}
|