X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=src%2FModuleBase%2FModuleBase_WidgetFileSelector.cpp;h=80e6d670a9a30baf33c0e629d79dc17e4a3f1295;hb=331af9b7eb4feddf17c37fe2ce1f0f046f522145;hp=7d834cca412be1c0266670bb609ece6bfc20d9a5;hpb=542c9d721fbef80eb2040ef248fdd431cad2e631;p=modules%2Fshaper.git diff --git a/src/ModuleBase/ModuleBase_WidgetFileSelector.cpp b/src/ModuleBase/ModuleBase_WidgetFileSelector.cpp index 7d834cca4..80e6d670a 100644 --- a/src/ModuleBase/ModuleBase_WidgetFileSelector.cpp +++ b/src/ModuleBase/ModuleBase_WidgetFileSelector.cpp @@ -1,3 +1,5 @@ +// Copyright (C) 2014-20xx CEA/DEN, EDF R&D + /* * ModuleBase_WidgetFileSelector.cpp * @@ -8,7 +10,10 @@ #include #include #include +#include +#include #include +#include #include @@ -21,7 +26,7 @@ #include #include -#include +#include #include ModuleBase_WidgetFileSelector::ModuleBase_WidgetFileSelector(QWidget* theParent, @@ -29,22 +34,19 @@ 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); myDefaultPath = QString::fromStdString(theData->getProperty("path")); myMainWidget = new QWidget(theParent); QGridLayout* aMainLay = new QGridLayout(myMainWidget); - aMainLay->setContentsMargins(0, 0, 0, 0); + ModuleBase_Tools::adjustMargins(aMainLay); QLabel* aTitleLabel = new QLabel(myTitle, myMainWidget); aTitleLabel->setIndent(1); aMainLay->addWidget(aTitleLabel, 0, 0); myPathField = new QLineEdit(myMainWidget); aMainLay->addWidget(myPathField, 1, 0); QPushButton* aSelectPathBtn = new QPushButton("...", myMainWidget); + aSelectPathBtn->setToolTip(tr("Select file...")); aSelectPathBtn->setMaximumWidth(20); aSelectPathBtn->setMaximumHeight(20); aMainLay->addWidget(aSelectPathBtn, 1, 1); @@ -65,19 +67,22 @@ 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 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() { + // A rare case when plugin was not loaded. + if(!myFeature) + return false; DataPtr aData = myFeature->data(); AttributeStringPtr aStringAttr = aData->string(attributeID()); @@ -96,8 +101,8 @@ QWidget* ModuleBase_WidgetFileSelector::getControl() const QList ModuleBase_WidgetFileSelector::getControls() const { QList result; - QPushButton * aButton = myMainWidget->findChild(); - result << aButton; + //QPushButton * aButton = myMainWidget->findChild(); + //result << aButton; result << myPathField; return result; } @@ -105,13 +110,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); @@ -126,20 +131,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; +}