From: sbh Date: Mon, 6 Oct 2014 07:48:43 +0000 (+0400) Subject: Use validators to set import file formats X-Git-Tag: V_0.5~114^2~1 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=2df4457d07ad1b2e2a1d48068a24fc024f1715e3;p=modules%2Fshaper.git Use validators to set import file formats --- diff --git a/src/ConstructionPlugin/plugin-Construction.xml b/src/ConstructionPlugin/plugin-Construction.xml index 381a3e656..217afb33e 100644 --- a/src/ConstructionPlugin/plugin-Construction.xml +++ b/src/ConstructionPlugin/plugin-Construction.xml @@ -1,14 +1,27 @@ - - - + + - - - - + + - - + + \ No newline at end of file diff --git a/src/ExchangePlugin/ExchangePlugin_Plugin.cpp b/src/ExchangePlugin/ExchangePlugin_Plugin.cpp index 3a4abd00b..b37a06906 100644 --- a/src/ExchangePlugin/ExchangePlugin_Plugin.cpp +++ b/src/ExchangePlugin/ExchangePlugin_Plugin.cpp @@ -4,8 +4,10 @@ #include #include +#include #include +#include #include @@ -17,7 +19,11 @@ static ExchangePlugin_Plugin* MY_EXCHANGE_INSTANCE = new ExchangePlugin_Plugin() ExchangePlugin_Plugin::ExchangePlugin_Plugin() { // register this plugin - ModelAPI_Session::get()->registerPlugin(this); + SessionPtr aSession = ModelAPI_Session::get(); + aSession->registerPlugin(this); + ModelAPI_ValidatorsFactory* aFactory = aSession->validators(); + aFactory->registerValidator("ExchangePlugin_ImportFormat", + new ExchangePlugin_ImportFormatValidator); } FeaturePtr ExchangePlugin_Plugin::createFeature(string theFeatureID) diff --git a/src/ExchangePlugin/ExchangePlugin_Validators.cpp b/src/ExchangePlugin/ExchangePlugin_Validators.cpp index f179e889b..7954c540b 100644 --- a/src/ExchangePlugin/ExchangePlugin_Validators.cpp +++ b/src/ExchangePlugin/ExchangePlugin_Validators.cpp @@ -6,16 +6,74 @@ #include #include #include +#include #include #include +#include +bool ExchangePlugin_ImportFormatValidator::parseFormats(const std::list& theArguments, + std::list& outFormats) +{ + std::list::const_iterator it = theArguments.begin(); + bool result = true; + for (; it != theArguments.end(); ++it) { + std::string anArg = *it; + int aSepPos = anArg.find(":"); + if (aSepPos == std::string::npos) { + result = false; + continue; + } + std::string aFormat = anArg.substr(0, aSepPos); + std::transform(aFormat.begin(), aFormat.end(), aFormat.begin(), toupper); + outFormats.push_back(aFormat); + } + return result; +} + +bool ExchangePlugin_ImportFormatValidator::parsePlugins(const std::list& theArguments, + std::list& outPlugins) +{ + std::list::const_iterator it = theArguments.begin(); + bool result = true; + for (; it != theArguments.end(); ++it) { + std::string anArg = *it; + int aSepPos = anArg.find(":"); + if (aSepPos == std::string::npos) { + result = false; + continue; + } + outPlugins.push_back(anArg.substr(aSepPos + 1)); + } + return result; +} -bool ExchangePlugin_ImportFormatValidator::isValid( - const AttributePtr& theAttribute, const std::list& theArguments) const +bool ExchangePlugin_ImportFormatValidator::isValid(const AttributePtr& theAttribute, + const std::list& theArguments) const { SessionPtr aMgr = ModelAPI_Session::get(); ModelAPI_ValidatorsFactory* aFactory = aMgr->validators(); + if (theAttribute->isInitialized()) { + const AttributeStringPtr aStrAttr = + boost::dynamic_pointer_cast(theAttribute); + if(!aStrAttr) + return false; + std::string aFileName = aStrAttr->value(); + if (!aFileName.empty()) { + std::list aFormats; + ExchangePlugin_ImportFormatValidator::parseFormats(theArguments, aFormats); + std::list::const_iterator itFormats = aFormats.begin(); + size_t aFileNameLen = aFileName.length(); + std::transform(aFileName.begin(), aFileName.end(), aFileName.begin(), toupper); + // Is file name ends with the format + for (; itFormats != aFormats.end(); ++itFormats) { + size_t aFormatBeginPos = aFileNameLen - (*itFormats).length(); + if (aFileName.compare(aFormatBeginPos, std::string::npos, *itFormats) == 0) { + return true; + } + } + } + } return false; } diff --git a/src/ExchangePlugin/ExchangePlugin_Validators.h b/src/ExchangePlugin/ExchangePlugin_Validators.h index 21d72765b..f390f5758 100644 --- a/src/ExchangePlugin/ExchangePlugin_Validators.h +++ b/src/ExchangePlugin/ExchangePlugin_Validators.h @@ -10,9 +10,20 @@ class ExchangePlugin_ImportFormatValidator : public ModelAPI_AttributeValidator { + /* + * Parses input arguments "BREP:BREPImport", "STEP:STEPImport" + * into list of file formats "BREP","STEP" + * and list of corresponding plugins: "BREPImport", "STEPImport" + */ + static bool parseFormats(const std::list& theArguments, + std::list& outFormats); + static bool parsePlugins(const std::list& theArguments, + std::list& outPlugins); public: - virtual bool isValid( - const AttributePtr& theAttribute, const std::list& theArguments) const; + virtual bool isValid(const AttributePtr& theAttribute, + const std::list& theArguments) const; + + }; diff --git a/src/ExchangePlugin/plugin-Exchange.xml b/src/ExchangePlugin/plugin-Exchange.xml index a21500e42..7e1c7f117 100644 --- a/src/ExchangePlugin/plugin-Exchange.xml +++ b/src/ExchangePlugin/plugin-Exchange.xml @@ -1,20 +1,14 @@ - + - + path=""> + + - + \ No newline at end of file diff --git a/src/GeomValidators/GeomValidators_Positive.h b/src/GeomValidators/GeomValidators_Positive.h index f526999f6..fcc741e3b 100644 --- a/src/GeomValidators/GeomValidators_Positive.h +++ b/src/GeomValidators/GeomValidators_Positive.h @@ -19,8 +19,8 @@ public: //! returns true if attribute is valid //! \param theAttribute the checked attribute //! \param theArguments arguments of the attribute - GEOMVALIDATORS_EXPORT virtual bool isValid( - const AttributePtr& theAttribute, const std::list& theArguments) const; + GEOMVALIDATORS_EXPORT virtual bool isValid(const AttributePtr& theAttribute, + const std::list& theArguments) const; }; diff --git a/src/Model/Model_Validator.cpp b/src/Model/Model_Validator.cpp index ee5ab1a9c..987db716e 100644 --- a/src/Model/Model_Validator.cpp +++ b/src/Model/Model_Validator.cpp @@ -186,7 +186,7 @@ bool Model_ValidatorsFactory::validate(const boost::shared_ptr std::list::iterator anAttrIter = aLtAttributes.begin(); for (; anAttrIter != aLtAttributes.end(); anAttrIter++) { std::map::const_iterator anAttr = - aFeatureIter->second.find(*anAttrIter); + aFeatureIter->second.find(*anAttrIter); if (anAttr != aFeatureIter->second.end()) { AttrValidators::const_iterator aValIter = anAttr->second.cbegin(); for (; aValIter != anAttr->second.cend(); aValIter++) { @@ -198,8 +198,8 @@ bool Model_ValidatorsFactory::validate(const boost::shared_ptr const ModelAPI_AttributeValidator* anAttrValidator = dynamic_cast(aFound->second); if (anAttrValidator) { - if (!anAttrValidator->isValid(theFeature->data()->attribute(*anAttrIter), - aValIter->second)) { + AttributePtr anAttribute = theFeature->data()->attribute(*anAttrIter); + if (!anAttrValidator->isValid(anAttribute, aValIter->second)) { return false; } } @@ -221,4 +221,4 @@ void Model_ValidatorsFactory::registerNotObligatory( aValidator->registerNotObligatory(theFeature, theAttribute); } } -} \ No newline at end of file +} diff --git a/src/ModelAPI/ModelAPI_AttributeValidator.h b/src/ModelAPI/ModelAPI_AttributeValidator.h index 63a1b9e6f..aa0680cbb 100644 --- a/src/ModelAPI/ModelAPI_AttributeValidator.h +++ b/src/ModelAPI/ModelAPI_AttributeValidator.h @@ -17,8 +17,8 @@ public: //! returns true if attribute is valid //! \param theAttribute the checked attribute //! \param theArguments arguments of the attribute - virtual bool isValid( - const AttributePtr& theAttribute, const std::list& theArguments) const = 0; + virtual bool isValid(const AttributePtr& theAttribute, + const std::list& theArguments) const = 0; }; #endif diff --git a/src/ModuleBase/ModuleBase_WidgetFileSelector.cpp b/src/ModuleBase/ModuleBase_WidgetFileSelector.cpp index 9f16b74f6..d2369702f 100644 --- a/src/ModuleBase/ModuleBase_WidgetFileSelector.cpp +++ b/src/ModuleBase/ModuleBase_WidgetFileSelector.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #include #include @@ -31,8 +32,6 @@ ModuleBase_WidgetFileSelector::ModuleBase_WidgetFileSelector(QWidget* theParent, : ModuleBase_ModelWidget(theParent, theData, theParentId) { myTitle = QString::fromStdString(theData->getProperty("title")); - //TODO(sbh): Get them from the feature - myFormats = getSupportedFormats(theData); myDefaultPath = QString::fromStdString(theData->getProperty("path")); myMainWidget = new QWidget(theParent); @@ -64,6 +63,9 @@ ModuleBase_WidgetFileSelector::~ModuleBase_WidgetFileSelector() bool ModuleBase_WidgetFileSelector::storeValue() const { + // A rare case when plugin was not loaded. + if(!myFeature) + return false; DataPtr aData = myFeature->data(); AttributeStringPtr aStringAttr = aData->string(attributeID()); QString aWidgetValue = myPathField->text(); @@ -74,6 +76,9 @@ bool ModuleBase_WidgetFileSelector::storeValue() const bool ModuleBase_WidgetFileSelector::restoreValue() { + // A rare case when plugin was not loaded. + if(!myFeature) + return false; DataPtr aData = myFeature->data(); AttributeStringPtr aStringAttr = aData->string(attributeID()); @@ -101,13 +106,13 @@ QList ModuleBase_WidgetFileSelector::getControls() const bool ModuleBase_WidgetFileSelector::isCurrentPathValid() { QFileInfo aFile (myPathField->text()); - return aFile.exists() && myFormats.contains(aFile.suffix(), Qt::CaseInsensitive); + return aFile.exists(); } void ModuleBase_WidgetFileSelector::onPathSelectionBtn() { - QString aFilter = formatsString(myFormats); + QString aFilter = formatsString(); QString aFileName = QFileDialog::getOpenFileName(myMainWidget, myTitle, myDefaultPath, aFilter); if (!aFileName.isEmpty()) { myPathField->setText(aFileName); @@ -122,20 +127,40 @@ void ModuleBase_WidgetFileSelector::onPathChanged() emit valuesChanged(); } -QStringList ModuleBase_WidgetFileSelector::getSupportedFormats(const Config_WidgetAPI* theData) const -{ - QString aXMLFormat = QString::fromStdString(theData->getProperty("formats")); - aXMLFormat = aXMLFormat.toUpper(); - const QChar kSep = ','; - return aXMLFormat.split(kSep, QString::SkipEmptyParts); -} - -QString ModuleBase_WidgetFileSelector::formatsString(const QStringList theFormats) const +QString ModuleBase_WidgetFileSelector::formatsString() const { QStringList aResult; - foreach(QString eachFormat, theFormats) { + QStringList aValidatorFormats = getValidatorFormats(); + + foreach(QString eachFormat, aValidatorFormats) { aResult << QString("%1 files (*.%1)").arg(eachFormat); } + aResult << QString("All files (*.*)"); return aResult.join(";;"); } +QStringList ModuleBase_WidgetFileSelector::getValidatorFormats() const +{ + SessionPtr aMgr = ModelAPI_Session::get(); + ModelAPI_ValidatorsFactory* aFactory = aMgr->validators(); + std::list allValidators; + std::list > allArguments; + aFactory->validators(myFeature->getKind(), myAttributeID, allValidators, allArguments); + //TODO(sbh): extract as separate method + if(allArguments.empty()) + return QStringList(); + std::list anArgumentList = allArguments.front(); + std::list::const_iterator it = anArgumentList.begin(); + QStringList aResult; + for (; it != anArgumentList.end(); ++it) { + std::string anArg = *it; + int aSepPos = anArg.find(":"); + if (aSepPos == std::string::npos) { + continue; + } + QString aFormat = QString::fromStdString(anArg.substr(0, aSepPos)); + aFormat = aFormat.toUpper(); + aResult.append(aFormat); + } + return aResult; +} diff --git a/src/ModuleBase/ModuleBase_WidgetFileSelector.h b/src/ModuleBase/ModuleBase_WidgetFileSelector.h index 2d48e430b..4e78830aa 100644 --- a/src/ModuleBase/ModuleBase_WidgetFileSelector.h +++ b/src/ModuleBase/ModuleBase_WidgetFileSelector.h @@ -50,13 +50,12 @@ class MODULEBASE_EXPORT ModuleBase_WidgetFileSelector : public ModuleBase_ModelW void onPathChanged(); protected: - QStringList getSupportedFormats(const Config_WidgetAPI* theData) const; - QString formatsString(const QStringList theFormats) const; + QString formatsString() const; + QStringList getValidatorFormats() const; private: QLineEdit* myPathField; QWidget* myMainWidget; - QStringList myFormats; QString myTitle; QString myDefaultPath;