Salome HOME
Use validators to set import file formats
authorsbh <sergey.belash@opencascade.com>
Mon, 6 Oct 2014 07:48:43 +0000 (11:48 +0400)
committersbh <sergey.belash@opencascade.com>
Mon, 6 Oct 2014 07:48:43 +0000 (11:48 +0400)
src/ConstructionPlugin/plugin-Construction.xml
src/ExchangePlugin/ExchangePlugin_Plugin.cpp
src/ExchangePlugin/ExchangePlugin_Validators.cpp
src/ExchangePlugin/ExchangePlugin_Validators.h
src/ExchangePlugin/plugin-Exchange.xml
src/GeomValidators/GeomValidators_Positive.h
src/Model/Model_Validator.cpp
src/ModelAPI/ModelAPI_AttributeValidator.h
src/ModuleBase/ModuleBase_WidgetFileSelector.cpp
src/ModuleBase/ModuleBase_WidgetFileSelector.h

index 381a3e656f587e26e70adc2ccce395ff44fc1d49..217afb33e3865245144c31679a79a535bf2b32cb 100644 (file)
@@ -1,14 +1,27 @@
 <plugin>
   <workbench id="Construction">
     <group id="Basic">
-      <feature id="Point" title="Point" tooltip="Create a new point" icon=":icons/point.png">
-        <!-- validator id="test" parameters="x,y,z"/ -->
-        <source path="point_widget.xml"/>
+      <feature
+        id="Point"
+        title="Point"
+        tooltip="Create a new point"
+        icon=":icons/point.png">
+        <source path="point_widget.xml" />
       </feature>
-      <feature id="Axis" title="Axis" tooltip="Create a new axis" icon=":icons/axis.png" keysequence="" internal="true">
-        <!-- validator id="inside" parameters="a,b"/ --> 
-      </feature>
-      <feature id="Plane" title="Plane" tooltip="Create a new plane" icon=":icons/plane.png" keysequence="" internal="true"/>
+      <feature
+        id="Axis"
+        title="Axis"
+        tooltip="Create a new axis"
+        icon=":icons/axis.png"
+        keysequence=""
+        internal="true" />
+      <feature
+        id="Plane"
+        title="Plane"
+        tooltip="Create a new plane"
+        icon=":icons/plane.png"
+        keysequence=""
+        internal="true" />
     </group>
-</workbench>  
-</plugin>
+  </workbench>
+</plugin>
\ No newline at end of file
index 3a4abd00bed00e7b23cb2079aaa74e37721acb6f..b37a0690623525edb7e7be443c9c5fa0c98c4ce6 100644 (file)
@@ -4,8 +4,10 @@
 
 #include <ExchangePlugin_Plugin.h>
 #include <ExchangePlugin_ImportFeature.h>
+#include <ExchangePlugin_Validators.h>
 
 #include <ModelAPI_Session.h>
+#include <ModelAPI_Validator.h>
 
 #include <boost/smart_ptr/shared_ptr.hpp>
 
@@ -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)
index f179e889bc0f7b632c7670555959636f208ab67f..7954c540be2e8a9c71230e78021b915c7b35a603 100644 (file)
@@ -6,16 +6,74 @@
 #include <ModelAPI_Feature.h>
 #include <ModelAPI_Object.h>
 #include <ModelAPI_Session.h>
+#include <ModelAPI_AttributeString.h>
 
 #include <list>
 #include <string>
+#include <algorithm>
 
+bool ExchangePlugin_ImportFormatValidator::parseFormats(const std::list<std::string>& theArguments,
+                                                        std::list<std::string>& outFormats)
+{
+  std::list<std::string>::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<std::string>& theArguments,
+                                                        std::list<std::string>& outPlugins)
+{
+  std::list<std::string>::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<std::string>& theArguments) const
+bool ExchangePlugin_ImportFormatValidator::isValid(const AttributePtr& theAttribute,
+                                                   const std::list<std::string>& theArguments) const
 {
   SessionPtr aMgr = ModelAPI_Session::get();
   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
+  if (theAttribute->isInitialized()) {
+    const AttributeStringPtr aStrAttr =
+        boost::dynamic_pointer_cast<ModelAPI_AttributeString>(theAttribute);
+    if(!aStrAttr)
+      return false;
+    std::string aFileName = aStrAttr->value();
+    if (!aFileName.empty()) {
+      std::list<std::string> aFormats;
+      ExchangePlugin_ImportFormatValidator::parseFormats(theArguments, aFormats);
+      std::list<std::string>::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;
 }
 
index 21d72765b0bce158c26a991448a79145a6151844..f390f57586309cfdf9353bd2d041688ec3956479 100644 (file)
 
 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<std::string>& theArguments,
+                             std::list<std::string>& outFormats);
+  static bool parsePlugins(const std::list<std::string>& theArguments,
+                           std::list<std::string>& outPlugins);
  public:
-  virtual bool isValid(
-    const AttributePtr& theAttribute, const std::list<std::string>& theArguments) const;
+  virtual bool isValid(const AttributePtr& theAttribute,
+                       const std::list<std::string>& theArguments) const;
+
+
 
 };
 
