Performance improvement. Speed up method VDomDocument::elementById.

--HG--
branch : develop
This commit is contained in:
Roman Telezhynskyi 2017-08-02 14:10:10 +03:00
parent ca66c8ebe2
commit acc340bff1
23 changed files with 73 additions and 53 deletions

View File

@ -2207,7 +2207,7 @@ QString VAbstractPattern::GetGroupName(quint32 id)
QDomElement groups = CreateGroups();
if (not groups.isNull())
{
QDomElement group = elementById(id);
QDomElement group = elementById(id, TagGroup);
if (group.isElement())
{
name = GetParametrString(group, AttrName, name);
@ -2238,7 +2238,7 @@ void VAbstractPattern::SetGroupName(quint32 id, const QString &name)
QDomElement groups = CreateGroups();
if (not groups.isNull())
{
QDomElement group = elementById(id);
QDomElement group = elementById(id, TagGroup);
if (group.isElement())
{
group.setAttribute(AttrName, name);
@ -2311,7 +2311,7 @@ QMap<quint32, QPair<QString, bool> > VAbstractPattern::GetGroups()
//---------------------------------------------------------------------------------------------------------------------
bool VAbstractPattern::GetGroupVisivility(quint32 id)
{
QDomElement group = elementById(id);
QDomElement group = elementById(id, TagGroup);
if (group.isElement())
{
return GetParametrBool(group, AttrVisible, trueStr);
@ -2326,7 +2326,7 @@ bool VAbstractPattern::GetGroupVisivility(quint32 id)
//---------------------------------------------------------------------------------------------------------------------
void VAbstractPattern::SetGroupVisivility(quint32 id, bool visible)
{
QDomElement group = elementById(id);
QDomElement group = elementById(id, TagGroup);
if (group.isElement())
{
SetAttribute(group, AttrVisible, visible);

View File

@ -194,7 +194,7 @@ VDomDocument::VDomDocument()
* @param id value id attribute.
* @return dom element.
*/
QDomElement VDomDocument::elementById(const QString& id)
QDomElement VDomDocument::elementById(const QString& id, const QString &tagName)
{
if (map.contains(id))
{
@ -206,18 +206,38 @@ QDomElement VDomDocument::elementById(const QString& id)
map.remove(id);
}
if (tagName.isEmpty())
{
if (this->find(this->documentElement(), id))
{
return map[id];
}
}
else
{
const QDomNodeList list = elementsByTagName(tagName);
for (int i=0; i < list.size(); ++i)
{
const QDomElement domElement = list.at(i).toElement();
if (not domElement.isNull() && domElement.hasAttribute(AttrId))
{
const QString value = domElement.attribute(AttrId);
this->map[value] = domElement;
if (value == id)
{
return domElement;
}
}
}
}
return QDomElement();
}
//---------------------------------------------------------------------------------------------------------------------
QDomElement VDomDocument::elementById(quint32 id)
QDomElement VDomDocument::elementById(quint32 id, const QString &tagName)
{
return elementById(QString().setNum(id));
return elementById(QString().setNum(id), tagName);
}
//---------------------------------------------------------------------------------------------------------------------

View File

@ -88,8 +88,8 @@ public:
VDomDocument();
virtual ~VDomDocument() Q_DECL_EQ_DEFAULT;
QDomElement elementById(const QString& id);
QDomElement elementById(quint32 id);
QDomElement elementById(const QString& id, const QString &tagName = QString());
QDomElement elementById(quint32 id, const QString &tagName = QString());
template <typename T>
void SetAttribute(QDomElement &domElement, const QString &name, const T &value) const;

View File

@ -106,7 +106,7 @@ void VDrawTool::ChangedNameDraw(const QString &oldName, const QString &newName)
void VDrawTool::SaveDialogChange()
{
qCDebug(vTool, "Saving tool options after using dialog");
QDomElement oldDomElement = doc->elementById(id);
QDomElement oldDomElement = doc->elementById(id, getTagName());
if (oldDomElement.isElement())
{
QDomElement newDomElement = oldDomElement.cloneNode().toElement();
@ -138,7 +138,7 @@ void VDrawTool::AddToFile()
void VDrawTool::SaveOption(QSharedPointer<VGObject> &obj)
{
qCDebug(vTool, "Saving tool options");
QDomElement oldDomElement = doc->elementById(id);
QDomElement oldDomElement = doc->elementById(id, getTagName());
if (oldDomElement.isElement())
{
QDomElement newDomElement = oldDomElement.cloneNode().toElement();
@ -179,7 +179,7 @@ bool VDrawTool::CorrectDisable(bool disable, const QString &namePP) const
//---------------------------------------------------------------------------------------------------------------------
void VDrawTool::ReadAttributes()
{
const QDomElement domElement = doc->elementById(id);
const QDomElement domElement = doc->elementById(id, getTagName());
if (domElement.isElement())
{
ReadToolAttributes(domElement);

View File

@ -90,7 +90,7 @@ void VAbstractNode::incrementReferens()
doc->IncrementReferens(node->getIdTool());
}
ShowNode();
QDomElement domElement = doc->elementById(id);
QDomElement domElement = doc->elementById(id, getTagName());
if (domElement.isElement())
{
doc->SetParametrUsage(domElement, AttrInUse, NodeUsage::InUse);
@ -117,7 +117,7 @@ void VAbstractNode::decrementReferens()
doc->DecrementReferens(node->getIdTool());
}
HideNode();
QDomElement domElement = doc->elementById(id);
QDomElement domElement = doc->elementById(id, getTagName());
if (domElement.isElement())
{
doc->SetParametrUsage(domElement, AttrInUse, NodeUsage::NotInUse);

View File

@ -230,7 +230,7 @@ void VNodePoint::NameChangePosition(const QPointF &pos)
*/
void VNodePoint::UpdateNamePosition(qreal mx, qreal my)
{
QDomElement domElement = doc->elementById(id);
QDomElement domElement = doc->elementById(id, getTagName());
if (domElement.isElement())
{
doc->SetAttribute(domElement, AttrMx, QString().setNum(qApp->fromPixel(mx)));

View File

@ -146,7 +146,7 @@ void VToolPiecePath::incrementReferens()
IncrementNodes(VAbstractTool::data.GetPiecePath(id));
}
ShowNode();
QDomElement domElement = doc->elementById(id);
QDomElement domElement = doc->elementById(id, getTagName());
if (domElement.isElement())
{
doc->SetParametrUsage(domElement, AttrInUse, NodeUsage::InUse);
@ -169,7 +169,7 @@ void VToolPiecePath::decrementReferens()
DecrementNodes(VAbstractTool::data.GetPiecePath(id));
}
HideNode();
QDomElement domElement = doc->elementById(id);
QDomElement domElement = doc->elementById(id, getTagName());
if (domElement.isElement())
{
doc->SetParametrUsage(domElement, AttrInUse, NodeUsage::NotInUse);

View File

@ -857,7 +857,7 @@ void VToolSeamAllowance::AddToFile()
//---------------------------------------------------------------------------------------------------------------------
void VToolSeamAllowance::RefreshDataInFile()
{
QDomElement domElement = doc->elementById(id);
QDomElement domElement = doc->elementById(id, getTagName());
if (domElement.isElement())
{
// Refresh only parts that we possibly need to update

View File

@ -97,7 +97,7 @@ namespace
//---------------------------------------------------------------------------------------------------------------------
VPiecePath GetPiecePath(int piece, VAbstractPattern *doc, quint32 id)
{
const QDomElement tool = doc->elementById(id);
const QDomElement tool = doc->elementById(id, VAbstractPattern::TagTools);
if (tool.isNull())
{
VException e(QString("Can't get tool by id='%1'.").arg(id));
@ -140,7 +140,7 @@ VPiecePath GetPiece2MainPath(VAbstractPattern *doc, quint32 id)
//---------------------------------------------------------------------------------------------------------------------
QVector<CustomSARecord> GetPiece2CSAPaths(VAbstractPattern *doc, quint32 id)
{
const QDomElement tool = doc->elementById(id);
const QDomElement tool = doc->elementById(id, VAbstractPattern::TagTools);
if (tool.isNull())
{
VException e(QString("Can't get tool by id='%1'.").arg(id));
@ -171,7 +171,7 @@ QVector<CustomSARecord> GetPiece2CSAPaths(VAbstractPattern *doc, quint32 id)
//---------------------------------------------------------------------------------------------------------------------
QVector<quint32> GetPiece2InternalPaths(VAbstractPattern *doc, quint32 id)
{
const QDomElement tool = doc->elementById(id);
const QDomElement tool = doc->elementById(id, VAbstractPattern::TagTools);
if (tool.isNull())
{
VException e(QString("Can't get tool by id='%1'.").arg(id));
@ -202,7 +202,7 @@ QVector<quint32> GetPiece2InternalPaths(VAbstractPattern *doc, quint32 id)
//---------------------------------------------------------------------------------------------------------------------
QVector<quint32> GetPiece2Pins(VAbstractPattern *doc, quint32 id)
{
const QDomElement tool = doc->elementById(id);
const QDomElement tool = doc->elementById(id, VAbstractPattern::TagTools);
if (tool.isNull())
{
VException e(QString("Can't get tool by id='%1'.").arg(id));
@ -233,13 +233,13 @@ QVector<quint32> GetPiece2Pins(VAbstractPattern *doc, quint32 id)
//---------------------------------------------------------------------------------------------------------------------
QString DrawName(VAbstractPattern *doc, quint32 d1id, quint32 d2id)
{
const QDomElement detail1 = doc->elementById(d1id);
const QDomElement detail1 = doc->elementById(d1id, VAbstractPattern::TagDetail);
if (detail1.isNull())
{
return QString();
}
const QDomElement detail2 = doc->elementById(d2id);
const QDomElement detail2 = doc->elementById(d2id, VAbstractPattern::TagDetail);
if (detail2.isNull())
{
return QString();
@ -656,7 +656,7 @@ void FindIndexJ(qint32 pointsD2, const VPiecePath &d2Path, quint32 indexD2, qint
//---------------------------------------------------------------------------------------------------------------------
QDomElement GetTagChildren(VAbstractPattern *doc, quint32 id)
{
QDomElement toolUnion = doc->elementById(id);
QDomElement toolUnion = doc->elementById(id, VAbstractPattern::TagTools);
if (toolUnion.isNull())
{
VException e(QString("Can't get tool by id='%1'.").arg(id));
@ -717,7 +717,7 @@ void SavePinsChildren(VAbstractPattern *doc, quint32 id, const QVector<quint32>
//---------------------------------------------------------------------------------------------------------------------
QVector<quint32> GetChildren(VAbstractPattern *doc, quint32 id, const QString &tagName)
{
const QDomElement toolUnion = doc->elementById(id);
const QDomElement toolUnion = doc->elementById(id, VAbstractPattern::TagTools);
if (toolUnion.isNull())
{
return QVector<quint32>();
@ -1390,7 +1390,7 @@ void VToolUnionDetails::incrementReferens()
doc->IncrementReferens(objects.at(i));
}
QDomElement domElement = doc->elementById(id);
QDomElement domElement = doc->elementById(id, getTagName());
if (domElement.isElement())
{
doc->SetParametrUsage(domElement, AttrInUse, NodeUsage::InUse);
@ -1410,7 +1410,7 @@ void VToolUnionDetails::decrementReferens()
doc->DecrementReferens(objects.at(i));
}
QDomElement domElement = doc->elementById(id);
QDomElement domElement = doc->elementById(id, getTagName());
if (domElement.isElement())
{
doc->SetParametrUsage(domElement, AttrInUse, NodeUsage::NotInUse);
@ -1558,7 +1558,7 @@ void VToolUnionDetails::AddToModeling(const QDomElement &domElement)
QVector<quint32> VToolUnionDetails::GetReferenceObjects() const
{
QVector<quint32> list;
const QDomElement tool = doc->elementById(id);
const QDomElement tool = doc->elementById(id, getTagName());
if (tool.isNull())
{
return list;

View File

@ -61,7 +61,7 @@ void AddGroup::undo()
QDomElement groups = doc->CreateGroups();
if (not groups.isNull())
{
QDomElement group = doc->elementById(nodeId);
QDomElement group = doc->elementById(nodeId, VAbstractPattern::TagGroup);
if (group.isElement())
{
group.setAttribute(VAbstractPattern::AttrVisible, trueStr);

View File

@ -53,7 +53,7 @@ void AddPiece::undo()
QDomElement details = GetDetailsSection();
if (not details.isNull())
{
QDomElement domElement = doc->elementById(nodeId);
QDomElement domElement = doc->elementById(nodeId, VAbstractPattern::TagDetail);
if (domElement.isElement())
{
if (details.removeChild(domElement).isNull())

View File

@ -50,7 +50,7 @@ DeletePiece::DeletePiece(VAbstractPattern *doc, quint32 id, const VPiece &detail
{
setText(tr("delete tool"));
nodeId = id;
QDomElement domElement = doc->elementById(id);
QDomElement domElement = doc->elementById(id, VAbstractPattern::TagDetail);
if (domElement.isElement())
{
xml = domElement.cloneNode().toElement();
@ -91,7 +91,7 @@ void DeletePiece::redo()
{
qCDebug(vUndo, "Redo.");
QDomElement domElement = doc->elementById(nodeId);
QDomElement domElement = doc->elementById(nodeId, VAbstractPattern::TagDetail);
if (domElement.isElement())
{
m_parentNode.removeChild(domElement);

View File

@ -86,7 +86,7 @@ void DelGroup::redo()
QDomElement groups = doc->CreateGroups();
if (not groups.isNull())
{
QDomElement group = doc->elementById(nodeId);
QDomElement group = doc->elementById(nodeId, VAbstractPattern::TagGroup);
if (group.isElement())
{
group.setAttribute(VAbstractPattern::AttrVisible, trueStr);

View File

@ -54,7 +54,7 @@ MoveDoubleLabel::MoveDoubleLabel(VAbstractPattern *doc, const double &x, const d
setText(tr("move the second dart label"));
}
const QDomElement domElement = doc->elementById(m_idTool);
const QDomElement domElement = doc->elementById(m_idTool, VAbstractPattern::TagPoint);
if (domElement.isElement())
{
if (type == DoublePoint::FirstPoint)
@ -134,7 +134,7 @@ void MoveDoubleLabel::Do(double mx, double my)
qCDebug(vUndo, "New my2 %f", my);
}
QDomElement domElement = doc->elementById(m_idTool);
QDomElement domElement = doc->elementById(m_idTool, VAbstractPattern::TagPoint);
if (domElement.isElement())
{
if (m_type == DoublePoint::FirstPoint)

View File

@ -44,7 +44,7 @@ MoveLabel::MoveLabel(VAbstractPattern *doc, const double &x, const double &y, co
{
setText(tr("move point label"));
QDomElement domElement = doc->elementById(nodeId);
QDomElement domElement = doc->elementById(nodeId, VAbstractPattern::TagPoint);
if (domElement.isElement())
{
m_oldMx = qApp->toPixel(doc->GetParametrDouble(domElement, AttrMx, "0.0"));
@ -95,7 +95,7 @@ void MoveLabel::Do(double mx, double my)
qCDebug(vUndo, "New mx %f", mx);
qCDebug(vUndo, "New my %f", my);
QDomElement domElement = doc->elementById(nodeId);
QDomElement domElement = doc->elementById(nodeId, VAbstractPattern::TagPoint);
if (domElement.isElement())
{
doc->SetAttribute(domElement, AttrMx, QString().setNum(qApp->fromPixel(mx)));

View File

@ -118,7 +118,7 @@ void OperationMoveLabel::Do(double mx, double my)
//---------------------------------------------------------------------------------------------------------------------
QDomElement OperationMoveLabel::GetDestinationObject(quint32 idTool, quint32 idPoint) const
{
const QDomElement tool = doc->elementById(idTool);
const QDomElement tool = doc->elementById(idTool, VAbstractPattern::TagOperation);
if (tool.isElement())
{
QDomElement correctDest;

View File

@ -52,7 +52,7 @@ MovePiece::MovePiece(VAbstractPattern *doc, const double &x, const double &y, co
nodeId = id;
SCASSERT(scene != nullptr)
QDomElement domElement = doc->elementById(id);
QDomElement domElement = doc->elementById(id, VAbstractPattern::TagDetail);
if (domElement.isElement())
{
m_oldX = qApp->toPixel(doc->GetParametrDouble(domElement, AttrMx, "0.0"));
@ -74,7 +74,7 @@ void MovePiece::undo()
{
qCDebug(vUndo, "Undo.");
QDomElement domElement = doc->elementById(nodeId);
QDomElement domElement = doc->elementById(nodeId, VAbstractPattern::TagDetail);
if (domElement.isElement())
{
SaveCoordinates(domElement, m_oldX, m_oldY);
@ -93,7 +93,7 @@ void MovePiece::redo()
{
qCDebug(vUndo, "Redo.");
QDomElement domElement = doc->elementById(nodeId);
QDomElement domElement = doc->elementById(nodeId, VAbstractPattern::TagDetail);
if (domElement.isElement())
{
SaveCoordinates(domElement, m_newX, m_newY);

View File

@ -101,7 +101,7 @@ int MoveSpline::id() const
//---------------------------------------------------------------------------------------------------------------------
void MoveSpline::Do(const VSpline &spl)
{
QDomElement domElement = doc->elementById(nodeId);
QDomElement domElement = doc->elementById(nodeId, VAbstractPattern::TagSpline);
if (domElement.isElement())
{
doc->SetAttribute(domElement, AttrPoint1, spl.GetP1().id());

View File

@ -100,7 +100,7 @@ int MoveSplinePath::id() const
//---------------------------------------------------------------------------------------------------------------------
void MoveSplinePath::Do(const VSplinePath &splPath)
{
QDomElement domElement = doc->elementById(nodeId);
QDomElement domElement = doc->elementById(nodeId, VAbstractPattern::TagSpline);
if (domElement.isElement())
{
VToolSplinePath::UpdatePathPoints(doc, domElement, splPath);

View File

@ -50,7 +50,7 @@ MoveSPoint::MoveSPoint(VAbstractPattern *doc, const double &x, const double &y,
qCDebug(vUndo, "SPoint newY %f", newY);
SCASSERT(scene != nullptr)
QDomElement domElement = doc->elementById(id);
QDomElement domElement = doc->elementById(id, VAbstractPattern::TagPoint);
if (domElement.isElement())
{
oldX = qApp->toPixel(doc->GetParametrDouble(domElement, AttrX, "0.0"));
@ -120,7 +120,7 @@ void MoveSPoint::Do(double x, double y)
qCDebug(vUndo, "Move to x %f", x);
qCDebug(vUndo, "Move to y %f", y);
QDomElement domElement = doc->elementById(nodeId);
QDomElement domElement = doc->elementById(nodeId, VAbstractPattern::TagPoint);
if (domElement.isElement())
{
doc->SetAttribute(domElement, AttrX, QString().setNum(qApp->fromPixel(x)));

View File

@ -64,7 +64,7 @@ void SavePieceOptions::undo()
{
qCDebug(vUndo, "Undo.");
QDomElement domElement = doc->elementById(nodeId);
QDomElement domElement = doc->elementById(nodeId, VAbstractPattern::TagDetail);
if (domElement.isElement())
{
VToolSeamAllowance::AddAttributes(doc, domElement, nodeId, m_oldDet);
@ -95,7 +95,7 @@ void SavePieceOptions::redo()
{
qCDebug(vUndo, "Redo.");
QDomElement domElement = doc->elementById(nodeId);
QDomElement domElement = doc->elementById(nodeId, VAbstractPattern::TagDetail);
if (domElement.isElement())
{
VToolSeamAllowance::AddAttributes(doc, domElement, nodeId, m_newDet);

View File

@ -58,7 +58,7 @@ void SavePiecePathOptions::undo()
{
qCDebug(vUndo, "Undo.");
QDomElement domElement = doc->elementById(nodeId);
QDomElement domElement = doc->elementById(nodeId, VAbstractPattern::TagPath);
if (domElement.isElement())
{
VToolPiecePath::AddAttributes(doc, domElement, nodeId, m_oldPath);
@ -82,7 +82,7 @@ void SavePiecePathOptions::redo()
{
qCDebug(vUndo, "Redo.");
QDomElement domElement = doc->elementById(nodeId);
QDomElement domElement = doc->elementById(nodeId, VAbstractPattern::TagPath);
if (domElement.isElement())
{
VToolPiecePath::AddAttributes(doc, domElement, nodeId, m_newPath);

View File

@ -101,7 +101,7 @@ bool TogglePieceInLayout::getNewState() const
//---------------------------------------------------------------------------------------------------------------------
void TogglePieceInLayout::Do(bool state)
{
QDomElement detail = doc->elementById(m_id);
QDomElement detail = doc->elementById(m_id, VAbstractPattern::TagDetail);
if (detail.isElement())
{
if (state == false)