Fix reference counting for modeling objects.

--HG--
branch : develop
This commit is contained in:
Roman Telezhynskyi 2019-02-16 11:57:47 +02:00
parent 939a18011c
commit 372df4661a
9 changed files with 48 additions and 19 deletions

View File

@ -1820,6 +1820,18 @@ void VPattern::ParsePinPoint(const QDomElement &domElement, const Document &pars
ToolsCommonAttributes(domElement, initData.id); ToolsCommonAttributes(domElement, initData.id);
initData.pointId = GetParametrUInt(domElement, AttrIdObject, NULL_ID_STR); initData.pointId = GetParametrUInt(domElement, AttrIdObject, NULL_ID_STR);
try
{
initData.data->GeometricObject<VPointF>(initData.pointId);
}
catch (const VExceptionBadId &)
{ // Possible case. Parent was deleted, but the node object is still here.
qDebug() << "Broken relation. Parent was deleted, but the place label object is still here. Place label "
"id =" << initData.id << ".";
return;// Just ignore
}
initData.idTool = GetParametrUInt(domElement, VAbstractNode::AttrIdTool, NULL_ID_STR); initData.idTool = GetParametrUInt(domElement, VAbstractNode::AttrIdTool, NULL_ID_STR);
VToolPin::Create(initData); VToolPin::Create(initData);
} }
@ -1846,6 +1858,18 @@ void VPattern::ParsePlaceLabel(QDomElement &domElement, const Document &parse)
ToolsCommonAttributes(domElement, initData.id); ToolsCommonAttributes(domElement, initData.id);
initData.centerPoint = GetParametrUInt(domElement, AttrIdObject, NULL_ID_STR); initData.centerPoint = GetParametrUInt(domElement, AttrIdObject, NULL_ID_STR);
try
{
initData.data->GeometricObject<VPointF>(initData.centerPoint);
}
catch (const VExceptionBadId &)
{ // Possible case. Parent was deleted, but the node object is still here.
qDebug() << "Broken relation. Parent was deleted, but the place label object is still here. Place label "
"id =" << initData.id << ".";
return;// Just ignore
}
initData.idTool = GetParametrUInt(domElement, VAbstractNode::AttrIdTool, NULL_ID_STR); initData.idTool = GetParametrUInt(domElement, VAbstractNode::AttrIdTool, NULL_ID_STR);
initData.width = GetParametrString(domElement, AttrWidth, QStringLiteral("1.0")); initData.width = GetParametrString(domElement, AttrWidth, QStringLiteral("1.0"));
@ -3787,6 +3811,26 @@ void VPattern::ParsePathElement(VMainGraphicsScene *scene, QDomElement &domEleme
if (not element.isNull()) if (not element.isNull())
{ {
initData.path = ParsePathNodes(element); initData.path = ParsePathNodes(element);
try
{
for (int i = 0; i < initData.path.CountNodes(); ++i)
{
initData.data->GetGObject(initData.path.at(i).GetId());
}
}
catch (const VExceptionBadId &)
{ // Possible case. Parent was deleted, but the node object is still here.
qDebug() << "Broken relation. Parent was deleted, but the piece path object is still here. Piece "
"path id =" << initData.id << ".";
return;// Just ignore
}
}
else
{
VExceptionObjectError excep(tr("Error creating or updating a piece path"), domElement);
excep.AddMoreInformation(tr("Piece path doesn't contain nodes"));
throw excep;
} }
const QString defType = QString().setNum(static_cast<int>(PiecePathType::CustomSeamAllowance)); const QString defType = QString().setNum(static_cast<int>(PiecePathType::CustomSeamAllowance));

View File

@ -226,18 +226,6 @@ QVariant VToolBasePoint::itemChange(QGraphicsItem::GraphicsItemChange change, co
return VToolSinglePoint::itemChange(change, value); return VToolSinglePoint::itemChange(change, value);
} }
//---------------------------------------------------------------------------------------------------------------------
/**
* @brief decrementReferens decrement referens parents objects.
*/
void VToolBasePoint::decrementReferens()
{
if (_referens > 1)
{
--_referens;
}
}
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
QPointF VToolBasePoint::GetBasePointPos() const QPointF VToolBasePoint::GetBasePointPos() const
{ {

View File

@ -74,7 +74,6 @@ public:
virtual int type() const override {return Type;} virtual int type() const override {return Type;}
enum { Type = UserType + static_cast<int>(Tool::BasePoint)}; enum { Type = UserType + static_cast<int>(Tool::BasePoint)};
virtual void ShowVisualization(bool show) override; virtual void ShowVisualization(bool show) override;
virtual void decrementReferens() override;
QPointF GetBasePointPos() const; QPointF GetBasePointPos() const;
void SetBasePointPos(const QPointF &pos); void SetBasePointPos(const QPointF &pos);

View File

@ -227,7 +227,7 @@ void VDrawTool::ContextMenu(QGraphicsSceneContextMenuEvent *event, quint32 itemI
{ {
if (ref == Referens::Follow) if (ref == Referens::Follow)
{ {
if (_referens > 1) if (_referens > 0)
{ {
qCDebug(vTool, "Remove disabled. Tool has childern."); qCDebug(vTool, "Remove disabled. Tool has childern.");
actionRemove->setEnabled(false); actionRemove->setEnabled(false);

View File

@ -294,7 +294,6 @@ VToolPiecePath::VToolPiecePath(const VToolPiecePathInitData &initData, QObject *
QGraphicsPathItem(parent), QGraphicsPathItem(parent),
m_pieceId(initData.idObject) m_pieceId(initData.idObject)
{ {
IncrementNodes(VAbstractTool::data.GetPiecePath(initData.id));
RefreshGeometry(); RefreshGeometry();
ToolCreation(initData.typeCreation); ToolCreation(initData.typeCreation);
} }

View File

@ -211,7 +211,7 @@ qreal VAbstractTool::CheckFormula(const quint32 &toolId, QString &formula, VCont
void VAbstractTool::DeleteToolWithConfirm(bool ask) void VAbstractTool::DeleteToolWithConfirm(bool ask)
{ {
qCDebug(vTool, "Deleting abstract tool."); qCDebug(vTool, "Deleting abstract tool.");
if (_referens <= 1) if (_referens == 0)
{ {
qCDebug(vTool, "No children."); qCDebug(vTool, "No children.");
emit qApp->getSceneView()->itemClicked(nullptr); emit qApp->getSceneView()->itemClicked(nullptr);

View File

@ -39,7 +39,7 @@ Q_LOGGING_CATEGORY(vTool, "v.tool")
* @param parent parent object * @param parent parent object
*/ */
VDataTool::VDataTool(VContainer *data, QObject *parent) VDataTool::VDataTool(VContainer *data, QObject *parent)
: QObject(parent), data(*data), _referens(1) : QObject(parent), data(*data), _referens(0)
{ {
SCASSERT(data != nullptr) SCASSERT(data != nullptr)
} }

View File

@ -1124,7 +1124,7 @@ void VToolSeamAllowance::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
forceFlippingOption->setChecked(detail.IsForceFlipping()); forceFlippingOption->setChecked(detail.IsForceFlipping());
QAction *actionRemove = menu.addAction(QIcon::fromTheme(QStringLiteral("edit-delete")), tr("Delete")); QAction *actionRemove = menu.addAction(QIcon::fromTheme(QStringLiteral("edit-delete")), tr("Delete"));
_referens > 1 ? actionRemove->setEnabled(false) : actionRemove->setEnabled(true); actionRemove->setDisabled(_referens > 0);
QAction *selectedAction = menu.exec(event->screenPos()); QAction *selectedAction = menu.exec(event->screenPos());
if (selectedAction == actionOption) if (selectedAction == actionOption)

View File

@ -1667,7 +1667,6 @@ VToolUnionDetails::VToolUnionDetails(const VToolUnionDetailsInitData &initData,
indexD2(initData.indexD2), indexD2(initData.indexD2),
version(initData.version) version(initData.version)
{ {
_referens = 0;
ToolCreation(initData.typeCreation); ToolCreation(initData.typeCreation);
} }