X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=src%2FModuleBase%2FModuleBase_WidgetFileSelector.cpp;h=be91840732b4aaaea150a6ec468399a73ddff41c;hb=5446f2f59af5cb087347b8b83ef830e2d69128dd;hp=139b3849a2f1caad2f5c1749febfe693cd141feb;hpb=66795fa02a6133abab5f79eefeba8a994e7d23f3;p=modules%2Fshaper.git diff --git a/src/ModuleBase/ModuleBase_WidgetFileSelector.cpp b/src/ModuleBase/ModuleBase_WidgetFileSelector.cpp index 139b3849a..be9184073 100644 --- a/src/ModuleBase/ModuleBase_WidgetFileSelector.cpp +++ b/src/ModuleBase/ModuleBase_WidgetFileSelector.cpp @@ -31,9 +31,8 @@ #include ModuleBase_WidgetFileSelector::ModuleBase_WidgetFileSelector(QWidget* theParent, - const Config_WidgetAPI* theData, - const std::string& theParentId) - : ModuleBase_ModelWidget(theParent, theData, theParentId) + const Config_WidgetAPI* theData) +: ModuleBase_ModelWidget(theParent, theData) { myTitle = QString::fromStdString(theData->getProperty("title")); myType = (theData->getProperty("type") == "save") ? WFS_SAVE : WFS_OPEN; @@ -66,7 +65,7 @@ ModuleBase_WidgetFileSelector::~ModuleBase_WidgetFileSelector() { } -bool ModuleBase_WidgetFileSelector::storeValueCustom() const +bool ModuleBase_WidgetFileSelector::storeValueCustom() { // A rare case when plugin was not loaded. if (!myFeature) @@ -79,7 +78,7 @@ bool ModuleBase_WidgetFileSelector::storeValueCustom() const return true; } -bool ModuleBase_WidgetFileSelector::restoreValue() +bool ModuleBase_WidgetFileSelector::restoreValueCustom() { // A rare case when plugin was not loaded. if (!myFeature) @@ -88,7 +87,9 @@ bool ModuleBase_WidgetFileSelector::restoreValue() AttributeStringPtr aStringAttr = aData->string(attributeID()); bool isBlocked = myPathField->blockSignals(true); - myPathField->setText(QString::fromStdString(aStringAttr->value())); + QString aNewText = QString::fromStdString(aStringAttr->value()); + if( myPathField->text() != aNewText ) + myPathField->setText( aNewText ); myPathField->blockSignals(isBlocked); return true; @@ -97,8 +98,6 @@ bool ModuleBase_WidgetFileSelector::restoreValue() QList ModuleBase_WidgetFileSelector::getControls() const { QList result; - //QPushButton * aButton = this->findChild(); - //result << aButton; result << myPathField; return result; } @@ -111,13 +110,18 @@ bool ModuleBase_WidgetFileSelector::isCurrentPathValid() void ModuleBase_WidgetFileSelector::onPathSelectionBtn() { - QString aDefaultPath = myPathField->text().isEmpty() ? myDefaultPath : myPathField->text(); + QString aDefaultPath = myPathField->text().isEmpty() + ? myDefaultPath + : QFileInfo(myPathField->text()).absolutePath(); QString aFilter = filterString(); QString aFileName = (myType == WFS_SAVE) - ? QFileDialog::getSaveFileName(this, myTitle, aDefaultPath, aFilter) - : QFileDialog::getOpenFileName(this, myTitle, aDefaultPath, aFilter); + ? 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); + emit focusOutWidget(this); } } @@ -129,43 +133,77 @@ void ModuleBase_WidgetFileSelector::onPathChanged() emit valuesChanged(); } -QString ModuleBase_WidgetFileSelector::filterString() const +QString ModuleBase_WidgetFileSelector::formatToFilter(const QString & theFormat) { - QStringList aResult; - QStringList aValidatorFormats = getValidatorFormats(); + if (theFormat.isEmpty() && !theFormat.contains(":")) + return QString(); - foreach(QString eachFormat, aValidatorFormats) { - QStringList aFormatList = eachFormat.split("|"); - aResult << QString("%1 files (%2)").arg(aFormatList.value(0)) - .arg(QStringList(aFormatList).replaceInStrings(QRegExp("^(.*)$"), "*.\\1").join(" ")); - } - aResult << QString("All files (*.*)"); - return aResult.join(";;"); + 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) +{ + // 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 allValidators; - std::list > allArguments; - aFactory->validators(myFeature->getKind(), myAttributeID, allValidators, allArguments); + + ModelAPI_ValidatorsFactory::Validators allValidators; + aFactory->validators(myFeature->getKind(), myAttributeID, allValidators); + QStringList aResult; - std::list anArgumentList = allArguments.front(); + std::list anArgumentList = allValidators.front().second; std::list::const_iterator it = anArgumentList.begin(); for (; it != anArgumentList.end(); ++it) { - QString aFormat = getFormat(*it); - if (!aFormat.isNull()) + QString aFormat = QString::fromStdString(*it); + if (!aFormat.isEmpty()) aResult << aFormat; } return aResult; } -QString ModuleBase_WidgetFileSelector::getFormat( const std::string& theArgument ) const +QString ModuleBase_WidgetFileSelector::filterString() const { - QString anArgument = QString::fromStdString(theArgument); - if (!anArgument.contains(":")) - return QString(); - return anArgument.section(":", 0, 0).toUpper(); + QStringList aResult; + 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; +}