index a21500e42ef53bc20f61cbbf745696a2f66f54a5..7e1c7f1178f8e667e6037b9cf8f06c2a4ca03075 100644 (file)
@@ -1,20 +1,14 @@
 <plugin>
   <workbench id="Features" document="Part">
     <group id="Exchange">
-      <feature
-        id="Import"
-        title="Import"
-        tooltip="Import shape"
-        icon=":icons/import.png">
+      <feature id="Import" title="Import" tooltip="Import shape" icon=":icons/import.png">
         <file_selector
           id="import_file_selector"
           title="Import file"
-          path=""
-          formats="BREP,STEP" />
-        <!-- TODO: pass formats as validator 
-        <validator id="Format" parameters="BREP:BREPImport,STEP:STEPImport" /> 
-        -->
+          path="">
+          <validator id="ExchangePlugin_ImportFormat" parameters="BREP:BREPImport,STEP:STEPImport" />
+        </file_selector>
       </feature>
     </group>
   </workbench>
-</plugin>
+</plugin>
\ No newline at end of file
index f526999f61c943c72b897c5dd629f78796c443d9..fcc741e3b30a18ab3d620107aa6992b54bf74c76 100644 (file)
@@ -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<std::string>& theArguments) const;
+  GEOMVALIDATORS_EXPORT virtual bool isValid(const AttributePtr& theAttribute,
+                                             const std::list<std::string>& theArguments) const;
 
 };
 
index ee5ab1a9ccad2ed6178752e68faec45fa51bfbdb..987db716ea98142151bf654ad24eda776f23bb18 100644 (file)
@@ -186,7 +186,7 @@ bool Model_ValidatorsFactory::validate(const boost::shared_ptr<ModelAPI_Feature>
     std::list<std::string>::iterator anAttrIter = aLtAttributes.begin();
     for (; anAttrIter != aLtAttributes.end(); anAttrIter++) {
       std::map<std::string, AttrValidators>::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<ModelAPI_Feature>
             const ModelAPI_AttributeValidator* anAttrValidator = 
               dynamic_cast<const ModelAPI_AttributeValidator*>(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
+}
index 63a1b9e6fb8e8e103e5a73b3a50b220f086acf08..aa0680cbb5311c5939590d570595af06697c7765 100644 (file)
@@ -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<std::string>& theArguments) const = 0;
+  virtual bool isValid(const AttributePtr& theAttribute,
+                       const std::list<std::string>& theArguments) const = 0;
 };
 
 #endif
index 9f16b74f6b9d20ee8869f8cdbb20146d3259a35b..d2369702f9ec3f2e2a106f267fba9cb6b7cf680c 100644 (file)
@@ -8,6 +8,7 @@
 #include <ModelAPI_AttributeString.h>
 #include <ModelAPI_Data.h>
 #include <ModelAPI_Object.h>
+#include <ModelAPI_Validator.h>
 #include <ModuleBase_WidgetFileSelector.h>
 #include <ModuleBase_Tools.h>
 
@@ -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<QWidget*> 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<ModelAPI_Validator*> allValidators;
+  std::list<std::list<std::string> > allArguments;
+  aFactory->validators(myFeature->getKind(), myAttributeID, allValidators, allArguments);
+  //TODO(sbh): extract as separate method
+  if(allArguments.empty())
+    return QStringList();
+  std::list<std::string> anArgumentList = allArguments.front();
+  std::list<std::string>::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;
+}
index 2d48e430b57a4510900234ec79090b636a2616e1..4e78830aa2b8b7fe8fed6dad7b857472f34b42bb 100644 (file)
@@ -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;