2014-11-09 21:58:46 +00:00
|
|
|
/*
|
2020-06-15 17:42:37 +02:00
|
|
|
Copyright 2006-2020 The QElectroTech Team
|
2014-11-09 21:58:46 +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 <QObject>
|
|
|
|
|
|
|
|
#include "elementscene.h"
|
|
|
|
#include "partarc.h"
|
|
|
|
#include "editorcommands.h"
|
|
|
|
#include "eseventaddarc.h"
|
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief ESEventAddArc::ESEventAddArc
|
|
|
|
@param scene
|
|
|
|
*/
|
2014-11-09 21:58:46 +00:00
|
|
|
ESEventAddArc::ESEventAddArc(ElementScene *scene) :
|
|
|
|
ESEventInterface(scene),
|
|
|
|
m_arc(nullptr),
|
|
|
|
m_inverted(false)
|
|
|
|
{}
|
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief ESEventAddArc::~ESEventAddArc
|
|
|
|
*/
|
2020-09-07 22:03:40 +02:00
|
|
|
ESEventAddArc::~ESEventAddArc()
|
|
|
|
{
|
2014-11-09 21:58:46 +00:00
|
|
|
if (m_running || m_abort)
|
|
|
|
delete m_arc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief ESEventAddPolygon::mousePressEvent
|
|
|
|
@param event
|
|
|
|
@return
|
|
|
|
*/
|
2015-02-09 08:57:40 +00:00
|
|
|
bool ESEventAddArc::mousePressEvent(QGraphicsSceneMouseEvent *event)
|
|
|
|
{
|
|
|
|
if (event -> button() == Qt::LeftButton)
|
|
|
|
{
|
2014-11-09 21:58:46 +00:00
|
|
|
if(!m_running) m_running = true;
|
|
|
|
QPointF pos = m_scene->snapToGrid(event -> scenePos());
|
|
|
|
|
2015-02-09 08:57:40 +00:00
|
|
|
//create new arc
|
|
|
|
if (!m_arc)
|
|
|
|
{
|
|
|
|
m_arc = new PartArc(m_editor);
|
|
|
|
m_scene -> addItem(m_arc);
|
|
|
|
m_arc -> setPos(pos);
|
|
|
|
m_arc -> setProperty("startAngle", 0);
|
|
|
|
m_arc -> setProperty("spanAngle", 1440);
|
2014-11-09 21:58:46 +00:00
|
|
|
m_arc -> setProperty("antialias", true);
|
|
|
|
m_origin = pos;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-02-09 08:57:40 +00:00
|
|
|
//At this point, m_arc is finish, we add it with an undo command
|
2014-11-09 21:58:46 +00:00
|
|
|
m_arc -> setRect(m_arc->rect().normalized());
|
|
|
|
m_scene -> undoStack().push(new AddPartCommand(QObject::tr("Arc"), m_scene, m_arc));
|
|
|
|
|
2015-02-09 08:57:40 +00:00
|
|
|
//Set m_arc to nullptr for create new ellipse at next mouse press
|
2014-11-09 21:58:46 +00:00
|
|
|
m_arc = nullptr;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2015-02-09 08:57:40 +00:00
|
|
|
|
2014-11-09 21:58:46 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief ESEventAddArc::mouseMoveEvent
|
|
|
|
@param event
|
|
|
|
@return
|
|
|
|
*/
|
2014-11-09 21:58:46 +00:00
|
|
|
bool ESEventAddArc::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
|
|
|
|
updateHelpCross(event -> scenePos());
|
|
|
|
if (!m_arc) return false;
|
|
|
|
|
|
|
|
m_mouse_pos = m_scene -> snapToGrid(event -> scenePos());
|
|
|
|
updateArc();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief ESEventAddArc::mouseReleaseEvent
|
|
|
|
@param event
|
|
|
|
@return
|
|
|
|
*/
|
2014-11-09 21:58:46 +00:00
|
|
|
bool ESEventAddArc::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
|
|
|
|
if (event -> button() == Qt::RightButton) {
|
|
|
|
if (m_arc) {delete m_arc; m_arc = nullptr;}
|
|
|
|
else {m_running = false;}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief ESEventAddArc::keyPressEvent
|
|
|
|
@param event
|
|
|
|
@return
|
|
|
|
*/
|
2014-11-09 21:58:46 +00:00
|
|
|
bool ESEventAddArc::keyPressEvent(QKeyEvent *event) {
|
|
|
|
if (m_arc && event->key() == Qt::Key_Space) {
|
|
|
|
m_inverted = m_inverted ? false : true;
|
|
|
|
updateArc();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (ESEventInterface::keyPressEvent(event));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief ESEventAddArc::updateArc
|
|
|
|
Redraw the arc with curent value
|
|
|
|
*/
|
2015-02-09 08:57:40 +00:00
|
|
|
void ESEventAddArc::updateArc()
|
|
|
|
{
|
2014-11-09 21:58:46 +00:00
|
|
|
qreal width = (m_mouse_pos.x() - m_origin.x())*2;
|
|
|
|
if (width < 0) width *= -1;
|
|
|
|
qreal height = (m_mouse_pos.y() - m_origin.y())*2;
|
|
|
|
if (height < 0) height *= -1;
|
|
|
|
|
2015-02-09 08:57:40 +00:00
|
|
|
QPointF pos_ = m_arc -> mapFromScene(m_origin);
|
2014-11-09 21:58:46 +00:00
|
|
|
|
|
|
|
//Draw arc inverted
|
2015-02-09 08:57:40 +00:00
|
|
|
if (m_inverted)
|
|
|
|
{
|
2014-11-09 21:58:46 +00:00
|
|
|
//Adjust the start angle to be snapped at the origin point of draw
|
2015-02-09 08:57:40 +00:00
|
|
|
if (m_mouse_pos.y() > m_origin.y())
|
|
|
|
{
|
|
|
|
if (m_mouse_pos.x() > m_origin.x())
|
|
|
|
{
|
2014-11-09 21:58:46 +00:00
|
|
|
pos_.ry() -= height/2;
|
2015-02-09 08:57:40 +00:00
|
|
|
m_arc->setStartAngle(2880);
|
2014-11-09 21:58:46 +00:00
|
|
|
}
|
2015-02-09 08:57:40 +00:00
|
|
|
else
|
|
|
|
{
|
2014-11-09 21:58:46 +00:00
|
|
|
pos_.rx() -= width/2;
|
2015-02-09 08:57:40 +00:00
|
|
|
m_arc->setStartAngle(1440);
|
2014-11-09 21:58:46 +00:00
|
|
|
}
|
|
|
|
}
|
2015-02-09 08:57:40 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if (m_mouse_pos.x() > m_origin.x())
|
|
|
|
{
|
2014-11-09 21:58:46 +00:00
|
|
|
pos_.ry() -= height;
|
|
|
|
pos_.rx() -= width/2;
|
2015-02-09 08:57:40 +00:00
|
|
|
m_arc->setStartAngle(4320);
|
2014-11-09 21:58:46 +00:00
|
|
|
}
|
2015-02-09 08:57:40 +00:00
|
|
|
else
|
|
|
|
{
|
2014-11-09 21:58:46 +00:00
|
|
|
pos_.rx() -= width;
|
|
|
|
pos_.ry() -= height/2;
|
|
|
|
m_arc->setStartAngle(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-02-09 08:57:40 +00:00
|
|
|
//Draw arc non inverted
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//Adjust the start angle to be snapped at the origin point of draw
|
|
|
|
if (m_mouse_pos.y() > m_origin.y())
|
|
|
|
{
|
|
|
|
if (m_mouse_pos.x() > m_origin.x())
|
|
|
|
{
|
2014-11-09 21:58:46 +00:00
|
|
|
pos_.rx() -= width/2;
|
|
|
|
m_arc->setStartAngle(0);
|
|
|
|
}
|
2015-02-09 08:57:40 +00:00
|
|
|
else
|
|
|
|
{
|
2014-11-09 21:58:46 +00:00
|
|
|
pos_.rx() -= width;
|
|
|
|
pos_.ry() -= height/2;
|
2015-02-09 08:57:40 +00:00
|
|
|
m_arc->setStartAngle(4320);
|
2014-11-09 21:58:46 +00:00
|
|
|
}
|
|
|
|
}
|
2015-02-09 08:57:40 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if (m_mouse_pos.x() > m_origin.x())
|
|
|
|
{
|
2014-11-09 21:58:46 +00:00
|
|
|
pos_.ry() -= height/2;
|
2015-02-09 08:57:40 +00:00
|
|
|
m_arc->setStartAngle(1440);
|
2014-11-09 21:58:46 +00:00
|
|
|
}
|
2015-02-09 08:57:40 +00:00
|
|
|
else
|
|
|
|
{
|
2014-11-09 21:58:46 +00:00
|
|
|
pos_.rx() -= width/2;
|
|
|
|
pos_.ry() -= height;
|
2015-02-09 08:57:40 +00:00
|
|
|
m_arc->setStartAngle(2880);
|
2014-11-09 21:58:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
m_arc -> setRect(QRectF(pos_, QSizeF(width, height)));
|
|
|
|
}
|