From 3bf561d1ad1773939ae43ffbe0b4c3df03d747e8 Mon Sep 17 00:00:00 2001 From: Sergey POKHODENKO Date: Thu, 28 May 2015 10:08:05 +0300 Subject: [PATCH] Issue #560: export is impossible - generated name should contain file extension automatically --- .../ModuleBase_WidgetFileSelector.cpp | 38 +++++++++++++++++-- .../ModuleBase_WidgetFileSelector.h | 10 ++++- src/PartSet/PartSet_WidgetFileSelector.cpp | 5 --- src/PartSet/PartSet_WidgetFileSelector.h | 3 -- 4 files changed, 44 insertions(+), 12 deletions(-) diff --git a/src/ModuleBase/ModuleBase_WidgetFileSelector.cpp b/src/ModuleBase/ModuleBase_WidgetFileSelector.cpp index 3292d70f2..ee47d10c9 100644 --- a/src/ModuleBase/ModuleBase_WidgetFileSelector.cpp +++ b/src/ModuleBase/ModuleBase_WidgetFileSelector.cpp @@ -97,8 +97,6 @@ bool ModuleBase_WidgetFileSelector::restoreValue() QList ModuleBase_WidgetFileSelector::getControls() const { QList result; - //QPushButton * aButton = this->findChild(); - //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,6 +142,21 @@ 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(); @@ -174,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; +} diff --git a/src/ModuleBase/ModuleBase_WidgetFileSelector.h b/src/ModuleBase/ModuleBase_WidgetFileSelector.h index 2ddc684ac..ae2d0daa7 100644 --- a/src/ModuleBase/ModuleBase_WidgetFileSelector.h +++ b/src/ModuleBase/ModuleBase_WidgetFileSelector.h @@ -69,7 +69,13 @@ protected: protected: /// Converts format to filter string - static QString formatToFilter( const QString & theFormat ); + static QString formatToFilter(const QString & theFormat); + + /// Returns a short format string of theFilter string + static QString filterToShortFormat(const QString & theFilter); + + /// Returns a list of extensions from theFilter + static QStringList filterToExtensions(const QString & theFilter); /// Returns list of validator formats QStringList getValidatorFormats() const; @@ -77,6 +83,8 @@ protected: /// Returns string containing formats QString filterString() const; + static QString applyExtension(const QString& theFileName, const QString& theFilter); + protected: /// A control for path input QLineEdit* myPathField; diff --git a/src/PartSet/PartSet_WidgetFileSelector.cpp b/src/PartSet/PartSet_WidgetFileSelector.cpp index 0b91dc048..4f4f9531d 100644 --- a/src/PartSet/PartSet_WidgetFileSelector.cpp +++ b/src/PartSet/PartSet_WidgetFileSelector.cpp @@ -46,11 +46,6 @@ bool PartSet_WidgetFileSelector::storeValueCustom() const return ModuleBase_WidgetFileSelector::storeValueCustom(); } -QString PartSet_WidgetFileSelector::filterToShortFormat( const QString & theFilter ) -{ - return theFilter.section(' ', 0, 0); -} - QString PartSet_WidgetFileSelector::shortFormatToFullFormat( const QString & theFormat ) const { foreach(const QString & eachFormat, getValidatorFormats()) diff --git a/src/PartSet/PartSet_WidgetFileSelector.h b/src/PartSet/PartSet_WidgetFileSelector.h index 8d8c8eeb6..9c5030909 100644 --- a/src/PartSet/PartSet_WidgetFileSelector.h +++ b/src/PartSet/PartSet_WidgetFileSelector.h @@ -44,9 +44,6 @@ protected: /// Reimplemented from ModuleBase_WidgetFileSelector::storeValueCustom() virtual bool storeValueCustom() const; - /// Returns a short format string of theFilter string - static QString filterToShortFormat( const QString & theFilter ); - /// Returns a full format string for the short format QString shortFormatToFullFormat( const QString & theShortFormat ) const; -- 2.39.2