X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=src%2FParametersPlugin%2FParametersPlugin_WidgetParamsMgr.cpp;h=9a8061a06b58e23957a11edbc58fecc488ec6131;hb=06e7f5859095193fc7f498bd89a7d28009794f53;hp=a0d88f734ee51265ee2c33fa6c151e463e8f2133;hpb=f2337211db0c2b449dd065dd195ffe76d5fd7116;p=modules%2Fshaper.git diff --git a/src/ParametersPlugin/ParametersPlugin_WidgetParamsMgr.cpp b/src/ParametersPlugin/ParametersPlugin_WidgetParamsMgr.cpp index a0d88f734..9a8061a06 100644 --- a/src/ParametersPlugin/ParametersPlugin_WidgetParamsMgr.cpp +++ b/src/ParametersPlugin/ParametersPlugin_WidgetParamsMgr.cpp @@ -1,13 +1,28 @@ -// Copyright (C) 2014-20xx CEA/DEN, EDF R&D --> - -// File: ParametersPlugin_WidgetParamsMgr.cpp -// Created: 11 Apr 2016 -// Author: Vitaly SMETANNIKOV +// Copyright (C) 2014-2023 CEA, EDF +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// +// This library 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 +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +// #include "ParametersPlugin_WidgetParamsMgr.h" #include "ParametersPlugin_Parameter.h" #include "ParametersPlugin_Validators.h" +#include + #include #include #include @@ -15,19 +30,35 @@ #include #include #include +#include +#include + +#include +#include #include +#include +#include #include +#include #include -#include #include #include #include #include #include #include +#include +#include +#include +#include +#include + +#ifdef WIN32 +#pragma warning(disable : 4456) // for nested foreach +#endif enum ColumnType { Col_Name, @@ -39,22 +70,39 @@ enum ColumnType { const char* NoName = ""; const char* NoValue = ""; +/*! + * \ingroup GUI + * ItemDelegate object in order to redefine items behavior + */ class ParametersPlugin_ItemDelegate : public QStyledItemDelegate { public: - ParametersPlugin_ItemDelegate(QObject* thaParent) : + /// Constructor + /// \param thaParent a parent + ParametersPlugin_ItemDelegate(QObject* thaParent) : QStyledItemDelegate(thaParent) {} - virtual void paint(QPainter* painter, - const QStyleOptionViewItem& option, + /// Redefinition of virtual method + /// \param painter a painter object + /// \param option the item options + /// \param index the current index + virtual void paint(QPainter* painter, + const QStyleOptionViewItem& option, const QModelIndex& index ) const; - - virtual QWidget* createEditor(QWidget* parent, - const QStyleOptionViewItem& option, + + /// Redefinition of virtual method + /// \param parent a parent widget + /// \param option the item options + /// \param index the current index + virtual QWidget* createEditor(QWidget* parent, + const QStyleOptionViewItem& option, const QModelIndex& index) const; + /// Returns True if the given index is editable item + /// \param theIndex an item index bool isEditable(const QModelIndex& theIndex) const; + /// Returns currently editing index QModelIndex editIndex() const { return myEditingIdx; } private: @@ -72,16 +120,18 @@ bool ParametersPlugin_ItemDelegate::isEditable(const QModelIndex& theIndex) cons return true; } -void ParametersPlugin_ItemDelegate::paint(QPainter* painter, - const QStyleOptionViewItem& option, +void ParametersPlugin_ItemDelegate::paint(QPainter* painter, + const QStyleOptionViewItem& option, const QModelIndex& index ) const { QBrush aBrush = painter->brush(); QPen aPen = painter->pen(); - if (!isEditable(index)) + //if (!isEditable(index)) + // painter->setBrush(Qt::lightGray); + if (!index.parent().isValid()) painter->setBrush(Qt::lightGray); - painter->setPen(Qt::darkGray); + painter->setPen(Qt::lightGray); painter->drawRect(option.rect); painter->setPen(aPen); @@ -89,26 +139,63 @@ void ParametersPlugin_ItemDelegate::paint(QPainter* painter, painter->setBrush(aBrush); } -QWidget* ParametersPlugin_ItemDelegate::createEditor(QWidget* parent, - const QStyleOptionViewItem& option, +QWidget* ParametersPlugin_ItemDelegate::createEditor(QWidget* parent, + const QStyleOptionViewItem& option, const QModelIndex& index) const { myEditingIdx = index; return QStyledItemDelegate::createEditor(parent, option, index); } +///////////////////////////////////////////////////////////////////////////////////////////////// +void ParametersPlugin_TreeWidget::closeEditor(QWidget* theEditor, + QAbstractItemDelegate::EndEditHint theHint) +{ + if (theHint == QAbstractItemDelegate::EditNextItem) { + QModelIndex aCurrent = currentIndex(); + QModelIndex aParent = model()->index(0, 0); + int aNbRows = model()->rowCount(aParent); + QModelIndex aIdx; + switch (aCurrent.column()) { + case 0: + aIdx = model()->index(aCurrent.row(), 1, aParent); + break; + case 1: + if (aCurrent.row() < (aNbRows - 1)) + aIdx = model()->index(aCurrent.row() + 1, 0, aParent); + else { + QTreeWidget::closeEditor(theEditor, QAbstractItemDelegate::NoHint); + return; + } + break; + case 3: + QTreeWidget::closeEditor(theEditor, theHint); + return; + } + if (aIdx.isValid()) { + QTreeWidget::closeEditor(theEditor, QAbstractItemDelegate::NoHint); + setCurrentIndex(aIdx); + edit(aIdx); + return; + } + } + QTreeWidget::closeEditor(theEditor, theHint); +} ///////////////////////////////////////////////////////////////////////////////////////////////// -ParametersPlugin_WidgetParamsMgr::ParametersPlugin_WidgetParamsMgr(QWidget* theParent, const Config_WidgetAPI* theData) - : ModuleBase_ModelWidget(theParent, theData) +ParametersPlugin_WidgetParamsMgr::ParametersPlugin_WidgetParamsMgr(QWidget* theParent, + const Config_WidgetAPI* theData, ModuleBase_IWorkshop* theWorkshop) + : ModuleBase_ModelDialogWidget(theParent, theData), + myWorkshop(theWorkshop), isUpplyBlocked(false) { QVBoxLayout* aLayout = new QVBoxLayout(this); - myTable = new QTreeWidget(this); + myTable = new ParametersPlugin_TreeWidget(this); myTable->setColumnCount(4); QStringList aHeaders; - aHeaders << tr("Name") << tr("Equation") << tr("Result") << tr("Comment"); + aHeaders << translate("Name") << translate("Expression") + << translate("Result") << translate("Comment"); myTable->setHeaderLabels(aHeaders); myTable->setColumnWidth(Col_Name, 200); myTable->setColumnWidth(Col_Equation, 100); @@ -123,7 +210,7 @@ ParametersPlugin_WidgetParamsMgr::ParametersPlugin_WidgetParamsMgr(QWidget* theP connect(myTable, SIGNAL(itemSelectionChanged()), SLOT(onSelectionChanged())); myDelegate = new ParametersPlugin_ItemDelegate(myTable); - connect(myDelegate, SIGNAL(closeEditor(QWidget*, QAbstractItemDelegate::EndEditHint)), + connect(myDelegate, SIGNAL(closeEditor(QWidget*, QAbstractItemDelegate::EndEditHint)), SLOT(onCloseEditor(QWidget*, QAbstractItemDelegate::EndEditHint))); myTable->setItemDelegate(myDelegate); @@ -131,13 +218,15 @@ ParametersPlugin_WidgetParamsMgr::ParametersPlugin_WidgetParamsMgr(QWidget* theP // Define root nodes QStringList aNames; - aNames<setFlags(Qt::ItemIsEnabled); myTable->addTopLevelItem(myParameters); aNames.clear(); - aNames<setFlags(Qt::ItemIsEnabled); myTable->addTopLevelItem(myFeatures); QHBoxLayout* aBtnLayout = new QHBoxLayout(this); @@ -154,15 +243,26 @@ ParametersPlugin_WidgetParamsMgr::ParametersPlugin_WidgetParamsMgr(QWidget* theP aBtnLayout->addStretch(); - myAddBtn = new QPushButton(tr("Add"), this); + myAddBtn = new QPushButton(translate("Add"), this); connect(myAddBtn, SIGNAL(clicked(bool)), SLOT(onAdd())); aBtnLayout->addWidget(myAddBtn); - myInsertBtn = new QPushButton(tr("Insert"), this); + QString aAddStr(Config_PropManager::string("Shortcuts", "add_parameter_shortcut").c_str()); + if (aAddStr.isEmpty()) + aAddStr = "Ctrl+A"; + + QShortcut* aAddShc = new QShortcut(QKeySequence(aAddStr), myAddBtn); + connect(aAddShc, SIGNAL(activated()), SLOT(onAdd())); + + myInsertBtn = new QPushButton(translate("Insert"), this); connect(myInsertBtn, SIGNAL(clicked(bool)), SLOT(onInsert())); aBtnLayout->addWidget(myInsertBtn); - myRemoveBtn = new QPushButton(tr("Remove"), this); + myImportBtn = new QPushButton(translate("Import file"), this); + connect(myImportBtn, SIGNAL(clicked(bool)), SLOT(onImport())); + aBtnLayout->addWidget(myImportBtn); + + myRemoveBtn = new QPushButton(translate("Remove"), this); connect(myRemoveBtn, SIGNAL(clicked(bool)), SLOT(onRemove())); aBtnLayout->addWidget(myRemoveBtn); @@ -171,6 +271,20 @@ ParametersPlugin_WidgetParamsMgr::ParametersPlugin_WidgetParamsMgr(QWidget* theP onSelectionChanged(); } +void ParametersPlugin_WidgetParamsMgr::setDialogButtons(QDialogButtonBox* theButtons) +{ + ModuleBase_ModelDialogWidget::setDialogButtons(theButtons); + + QWidget* aBtnParentWgt = myOkCancelBtn->parentWidget(); + QHBoxLayout* aBtnParentLayout = dynamic_cast(aBtnParentWgt->layout()); + + QPushButton* aPreviewBtn = new QPushButton(translate("See preview"), aBtnParentWgt); + aBtnParentLayout->insertWidget(0, aPreviewBtn); + aBtnParentLayout->insertStretch(1, 1); + connect(aPreviewBtn, SIGNAL(clicked(bool)), SLOT(onShowPreview())); +} + + QList ParametersPlugin_WidgetParamsMgr::getControls() const { QList aList; @@ -184,7 +298,7 @@ void ParametersPlugin_WidgetParamsMgr::selectItemScroll(QTreeWidgetItem* aItem) QModelIndex aParent = myTable->model()->index(0, 0); int aChildIdx = myParameters->indexOfChild(aItem); QModelIndex aIndex = myTable->model()->index(aChildIdx, Col_Name, aParent); - myTable->selectionModel()->select(aIndex, + myTable->selectionModel()->select(aIndex, QItemSelectionModel::SelectCurrent | QItemSelectionModel::Rows); myTable->scrollToItem(aItem); } @@ -195,16 +309,21 @@ bool ParametersPlugin_WidgetParamsMgr::storeValueCustom() ParametersPlugin_ExpressionValidator aValidator; std::list aArgs; std::string aAttrId = ParametersPlugin_Parameter::VARIABLE_ID(); - std::string aErr; + Events_InfoMessage aErr; int aId = 0; foreach(FeaturePtr aFeature, myParametersList) { if (!aValidator.isValid(aFeature->attribute(aAttrId), aArgs, aErr)) { - QMessageBox::warning(this, tr("Warning"), aErr.c_str()); + QMessageBox::warning(this, translate("Warning"), aErr.messageString().c_str()); selectItemScroll(myParameters->child(aId)); return false; } aId++; } + // #2813 : make the current feature the latest in the document + std::list allFeatures = myFeature->document()->allFeatures(); + if (!allFeatures.empty()) { + myFeature->document()->setCurrentFeature(*(allFeatures.rbegin()), true); + } return true; } @@ -228,13 +347,12 @@ void ParametersPlugin_WidgetParamsMgr::updateParametersFeatures() myParametersList.clear(); FeaturePtr aFeature = feature(); DocumentPtr aDoc = aFeature->document(); - int aNbParam = aDoc->size(ModelAPI_ResultParameter::group()); ObjectPtr aObj; FeaturePtr aParamFeature; - for (int i = 0; i < aNbParam; i++) { - aObj = aDoc->object(ModelAPI_ResultParameter::group(), i); - aParamFeature = ModelAPI_Feature::feature(aObj); - if (aParamFeature.get() && (aParamFeature->getKind() == ParametersPlugin_Parameter::ID())) { + int aNbFeatures = aDoc->numInternalFeatures(); + for (int i = 0; i < aNbFeatures; i++) { + aParamFeature = aDoc->internalFeature(i); + if (aParamFeature && aParamFeature->getKind() == ParametersPlugin_Parameter::ID()) { myParametersList.append(aParamFeature); } } @@ -242,17 +360,20 @@ void ParametersPlugin_WidgetParamsMgr::updateParametersFeatures() void ParametersPlugin_WidgetParamsMgr::updateFeaturesPart() { - updateItem(myFeatures, featuresItems(myParametersList)); + QList aFeatureList; + updateItem(myFeatures, featuresItems(myParametersList, aFeatureList)); } void ParametersPlugin_WidgetParamsMgr::updateParametersPart() { updateItem(myParameters, parametersItems(myParametersList)); + bool aIsValid = isValid(); + enableButtons(aIsValid); } QList ParametersPlugin_WidgetParamsMgr:: - featuresItems(const QList& theFeatures) const + featuresItems(const QList& theFeatures, QList& theFeatureList) const { QList aItemsList; ResultParameterPtr aParam; @@ -263,29 +384,57 @@ QList ParametersPlugin_WidgetParamsMgr:: for(aIt = aRefs.cbegin(); aIt != aRefs.cend(); aIt++) { std::shared_ptr aAttr = (*aIt); FeaturePtr aReferenced = std::dynamic_pointer_cast(aAttr->owner()); - if (aReferenced.get()) { + if (aReferenced.get() && (aReferenced != aParameter)) { if (aReferenced->getKind() == ParametersPlugin_Parameter::ID()) { // Find referenced feature Recursive QList aList; aList.append(aReferenced); - QList aItems = featuresItems(aList); + QList aItems = featuresItems(aList, theFeatureList); aItemsList.append(aItems); } else { - 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(); + if (!theFeatureList.contains(aReferenced)) { + QStringList aValNames; + aValNames << QString::fromStdWString(aReferenced->data()->name()); + + std::string aId = aAttr->attributeType(); + if (aId == ModelAPI_AttributeDouble::typeId()) { + AttributeDoublePtr aDouble = + std::dynamic_pointer_cast(aAttr); + aValNames << QString::fromStdWString(aDouble->text()); + aValNames << QString::number(aDouble->value()); + } + else if (aId == ModelAPI_AttributeInteger::typeId()) { + AttributeIntegerPtr aInt = + std::dynamic_pointer_cast(aAttr); + aValNames << QString::fromStdWString(aInt->text()); aValNames << QString::number(aInt->value()); } + else if (aId == GeomDataAPI_Point::typeId()) { + std::shared_ptr aPnt = + std::dynamic_pointer_cast(aAttr); + + QString aExpr = QString("%1,%2,%3").arg(QString::fromStdWString(aPnt->textX())). + arg(QString::fromStdWString(aPnt->textY())). + arg(QString::fromStdWString(aPnt->textZ())); + aValNames << aExpr; + + QString aRes = QString("%1,%2,%3").arg(aPnt->x()).arg(aPnt->y()).arg(aPnt->z()); + aValNames << aRes; + } + else if (aId == GeomDataAPI_Point2D::typeId()) { + std::shared_ptr aPnt = + std::dynamic_pointer_cast(aAttr); + + QString aExpr = QString("%1,%2").arg(QString::fromStdWString(aPnt->textX())). + arg(QString::fromStdWString(aPnt->textY())); + aValNames << aExpr; + + QString aRes = QString("%1,%2").arg(aPnt->x()).arg(aPnt->y()); + aValNames << aRes; + } + aItemsList.append(aValNames); + theFeatureList.append(aReferenced); } - aItemsList.append(aValNames); } } } @@ -297,16 +446,32 @@ QList ParametersPlugin_WidgetParamsMgr:: QList ParametersPlugin_WidgetParamsMgr:: parametersItems(const QList& theFeatures) const { + std::list aArgs; + std::string aErr; QList aItemsList; foreach(FeaturePtr aParameter, theFeatures) { ResultPtr aParam = aParameter->firstResult(); QStringList aValues; - aValues << aParameter->string(ParametersPlugin_Parameter::VARIABLE_ID())->value().c_str(); - aValues << aParameter->string(ParametersPlugin_Parameter::EXPRESSION_ID())->value().c_str(); - - AttributeDoublePtr aValueAttribute = aParam->data()->real(ModelAPI_ResultParameter::VALUE()); - aValues << QString::number(aValueAttribute->value()); + std::string aName = aParameter->string(ParametersPlugin_Parameter::VARIABLE_ID())->value(); + if (aName.empty()) { + aValues << translate(NoName); + } else + aValues << aName.c_str(); + + std::string aExpr = aParameter->string(ParametersPlugin_Parameter::EXPRESSION_ID())->value(); + if (aName.empty()) { + aValues << translate(NoValue); + } else + aValues << aExpr.c_str(); + + aErr = aParameter->data()->string(ParametersPlugin_Parameter::EXPRESSION_ERROR_ID())->value(); + if (aErr.empty()) { + AttributeDoublePtr aValueAttribute = aParam->data()->real(ModelAPI_ResultParameter::VALUE()); + aValues << QString::number(aValueAttribute->value()); + } else { + aValues << aErr.c_str(); + } aValues << aParameter->string(ParametersPlugin_Parameter::COMMENT_ID())->value().c_str(); aItemsList.append(aValues); } @@ -317,16 +482,18 @@ QList ParametersPlugin_WidgetParamsMgr:: void ParametersPlugin_WidgetParamsMgr::onDoubleClick(const QModelIndex& theIndex) { if (myDelegate->isEditable(theIndex)) { + myTable->setCurrentIndex(theIndex); myTable->edit(theIndex); } } -void ParametersPlugin_WidgetParamsMgr::onCloseEditor(QWidget* theEditor, +void ParametersPlugin_WidgetParamsMgr::onCloseEditor(QWidget* theEditor, QAbstractItemDelegate::EndEditHint theHint) { FeaturePtr aFeature = myParametersList.at(myDelegate->editIndex().row()); QTreeWidgetItem* aItem = myParameters->child(myDelegate->editIndex().row()); int aColumn = myDelegate->editIndex().column(); + int aRow = myDelegate->editIndex().row(); QString aText = aItem->text(aColumn); bool isModified = false; @@ -335,39 +502,36 @@ void ParametersPlugin_WidgetParamsMgr::onCloseEditor(QWidget* theEditor, { AttributeStringPtr aStringAttr = aFeature->string(ParametersPlugin_Parameter::VARIABLE_ID()); if (!aText.isEmpty()) { - if (hasName(aText)) { - myMessage = tr("Name %1 already exists.").arg(aText); - if (aStringAttr->value().length() > 0) - aItem->setText(Col_Name, aStringAttr->value().c_str()); - else - aItem->setText(Col_Name, NoName); + while (aText.indexOf(" ") != -1) { + aText.replace(" ", ""); + } + if (hasName(aText, aRow)) { + myMessage = translate("Name '%1' already exists.").arg(aText); QTimer::singleShot(50, this, SLOT(sendWarning())); return; } aStringAttr->setValue(aText.toStdString()); isModified = true; - } else { - aItem->setText(Col_Name, aStringAttr->value().c_str()); } } break; case Col_Equation: { - AttributeStringPtr aStringAttr = aFeature->string(ParametersPlugin_Parameter::EXPRESSION_ID()); + AttributeStringPtr aStringAttr = + aFeature->string(ParametersPlugin_Parameter::EXPRESSION_ID()); if (!aText.isEmpty()) { if (aText != aStringAttr->value().c_str()) { aStringAttr->setValue(aText.toStdString()); + aFeature->execute(); isModified = true; } - } else { - aItem->setText(Col_Equation, aStringAttr->value().c_str()); } } break; case Col_Comment: { AttributeStringPtr aStringAttr = aFeature->string(ParametersPlugin_Parameter::COMMENT_ID()); - aStringAttr->setValue(aText.toStdString()); + aStringAttr->setValue(aText.toStdWString()); isModified = true; } break; @@ -380,27 +544,22 @@ void ParametersPlugin_WidgetParamsMgr::onCloseEditor(QWidget* theEditor, aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED)); aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_DELETED)); - ResultParameterPtr aResult = - std::dynamic_pointer_cast(aFeature->firstResult()); - if (aResult.get()) { - AttributeDoublePtr aValueAttribute = - aResult->data()->real(ModelAPI_ResultParameter::VALUE()); - aItem->setText(Col_Result, QString::number(aValueAttribute->value())); - } - if (aColumn == Col_Equation) + if (aColumn != Col_Comment) updateParametersPart(); updateFeaturesPart(); + + onSelectionChanged(); } -void ParametersPlugin_WidgetParamsMgr::updateItem(QTreeWidgetItem* theItem, +void ParametersPlugin_WidgetParamsMgr::updateItem(QTreeWidgetItem* theItem, const QList& theFeaturesList) { if (theFeaturesList.count() != theItem->childCount()) { if (theItem->childCount() < theFeaturesList.count()) { - while (theItem->childCount() != theFeaturesList.count()) - theItem->addChild(createNewItem()); + while (theItem->childCount() != theFeaturesList.count()) + theItem->addChild(createNewItem(theItem)); } else { - while (theItem->childCount() != theFeaturesList.count()) + while (theItem->childCount() != theFeaturesList.count()) theItem->removeChild(theItem->child(theItem->childCount() - 1)); } } @@ -408,8 +567,10 @@ void ParametersPlugin_WidgetParamsMgr::updateItem(QTreeWidgetItem* theItem, foreach(QStringList aFeature, theFeaturesList) { int aCol = 0; foreach(QString aText, aFeature) { - if (aText.length() > 0) + if (aText.length() > 0) { theItem->child(i)->setText(aCol, aText); + theItem->child(i)->setToolTip(aCol, aText); + } aCol++; } i++; @@ -430,14 +591,18 @@ FeaturePtr ParametersPlugin_WidgetParamsMgr::createParameter() const } -QTreeWidgetItem* ParametersPlugin_WidgetParamsMgr::createNewItem() const +QTreeWidgetItem* ParametersPlugin_WidgetParamsMgr::createNewItem(QTreeWidgetItem* theParent) const { QStringList aValues; - aValues << NoName; - aValues << NoValue; + aValues << translate(NoName); + aValues << translate(NoValue); QTreeWidgetItem* aItem = new QTreeWidgetItem(aValues); - aItem->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsEnabled); + if (theParent == myParameters) { + aItem->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsEnabled); + aItem->setForeground(2, Qt::darkGray); + } else + aItem->setFlags(Qt::NoItemFlags); return aItem; } @@ -448,13 +613,16 @@ void ParametersPlugin_WidgetParamsMgr::onAdd() if (!aFeature.get()) return; - QTreeWidgetItem* aItem = createNewItem(); - if (aFeature->name().length() > 0) - aItem->setText(Col_Name, aFeature->name().c_str()); - myParameters->addChild(aItem); myParametersList.append(aFeature); - + updateParametersPart(); + + QTreeWidgetItem* aItem = myParameters->child(myParameters->childCount() - 1); + myTable->scrollToItem(aItem); + myTable->setCurrentItem(aItem); + myTable->editItem(aItem); + + enableButtons(false); } QTreeWidgetItem* ParametersPlugin_WidgetParamsMgr::selectedItem() const @@ -484,7 +652,6 @@ void ParametersPlugin_WidgetParamsMgr::onInsert() if (!aNewFeature.get()) return; - QTreeWidgetItem* aItem = createNewItem(); int aCurrentPos = myParameters->indexOfChild(aCurrentItem); if (aCurrentPos == 0) { aDoc->moveFeature(aNewFeature, FeaturePtr()); @@ -492,8 +659,12 @@ void ParametersPlugin_WidgetParamsMgr::onInsert() FeaturePtr aCurFeature = myParametersList.at(aCurrentPos - 1); aDoc->moveFeature(aNewFeature, aCurFeature); } - myParametersList.insert(aCurrentPos, aNewFeature); - myParameters->insertChild(aCurrentPos, aItem); + updateParametersFeatures(); + updateParametersPart(); + + myTable->scrollToItem(aCurrentItem); + myTable->setCurrentItem(aCurrentItem); + myTable->editItem(aCurrentItem); } void ParametersPlugin_WidgetParamsMgr::onRemove() @@ -511,28 +682,16 @@ void ParametersPlugin_WidgetParamsMgr::onRemove() QObjectPtrList anObjects; anObjects.append(aCurFeature); - std::set aDirectRefFeatures, aIndirectRefFeatures; - ModuleBase_Tools::findReferences(anObjects, aDirectRefFeatures, aIndirectRefFeatures); - - bool doDeleteReferences = true; - if (ModuleBase_Tools::isDeleteFeatureWithReferences(anObjects, aDirectRefFeatures, - aIndirectRefFeatures, this, doDeleteReferences)) { - myParametersList.removeOne(aCurFeature); - myParameters->removeChild(aCurrentItem); + std::map > aReferences; + std::set aFeatures; + ModuleBase_Tools::convertToFeatures(anObjects, aFeatures); + ModelAPI_Tools::findAllReferences(aFeatures, aReferences); - std::set aFeaturesToDelete; - if (doDeleteReferences) { - aFeaturesToDelete = aDirectRefFeatures; - aFeaturesToDelete.insert(aIndirectRefFeatures.begin(), aIndirectRefFeatures.end()); - } - aDoc->removeFeature(aCurFeature); - std::set::const_iterator anIt = aFeaturesToDelete.begin(), - aLast = aFeaturesToDelete.end(); - for (; anIt != aLast; anIt++) { - FeaturePtr aFeature = (*anIt); - DocumentPtr aDoc = aFeature->document(); - aDoc->removeFeature(aFeature); - } + std::set aFeatureRefsToDelete; + if (ModuleBase_Tools::askToDelete(aFeatures, aReferences, this, aFeatureRefsToDelete)) { + if (!aFeatureRefsToDelete.empty()) + aFeatures.insert(aFeatureRefsToDelete.begin(), aFeatureRefsToDelete.end()); + ModelAPI_Tools::removeFeatures(aFeatures, false); Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_DELETED)); Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY)); @@ -542,46 +701,63 @@ void ParametersPlugin_WidgetParamsMgr::onRemove() } } +void ParametersPlugin_WidgetParamsMgr::onImport() +{ + std::string aWinText("Select txt file"); + std::string aFileType("Text files (*.txt);;All files (*.*)"); + QString aQPath = QFileDialog::getOpenFileName(nullptr, + aWinText.c_str(), "", + aFileType.c_str()); + if (aQPath.size() == 0) + return; + + std::string aPath(aQPath.toStdString()); + std::shared_ptr aMessage = + std::shared_ptr( + new ModelAPI_ImportParametersMessage(ModelAPI_ImportParametersMessage::eventId())); + aMessage->setFilename(aPath); + Events_Loop::loop()->send(aMessage); + + updateParametersFeatures(); + updateParametersPart(); +} + void ParametersPlugin_WidgetParamsMgr::onUp() { QTreeWidgetItem* aCurrentItem = selectedItem(); if (!aCurrentItem) return; - QString aName = aCurrentItem->text(0); - int aCurrentPos = myParameters->indexOfChild(aCurrentItem); if (aCurrentPos == 0) return; FeaturePtr aCurFeature = myParametersList.at(aCurrentPos); - std::string aNm = aCurFeature->data()->name(); - SessionPtr aMgr = ModelAPI_Session::get(); std::shared_ptr aDoc = aMgr->activeDocument(); - FeaturePtr aa = myParametersList.at(aCurrentPos - 1); - std::string aN = aa->data()->name(); - if (aCurrentPos == 1) aDoc->moveFeature(aCurFeature, FeaturePtr()); else aDoc->moveFeature(aCurFeature, myParametersList.at(aCurrentPos - 2)); - myParametersList.removeOne(aCurFeature); - myParametersList.insert(aCurrentPos - 1, aCurFeature); - - myParameters->removeChild(aCurrentItem); - myParameters->insertChild(aCurrentPos - 1, aCurrentItem); - - selectItemScroll(aCurrentItem); + // add the updated also the feature that goes down + Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_CREATED)); + static Events_ID EVENT_UPD = Events_Loop::loop()->eventByName(EVENT_OBJECT_UPDATED); + ModelAPI_EventCreator::get()->sendUpdated(myParametersList.at(aCurrentPos - 1), EVENT_UPD); + Events_Loop::loop()->flush(EVENT_UPD); + Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_CREATED)); + Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_DELETED)); + Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY)); + updateParametersFeatures(); + updateParametersPart(); + updateFeaturesPart(); - //Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_UPDATED)); - //Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_DELETED)); - //Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY)); - //updateParametersFeatures(); - //updateParametersPart(); - //updateFeaturesPart(); + if (aCurrentPos > 0) { + aCurrentItem = myParameters->child(aCurrentPos - 1); + myTable->setCurrentItem(aCurrentItem); + selectItemScroll(aCurrentItem); + } } void ParametersPlugin_WidgetParamsMgr::onDown() @@ -598,30 +774,32 @@ void ParametersPlugin_WidgetParamsMgr::onDown() SessionPtr aMgr = ModelAPI_Session::get(); std::shared_ptr aDoc = aMgr->activeDocument(); aDoc->moveFeature(aCurFeature, myParametersList.at(aCurrentPos + 1)); + // add the updated also the feature that goes up + static Events_ID EVENT_UPD = Events_Loop::loop()->eventByName(EVENT_OBJECT_UPDATED); + ModelAPI_EventCreator::get()->sendUpdated(myParametersList.at(aCurrentPos + 1), EVENT_UPD); + + Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_CREATED)); + Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_UPDATED)); + Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_CREATED)); + Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_DELETED)); + Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY)); + updateParametersFeatures(); + updateParametersPart(); + updateFeaturesPart(); - myParametersList.removeOne(aCurFeature); - myParametersList.insert(aCurrentPos + 1, aCurFeature); - - myParameters->removeChild(aCurrentItem); - myParameters->insertChild(aCurrentPos + 1, aCurrentItem); - - selectItemScroll(aCurrentItem); - - //Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_UPDATED)); - //Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_DELETED)); - //Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY)); - //updateParametersFeatures(); - //updateParametersPart(); - //updateFeaturesPart(); + if (aCurrentPos < myParameters->childCount() - 1) { + aCurrentItem = myParameters->child(aCurrentPos + 1); + myTable->setCurrentItem(aCurrentItem); + selectItemScroll(aCurrentItem); + } } -bool ParametersPlugin_WidgetParamsMgr::hasName(const QString& theName) const +bool ParametersPlugin_WidgetParamsMgr::hasName(const QString& theName, int theIndex) const { - int aCurrent = myDelegate->editIndex().row(); int i = 0; foreach(FeaturePtr aFeature, myParametersList) { - if ((i != aCurrent) && (aFeature->data()->name() == theName.toStdString())) + if ((i != theIndex) && (aFeature->data()->name() == theName.toStdWString())) return true; i++; } @@ -630,21 +808,90 @@ bool ParametersPlugin_WidgetParamsMgr::hasName(const QString& theName) const void ParametersPlugin_WidgetParamsMgr::sendWarning() { - QMessageBox::warning(this, tr("Warning"), myMessage); + QMessageBox::warning(this, translate("Warning"), myMessage); + QTreeWidgetItem* aItem = myTable->currentItem(); + if (aItem) + myTable->editItem(aItem); } void ParametersPlugin_WidgetParamsMgr::onSelectionChanged() { QList aItemsList = myTable->selectedItems(); - bool isParameter = false; - foreach(QTreeWidgetItem* aItem, aItemsList) { - if (aItem->parent() == myParameters) { - isParameter = true; - break; + bool aIsValid = isValid(); + if (aIsValid) { + bool isParameter = false; + foreach(QTreeWidgetItem* aItem, aItemsList) { + if (aItem->parent() == myParameters) { + isParameter = true; + break; + } } + myInsertBtn->setEnabled(isParameter); + //myRemoveBtn->setEnabled(isParameter); + myUpBtn->setEnabled(isParameter); + myDownBtn->setEnabled(isParameter); + myImportBtn->setEnabled(true); + } else { + myInsertBtn->setEnabled(false); + //myRemoveBtn->setEnabled(false); + myUpBtn->setEnabled(false); + myDownBtn->setEnabled(false); + myImportBtn->setEnabled(false); } - myInsertBtn->setEnabled(isParameter); - myRemoveBtn->setEnabled(isParameter); - myUpBtn->setEnabled(isParameter); - myDownBtn->setEnabled(isParameter); + myRemoveBtn->setEnabled(!aItemsList.isEmpty()); +} + +void ParametersPlugin_WidgetParamsMgr::enableButtons(bool theEnable) +{ + myAddBtn->setEnabled(theEnable); + if (theEnable) + onSelectionChanged(); + else { + myInsertBtn->setEnabled(theEnable); + //myRemoveBtn->setEnabled(theEnable); + myUpBtn->setEnabled(theEnable); + myDownBtn->setEnabled(theEnable); + myImportBtn->setEnabled(theEnable); + } + myOkCancelBtn->button(QDialogButtonBox::Ok)->setEnabled(theEnable); +} + +bool ParametersPlugin_WidgetParamsMgr::isValid() +{ + QTreeWidgetItem* aItem; + for(int i = 0; i < myParameters->childCount(); i++) { + aItem = myParameters->child(i); + if ((aItem->text(Col_Name) == NoName) || + (aItem->text(Col_Equation) == translate(NoValue)) || + (!ModelAPI_Expression::isVariable(aItem->text(Col_Name).toStdString())) || + (hasName(aItem->text(Col_Name), i)) ) { + return false; + } + } + return true; +} + +void ParametersPlugin_WidgetParamsMgr::showEvent(QShowEvent* theEvent) +{ + ModuleBase_ModelDialogWidget::showEvent(theEvent); + SessionPtr aMgr = ModelAPI_Session::get(); + isUpplyBlocked = aMgr->isAutoUpdateBlocked(); + aMgr->blockAutoUpdate(true); + Events_Loop* aLoop = Events_Loop::loop(); + aLoop->flush(aLoop->eventByName(EVENT_AUTOMATIC_RECOMPUTATION_DISABLE)); +} + +void ParametersPlugin_WidgetParamsMgr::hideEvent(QHideEvent* theEvent) +{ + ModuleBase_ModelDialogWidget::hideEvent(theEvent); + SessionPtr aMgr = ModelAPI_Session::get(); + aMgr->blockAutoUpdate(isUpplyBlocked); +} + +void ParametersPlugin_WidgetParamsMgr::onShowPreview() +{ + SessionPtr aMgr = ModelAPI_Session::get(); + aMgr->blockAutoUpdate(false); + aMgr->blockAutoUpdate(true); + myWorkshop->viewer()->update(); }