Salome HOME
Fix for the issue #593: do not remove naming attribute, but use TNaming_Builder for...
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetFileSelector.cpp
index 5da22f5ebf6ccedab6b9e37ffc91377e0adea482..ee47d10c944c7cdcb58992f9c0617337fbb0b3d3 100644 (file)
@@ -97,8 +97,6 @@ bool ModuleBase_WidgetFileSelector::restoreValue()
 QList<QWidget*> ModuleBase_WidgetFileSelector::getControls() const
 {
   QList<QWidget*> result;
-  //QPushButton * aButton = this->findChild<QPushButton *>();
-  //result << aButton;
   result << myPathField;
   return result;
 }
@@ -119,6 +117,8 @@ void ModuleBase_WidgetFileSelector::onPathSelectionBtn()
       ? QFileDialog::getSaveFileName(this, myTitle, aDefaultPath, aFilter, &mySelectedFilter)
       : QFileDialog::getOpenFileName(this, myTitle, aDefaultPath, aFilter, &mySelectedFilter);
   if (!aFileName.isEmpty()) {
+    if (myType == WFS_SAVE)
+      aFileName = applyExtension(aFileName, mySelectedFilter);
     myPathField->setText(aFileName);
   }
 }
@@ -131,7 +131,7 @@ void ModuleBase_WidgetFileSelector::onPathChanged()
   emit valuesChanged();
 }
 
-QString ModuleBase_WidgetFileSelector::formatToFilter( const QString & theFormat )
+QString ModuleBase_WidgetFileSelector::formatToFilter(const QString & theFormat)
 {
   if (theFormat.isEmpty() && !theFormat.contains(":"))
     return QString();
@@ -142,13 +142,30 @@ QString ModuleBase_WidgetFileSelector::formatToFilter( const QString & theFormat
       .arg(QStringList(aExtesionList).replaceInStrings(QRegExp("^(.*)$"), "*.\\1").join(" "));
 }
 
+QString ModuleBase_WidgetFileSelector::filterToShortFormat(const QString & theFilter)
+{
+  // Simplified implementation.
+  // It relies on theFilter was made by formatToFilter() function.
+  return theFilter.section(' ', 0, 0);
+}
+
+QStringList ModuleBase_WidgetFileSelector::filterToExtensions(const QString & theFilter)
+{
+  // Simplified implementation.
+  // It relies on theFilter was made by formatToFilter() function.
+  QStringList anExtensions = theFilter.section("(", 1, 1).section(")", 0, 0).split(" ");
+  return anExtensions;
+}
+
 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);
+
   QStringList aResult;
   std::list<std::string> anArgumentList = allArguments.front();
   std::list<std::string>::const_iterator it = anArgumentList.begin();
@@ -172,3 +189,20 @@ QString ModuleBase_WidgetFileSelector::filterString() const
     aResult << QString("All files (*.*)");
   return aResult.join(";;");
 }
+
+QString ModuleBase_WidgetFileSelector::applyExtension(const QString& theFileName,
+                                                      const QString& theFilter)
+{
+  QString aResult = theFileName;
+  bool hasExtension = false;
+  QStringList anExtensions = filterToExtensions(theFilter);
+  foreach(const QString& anExtension, anExtensions) {
+    if (theFileName.endsWith(anExtension.section(".", 1, 1), Qt::CaseInsensitive)) {
+      hasExtension = true;
+      break;
+    }
+  }
+  if (!hasExtension && !anExtensions.isEmpty())
+    aResult = QString("%1.%2").arg(theFileName).arg(anExtensions[0].section(".", 1, 1));
+  return aResult;
+}