X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;ds=sidebyside;f=src%2FModuleBase%2FModuleBase_WidgetFileSelector.cpp;h=ee47d10c944c7cdcb58992f9c0617337fbb0b3d3;hb=840655e90a46754f2dd37aac6b888ec32eec69d3;hp=ac79ba82330ead050dcd9f6b306741734b812950;hpb=5ba89a77cdb9638a6d298c2aa5233d26576d9ed0;p=modules%2Fshaper.git diff --git a/src/ModuleBase/ModuleBase_WidgetFileSelector.cpp b/src/ModuleBase/ModuleBase_WidgetFileSelector.cpp index ac79ba823..ee47d10c9 100644 --- a/src/ModuleBase/ModuleBase_WidgetFileSelector.cpp +++ b/src/ModuleBase/ModuleBase_WidgetFileSelector.cpp @@ -17,14 +17,15 @@ #include -#include #include +#include +#include #include #include #include #include +#include #include -#include #include #include @@ -35,6 +36,7 @@ ModuleBase_WidgetFileSelector::ModuleBase_WidgetFileSelector(QWidget* theParent, : ModuleBase_ModelWidget(theParent, theData, theParentId) { myTitle = QString::fromStdString(theData->getProperty("title")); + myType = (theData->getProperty("type") == "save") ? WFS_SAVE : WFS_OPEN; myDefaultPath = QString::fromStdString(theData->getProperty("path")); QGridLayout* aMainLay = new QGridLayout(this); @@ -67,7 +69,7 @@ ModuleBase_WidgetFileSelector::~ModuleBase_WidgetFileSelector() bool ModuleBase_WidgetFileSelector::storeValueCustom() const { // A rare case when plugin was not loaded. - if(!myFeature) + if (!myFeature) return false; DataPtr aData = myFeature->data(); AttributeStringPtr aStringAttr = aData->string(attributeID()); @@ -80,7 +82,7 @@ bool ModuleBase_WidgetFileSelector::storeValueCustom() const bool ModuleBase_WidgetFileSelector::restoreValue() { // A rare case when plugin was not loaded. - if(!myFeature) + if (!myFeature) return false; DataPtr aData = myFeature->data(); AttributeStringPtr aStringAttr = aData->string(attributeID()); @@ -95,70 +97,112 @@ bool ModuleBase_WidgetFileSelector::restoreValue() QList ModuleBase_WidgetFileSelector::getControls() const { QList result; - //QPushButton * aButton = this->findChild(); - //result << aButton; result << myPathField; return result; } bool ModuleBase_WidgetFileSelector::isCurrentPathValid() { - QFileInfo aFile (myPathField->text()); + QFileInfo aFile(myPathField->text()); return aFile.exists(); } - void ModuleBase_WidgetFileSelector::onPathSelectionBtn() { - QString aFilter = formatsString(); - QString aFileName = QFileDialog::getOpenFileName(this, 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(); } -QString ModuleBase_WidgetFileSelector::formatsString() const +QString ModuleBase_WidgetFileSelector::formatToFilter(const QString & theFormat) { - QStringList aResult; - QStringList aValidatorFormats = getValidatorFormats(); + if (theFormat.isEmpty() && !theFormat.contains(":")) + return QString(); - foreach(QString eachFormat, aValidatorFormats) { - aResult << QString("%1 files (*.%1)").arg(eachFormat); - } - 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); - //TODO(sbh): extract as separate method - if(allArguments.empty()) - return QStringList(); + + QStringList aResult; 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(*it); + if (!aFormat.isEmpty()) + aResult << aFormat; + } + return aResult; +} + +QString ModuleBase_WidgetFileSelector::filterString() const +{ + 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; } - QString aFormat = QString::fromStdString(anArg.substr(0, aSepPos)); - aFormat = aFormat.toUpper(); - aResult.append(aFormat); } + if (!hasExtension && !anExtensions.isEmpty()) + aResult = QString("%1.%2").arg(theFileName).arg(anExtensions[0].section(".", 1, 1)); return aResult; }