From 7df6404a744a9002c5df0b33db96e81955a54e27 Mon Sep 17 00:00:00 2001 From: vsv Date: Fri, 22 Apr 2016 12:16:17 +0300 Subject: [PATCH] Optimization of the widget code --- .../ParametersPlugin_WidgetParamsMgr.cpp | 91 ++++++------------- .../ParametersPlugin_WidgetParamsMgr.h | 3 +- 2 files changed, 28 insertions(+), 66 deletions(-) diff --git a/src/ParametersPlugin/ParametersPlugin_WidgetParamsMgr.cpp b/src/ParametersPlugin/ParametersPlugin_WidgetParamsMgr.cpp index 183708fdb..508303bba 100644 --- a/src/ParametersPlugin/ParametersPlugin_WidgetParamsMgr.cpp +++ b/src/ParametersPlugin/ParametersPlugin_WidgetParamsMgr.cpp @@ -247,47 +247,21 @@ void ParametersPlugin_WidgetParamsMgr::activateCustom() myParameters->addChild(aItem); myParametersList.append(aParamFeature); - - // Set features where the parameter is used - const std::set>& aRefs = aParam->data()->refsToMe(); - std::set >::const_iterator aIt; - for(aIt = aRefs.cbegin(); aIt != aRefs.cend(); aIt++) { - std::shared_ptr aAttr = (*aIt); - FeaturePtr aReferenced = std::dynamic_pointer_cast(aAttr->owner()); - if (aReferenced.get()) { - QStringList aValNames; - aValNames << aReferenced->data()->name().c_str(); - - AttributeDoublePtr aDouble = std::dynamic_pointer_cast(aAttr); - if (aDouble.get()) { - aValNames << aDouble->text().c_str(); - aValNames << QString::number(aDouble->value()); - } else { - AttributeIntegerPtr aInt = std::dynamic_pointer_cast(aAttr); - if (aInt.get()) { - aValNames << aInt->text().c_str(); - aValNames << QString::number(aInt->value()); - } - } - - aItem = new QTreeWidgetItem(aValNames); - myFeatures->addChild(aItem); - } - } } } + updateFeaturesPart(); + myFeatures->setExpanded(true); myParameters->setExpanded(true); } -void ParametersPlugin_WidgetParamsMgr::rebuildFeatures() +QList ParametersPlugin_WidgetParamsMgr:: + featuresItems(const QList& theFeatures) const { - myFeatures->takeChildren(); // Clear list - + QList aItemsList; ResultParameterPtr aParam; - QTreeWidgetItem* aItem; - foreach(FeaturePtr aParameter, myParametersList) { + foreach(FeaturePtr aParameter, theFeatures) { aParam = std::dynamic_pointer_cast(aParameter->firstResult()); const std::set>& aRefs = aParam->data()->refsToMe(); std::set >::const_iterator aIt; @@ -309,12 +283,11 @@ void ParametersPlugin_WidgetParamsMgr::rebuildFeatures() aValNames << QString::number(aInt->value()); } } - - aItem = new QTreeWidgetItem(aValNames); - myFeatures->addChild(aItem); + aItemsList.append(aValNames); } } } + return aItemsList; } @@ -393,36 +366,24 @@ void ParametersPlugin_WidgetParamsMgr::onCloseEditor(QWidget* theEditor, void ParametersPlugin_WidgetParamsMgr::updateFeaturesPart() { - std::shared_ptr aParam; + QList aFeaturesList = featuresItems(myParametersList); + if (aFeaturesList.count() != myFeatures->childCount()) { + if (myFeatures->childCount() < aFeaturesList.count()) { + while (myFeatures->childCount() != aFeaturesList.count()) + myFeatures->addChild(new QTreeWidgetItem()); + } else { + while (myFeatures->childCount() != aFeaturesList.count()) + myFeatures->removeChild(myFeatures->child(myFeatures->childCount() - 1)); + } + } int i = 0; - foreach(FeaturePtr aFeature, myParametersList) { - aParam = std::dynamic_pointer_cast(aFeature->firstResult()); - const std::set>& aRefs = aParam->data()->refsToMe(); - std::set >::const_iterator aIt; - for(aIt = aRefs.cbegin(); aIt != aRefs.cend(); aIt++) { - std::shared_ptr aAttr = (*aIt); - FeaturePtr aReferenced = std::dynamic_pointer_cast(aAttr->owner()); - if (aReferenced.get()) { - QStringList aValNames; - aValNames << aReferenced->data()->name().c_str(); - - AttributeDoublePtr aDouble = std::dynamic_pointer_cast(aAttr); - if (aDouble.get()) { - aValNames << aDouble->text().c_str(); - aValNames << QString::number(aDouble->value()); - } else { - AttributeIntegerPtr aInt = std::dynamic_pointer_cast(aAttr); - if (aInt.get()) { - aValNames << aInt->text().c_str(); - aValNames << QString::number(aInt->value()); - } - } - - QTreeWidgetItem* aItem = myFeatures->child(i++); - for(int i = 0; i < aValNames.count(); i++) - aItem->setText(i, aValNames.at(i)); - } + foreach(QStringList aFeature, aFeaturesList) { + int aCol = 0; + foreach(QString aText, aFeature) { + myFeatures->child(i)->setText(aCol, aText); + aCol++; } + i++; } } @@ -459,6 +420,8 @@ void ParametersPlugin_WidgetParamsMgr::onAdd() return; QTreeWidgetItem* aItem = createNewItem(); + if (aFeature->name().length() > 0) + aItem->setText(Col_Name, aFeature->name().c_str()); myParameters->addChild(aItem); myParametersList.append(aFeature); @@ -543,7 +506,7 @@ void ParametersPlugin_WidgetParamsMgr::onRemove() } Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY)); - rebuildFeatures(); + updateFeaturesPart(); } } diff --git a/src/ParametersPlugin/ParametersPlugin_WidgetParamsMgr.h b/src/ParametersPlugin/ParametersPlugin_WidgetParamsMgr.h index ff4d5abb0..9f9fda3b0 100644 --- a/src/ParametersPlugin/ParametersPlugin_WidgetParamsMgr.h +++ b/src/ParametersPlugin/ParametersPlugin_WidgetParamsMgr.h @@ -96,8 +96,7 @@ private: /// Returns true if parameter with the given name already exists bool hasName(const QString& theName) const; - /// Clear and rebuild children of Features item - void rebuildFeatures(); + QList featuresItems(const QList& theFeatures) const; QTreeWidget* myTable; QTreeWidgetItem* myFeatures; -- 2.39.2