Fix bug. Modeling objects should be separated from calculation objects and stay

in not copied container.

--HG--
branch : feature
This commit is contained in:
Roman Telezhynskyi 2017-10-26 12:54:08 +03:00
parent 9b54b0c069
commit e8ca7f575d
5 changed files with 122 additions and 145 deletions

View File

@ -260,7 +260,7 @@ void MainWindow::AddPP(const QString &PPName)
comboBoxDraws->blockSignals(true);
comboBoxDraws->addItem(PPName);
pattern->ClearGObjects();
pattern->ClearCalculationGObjects();
//Create single point
ui->view->itemClicked(nullptr);//hide options previous tool
const QString label = doc->GenerateLabel(LabelType::NewPatternPiece);

View File

@ -103,10 +103,7 @@ VContainer::VContainer(const VContainer &data)
//---------------------------------------------------------------------------------------------------------------------
VContainer::~VContainer()
{
ClearGObjects();
ClearVariables();
}
{}
//---------------------------------------------------------------------------------------------------------------------
/**
@ -117,7 +114,18 @@ VContainer::~VContainer()
// cppcheck-suppress unusedFunction
const QSharedPointer<VGObject> VContainer::GetGObject(quint32 id) const
{
return GetObject(d->gObjects, id);
if (d->calculationObjects.contains(id))
{
return d->calculationObjects.value(id);
}
else if (d->modelingObjects->contains(id))
{
return d->modelingObjects->value(id);
}
else
{
throw VExceptionBadId(tr("Can't find object"), id);
}
}
//---------------------------------------------------------------------------------------------------------------------
@ -129,26 +137,6 @@ const QSharedPointer<VGObject> VContainer::GetFakeGObject(quint32 id)
return pointer;
}
//---------------------------------------------------------------------------------------------------------------------
/**
* @brief GetObject return object from container
* @param obj container
* @param id id of object
* @return Object
*/
template <typename key, typename val>
const val VContainer::GetObject(const QHash<key, val> &obj, key id) const
{
if (obj.contains(id))
{
return obj.value(id);
}
else
{
throw VExceptionBadId(tr("Can't find object"), id);
}
}
//---------------------------------------------------------------------------------------------------------------------
VPiece VContainer::GetPiece(quint32 id) const
{
@ -192,8 +180,27 @@ quint32 VContainer::AddGObject(VGObject *obj)
quint32 VContainer::AddGObject(const QSharedPointer<VGObject> &obj)
{
SCASSERT(not obj.isNull())
if (obj->getMode() == Draw::Layout)
{
qWarning("Can't add an object with mode 'Layout'");
return NULL_ID;
}
uniqueNames.insert(obj->name());
return AddObject(d->gObjects, obj);
const quint32 id = getNextId();
obj->setId(id);
if (obj->getMode() == Draw::Calculation)
{
d->calculationObjects.insert(id, obj);
}
else if (obj->getMode() == Draw::Modeling)
{
d->modelingObjects->insert(id, obj);
}
return id;
}
//---------------------------------------------------------------------------------------------------------------------
@ -290,33 +297,14 @@ void VContainer::ClearForFullParse()
*/
void VContainer::ClearGObjects()
{
d->gObjects.clear();
d->calculationObjects.clear();
d->modelingObjects->clear();
}
//---------------------------------------------------------------------------------------------------------------------
void VContainer::ClearCalculationGObjects()
{
if (not d->gObjects.isEmpty()) //-V807
{
QVector<quint32> keys;
QHash<quint32, QSharedPointer<VGObject> >::iterator i;
for (i = d->gObjects.begin(); i != d->gObjects.end(); ++i)
{
if (i.value()->getMode() == Draw::Calculation)
{
i.value().clear();
keys.append(i.key());
}
}
// We can't delete objects in previous loop it will destroy the iterator.
if (not keys.isEmpty())
{
for (int i = 0; i < keys.size(); ++i)
{
d->gObjects.remove(keys.at(i));
}
}
}
d->calculationObjects.clear();
}
//---------------------------------------------------------------------------------------------------------------------
@ -464,23 +452,6 @@ void VContainer::RemovePiece(quint32 id)
d->pieces->remove(id);
}
//---------------------------------------------------------------------------------------------------------------------
/**
* @brief AddObject add object to container
* @param obj container
* @param value object
* @return id of object in container
*/
template <typename key, typename val>
quint32 VContainer::AddObject(QHash<key, val> &obj, val value)
{
SCASSERT(value != nullptr)
const quint32 id = getNextId();
value->setId(id);
obj[id] = value;
return id;
}
//---------------------------------------------------------------------------------------------------------------------
void VContainer::UpdatePiece(quint32 id, const VPiece &detail)
{
@ -678,9 +649,9 @@ qreal *VContainer::rheight()
* @brief data container with datagObjects return container of gObjects
* @return pointer on container of gObjects
*/
const QHash<quint32, QSharedPointer<VGObject> > *VContainer::DataGObjects() const
const QHash<quint32, QSharedPointer<VGObject> > *VContainer::CalculationGObjects() const
{
return &d->gObjects;
return &d->calculationObjects;
}
//---------------------------------------------------------------------------------------------------------------------

View File

@ -69,7 +69,8 @@ class VContainerData : public QSharedData //-V690
public:
VContainerData(const VTranslateVars *trVars, const Unit *patternUnit)
: gObjects(QHash<quint32, QSharedPointer<VGObject> >()),
: calculationObjects(QHash<quint32, QSharedPointer<VGObject> >()),
modelingObjects(QSharedPointer<QHash<quint32, QSharedPointer<VGObject>>>::create()),
variables(QHash<QString, QSharedPointer<VInternalVariable> > ()),
pieces(QSharedPointer<QHash<quint32, VPiece>>::create()),
piecePaths(QSharedPointer<QHash<quint32, VPiecePath>>::create()),
@ -79,7 +80,8 @@ public:
VContainerData(const VContainerData &data)
: QSharedData(data),
gObjects(data.gObjects),
calculationObjects(data.calculationObjects),
modelingObjects(data.modelingObjects),
variables(data.variables),
pieces(data.pieces),
piecePaths(data.piecePaths),
@ -89,10 +91,8 @@ public:
virtual ~VContainerData();
/**
* @brief gObjects graphicals objects of pattern.
*/
QHash<quint32, QSharedPointer<VGObject> > gObjects;
QHash<quint32, QSharedPointer<VGObject> > calculationObjects;
QSharedPointer<QHash<quint32, QSharedPointer<VGObject>>> modelingObjects;
/**
* @brief variables container for measurements, increments, lines lengths, lines angles, arcs lengths, curve lengths
@ -184,7 +184,7 @@ public:
void RemoveIncrement(const QString& name);
const QHash<quint32, QSharedPointer<VGObject> > *DataGObjects() const;
const QHash<quint32, QSharedPointer<VGObject> > *CalculationGObjects() const;
const QHash<quint32, VPiece> *DataPieces() const;
const QHash<QString, QSharedPointer<VInternalVariable>> *DataVariables() const;
@ -219,16 +219,9 @@ private:
template <class T>
uint qHash( const QSharedPointer<T> &p );
template <typename key, typename val>
// cppcheck-suppress functionStatic
const val GetObject(const QHash<key, val> &obj, key id) const;
template <typename T>
void UpdateObject(const quint32 &id, const QSharedPointer<T> &point);
template <typename key, typename val>
static quint32 AddObject(QHash<key, val> &obj, val value);
template <typename T>
const QMap<QString, QSharedPointer<T> > DataVar(const VarType &type) const;
};
@ -248,26 +241,24 @@ const QSharedPointer<T> VContainer::GeometricObject(const quint32 &id) const
throw VExceptionBadId(tr("Can't find object"), id);
}
QSharedPointer<VGObject> gObj = QSharedPointer<VGObject>();
if (d->gObjects.contains(id))
QSharedPointer<VGObject> gObj;
if (d->calculationObjects.contains(id))
{
gObj = d->gObjects.value(id);
gObj = d->calculationObjects.value(id);
}
else if (d->modelingObjects->contains(id))
{
gObj = d->modelingObjects->value(id);
}
else
{
throw VExceptionBadId(tr("Can't find object"), id);
}
try
{
QSharedPointer<T> obj = qSharedPointerDynamicCast<T>(gObj);
SCASSERT(obj.isNull() == false)
return obj;
}
catch (const std::bad_alloc &)
{
throw VExceptionBadId(tr("Can't cast object"), id);
}
}
//---------------------------------------------------------------------------------------------------------------------
@ -375,19 +366,39 @@ void VContainer::UpdateObject(const quint32 &id, const QSharedPointer<T> &point)
Q_ASSERT_X(id != NULL_ID, Q_FUNC_INFO, "id == 0"); //-V654 //-V712
SCASSERT(point.isNull() == false)
point->setId(id);
if (d->gObjects.contains(id))
if (d->calculationObjects.contains(id) && point->getMode() == Draw::Calculation)
{
QSharedPointer<T> obj = qSharedPointerDynamicCast<T>(d->gObjects.value(id));
QSharedPointer<T> obj = qSharedPointerDynamicCast<T>(d->calculationObjects.value(id));
if (obj.isNull())
{
throw VExceptionBadId(tr("Can't cast object"), id);
}
*obj = *point;
}
else if (d->modelingObjects->contains(id) && point->getMode() == Draw::Modeling)
{
QSharedPointer<T> obj = qSharedPointerDynamicCast<T>(d->modelingObjects->value(id));
if (obj.isNull())
{
throw VExceptionBadId(tr("Can't cast object"), id);
}
*obj = *point;
}
else if (point->getMode() == Draw::Calculation)
{
d->calculationObjects.insert(id, point);
}
else if (point->getMode() == Draw::Modeling)
{
d->modelingObjects->insert(id, point);
}
else
{
d->gObjects.insert(id, point);
qWarning("Can't update an object with mode 'Layout'");
return;
}
UpdateId(id);
}
#endif // VCONTAINER_H

View File

@ -215,7 +215,7 @@ void DialogTool::FillComboBoxSplines(QComboBox *box) const
SCASSERT(box != nullptr)
box->blockSignals(true);
const auto objs = data->DataGObjects();
const auto objs = data->CalculationGObjects();
QHash<quint32, QSharedPointer<VGObject> >::const_iterator i;
QMap<QString, quint32> list;
for (i = objs->constBegin(); i != objs->constEnd(); ++i)
@ -239,7 +239,7 @@ void DialogTool::FillComboBoxSplinesPath(QComboBox *box) const
SCASSERT(box != nullptr)
box->blockSignals(true);
const auto objs = data->DataGObjects();
const auto objs = data->CalculationGObjects();
QHash<quint32, QSharedPointer<VGObject> >::const_iterator i;
QMap<QString, quint32> list;
for (i = objs->constBegin(); i != objs->constEnd(); ++i)
@ -261,7 +261,7 @@ void DialogTool::FillComboBoxSplinesPath(QComboBox *box) const
void DialogTool::FillComboBoxCurves(QComboBox *box) const
{
SCASSERT(box != nullptr)
const auto objs = data->DataGObjects();
const auto objs = data->CalculationGObjects();
QMap<QString, quint32> list;
QHash<quint32, QSharedPointer<VGObject> >::const_iterator i;
for (i = objs->constBegin(); i != objs->constEnd(); ++i)
@ -269,12 +269,12 @@ void DialogTool::FillComboBoxCurves(QComboBox *box) const
if (i.key() != toolId)
{
QSharedPointer<VGObject> obj = i.value();
if ((obj->getType() == GOType::Arc
if (obj->getType() == GOType::Arc
|| obj->getType() == GOType::EllipticalArc
|| obj->getType() == GOType::Spline
|| obj->getType() == GOType::SplinePath
|| obj->getType() == GOType::CubicBezier
|| obj->getType() == GOType::CubicBezierPath) && obj->getMode() == Draw::Calculation)
|| obj->getType() == GOType::CubicBezierPath)
{
PrepareList<VAbstractCurve>(list, i.key());
}
@ -683,8 +683,7 @@ void DialogTool::InitNodeAngles(QComboBox *box)
//---------------------------------------------------------------------------------------------------------------------
bool DialogTool::IsSplinePath(const QSharedPointer<VGObject> &obj) const
{
return (obj->getType() == GOType::SplinePath || obj->getType() == GOType::CubicBezierPath) &&
obj->getMode() == Draw::Calculation;
return obj->getType() == GOType::SplinePath || obj->getType() == GOType::CubicBezierPath;
}
//---------------------------------------------------------------------------------------------------------------------
@ -1010,8 +1009,7 @@ void DialogTool::PrepareList(QMap<QString, quint32> &list, quint32 id) const
//---------------------------------------------------------------------------------------------------------------------
bool DialogTool::IsSpline(const QSharedPointer<VGObject> &obj) const
{
return (obj->getType() == GOType::Spline || obj->getType() == GOType::CubicBezier) &&
obj->getMode() == Draw::Calculation;
return obj->getType() == GOType::Spline || obj->getType() == GOType::CubicBezier;
}
//---------------------------------------------------------------------------------------------------------------------
@ -1290,7 +1288,7 @@ void DialogTool::FillCombo(QComboBox *box, GOType gType, FillComboBox rule, cons
SCASSERT(box != nullptr)
box->blockSignals(true);
const QHash<quint32, QSharedPointer<VGObject> > *objs = data->DataGObjects();
const QHash<quint32, QSharedPointer<VGObject> > *objs = data->CalculationGObjects();
QHash<quint32, QSharedPointer<VGObject> >::const_iterator i;
QMap<QString, quint32> list;
for (i = objs->constBegin(); i != objs->constEnd(); ++i)
@ -1300,7 +1298,7 @@ void DialogTool::FillCombo(QComboBox *box, GOType gType, FillComboBox rule, cons
if (i.key() != toolId && i.key() != ch1 && i.key() != ch2)
{
QSharedPointer<VGObject> obj = i.value();
if (obj->getType() == gType && obj->getMode() == Draw::Calculation)
if (obj->getType() == gType)
{
PrepareList<GObject>(list, i.key());
}

View File

@ -83,35 +83,6 @@ template <class T> class QSharedPointer;
const QString VAbstractTool::AttrInUse = QStringLiteral("inUse");
namespace
{
//---------------------------------------------------------------------------------------------------------------------
quint32 CreateNodeSpline(VContainer *data, quint32 id)
{
if (data->GetGObject(id)->getType() == GOType::Spline)
{
return VAbstractTool::CreateNode<VSpline>(data, id);
}
else
{
return VAbstractTool::CreateNode<VCubicBezier>(data, id);
}
}
//---------------------------------------------------------------------------------------------------------------------
quint32 CreateNodeSplinePath(VContainer *data, quint32 id)
{
if (data->GetGObject(id)->getType() == GOType::SplinePath)
{
return VAbstractTool::CreateNode<VSplinePath>(data, id);
}
else
{
return VAbstractTool::CreateNode<VCubicBezierPath>(data, id);
}
}
}//static functions
//---------------------------------------------------------------------------------------------------------------------
/**
* @brief VAbstractTool container.
@ -379,7 +350,7 @@ QMap<QString, QString> VAbstractTool::ColorsList()
// cppcheck-suppress unusedFunction
QMap<QString, quint32> VAbstractTool::PointsList() const
{
const QHash<quint32, QSharedPointer<VGObject> > *objs = data.DataGObjects();
const QHash<quint32, QSharedPointer<VGObject> > *objs = data.CalculationGObjects();
QMap<QString, quint32> list;
QHash<quint32, QSharedPointer<VGObject> >::const_iterator i;
for (i = objs->constBegin(); i != objs->constEnd(); ++i)
@ -387,7 +358,7 @@ QMap<QString, quint32> VAbstractTool::PointsList() const
if (i.key() != m_id)
{
QSharedPointer<VGObject> obj = i.value();
if (obj->getType() == GOType::Point && obj->getMode() == Draw::Calculation)
if (obj->getType() == GOType::Point)
{
const QSharedPointer<VPointF> point = data.GeometricObject<VPointF>(i.key());
list[point->name()] = i.key();
@ -694,3 +665,29 @@ quint32 VAbstractTool::PrepareNode(const VPieceNode &node, VMainGraphicsScene *s
}
return initData.id;
}
//---------------------------------------------------------------------------------------------------------------------
quint32 VAbstractTool::CreateNodeSpline(VContainer *data, quint32 id)
{
if (data->GetGObject(id)->getType() == GOType::Spline)
{
return VAbstractTool::CreateNode<VSpline>(data, id);
}
else
{
return VAbstractTool::CreateNode<VCubicBezier>(data, id);
}
}
//---------------------------------------------------------------------------------------------------------------------
quint32 VAbstractTool::CreateNodeSplinePath(VContainer *data, quint32 id)
{
if (data->GetGObject(id)->getType() == GOType::SplinePath)
{
return VAbstractTool::CreateNode<VSplinePath>(data, id);
}
else
{
return VAbstractTool::CreateNode<VCubicBezierPath>(data, id);
}
}