Salome HOME
Make ModuleBase_ModelWidget::restoreValue() non-virtual and create virtual ModuleBase...
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetFileSelector.cpp
index 7d834cca412be1c0266670bb609ece6bfc20d9a5..a18f059399419bb5d9d2d2f351e17270a628eef3 100644 (file)
@@ -1,3 +1,5 @@
+// Copyright (C) 2014-20xx CEA/DEN, EDF R&D
+
 /*
  * ModuleBase_WidgetFileSelector.cpp
  *
 #include <ModelAPI_AttributeString.h>
 #include <ModelAPI_Data.h>
 #include <ModelAPI_Object.h>
+#include <ModelAPI_Validator.h>
+#include <ModelAPI_Session.h>
 #include <ModuleBase_WidgetFileSelector.h>
+#include <ModuleBase_Tools.h>
 
 #include <Config_WidgetAPI.h>
 
-#include <QGridLayout>
 #include <QFileDialog>
+#include <QGridLayout>
+#include <QLabel>
 #include <QLineEdit>
 #include <QList>
 #include <QObject>
 #include <QPushButton>
+#include <QRegExp>
 #include <QString>
-#include <QLabel>
 
-#include <boost/smart_ptr/shared_ptr.hpp>
+#include <memory>
 #include <string>
 
 ModuleBase_WidgetFileSelector::ModuleBase_WidgetFileSelector(QWidget* theParent,
@@ -29,29 +35,26 @@ ModuleBase_WidgetFileSelector::ModuleBase_WidgetFileSelector(QWidget* theParent,
                                                              const std::string& theParentId)
     : ModuleBase_ModelWidget(theParent, theData, theParentId)
 {
-  myHasDefaultValue = false;
-
   myTitle = QString::fromStdString(theData->getProperty("title"));
-  //TODO(sbh): Get them from the feature
-  myFormats = getSupportedFormats(theData);
+  myType = (theData->getProperty("type") == "save") ? WFS_SAVE : WFS_OPEN;
   myDefaultPath = QString::fromStdString(theData->getProperty("path"));
 
-  myMainWidget = new QWidget(theParent);
-  QGridLayout* aMainLay = new QGridLayout(myMainWidget);
-  aMainLay->setContentsMargins(0, 0, 0, 0);
-  QLabel* aTitleLabel = new QLabel(myTitle, myMainWidget);
+  QGridLayout* aMainLay = new QGridLayout(this);
+  ModuleBase_Tools::adjustMargins(aMainLay);
+  QLabel* aTitleLabel = new QLabel(myTitle, this);
   aTitleLabel->setIndent(1);
   aMainLay->addWidget(aTitleLabel, 0, 0);
-  myPathField = new QLineEdit(myMainWidget);
+  myPathField = new QLineEdit(this);
   aMainLay->addWidget(myPathField, 1, 0);
-  QPushButton* aSelectPathBtn = new QPushButton("...", myMainWidget);
+  QPushButton* aSelectPathBtn = new QPushButton("...", this);
+  aSelectPathBtn->setToolTip(tr("Select file..."));
   aSelectPathBtn->setMaximumWidth(20);
   aSelectPathBtn->setMaximumHeight(20);
   aMainLay->addWidget(aSelectPathBtn, 1, 1);
   aMainLay->setColumnStretch(0, 1);
   myPathField->setMinimumHeight(20);
   aMainLay->setHorizontalSpacing(1);
-  myMainWidget->setLayout(aMainLay);
+  this->setLayout(aMainLay);
 
   connect(myPathField, SIGNAL(textChanged(const QString&)),
           this,        SLOT(onPathChanged()));
@@ -63,21 +66,24 @@ ModuleBase_WidgetFileSelector::~ModuleBase_WidgetFileSelector()
 {
 }
 
-bool ModuleBase_WidgetFileSelector::storeValue() const
+bool ModuleBase_WidgetFileSelector::storeValueCustom() const
 {
+  // A rare case when plugin was not loaded. 
+  if (!myFeature)
+    return false;
   DataPtr aData = myFeature->data();
   AttributeStringPtr aStringAttr = aData->string(attributeID());
-  QString aModelValue = QString::fromStdString(aStringAttr->value());
   QString aWidgetValue = myPathField->text();
-  if(aModelValue != aWidgetValue) {
-    aStringAttr->setValue(aWidgetValue.toStdString());
-    updateObject(myFeature);
-  }
+  aStringAttr->setValue(aWidgetValue.toStdString());
+  updateObject(myFeature);
   return true;
 }
 
-bool ModuleBase_WidgetFileSelector::restoreValue()
+bool ModuleBase_WidgetFileSelector::restoreValueCustom()
 {
+  // A rare case when plugin was not loaded. 
+  if (!myFeature)
+    return false;
   DataPtr aData = myFeature->data();
   AttributeStringPtr aStringAttr = aData->string(attributeID());
 
@@ -88,58 +94,115 @@ bool ModuleBase_WidgetFileSelector::restoreValue()
   return true;
 }
 
-QWidget* ModuleBase_WidgetFileSelector::getControl() const
-{
-  return myMainWidget;
-}
-
 QList<QWidget*> ModuleBase_WidgetFileSelector::getControls() const
 {
   QList<QWidget*> result;
-  QPushButton * aButton = myMainWidget->findChild<QPushButton *>();
-  result << aButton;
   result << myPathField;
   return result;
 }
 
 bool ModuleBase_WidgetFileSelector::isCurrentPathValid()
 {
-  QFileInfo aFile (myPathField->text());
-  return aFile.exists() && myFormats.contains(aFile.suffix(), Qt::CaseInsensitive);
+  QFileInfo aFile(myPathField->text());
+  return aFile.exists();
 }
 
-
 void ModuleBase_WidgetFileSelector::onPathSelectionBtn()
 {
-  QString aFilter = formatsString(myFormats);
-  QString aFileName = QFileDialog::getOpenFileName(myMainWidget, myTitle, myDefaultPath, aFilter);
+  QString aDefaultPath = myPathField->text().isEmpty()
+      ? myDefaultPath
+      : QFileInfo(myPathField->text()).absolutePath();
+  QString aFilter = filterString();
+  QString aFileName = (myType == WFS_SAVE)
+      ? 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);
   }
 }
 
 void ModuleBase_WidgetFileSelector::onPathChanged()
 {
-  if(!isCurrentPathValid())
+  if (myType == WFS_OPEN && !isCurrentPathValid())
     return;
   storeValue();
   emit valuesChanged();
 }
 
-QStringList ModuleBase_WidgetFileSelector::getSupportedFormats(const Config_WidgetAPI* theData) const
+QString ModuleBase_WidgetFileSelector::formatToFilter(const QString & theFormat)
+{
+  if (theFormat.isEmpty() && !theFormat.contains(":"))
+    return QString();
+
+  QStringList aExtesionList = theFormat.section(':', 0, 0).split("|");
+  QString aFormat = theFormat.section(':', 1, 1);
+  return QString("%1 files (%2)").arg(aFormat)
+      .arg(QStringList(aExtesionList).replaceInStrings(QRegExp("^(.*)$"), "*.\\1").join(" "));
+}
+
+QString ModuleBase_WidgetFileSelector::filterToShortFormat(const QString & theFilter)
 {
-  QString aXMLFormat = QString::fromStdString(theData->getProperty("formats"));
-  aXMLFormat = aXMLFormat.toUpper();
-  const QChar kSep = ',';
-  return aXMLFormat.split(kSep, QString::SkipEmptyParts);
+  // Simplified implementation.
+  // It relies on theFilter was made by formatToFilter() function.
+  return theFilter.section(' ', 0, 0);
 }
 
-QString ModuleBase_WidgetFileSelector::formatsString(const QStringList theFormats) const
+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();
+  for (; it != anArgumentList.end(); ++it) {
+    QString aFormat = QString::fromStdString(*it);
+    if (!aFormat.isEmpty())
+      aResult << aFormat;
+  }
+  return aResult;
+}
+
+QString ModuleBase_WidgetFileSelector::filterString() const
 {
   QStringList aResult;
-  foreach(QString eachFormat, theFormats)  {
-    aResult << QString("%1 files (*.%1)").arg(eachFormat);
+  QStringList aValidatorFormats = getValidatorFormats();
+
+  foreach(const QString & eachFormat, aValidatorFormats) {
+    aResult << formatToFilter(eachFormat);
   }
+  if (myType == WFS_OPEN)
+    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;
+}