/************************************************************************ ** ** @file vcontainer.h ** @author Roman Telezhynskyi ** @date November 15, 2013 ** ** @brief ** @copyright ** This source code is part of the Valentine project, a pattern making ** program, whose allow create and modeling patterns of clothing. ** Copyright (C) 2013-2015 Valentina project ** All Rights Reserved. ** ** Valentina 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 3 of the License, or ** (at your option) any later version. ** ** Valentina 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 Valentina. If not, see . ** *************************************************************************/ #ifndef VCONTAINER_H #define VCONTAINER_H #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "../vmisc/def.h" #include "../ifc/exception/vexceptionbadid.h" #include "../ifc/ifcdef.h" #include "../vgeometry/vabstractcubicbezierpath.h" #include "../vgeometry/vabstractcurve.h" #include "../vgeometry/vgobject.h" #include "../vmisc/diagnostic.h" #include "variables.h" #include "variables/vinternalvariable.h" #include "vpiece.h" #include "vpiecepath.h" #include "vtranslatevars.h" class VEllipticalArc; QT_WARNING_PUSH QT_WARNING_DISABLE_GCC("-Weffc++") QT_WARNING_DISABLE_INTEL(2021) QT_WARNING_DISABLE_GCC("-Wnon-virtual-dtor") class VContainerData : public QSharedData //-V690 { public: VContainerData(const VTranslateVars *trVars, const Unit *patternUnit) : gObjects(QHash >()), variables(QHash > ()), pieces(QSharedPointer>(new QHash())), piecePaths(QSharedPointer>(new QHash())), trVars(trVars), patternUnit(patternUnit) {} VContainerData(const VContainerData &data) : QSharedData(data), gObjects(data.gObjects), variables(data.variables), pieces(data.pieces), piecePaths(data.piecePaths), trVars(data.trVars), patternUnit(data.patternUnit) {} virtual ~VContainerData(); /** * @brief gObjects graphicals objects of pattern. */ QHash > gObjects; /** * @brief variables container for measurements, increments, lines lengths, lines angles, arcs lengths, curve lengths */ QHash> variables; QSharedPointer> pieces; QSharedPointer> piecePaths; const VTranslateVars *trVars; const Unit *patternUnit; private: VContainerData &operator=(const VContainerData &) Q_DECL_EQ_DELETE; }; QT_WARNING_POP /** * @brief The VContainer class container of all variables. */ class VContainer { Q_DECLARE_TR_FUNCTIONS(VContainer) public: VContainer(const VTranslateVars *trVars, const Unit *patternUnit); VContainer(const VContainer &data); ~VContainer(); VContainer &operator=(const VContainer &data); #ifdef Q_COMPILER_RVALUE_REFS VContainer &operator=(VContainer &&data) Q_DECL_NOTHROW { Swap(data); return *this; } #endif void Swap(VContainer &data) Q_DECL_NOTHROW { std::swap(d, data.d); } template const QSharedPointer GeometricObject(const quint32 &id) const; const QSharedPointer GetGObject(quint32 id) const; static const QSharedPointer GetFakeGObject(quint32 id); VPiece GetPiece(quint32 id) const; VPiecePath GetPiecePath(quint32 id) const; template QSharedPointer GetVariable(QString name) const; static quint32 getId(); static quint32 getNextId(); static void UpdateId(quint32 newId); quint32 AddGObject(VGObject *obj); quint32 AddPiece(const VPiece &detail); quint32 AddPiecePath(const VPiecePath &path); void AddLine(const quint32 &firstPointId, const quint32 &secondPointId); void AddArc(const QSharedPointer &arc, const quint32 &id, const quint32 &parentId = NULL_ID); void AddSpline(const QSharedPointer &curve, quint32 id, quint32 parentId = NULL_ID); void AddCurveWithSegments(const QSharedPointer &curve, const quint32 &id, quint32 parentId = NULL_ID); template void AddVariable(const QString& name, T *var); template void AddVariable(const QString& name, const QSharedPointer &var); void RemoveVariable(const QString& name); void RemovePiece(quint32 id); void UpdateGObject(quint32 id, VGObject* obj); void UpdatePiece(quint32 id, const VPiece &detail); void UpdatePiecePath(quint32 id, const VPiecePath &path); void Clear(); void ClearForFullParse(); void ClearGObjects(); void ClearCalculationGObjects(); void ClearVariables(const VarType &type = VarType::Unknown); static void ClearUniqueNames(); static void ClearUniqueIncrementNames(); static void SetSize(qreal size); static void SetHeight(qreal height); static qreal size(); static qreal *rsize(); static qreal height(); static qreal *rheight(); void RemoveIncrement(const QString& name); const QHash > *DataGObjects() const; const QHash *DataPieces() const; const QHash> *DataVariables() const; const QMap > DataMeasurements() const; const QMap > DataIncrements() const; const QMap > DataLengthLines() const; const QMap > DataLengthCurves() const; const QMap > DataCurvesCLength() const; const QMap > DataAngleLines() const; const QMap > DataRadiusesArcs() const; const QMap > DataAnglesCurves() const; static bool IsUnique(const QString &name); static QStringList AllUniqueNames(); const Unit *GetPatternUnit() const; const VTranslateVars *GetTrVars() const; private: /** * @brief _id current id. New object will have value +1. For empty class equal 0. */ static quint32 _id; static qreal _size; static qreal _height; static QSet uniqueNames; QSharedDataPointer d; void AddCurve(const QSharedPointer &curve, const quint32 &id, quint32 parentId = NULL_ID); template uint qHash( const QSharedPointer &p ); template // cppcheck-suppress functionStatic const val GetObject(const QHash &obj, key id) const; template void UpdateObject(QHash &obj, const quint32 &id, val point); template static quint32 AddObject(QHash &obj, val value); template const QMap > DataVar(const VarType &type) const; }; Q_DECLARE_TYPEINFO(VContainer, Q_MOVABLE_TYPE); /* * Defintion of templated member functions of VContainer */ //--------------------------------------------------------------------------------------------------------------------- template const QSharedPointer VContainer::GeometricObject(const quint32 &id) const { if (id == NULL_ID) { throw VExceptionBadId(tr("Can't find object"), id); } QSharedPointer gObj = QSharedPointer(); if (d->gObjects.contains(id)) { gObj = d->gObjects.value(id); } else { throw VExceptionBadId(tr("Can't find object"), id); } try { QSharedPointer obj = qSharedPointerDynamicCast(gObj); SCASSERT(obj.isNull() == false) return obj; } catch (const std::bad_alloc &) { throw VExceptionBadId(tr("Can't cast object"), id); } } //--------------------------------------------------------------------------------------------------------------------- /** * @brief GetVariable return varible by name * @param name variable's name * @return variable */ template QSharedPointer VContainer::GetVariable(QString name) const { SCASSERT(name.isEmpty()==false) if (d->variables.contains(name)) { try { QSharedPointer value = qSharedPointerDynamicCast(d->variables.value(name)); SCASSERT(value.isNull() == false) return value; } catch (const std::bad_alloc &) { throw VExceptionBadId(tr("Can't cast object"), name); } } else { throw VExceptionBadId(tr("Can't find object"), name); } } //--------------------------------------------------------------------------------------------------------------------- template void VContainer::AddVariable(const QString& name, T *var) { AddVariable(name, QSharedPointer(var)); } //--------------------------------------------------------------------------------------------------------------------- template void VContainer::AddVariable(const QString& name, const QSharedPointer &var) { if (d->variables.contains(name)) { if (d->variables.value(name)->GetType() == var->GetType()) { d->variables[name].clear(); } else { throw VExceptionBadId(tr("Can't find object. Type mismatch."), name); } } d->variables.insert(name, var); uniqueNames.insert(name); } //--------------------------------------------------------------------------------------------------------------------- template uint VContainer::qHash( const QSharedPointer &p ) { return qHash( p.data() ); } #endif // VCONTAINER_H