2007-12-01 10:47:15 +00:00
|
|
|
/*
|
2016-05-13 17:40:36 +00:00
|
|
|
Copyright 2006-2016 The QElectroTech Team
|
2007-12-01 10:47: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/>.
|
|
|
|
*/
|
2007-07-10 22:54:22 +00:00
|
|
|
#include "partarc.h"
|
2015-07-20 21:06:00 +00:00
|
|
|
#include "QPropertyUndoCommand/qpropertyundocommand.h"
|
|
|
|
#include "elementscene.h"
|
|
|
|
|
2007-07-10 22:54:22 +00:00
|
|
|
|
2007-12-05 21:16:01 +00:00
|
|
|
/**
|
2015-02-09 08:57:40 +00:00
|
|
|
* @brief PartArc::PartArc
|
|
|
|
* Constructor
|
|
|
|
* @param editor : QETElementEditor of this part
|
|
|
|
* @param parent : parent item
|
|
|
|
*/
|
|
|
|
PartArc::PartArc(QETElementEditor *editor, QGraphicsItem *parent) :
|
2016-09-25 08:14:29 +00:00
|
|
|
AbstractPartEllipse(editor, parent)
|
2007-07-10 22:54:22 +00:00
|
|
|
{
|
2015-02-09 08:57:40 +00:00
|
|
|
m_start_angle = 0;
|
|
|
|
m_span_angle = -1440;
|
2007-07-10 22:54:22 +00:00
|
|
|
}
|
|
|
|
|
2015-02-09 08:57:40 +00:00
|
|
|
/**
|
|
|
|
* @brief PartArc::~PartArc
|
|
|
|
* Destructor
|
|
|
|
*/
|
2015-07-20 17:45:37 +00:00
|
|
|
PartArc::~PartArc() {
|
|
|
|
if(m_undo_command) delete m_undo_command;
|
|
|
|
}
|
2007-12-05 21:16:01 +00:00
|
|
|
|
|
|
|
/**
|
2015-02-09 08:57:40 +00:00
|
|
|
* @brief PartArc::paint
|
|
|
|
* Draw this arc
|
|
|
|
* @param painter
|
|
|
|
* @param options
|
|
|
|
* @param widget
|
|
|
|
*/
|
|
|
|
void PartArc::paint(QPainter *painter, const QStyleOptionGraphicsItem *options, QWidget *widget)
|
|
|
|
{
|
2009-11-22 16:12:22 +00:00
|
|
|
Q_UNUSED(widget);
|
2015-02-09 08:57:40 +00:00
|
|
|
|
2007-07-10 22:54:22 +00:00
|
|
|
applyStylesToQPainter(*painter);
|
2015-02-09 08:57:40 +00:00
|
|
|
|
|
|
|
//Always remove the brush
|
2007-07-10 22:54:22 +00:00
|
|
|
painter -> setBrush(Qt::NoBrush);
|
|
|
|
QPen t = painter -> pen();
|
2009-04-18 18:54:25 +00:00
|
|
|
t.setCosmetic(options && options -> levelOfDetail < 1.0);
|
|
|
|
painter -> setPen(t);
|
|
|
|
|
2015-02-09 08:57:40 +00:00
|
|
|
if (isSelected())
|
|
|
|
{
|
2015-08-03 17:26:57 +00:00
|
|
|
painter->save();
|
|
|
|
QPen pen(Qt::DotLine);
|
|
|
|
pen.setWidth(1);
|
|
|
|
pen.setCosmetic(true);
|
|
|
|
painter->setPen(pen);
|
2015-02-09 08:57:40 +00:00
|
|
|
//Draw the ellipse in black
|
2007-07-10 22:54:22 +00:00
|
|
|
painter -> drawEllipse(rect());
|
2015-08-03 17:26:57 +00:00
|
|
|
painter->restore();
|
2015-02-09 08:57:40 +00:00
|
|
|
|
|
|
|
//Draw the arc in red
|
2007-07-10 22:54:22 +00:00
|
|
|
t.setColor(Qt::red);
|
|
|
|
painter -> setPen(t);
|
|
|
|
}
|
2009-04-18 18:54:25 +00:00
|
|
|
|
2015-02-09 08:57:40 +00:00
|
|
|
painter -> drawArc(m_rect, m_start_angle, m_span_angle);
|
|
|
|
|
|
|
|
if (m_hovered)
|
|
|
|
drawShadowShape(painter);
|
|
|
|
|
|
|
|
if (isSelected())
|
2015-07-20 17:45:37 +00:00
|
|
|
{
|
2015-02-09 08:57:40 +00:00
|
|
|
drawCross(m_rect.center(), painter);
|
2016-09-25 08:14:29 +00:00
|
|
|
if (scene()->selectedItems().size() == 1) {
|
|
|
|
if (m_resize_mode == 3)
|
2016-09-26 00:46:02 +00:00
|
|
|
m_handler.drawHandler(painter, m_handler.pointsForArc(m_rect, m_start_angle /16, m_span_angle /16));
|
2016-09-25 08:14:29 +00:00
|
|
|
else
|
|
|
|
m_handler.drawHandler(painter, m_handler.pointsForRect(m_rect));
|
|
|
|
}
|
2015-07-20 17:45:37 +00:00
|
|
|
}
|
2007-07-10 22:54:22 +00:00
|
|
|
}
|
|
|
|
|
2007-12-05 21:16:01 +00:00
|
|
|
/**
|
2015-02-09 08:57:40 +00:00
|
|
|
* @brief PartArc::toXml
|
|
|
|
* Export this arc in xml
|
|
|
|
* @param xml_document : Xml document to use for create the xml element.
|
|
|
|
* @return : an xml element that describe this arc
|
|
|
|
*/
|
2007-07-10 22:54:22 +00:00
|
|
|
const QDomElement PartArc::toXml(QDomDocument &xml_document) const {
|
|
|
|
QDomElement xml_element = xml_document.createElement("arc");
|
|
|
|
QPointF top_left(sceneTopLeft());
|
2008-08-23 19:27:14 +00:00
|
|
|
xml_element.setAttribute("x", QString("%1").arg(top_left.x()));
|
|
|
|
xml_element.setAttribute("y", QString("%1").arg(top_left.y()));
|
|
|
|
xml_element.setAttribute("width", QString("%1").arg(rect().width()));
|
|
|
|
xml_element.setAttribute("height", QString("%1").arg(rect().height()));
|
2015-02-09 08:57:40 +00:00
|
|
|
//to maintain compatibility with the previous version, we write the angle in degrees.
|
|
|
|
xml_element.setAttribute("start", QString("%1").arg(m_start_angle / 16));
|
|
|
|
xml_element.setAttribute("angle", QString("%1").arg(m_span_angle / 16));
|
2007-07-10 22:54:22 +00:00
|
|
|
stylesToXml(xml_element);
|
|
|
|
return(xml_element);
|
|
|
|
}
|
|
|
|
|
2007-12-05 21:16:01 +00:00
|
|
|
/**
|
2015-02-09 08:57:40 +00:00
|
|
|
* @brief PartArc::fromXml
|
|
|
|
* Import the properties of this arc from a xml element.
|
|
|
|
* @param qde : Xml document to use.
|
|
|
|
*/
|
2007-07-10 22:54:22 +00:00
|
|
|
void PartArc::fromXml(const QDomElement &qde) {
|
|
|
|
stylesFromXml(qde);
|
2015-02-09 08:57:40 +00:00
|
|
|
m_rect = QRectF(mapFromScene(qde.attribute("x", "0").toDouble(),
|
|
|
|
qde.attribute("y", "0").toDouble()),
|
|
|
|
QSizeF(qde.attribute("width", "0").toDouble(),
|
|
|
|
qde.attribute("height", "0").toDouble()) );
|
2007-07-10 22:54:22 +00:00
|
|
|
|
2016-09-25 08:14:29 +00:00
|
|
|
m_start_angle = qde.attribute("start", "0").toDouble() * 16;
|
|
|
|
m_span_angle = qde.attribute("angle", "-1440").toDouble() * 16;
|
2014-05-29 13:46:04 +00:00
|
|
|
}
|
|
|
|
|
2015-07-21 12:29:43 +00:00
|
|
|
QRectF PartArc::boundingRect() const
|
|
|
|
{
|
|
|
|
QRectF r = AbstractPartEllipse::boundingRect();
|
|
|
|
|
|
|
|
foreach(QRectF rect, m_handler.handlerRect(m_handler.pointsForRect(m_rect)))
|
|
|
|
r |= rect;
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2014-05-29 13:46:04 +00:00
|
|
|
/**
|
2015-02-09 08:57:40 +00:00
|
|
|
* @brief PartArc::shape
|
|
|
|
* @return the shape of this item
|
2014-05-29 13:46:04 +00:00
|
|
|
*/
|
2015-02-09 08:57:40 +00:00
|
|
|
QPainterPath PartArc::shape() const
|
|
|
|
{
|
|
|
|
QPainterPath shape;
|
|
|
|
shape.arcMoveTo(m_rect, m_start_angle/16);
|
2016-09-26 00:46:02 +00:00
|
|
|
shape.arcTo(m_rect, m_start_angle /16, m_span_angle /16);
|
2013-02-08 22:05:12 +00:00
|
|
|
|
2015-02-09 08:57:40 +00:00
|
|
|
QPainterPathStroker pps;
|
2015-08-03 17:26:57 +00:00
|
|
|
pps.setWidth(m_hovered? penWeight()+SHADOWS_HEIGHT : penWeight());
|
2015-07-20 17:45:37 +00:00
|
|
|
shape = pps.createStroke(shape);
|
|
|
|
|
|
|
|
if (isSelected())
|
|
|
|
foreach(QRectF rect, m_handler.handlerRect(m_handler.pointsForRect(m_rect)))
|
|
|
|
shape.addRect(rect);
|
|
|
|
|
|
|
|
return shape;
|
|
|
|
}
|
|
|
|
|
2015-07-21 12:29:43 +00:00
|
|
|
QPainterPath PartArc::shadowShape() const
|
|
|
|
{
|
|
|
|
QPainterPath shape;
|
|
|
|
shape.arcMoveTo(m_rect, m_start_angle/16);
|
2016-09-26 00:46:02 +00:00
|
|
|
shape.arcTo(m_rect, m_start_angle /16, m_span_angle /16);
|
2015-07-21 12:29:43 +00:00
|
|
|
|
|
|
|
QPainterPathStroker pps;
|
|
|
|
pps.setWidth(penWeight());
|
|
|
|
|
|
|
|
return (pps.createStroke(shape));
|
|
|
|
}
|
|
|
|
|
2015-08-03 17:26:57 +00:00
|
|
|
void PartArc::hoverMoveEvent(QGraphicsSceneHoverEvent *event)
|
|
|
|
{
|
|
|
|
if (!isSelected())
|
|
|
|
{
|
|
|
|
CustomElementGraphicPart::hoverMoveEvent(event);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-09-25 08:14:29 +00:00
|
|
|
if (m_resize_mode == 1 || m_resize_mode == 2) {
|
|
|
|
int handler = m_handler.pointIsHoverHandler(event->pos(), m_handler.pointsForRect(m_rect));
|
2015-08-03 17:26:57 +00:00
|
|
|
|
2016-09-25 08:14:29 +00:00
|
|
|
if (handler >= 0)
|
|
|
|
{
|
|
|
|
if (handler == 0 || handler == 2 || handler == 5 || handler == 7)
|
|
|
|
setCursor(Qt::SizeAllCursor);
|
|
|
|
else if (handler == 1 || handler == 6)
|
|
|
|
setCursor(Qt::SizeVerCursor);
|
|
|
|
else if (handler == 3 || handler == 4)
|
|
|
|
setCursor(Qt::SizeHorCursor);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (m_resize_mode == 3) {
|
2016-09-25 16:57:12 +00:00
|
|
|
if (m_handler.pointIsHoverHandler(event->pos(), m_handler.pointsForArc(m_rect, m_start_angle /16, m_span_angle /16)) >= 0) {
|
2015-08-03 17:26:57 +00:00
|
|
|
setCursor(Qt::SizeAllCursor);
|
2016-09-25 10:44:38 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-08-03 17:26:57 +00:00
|
|
|
}
|
2016-09-25 08:14:29 +00:00
|
|
|
|
|
|
|
CustomElementGraphicPart::hoverMoveEvent(event);
|
2015-08-03 17:26:57 +00:00
|
|
|
}
|
|
|
|
|
2015-07-20 17:45:37 +00:00
|
|
|
/**
|
|
|
|
* @brief PartArc::mousePressEvent
|
|
|
|
* Handle mouse press event
|
|
|
|
* @param event
|
|
|
|
*/
|
|
|
|
void PartArc::mousePressEvent(QGraphicsSceneMouseEvent *event)
|
|
|
|
{
|
2015-08-03 17:26:57 +00:00
|
|
|
if (event->button() == Qt::LeftButton)
|
2015-07-20 17:45:37 +00:00
|
|
|
{
|
2015-08-03 17:26:57 +00:00
|
|
|
setCursor(Qt::ClosedHandCursor);
|
|
|
|
if (isSelected())
|
2015-07-20 21:06:00 +00:00
|
|
|
{
|
2016-09-25 08:14:29 +00:00
|
|
|
//resize rect
|
|
|
|
if (m_resize_mode == 1 || m_resize_mode == 2) {
|
|
|
|
m_handler_index = m_handler.pointIsHoverHandler(event->pos(), m_handler.pointsForRect(m_rect));
|
|
|
|
|
|
|
|
if(m_handler_index >= 0 && m_handler_index <= 7) //User click on an handler
|
|
|
|
{
|
|
|
|
m_undo_command = new QPropertyUndoCommand(this, "rect", QVariant(m_rect));
|
|
|
|
m_undo_command->setText(tr("Modifier un arc"));
|
|
|
|
m_undo_command->enableAnimation();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//resize angle
|
|
|
|
if (m_resize_mode == 3) {
|
2016-09-26 00:46:02 +00:00
|
|
|
m_handler_index = m_handler.pointIsHoverHandler(event->pos(), m_handler.pointsForArc(m_rect, m_start_angle /16, m_span_angle /16));
|
2016-09-25 08:14:29 +00:00
|
|
|
if (m_handler_index == 0) {
|
2016-09-26 00:46:02 +00:00
|
|
|
m_span_point = m_handler.pointsForArc(m_rect, m_start_angle /16, m_span_angle /16).at(1);
|
2016-09-25 08:14:29 +00:00
|
|
|
|
|
|
|
m_undo_command = new QPropertyUndoCommand(this, "startAngle", QVariant(m_start_angle));
|
|
|
|
m_undo_command->setText(tr("Modifier un arc"));
|
|
|
|
m_undo_command->enableAnimation();
|
|
|
|
|
|
|
|
m_undo_command2 = new QPropertyUndoCommand(this, "spanAngle", QVariant(m_span_angle), m_undo_command);
|
|
|
|
m_undo_command2->setText(tr("Modifier un arc"));
|
|
|
|
m_undo_command2->enableAnimation();
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if (m_handler_index == 1) {
|
|
|
|
m_undo_command = new QPropertyUndoCommand(this, "spanAngle", QVariant(m_span_angle));
|
|
|
|
m_undo_command->setText(tr("Modifier un arc"));
|
|
|
|
m_undo_command->enableAnimation();
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2015-08-03 17:26:57 +00:00
|
|
|
}
|
2016-09-25 08:14:29 +00:00
|
|
|
|
2015-07-20 21:06:00 +00:00
|
|
|
}
|
2015-07-20 17:45:37 +00:00
|
|
|
}
|
2015-08-03 17:26:57 +00:00
|
|
|
|
|
|
|
CustomElementGraphicPart::mousePressEvent(event);
|
2015-07-20 17:45:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief PartArc::mouseMoveEvent
|
|
|
|
* Handle mouse move event
|
|
|
|
* @param event
|
|
|
|
*/
|
|
|
|
void PartArc::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
|
|
|
|
{
|
2016-09-25 08:14:29 +00:00
|
|
|
if (m_resize_mode == 1 || m_resize_mode == 2) {
|
|
|
|
if (m_handler_index >= 0 && m_handler_index <= 7) {
|
|
|
|
QPointF pos_ = event->modifiers() == Qt::ControlModifier ? event->pos() : mapFromScene(elementScene()->snapToGrid(event->scenePos()));
|
|
|
|
prepareGeometryChange();
|
|
|
|
|
|
|
|
if (m_resize_mode == 1)
|
|
|
|
setRect(m_handler.rectForPosAtIndex(m_rect, pos_, m_handler_index));
|
|
|
|
else
|
|
|
|
setRect(m_handler.mirrorRectForPosAtIndex(m_rect, pos_, m_handler_index));
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (m_resize_mode == 3) {
|
|
|
|
if (m_handler_index == 0 || m_handler_index == 1) {
|
|
|
|
QLineF line(m_rect.center(), event->pos());
|
|
|
|
prepareGeometryChange();
|
|
|
|
|
|
|
|
if (m_handler_index == 0) {
|
|
|
|
setStartAngle(line.angle()*16);
|
|
|
|
setSpanAngle(line.angleTo(QLineF(m_rect.center(), m_span_point))*16);
|
|
|
|
}
|
|
|
|
else if (m_handler_index == 1) {
|
|
|
|
QLineF line2(m_rect.center(), m_handler.pointsForArc(m_rect, m_start_angle/16, m_span_angle/16).at(0));
|
|
|
|
setSpanAngle (line2.angleTo(line)*16);
|
|
|
|
}
|
2016-07-19 17:12:30 +00:00
|
|
|
|
2016-09-25 08:14:29 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-07-20 17:45:37 +00:00
|
|
|
}
|
2016-09-25 08:14:29 +00:00
|
|
|
|
|
|
|
CustomElementGraphicPart::mouseMoveEvent(event);
|
2015-07-20 17:45:37 +00:00
|
|
|
}
|
2013-02-08 22:05:12 +00:00
|
|
|
|
2015-07-20 17:45:37 +00:00
|
|
|
/**
|
|
|
|
* @brief PartArc::mouseReleaseEvent
|
|
|
|
* Handle mouse release event
|
|
|
|
* @param event
|
|
|
|
*/
|
|
|
|
void PartArc::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
|
|
|
|
{
|
2016-07-19 17:12:30 +00:00
|
|
|
if (event->button() == Qt::LeftButton) {
|
2015-08-03 17:26:57 +00:00
|
|
|
setCursor(Qt::OpenHandCursor);
|
2016-07-19 17:12:30 +00:00
|
|
|
if (event->buttonDownPos(Qt::LeftButton) == event->pos())
|
|
|
|
switchResizeMode();
|
|
|
|
}
|
2015-08-03 17:26:57 +00:00
|
|
|
|
2016-09-25 08:14:29 +00:00
|
|
|
if (m_resize_mode == 1 || m_resize_mode == 2) {
|
|
|
|
if (m_handler_index >= 0 && m_handler_index <= 7) {
|
|
|
|
if (!m_rect.isValid())
|
|
|
|
m_rect = m_rect.normalized();
|
2015-07-20 17:45:37 +00:00
|
|
|
|
2016-09-25 08:14:29 +00:00
|
|
|
m_undo_command->setNewValue(QVariant(m_rect));
|
|
|
|
elementScene()->undoStack().push(m_undo_command);
|
|
|
|
m_undo_command = nullptr;
|
|
|
|
m_handler_index = -1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (m_resize_mode == 3) {
|
|
|
|
if (m_handler_index == 0) {
|
|
|
|
m_undo_command->setNewValue(QVariant(m_start_angle));
|
|
|
|
m_undo_command2->setNewValue(QVariant(m_span_angle));
|
|
|
|
elementScene()->undoStack().push(m_undo_command);
|
|
|
|
m_undo_command = nullptr;
|
|
|
|
m_undo_command2 = nullptr;
|
|
|
|
m_handler_index = -1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if (m_handler_index == 1) {
|
|
|
|
m_undo_command->setNewValue(QVariant(m_span_angle));
|
|
|
|
elementScene()->undoStack().push(m_undo_command);
|
|
|
|
m_undo_command = nullptr;
|
|
|
|
m_handler_index = -1;
|
|
|
|
return;
|
|
|
|
}
|
2015-07-20 17:45:37 +00:00
|
|
|
}
|
2016-09-25 08:14:29 +00:00
|
|
|
|
|
|
|
CustomElementGraphicPart::mouseReleaseEvent(event);
|
2008-01-07 19:40:08 +00:00
|
|
|
}
|
2016-07-19 17:12:30 +00:00
|
|
|
|
|
|
|
void PartArc::switchResizeMode()
|
|
|
|
{
|
|
|
|
if (m_resize_mode == 1) {
|
|
|
|
m_resize_mode = 2;
|
|
|
|
m_handler.setOuterColor(Qt::darkGreen);
|
|
|
|
}
|
2016-09-25 08:14:29 +00:00
|
|
|
else if (m_resize_mode == 2 ) {
|
|
|
|
m_resize_mode = 3;
|
|
|
|
m_handler.setOuterColor(Qt::magenta);
|
|
|
|
}
|
2016-07-19 17:12:30 +00:00
|
|
|
else {
|
|
|
|
m_resize_mode = 1;
|
|
|
|
m_handler.setOuterColor(Qt::blue);
|
|
|
|
}
|
|
|
|
update();
|
|
|
|
}
|