2007-12-01 10:47:15 +00:00
|
|
|
/*
|
2025-01-04 13:37:40 +01:00
|
|
|
Copyright 2006-2025 The QElectroTech Team
|
2020-10-17 20:25:30 +02:00
|
|
|
This file is part of QElectroTech.
|
2020-10-03 15:48:40 +02:00
|
|
|
|
2020-10-17 20:25:30 +02:00
|
|
|
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.
|
2020-10-03 15:48:40 +02:00
|
|
|
|
2020-10-17 20:25:30 +02:00
|
|
|
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.
|
2020-10-03 15:48:40 +02:00
|
|
|
|
2020-10-17 20:25:30 +02:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with QElectroTech. If not, see <http://www.gnu.org/licenses/>.
|
2007-12-01 10:47:15 +00:00
|
|
|
*/
|
2007-06-30 17:41:07 +00:00
|
|
|
#include "partline.h"
|
|
|
|
|
2020-12-10 18:44:03 +01:00
|
|
|
#include "../../QPropertyUndoCommand/qpropertyundocommand.h"
|
|
|
|
#include "../../QetGraphicsItemModeler/qetgraphicshandleritem.h"
|
|
|
|
#include "../elementscene.h"
|
2007-06-30 17:41:07 +00:00
|
|
|
|
2020-12-09 15:28:43 +01:00
|
|
|
#include <cmath>
|
2015-02-09 08:57:40 +00:00
|
|
|
|
2007-12-05 21:16:01 +00:00
|
|
|
/**
|
2020-10-17 20:25:30 +02:00
|
|
|
@brief PartLine::PartLine
|
|
|
|
Constructor
|
|
|
|
@param editor : QETElementEditor of this part
|
|
|
|
@param parent : parent item
|
2020-08-16 11:19:36 +02:00
|
|
|
*/
|
2015-02-09 08:57:40 +00:00
|
|
|
PartLine::PartLine(QETElementEditor *editor, QGraphicsItem *parent) :
|
2021-03-11 19:52:50 +01:00
|
|
|
CustomElementGraphicPart(editor, parent),
|
|
|
|
first_end(Qet::None),
|
|
|
|
first_length(1.5),
|
|
|
|
second_end(Qet::None),
|
|
|
|
second_length(1.5),
|
|
|
|
m_undo_command(nullptr)
|
|
|
|
{}
|
2007-06-30 17:41:07 +00:00
|
|
|
|
2007-12-05 21:16:01 +00:00
|
|
|
/// Destructeur
|
2017-08-02 15:26:14 +00:00
|
|
|
PartLine::~PartLine()
|
|
|
|
{
|
2020-10-17 20:25:30 +02:00
|
|
|
if(m_undo_command)
|
|
|
|
delete m_undo_command;
|
2020-10-03 15:48:40 +02:00
|
|
|
|
2020-10-17 20:25:30 +02:00
|
|
|
removeHandler();
|
2015-07-20 17:45:37 +00:00
|
|
|
}
|
2007-12-05 21:16:01 +00:00
|
|
|
|
2009-04-03 19:30:25 +00:00
|
|
|
/**
|
2020-10-17 20:25:30 +02:00
|
|
|
@brief PartLine::requiredLengthForEndType
|
|
|
|
@param end_type
|
|
|
|
@return the number of "length" needed to draw a extremity of type Qet::EndType.
|
2020-08-16 11:19:36 +02:00
|
|
|
*/
|
2015-02-09 08:57:40 +00:00
|
|
|
uint PartLine::requiredLengthForEndType(const Qet::EndType &end_type)
|
|
|
|
{
|
2020-10-17 20:25:30 +02:00
|
|
|
uint length_count_required = 0;
|
2015-02-09 08:57:40 +00:00
|
|
|
|
2020-10-17 20:25:30 +02:00
|
|
|
if (end_type == Qet::Circle || end_type == Qet::Diamond)
|
|
|
|
length_count_required = 2;
|
|
|
|
else if (end_type == Qet::Simple || end_type == Qet::Triangle)
|
|
|
|
length_count_required = 1;
|
2015-02-09 08:57:40 +00:00
|
|
|
|
2020-10-17 20:25:30 +02:00
|
|
|
return(length_count_required);
|
2009-04-03 19:30:25 +00:00
|
|
|
}
|
|
|
|
|
2007-12-05 21:16:01 +00:00
|
|
|
/**
|
2020-10-17 20:25:30 +02:00
|
|
|
@brief PartLine::paint
|
|
|
|
Draw this line
|
|
|
|
@param painter
|
|
|
|
@param options
|
|
|
|
@param widget
|
2020-08-16 11:19:36 +02:00
|
|
|
*/
|
2015-02-09 08:57:40 +00:00
|
|
|
void PartLine::paint(QPainter *painter, const QStyleOptionGraphicsItem *options, QWidget *widget)
|
|
|
|
{
|
2020-10-17 20:25:30 +02:00
|
|
|
Q_UNUSED(widget)
|
|
|
|
if (isUseless()) return;
|
2015-02-09 08:57:40 +00:00
|
|
|
|
2020-10-17 20:25:30 +02:00
|
|
|
painter->save();
|
|
|
|
applyStylesToQPainter(*painter);
|
|
|
|
QPen t = painter -> pen();
|
|
|
|
t.setJoinStyle(Qt::MiterJoin);
|
2015-02-09 08:57:40 +00:00
|
|
|
|
2020-10-17 20:25:30 +02:00
|
|
|
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) // ### Qt 6: remove
|
|
|
|
t.setCosmetic(options && options -> levelOfDetail < 1.0);
|
2020-10-03 15:48:40 +02:00
|
|
|
#else
|
|
|
|
#if TODO_LIST
|
|
|
|
#pragma message("@TODO remove code for QT 6 or later")
|
|
|
|
#endif
|
2020-10-17 20:25:30 +02:00
|
|
|
t.setCosmetic(options && options -> levelOfDetailFromTransform(painter->worldTransform()) < 1.0);
|
2020-10-03 15:48:40 +02:00
|
|
|
#endif
|
2020-10-17 20:25:30 +02:00
|
|
|
if (isSelected()) t.setColor(Qt::red);
|
2015-02-09 08:57:40 +00:00
|
|
|
|
2020-10-17 20:25:30 +02:00
|
|
|
painter -> setPen(t);
|
2020-10-03 15:48:40 +02:00
|
|
|
|
2020-10-17 20:25:30 +02:00
|
|
|
if (first_end || second_end)
|
|
|
|
painter -> drawPath(path());
|
|
|
|
else
|
|
|
|
painter -> drawLine(m_line);
|
2015-02-09 08:57:40 +00:00
|
|
|
|
2020-10-17 20:25:30 +02:00
|
|
|
if (m_hovered)
|
|
|
|
drawShadowShape(painter);
|
2015-07-20 17:45:37 +00:00
|
|
|
|
2020-10-17 20:25:30 +02:00
|
|
|
painter->restore();
|
2007-06-30 17:41:07 +00:00
|
|
|
}
|
|
|
|
|
2007-12-05 21:16:01 +00:00
|
|
|
/**
|
2021-03-11 19:52:50 +01:00
|
|
|
@brief PartLine::toXml
|
2020-10-17 20:25:30 +02:00
|
|
|
Export this line in xml
|
|
|
|
@param xml_document : Xml document to use for create the xml element.
|
|
|
|
@return an xml element that describe this line
|
2020-08-16 11:19:36 +02:00
|
|
|
*/
|
2021-03-11 19:52:50 +01:00
|
|
|
const QDomElement PartLine::toXml(QDomDocument &xml_document) const
|
2015-02-09 08:57:40 +00:00
|
|
|
{
|
2021-03-11 19:52:50 +01:00
|
|
|
QPointF p1(sceneP1());
|
|
|
|
QPointF p2(sceneP2());
|
|
|
|
|
2025-02-09 12:33:24 +01:00
|
|
|
p1.setX((qRound(p1.x() * 100.0)) / 100.0);
|
|
|
|
p1.setY((qRound(p1.y() * 100.0)) / 100.0);
|
|
|
|
p2.setX((qRound(p2.x() * 100.0)) / 100.0);
|
|
|
|
p2.setY((qRound(p2.y() * 100.0)) / 100.0);
|
|
|
|
qreal firstLength = ((qRound(first_length * 100.0)) / 100.0);
|
|
|
|
qreal secondLength = ((qRound(second_length * 100.0)) / 100.0);
|
|
|
|
|
2021-03-11 19:52:50 +01:00
|
|
|
QDomElement xml_element = xml_document.createElement("line");
|
|
|
|
xml_element.setAttribute("x1", QString("%1").arg(p1.x()));
|
|
|
|
xml_element.setAttribute("y1", QString("%1").arg(p1.y()));
|
|
|
|
xml_element.setAttribute("x2", QString("%1").arg(p2.x()));
|
|
|
|
xml_element.setAttribute("y2", QString("%1").arg(p2.y()));
|
|
|
|
xml_element.setAttribute("end1", Qet::endTypeToString(first_end));
|
2025-02-09 12:33:24 +01:00
|
|
|
xml_element.setAttribute("length1", QString("%1").arg(firstLength));
|
2021-03-11 19:52:50 +01:00
|
|
|
xml_element.setAttribute("end2", Qet::endTypeToString(second_end));
|
2025-02-09 12:33:24 +01:00
|
|
|
xml_element.setAttribute("length2", QString("%1").arg(secondLength));
|
2021-03-11 19:52:50 +01:00
|
|
|
|
|
|
|
stylesToXml(xml_element);
|
|
|
|
return(xml_element);
|
2007-06-30 17:41:07 +00:00
|
|
|
}
|
|
|
|
|
2007-12-05 21:16:01 +00:00
|
|
|
/**
|
2020-10-17 20:25:30 +02:00
|
|
|
@brief PartLine::fromXml
|
|
|
|
Import the properties of this line from a xml element.
|
|
|
|
@param qde : Xml document to use
|
2020-08-16 11:19:36 +02:00
|
|
|
*/
|
2021-03-11 19:52:50 +01:00
|
|
|
void PartLine::fromXml(const QDomElement &qde) {
|
2020-10-17 20:25:30 +02:00
|
|
|
stylesFromXml(qde);
|
2021-03-11 19:52:50 +01:00
|
|
|
m_line = QLineF(mapFromScene(qde.attribute("x1", "0").toDouble(),
|
|
|
|
qde.attribute("y1", "0").toDouble()),
|
|
|
|
mapFromScene(qde.attribute("x2", "0").toDouble(),
|
|
|
|
qde.attribute("y2", "0").toDouble()));
|
|
|
|
|
|
|
|
first_end = Qet::endTypeFromString(qde.attribute("end1"));
|
|
|
|
first_length = qde.attribute("length1", "1.5").toDouble();
|
|
|
|
second_end = Qet::endTypeFromString(qde.attribute("end2"));
|
|
|
|
second_length = qde.attribute("length2", "1.5").toDouble();
|
2007-08-25 03:43:05 +00:00
|
|
|
}
|
|
|
|
|
2015-07-20 17:45:37 +00:00
|
|
|
/**
|
2020-10-17 20:25:30 +02:00
|
|
|
@brief PartLine::itemChange
|
|
|
|
@param change
|
|
|
|
@param value
|
|
|
|
@return
|
2020-08-16 11:19:36 +02:00
|
|
|
*/
|
2017-08-02 15:26:14 +00:00
|
|
|
QVariant PartLine::itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant &value)
|
2015-07-20 17:45:37 +00:00
|
|
|
{
|
2022-08-13 12:40:59 +02:00
|
|
|
if (change == ItemPositionHasChanged)
|
2020-10-17 20:25:30 +02:00
|
|
|
{
|
|
|
|
adjusteHandlerPos();
|
|
|
|
}
|
|
|
|
else if (change == ItemSceneChange)
|
|
|
|
{
|
|
|
|
setSelected(false); //This is item removed from scene, then we deselect this, and so, the handlers is also removed.
|
|
|
|
}
|
|
|
|
|
|
|
|
return QGraphicsItem::itemChange(change, value);
|
2017-08-02 15:26:14 +00:00
|
|
|
}
|
2015-08-03 17:26:57 +00:00
|
|
|
|
2017-08-02 15:26:14 +00:00
|
|
|
/**
|
2020-10-17 20:25:30 +02:00
|
|
|
@brief PartLine::sceneEventFilter
|
|
|
|
@param watched
|
|
|
|
@param event
|
|
|
|
@return
|
2020-08-16 11:19:36 +02:00
|
|
|
*/
|
2017-08-02 15:26:14 +00:00
|
|
|
bool PartLine::sceneEventFilter(QGraphicsItem *watched, QEvent *event)
|
|
|
|
{
|
2020-10-17 20:25:30 +02:00
|
|
|
//Watched must be an handler
|
|
|
|
if(watched->type() == QetGraphicsHandlerItem::Type)
|
|
|
|
{
|
|
|
|
QetGraphicsHandlerItem *qghi = qgraphicsitem_cast<QetGraphicsHandlerItem *>(watched);
|
|
|
|
|
|
|
|
if(m_handler_vector.contains(qghi)) //Handler must be in m_vector_index, then we can start resize
|
|
|
|
{
|
|
|
|
m_vector_index = m_handler_vector.indexOf(qghi);
|
|
|
|
if (m_vector_index != -1)
|
|
|
|
{
|
|
|
|
if(event->type() == QEvent::GraphicsSceneMousePress) //Click
|
|
|
|
{
|
|
|
|
handlerMousePressEvent(qghi, static_cast<QGraphicsSceneMouseEvent *>(event));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if(event->type() == QEvent::GraphicsSceneMouseMove) //Move
|
|
|
|
{
|
|
|
|
handlerMouseMoveEvent(qghi, static_cast<QGraphicsSceneMouseEvent *>(event));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if (event->type() == QEvent::GraphicsSceneMouseRelease) //Release
|
|
|
|
{
|
|
|
|
handlerMouseReleaseEvent(qghi, static_cast<QGraphicsSceneMouseEvent *>(event));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2015-07-20 17:45:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-10-17 20:25:30 +02:00
|
|
|
@brief PartLine::adjusteHandlerPos
|
|
|
|
Adjust the position of the handler item
|
2020-08-16 11:19:36 +02:00
|
|
|
*/
|
2017-08-02 15:26:14 +00:00
|
|
|
void PartLine::adjusteHandlerPos()
|
2015-07-20 17:45:37 +00:00
|
|
|
{
|
2020-10-17 20:25:30 +02:00
|
|
|
if(m_handler_vector.isEmpty())
|
|
|
|
return;
|
2020-10-03 15:48:40 +02:00
|
|
|
|
2020-10-17 20:25:30 +02:00
|
|
|
QVector<QPointF> points_vector;
|
|
|
|
points_vector << m_line.p1() << m_line.p2();
|
2020-10-03 15:48:40 +02:00
|
|
|
|
2020-10-17 20:25:30 +02:00
|
|
|
if (m_handler_vector.size() == points_vector.size())
|
|
|
|
{
|
|
|
|
points_vector = mapToScene(points_vector);
|
|
|
|
for (int i = 0 ; i < points_vector.size() ; ++i)
|
|
|
|
m_handler_vector.at(i)->setPos(points_vector.at(i));
|
|
|
|
}
|
2017-08-02 15:26:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-10-17 20:25:30 +02:00
|
|
|
@brief PartLine::handlerMousePressEvent
|
|
|
|
@param qghi
|
|
|
|
@param event
|
2020-08-16 11:19:36 +02:00
|
|
|
*/
|
2017-08-02 15:26:14 +00:00
|
|
|
void PartLine::handlerMousePressEvent(QetGraphicsHandlerItem *qghi, QGraphicsSceneMouseEvent *event)
|
|
|
|
{
|
2020-10-17 20:25:30 +02:00
|
|
|
Q_UNUSED(qghi)
|
|
|
|
Q_UNUSED(event)
|
2020-10-03 15:48:40 +02:00
|
|
|
|
2020-10-17 20:25:30 +02:00
|
|
|
m_undo_command = new QPropertyUndoCommand(this, "line", QVariant(m_line));
|
|
|
|
m_undo_command->setText(tr("Modifier une ligne"));
|
|
|
|
m_undo_command->enableAnimation();
|
|
|
|
return;
|
2017-08-02 15:26:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-10-17 20:25:30 +02:00
|
|
|
@brief PartLine::handlerMouseMoveEvent
|
|
|
|
@param qghi
|
|
|
|
@param event
|
2020-08-16 11:19:36 +02:00
|
|
|
*/
|
2017-08-02 15:26:14 +00:00
|
|
|
void PartLine::handlerMouseMoveEvent(QetGraphicsHandlerItem *qghi, QGraphicsSceneMouseEvent *event)
|
|
|
|
{
|
2020-10-17 20:25:30 +02:00
|
|
|
Q_UNUSED(qghi)
|
2020-10-03 15:48:40 +02:00
|
|
|
|
2020-10-17 20:25:30 +02:00
|
|
|
QPointF new_pos = event->scenePos();
|
|
|
|
if (event->modifiers() != Qt::ControlModifier)
|
|
|
|
new_pos = elementScene()->snapToGrid(event->scenePos());
|
|
|
|
new_pos = mapFromScene(new_pos);
|
2020-10-03 15:48:40 +02:00
|
|
|
|
2020-10-17 20:25:30 +02:00
|
|
|
prepareGeometryChange();
|
|
|
|
if (m_vector_index == 0)
|
|
|
|
m_line.setP1(new_pos);
|
|
|
|
else
|
|
|
|
m_line.setP2(new_pos);
|
2020-09-13 15:11:45 +02:00
|
|
|
|
2020-10-17 20:25:30 +02:00
|
|
|
emit lineChanged();
|
2020-10-03 15:48:40 +02:00
|
|
|
|
2020-10-17 20:25:30 +02:00
|
|
|
adjusteHandlerPos();
|
2015-07-20 17:45:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-10-17 20:25:30 +02:00
|
|
|
@brief PartLine::handlerMouseReleaseEvent
|
|
|
|
@param qghi
|
|
|
|
@param event
|
2020-08-16 11:19:36 +02:00
|
|
|
*/
|
2017-08-02 15:26:14 +00:00
|
|
|
void PartLine::handlerMouseReleaseEvent(QetGraphicsHandlerItem *qghi, QGraphicsSceneMouseEvent *event)
|
|
|
|
{
|
2020-10-17 20:25:30 +02:00
|
|
|
Q_UNUSED(qghi)
|
|
|
|
Q_UNUSED(event)
|
2020-10-03 15:48:40 +02:00
|
|
|
|
2020-10-17 20:25:30 +02:00
|
|
|
m_undo_command->setNewValue(QVariant(m_line));
|
|
|
|
elementScene()->undoStack().push(m_undo_command);
|
|
|
|
m_undo_command = nullptr;
|
|
|
|
m_vector_index = -1;
|
2017-08-02 15:26:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-10-17 20:25:30 +02:00
|
|
|
@brief PartLine::addHandler
|
|
|
|
Add handlers for this item
|
2020-08-16 11:19:36 +02:00
|
|
|
*/
|
2017-08-02 15:26:14 +00:00
|
|
|
void PartLine::addHandler()
|
|
|
|
{
|
2020-10-17 20:25:30 +02:00
|
|
|
if (m_handler_vector.isEmpty() && scene())
|
|
|
|
{
|
|
|
|
QVector<QPointF> points_vector;
|
|
|
|
points_vector << m_line.p1() << m_line.p2();
|
2020-10-03 15:48:40 +02:00
|
|
|
|
2020-10-17 20:25:30 +02:00
|
|
|
m_handler_vector = QetGraphicsHandlerItem::handlerForPoint(mapToScene(points_vector));
|
2020-10-03 15:48:40 +02:00
|
|
|
|
2025-02-14 16:17:58 +01:00
|
|
|
for(QetGraphicsHandlerItem *handler : m_handler_vector)
|
2020-10-17 20:25:30 +02:00
|
|
|
{
|
|
|
|
handler->setColor(Qt::blue);
|
|
|
|
scene()->addItem(handler);
|
|
|
|
handler->installSceneEventFilter(this);
|
|
|
|
handler->setZValue(this->zValue()+1);
|
|
|
|
}
|
|
|
|
}
|
2017-08-02 15:26:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-10-17 20:25:30 +02:00
|
|
|
@brief PartLine::removeHandler
|
|
|
|
Remove the handlers of this item
|
2020-08-16 11:19:36 +02:00
|
|
|
*/
|
2017-08-02 15:26:14 +00:00
|
|
|
void PartLine::removeHandler()
|
|
|
|
{
|
2020-10-17 20:25:30 +02:00
|
|
|
if (!m_handler_vector.isEmpty())
|
|
|
|
{
|
|
|
|
qDeleteAll(m_handler_vector);
|
|
|
|
m_handler_vector.clear();
|
|
|
|
}
|
2015-07-20 17:45:37 +00:00
|
|
|
}
|
|
|
|
|
2007-12-05 21:16:01 +00:00
|
|
|
/**
|
2020-10-17 20:25:30 +02:00
|
|
|
@brief PartLine::sceneP1
|
|
|
|
@return the point p1 in scene coordinate
|
2020-08-16 11:19:36 +02:00
|
|
|
*/
|
2020-09-07 22:03:40 +02:00
|
|
|
QPointF PartLine::sceneP1() const
|
|
|
|
{
|
2020-10-17 20:25:30 +02:00
|
|
|
return(mapToScene(m_line.p1()));
|
2007-06-30 17:41:07 +00:00
|
|
|
}
|
|
|
|
|
2007-12-05 21:16:01 +00:00
|
|
|
/**
|
2020-10-17 20:25:30 +02:00
|
|
|
@brief PartLine::sceneP2
|
|
|
|
@return the point p2 in scen coordinate
|
2020-08-16 11:19:36 +02:00
|
|
|
*/
|
2020-09-07 22:03:40 +02:00
|
|
|
QPointF PartLine::sceneP2() const
|
|
|
|
{
|
2020-10-17 20:25:30 +02:00
|
|
|
return(mapToScene(m_line.p2()));
|
2007-06-30 17:41:07 +00:00
|
|
|
}
|
|
|
|
|
2007-12-05 21:16:01 +00:00
|
|
|
/**
|
2020-10-17 20:25:30 +02:00
|
|
|
@brief PartLine::shape
|
|
|
|
@return the shape of this item
|
2020-08-16 11:19:36 +02:00
|
|
|
*/
|
2015-02-09 08:57:40 +00:00
|
|
|
QPainterPath PartLine::shape() const
|
|
|
|
{
|
2020-10-17 20:25:30 +02:00
|
|
|
QPainterPath shape;
|
2015-02-09 08:57:40 +00:00
|
|
|
|
2020-10-17 20:25:30 +02:00
|
|
|
//We calcul path only if there is an end type
|
|
|
|
//Else we just draw a line
|
|
|
|
if (first_end || second_end)
|
|
|
|
shape.addPath(path());
|
|
|
|
else
|
|
|
|
{
|
|
|
|
shape.moveTo(m_line.p1());
|
|
|
|
shape.lineTo(m_line.p2());
|
|
|
|
}
|
2015-02-09 08:57:40 +00:00
|
|
|
|
2020-10-17 20:25:30 +02:00
|
|
|
QPainterPathStroker pps;
|
|
|
|
pps.setWidth(m_hovered? penWeight()+SHADOWS_HEIGHT : penWeight());
|
|
|
|
shape = pps.createStroke(shape);
|
2015-07-21 12:29:43 +00:00
|
|
|
|
2020-10-17 20:25:30 +02:00
|
|
|
return shape;
|
2015-07-21 12:29:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QPainterPath PartLine::shadowShape() const
|
|
|
|
{
|
2020-10-17 20:25:30 +02:00
|
|
|
QPainterPath shape;
|
2015-07-21 12:29:43 +00:00
|
|
|
|
2020-10-17 20:25:30 +02:00
|
|
|
//We calcul path only if there is an end type
|
|
|
|
//Else we just draw a line
|
|
|
|
if (first_end || second_end)
|
|
|
|
shape.addPath(path());
|
|
|
|
else
|
|
|
|
{
|
|
|
|
shape.moveTo(m_line.p1());
|
|
|
|
shape.lineTo(m_line.p2());
|
|
|
|
}
|
2015-07-21 12:29:43 +00:00
|
|
|
|
2020-10-17 20:25:30 +02:00
|
|
|
QPainterPathStroker pps;
|
|
|
|
pps.setWidth(penWeight());
|
2015-02-09 08:57:40 +00:00
|
|
|
|
2020-10-17 20:25:30 +02:00
|
|
|
return (pps.createStroke(shape));
|
2007-06-30 17:41:07 +00:00
|
|
|
}
|
|
|
|
|
2009-04-03 19:30:25 +00:00
|
|
|
/**
|
2020-10-17 20:25:30 +02:00
|
|
|
@brief PartLine::firstEndCircleRect
|
|
|
|
@return the rectangle bordering the entirety of the first extremity
|
2020-08-16 11:19:36 +02:00
|
|
|
*/
|
2015-02-09 08:57:40 +00:00
|
|
|
QRectF PartLine::firstEndCircleRect() const
|
|
|
|
{
|
2020-10-17 20:25:30 +02:00
|
|
|
QList<QPointF> interesting_points = fourEndPoints(m_line.p1(),
|
|
|
|
m_line.p2(),
|
|
|
|
first_length);
|
2020-10-03 15:48:40 +02:00
|
|
|
|
2020-10-17 20:25:30 +02:00
|
|
|
QRectF end_rect(
|
|
|
|
interesting_points[0] - QPointF(first_length, first_length),
|
|
|
|
QSizeF(2.0 * first_length, 2.0 * first_length)
|
|
|
|
);
|
2020-10-03 15:48:40 +02:00
|
|
|
|
2020-10-17 20:25:30 +02:00
|
|
|
return(end_rect);
|
2009-04-03 19:30:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-10-17 20:25:30 +02:00
|
|
|
@brief PartLine::secondEndCircleRect
|
|
|
|
@return the rectangle bordering the entirety of the second extremity
|
2020-08-16 11:19:36 +02:00
|
|
|
*/
|
2020-09-07 22:03:40 +02:00
|
|
|
QRectF PartLine::secondEndCircleRect() const
|
|
|
|
{
|
2020-10-17 20:25:30 +02:00
|
|
|
QList<QPointF> interesting_points = fourEndPoints(m_line.p2(),
|
|
|
|
m_line.p1(),
|
|
|
|
second_length);
|
2020-10-03 15:48:40 +02:00
|
|
|
|
2020-10-17 20:25:30 +02:00
|
|
|
QRectF end_rect(
|
|
|
|
interesting_points[0] - QPointF(second_length, second_length),
|
|
|
|
QSizeF(2.0 * second_length, 2.0 * second_length)
|
|
|
|
);
|
2020-10-03 15:48:40 +02:00
|
|
|
|
2020-10-17 20:25:30 +02:00
|
|
|
return(end_rect);
|
2009-04-03 19:30:25 +00:00
|
|
|
}
|
|
|
|
|
2007-12-05 21:16:01 +00:00
|
|
|
/**
|
2020-10-17 20:25:30 +02:00
|
|
|
@brief PartLine::boundingRect
|
|
|
|
@return the bounding rect of this part
|
2020-08-16 11:19:36 +02:00
|
|
|
*/
|
2015-02-09 08:57:40 +00:00
|
|
|
QRectF PartLine::boundingRect() const
|
2020-10-03 15:48:40 +02:00
|
|
|
{
|
2020-10-17 20:25:30 +02:00
|
|
|
QRectF bound;
|
|
|
|
if (first_end || second_end)
|
|
|
|
bound = path().boundingRect();
|
|
|
|
else
|
|
|
|
bound = QRectF (m_line.p1(), m_line.p2());
|
2015-02-09 08:57:40 +00:00
|
|
|
|
2020-10-17 20:25:30 +02:00
|
|
|
qreal adjust = (SHADOWS_HEIGHT + penWeight()) / 2;
|
|
|
|
//We add 0.5 because CustomElementGraphicPart::drawShadowShape
|
|
|
|
//draw a shape bigger of 0.5 when pen weight is to 0.
|
|
|
|
if (penWeight() == 0) adjust += 0.5;
|
2015-02-09 08:57:40 +00:00
|
|
|
|
2020-10-17 20:25:30 +02:00
|
|
|
bound = bound.normalized();
|
|
|
|
bound.adjust(-adjust, -adjust, adjust, adjust);
|
2015-07-21 12:29:43 +00:00
|
|
|
|
2020-10-17 20:25:30 +02:00
|
|
|
return bound;
|
2007-06-30 17:41:07 +00:00
|
|
|
}
|
2007-12-15 21:57:00 +00:00
|
|
|
|
|
|
|
/**
|
2020-10-17 20:25:30 +02:00
|
|
|
@brief PartLine::isUseless
|
|
|
|
@return true if this part is irrelevant and does not deserve to be Retained / registered.
|
|
|
|
A line is relevant when is two point is different
|
2020-08-16 11:19:36 +02:00
|
|
|
*/
|
2020-09-07 22:03:40 +02:00
|
|
|
bool PartLine::isUseless() const
|
|
|
|
{
|
2020-10-17 20:25:30 +02:00
|
|
|
return(m_line.p1() == m_line.p2());
|
2007-12-15 21:57:00 +00:00
|
|
|
}
|
2009-04-03 19:30:25 +00:00
|
|
|
|
2013-02-11 18:35:13 +00:00
|
|
|
/**
|
2020-10-17 20:25:30 +02:00
|
|
|
@brief PartLine::sceneGeometricRect
|
|
|
|
@return the minimum, margin-less rectangle this part can fit into, in scene
|
|
|
|
coordinates. It is different from boundingRect() because it is not supposed
|
|
|
|
to imply any margin, and it is different from shape because it is a regular
|
|
|
|
rectangle, not a complex shape.
|
2020-08-16 11:19:36 +02:00
|
|
|
*/
|
2020-09-07 22:03:40 +02:00
|
|
|
QRectF PartLine::sceneGeometricRect() const
|
|
|
|
{
|
2020-10-17 20:25:30 +02:00
|
|
|
return(QRectF(sceneP1(), sceneP2()));
|
2013-02-11 18:35:13 +00:00
|
|
|
}
|
|
|
|
|
2013-02-08 22:05:12 +00:00
|
|
|
/**
|
2020-10-17 20:25:30 +02:00
|
|
|
@brief PartLine::startUserTransformation
|
|
|
|
Start the user-induced transformation, provided this primitive is contained
|
|
|
|
within the \a initial_selection_rect bounding rectangle.
|
|
|
|
@param initial_selection_rect
|
2020-08-16 11:19:36 +02:00
|
|
|
*/
|
2015-02-09 08:57:40 +00:00
|
|
|
void PartLine::startUserTransformation(const QRectF &initial_selection_rect)
|
|
|
|
{
|
2020-10-17 20:25:30 +02:00
|
|
|
Q_UNUSED(initial_selection_rect)
|
|
|
|
saved_points_.clear();
|
|
|
|
saved_points_ << sceneP1() << sceneP2();
|
2013-02-08 22:05:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-10-17 20:25:30 +02:00
|
|
|
@brief PartLine::handleUserTransformation
|
|
|
|
Handle the user-induced transformation from \a initial_selection_rect to \a new_selection_rect
|
|
|
|
@param initial_selection_rect
|
|
|
|
@param new_selection_rect
|
2020-08-16 11:19:36 +02:00
|
|
|
*/
|
2015-02-09 08:57:40 +00:00
|
|
|
void PartLine::handleUserTransformation(const QRectF &initial_selection_rect, const QRectF &new_selection_rect)
|
|
|
|
{
|
2020-10-17 20:25:30 +02:00
|
|
|
QList<QPointF> mapped_points = mapPoints(initial_selection_rect, new_selection_rect, saved_points_);
|
|
|
|
prepareGeometryChange();
|
|
|
|
m_line = QLineF(mapFromScene(mapped_points.at(0)), mapFromScene(mapped_points.at(1)));
|
2013-02-08 22:05:12 +00:00
|
|
|
}
|
|
|
|
|
2009-04-03 19:30:25 +00:00
|
|
|
/**
|
2020-10-17 20:25:30 +02:00
|
|
|
@brief PartLine::fourEndPoints
|
|
|
|
Return the four interesting point needed to draw the shape
|
|
|
|
at extremity of line (circle, diamond, arrow, triangle)
|
|
|
|
This points are in order :
|
|
|
|
* O : point on the line, at a distance 'length' of the extremity
|
|
|
|
* A : point on the line at a 'length' of 2x the extremity length
|
|
|
|
* B : point at a distance of length O - O is the projection of B on the line
|
|
|
|
* C : point at a distance of length O - O is the projection of C on the line
|
|
|
|
@param end_point : The concerned extremity
|
|
|
|
@param other_point : other needed point to define the line
|
|
|
|
@param length : length to use between the extremity and the point O
|
|
|
|
@return
|
2020-08-16 11:19:36 +02:00
|
|
|
*/
|
2015-02-09 08:57:40 +00:00
|
|
|
QList<QPointF> PartLine::fourEndPoints(const QPointF &end_point, const QPointF &other_point, const qreal &length)
|
|
|
|
{
|
2020-10-17 20:25:30 +02:00
|
|
|
//Vector and length of the line
|
|
|
|
QPointF line_vector = end_point - other_point;
|
|
|
|
qreal line_length = sqrt(pow(line_vector.x(), 2) + pow(line_vector.y(), 2));
|
2015-02-09 08:57:40 +00:00
|
|
|
|
2020-10-17 20:25:30 +02:00
|
|
|
//Unitary vector and perpendicular vector
|
|
|
|
QPointF u(line_vector / line_length * length);
|
|
|
|
QPointF v(-u.y(), u.x());
|
2020-10-03 15:48:40 +02:00
|
|
|
|
2020-10-17 20:25:30 +02:00
|
|
|
// points O, A, B, C
|
|
|
|
QPointF o(end_point - u);
|
|
|
|
QPointF a(o - u);
|
|
|
|
QPointF b(o + v);
|
|
|
|
QPointF c(o - v);
|
2020-10-03 15:48:40 +02:00
|
|
|
|
2020-10-17 20:25:30 +02:00
|
|
|
return(QList<QPointF>() << o << a << b << c);
|
2009-04-03 19:30:25 +00:00
|
|
|
}
|
2015-02-09 08:57:40 +00:00
|
|
|
|
2020-09-07 22:03:40 +02:00
|
|
|
QLineF PartLine::line() const
|
|
|
|
{
|
2020-10-17 20:25:30 +02:00
|
|
|
return m_line;
|
2015-07-20 17:45:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PartLine::setLine(const QLineF &line)
|
|
|
|
{
|
2020-10-17 20:25:30 +02:00
|
|
|
if (m_line == line) return;
|
|
|
|
prepareGeometryChange();
|
|
|
|
m_line = line;
|
|
|
|
adjusteHandlerPos();
|
|
|
|
emit lineChanged();
|
2015-07-23 10:52:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PartLine::setFirstEndType(const Qet::EndType &et)
|
|
|
|
{
|
2020-10-17 20:25:30 +02:00
|
|
|
if (first_end == et) return;
|
|
|
|
prepareGeometryChange();
|
|
|
|
first_end = et;
|
|
|
|
emit firstEndTypeChanged();
|
2015-07-23 10:52:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PartLine::setSecondEndType(const Qet::EndType &et)
|
|
|
|
{
|
2020-10-17 20:25:30 +02:00
|
|
|
if (second_end == et) return;
|
|
|
|
prepareGeometryChange();
|
|
|
|
second_end = et;
|
|
|
|
emit secondEndTypeChanged();
|
2015-07-23 10:52:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PartLine::setFirstEndLength(const qreal &l)
|
|
|
|
{
|
2020-10-17 20:25:30 +02:00
|
|
|
qreal length = qMin(qAbs(l), m_line.length());
|
|
|
|
if (first_length == length) return;
|
|
|
|
prepareGeometryChange();
|
|
|
|
first_length = length;
|
|
|
|
emit firstEndLengthChanged();
|
2015-07-23 10:52:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PartLine::setSecondEndLength(const qreal &l)
|
|
|
|
{
|
2020-10-17 20:25:30 +02:00
|
|
|
qreal length = qMin(qAbs(l), m_line.length());
|
|
|
|
if (second_length == length) return;
|
|
|
|
prepareGeometryChange();
|
|
|
|
second_length = length;
|
|
|
|
emit secondEndLengthChanged();
|
2015-07-20 17:45:37 +00:00
|
|
|
}
|
|
|
|
|
2021-09-13 20:38:39 +02:00
|
|
|
void PartLine::setRotation(qreal angle) {
|
|
|
|
|
2021-09-13 21:00:44 +02:00
|
|
|
QTransform rotation = QTransform().translate(m_line.p1().x(),m_line.p1().y()).rotate(angle-m_rot).translate(-m_line.p1().x(),-m_line.p1().y());
|
|
|
|
m_rot=angle;
|
2021-09-13 20:38:39 +02:00
|
|
|
|
2021-09-13 21:00:44 +02:00
|
|
|
setLine(rotation.map(m_line));
|
2021-09-13 20:38:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
qreal PartLine::rotation() const {
|
2021-09-13 21:00:44 +02:00
|
|
|
return m_rot;
|
2021-09-13 20:38:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-02-09 08:57:40 +00:00
|
|
|
/**
|
2020-10-17 20:25:30 +02:00
|
|
|
@brief PartLine::path
|
|
|
|
@return this line has a QPainterPath.
|
|
|
|
It's notably use when this line have an end type (circle, triangle etc....),
|
|
|
|
because return a QPainterPath with end already draw.
|
|
|
|
Else if there isn't an end type get P1 and P2 of line is better (faster).
|
2020-08-16 11:19:36 +02:00
|
|
|
*/
|
2015-02-09 08:57:40 +00:00
|
|
|
QPainterPath PartLine::path() const
|
|
|
|
{
|
2020-10-17 20:25:30 +02:00
|
|
|
QPainterPath path;
|
|
|
|
|
|
|
|
QPointF point1(m_line.p1());
|
|
|
|
QPointF point2(m_line.p2());
|
|
|
|
|
|
|
|
qreal line_length(m_line.length());
|
|
|
|
qreal pen_width = penWeight();
|
|
|
|
|
|
|
|
qreal length1 = first_length;
|
|
|
|
qreal length2 = second_length;
|
|
|
|
|
|
|
|
//debugPaint(painter);
|
|
|
|
|
2022-12-04 08:21:12 -05:00
|
|
|
//Determine if we must draw extremity
|
2020-10-17 20:25:30 +02:00
|
|
|
qreal reduced_line_length = line_length - (length1 * requiredLengthForEndType(first_end));
|
|
|
|
bool draw_1st_end = first_end && reduced_line_length >= 0;
|
|
|
|
|
|
|
|
if (draw_1st_end)
|
|
|
|
reduced_line_length -= (length2 * requiredLengthForEndType(second_end));
|
|
|
|
else
|
|
|
|
reduced_line_length = line_length - (length2 * requiredLengthForEndType(second_end));
|
|
|
|
|
|
|
|
|
|
|
|
//Draw the first extremity
|
|
|
|
QPointF start_point;
|
|
|
|
if (draw_1st_end)
|
|
|
|
{
|
|
|
|
QList<QPointF> four_points1(fourEndPoints(point1, point2, length1));
|
|
|
|
|
|
|
|
if (first_end == Qet::Circle)
|
|
|
|
{
|
|
|
|
path.addEllipse(QRectF(four_points1[0] - QPointF(length1, length1), QSizeF(length1 * 2.0, length1 * 2.0)));
|
|
|
|
start_point = four_points1[1];
|
|
|
|
}
|
|
|
|
else if (first_end == Qet::Diamond)
|
|
|
|
{
|
|
|
|
path.addPolygon(QPolygonF() << four_points1[1] << four_points1[2] << point1 << four_points1[3] << four_points1[1]);
|
|
|
|
start_point = four_points1[1];
|
|
|
|
}
|
|
|
|
else if (first_end == Qet::Simple)
|
|
|
|
{
|
|
|
|
path.addPolygon(QPolygonF() << four_points1[3] << point1 << four_points1[2]);
|
|
|
|
start_point = point1;
|
|
|
|
|
|
|
|
}
|
|
|
|
else if (first_end == Qet::Triangle)
|
|
|
|
{
|
|
|
|
path.addPolygon(QPolygonF() << four_points1[0] << four_points1[2] << point1 << four_points1[3] << four_points1[0]);
|
|
|
|
start_point = four_points1[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
//Adjust the start point according to the pen width
|
|
|
|
if (pen_width && (first_end == Qet::Simple || first_end == Qet::Circle))
|
|
|
|
start_point = QLineF(start_point, point2).pointAt(pen_width / 2.0 / line_length);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
start_point = point1;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Draw the second extremity
|
|
|
|
QPointF stop_point;
|
|
|
|
bool draw_2nd_end = second_end && reduced_line_length >= 0;
|
|
|
|
if (draw_2nd_end)
|
|
|
|
{
|
|
|
|
QList<QPointF> four_points2(fourEndPoints(point2, point1, length2));
|
|
|
|
|
|
|
|
if (second_end == Qet::Circle)
|
|
|
|
{
|
|
|
|
path.addEllipse(QRectF(four_points2[0] - QPointF(length2, length2), QSizeF(length2 * 2.0, length2 * 2.0)));
|
|
|
|
stop_point = four_points2[1];
|
|
|
|
}
|
|
|
|
else if (second_end == Qet::Diamond)
|
|
|
|
{
|
|
|
|
path.addPolygon(QPolygonF() << four_points2[2] << point2 << four_points2[3] << four_points2[1] << four_points2[2]);
|
|
|
|
stop_point = four_points2[1];
|
|
|
|
}
|
|
|
|
else if (second_end == Qet::Simple)
|
|
|
|
{
|
|
|
|
path.addPolygon(QPolygonF() << four_points2[3] << point2 << four_points2[2]);
|
|
|
|
stop_point = point2;
|
|
|
|
}
|
|
|
|
else if (second_end == Qet::Triangle)
|
|
|
|
{/**
|
|
|
|
@return true si cette partie n'est pas pertinente et ne merite pas d'etre
|
|
|
|
conservee / enregistree.
|
|
|
|
Une ligne est pertinente des lors que ses deux points sont differents
|
2015-02-09 08:57:40 +00:00
|
|
|
*/
|
2020-10-17 20:25:30 +02:00
|
|
|
path.addPolygon(QPolygonF() << four_points2[0] << four_points2[2] << point2 << four_points2[3] << four_points2[0]);
|
|
|
|
stop_point = four_points2[0];
|
|
|
|
}
|
|
|
|
|
2022-12-04 08:21:12 -05:00
|
|
|
//Adjust the end point according to the pen width
|
2020-10-17 20:25:30 +02:00
|
|
|
if (pen_width && (second_end == Qet::Simple || second_end == Qet::Circle))
|
|
|
|
stop_point = QLineF(point1, stop_point).pointAt((line_length - (pen_width / 2.0)) / line_length);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
stop_point = point2;
|
|
|
|
}
|
|
|
|
|
|
|
|
path.moveTo(start_point);
|
|
|
|
path.lineTo(stop_point);
|
|
|
|
|
|
|
|
return path;
|
2015-02-09 08:57:40 +00:00
|
|
|
}
|