X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=src%2FModuleBase%2FModuleBase_WidgetFileSelector.cpp;h=d10cf72489339e12931d24496307d19514745b70;hb=77ce6d35ac8d2f0fdaecb4f23e0870bf74e36103;hp=7d834cca412be1c0266670bb609ece6bfc20d9a5;hpb=b97f4d67421685f99412ee79484c7a8482da1d2e;p=modules%2Fshaper.git diff --git a/src/ModuleBase/ModuleBase_WidgetFileSelector.cpp b/src/ModuleBase/ModuleBase_WidgetFileSelector.cpp index 7d834cca4..d10cf7248 100644 --- a/src/ModuleBase/ModuleBase_WidgetFileSelector.cpp +++ b/src/ModuleBase/ModuleBase_WidgetFileSelector.cpp @@ -1,57 +1,80 @@ -/* - * ModuleBase_WidgetFileSelector.cpp - * - * Created on: Aug 28, 2014 - * Author: sbh - */ +// Copyright (C) 2014-2024 CEA, EDF +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +// #include #include #include +#include +#include #include +#include +#include #include -#include #include +#include +#include #include #include #include #include +#include #include -#include -#include +#include #include + +/// Default path +static QString myDefaultPath; + + 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), myFileDialog(0) { - myHasDefaultValue = false; + myTitle = translate(theData->getProperty("title")); + myType = (theData->getProperty("type") == "save") ? WFS_SAVE : WFS_OPEN; + if (myDefaultPath.isNull() || myDefaultPath.isEmpty()) + myDefaultPath = QString::fromStdString(theData->getProperty("path")); - myTitle = QString::fromStdString(theData->getProperty("title")); - //TODO(sbh): Get them from the feature - myFormats = getSupportedFormats(theData); - myDefaultPath = QString::fromStdString(theData->getProperty("path")); + if (myDefaultPath.isEmpty()) + myDefaultPath = Config_PropManager::string("Plugins", "import_initial_path").c_str(); - 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,83 +86,172 @@ ModuleBase_WidgetFileSelector::~ModuleBase_WidgetFileSelector() { } -bool ModuleBase_WidgetFileSelector::storeValue() const +bool ModuleBase_WidgetFileSelector::storeValueCustom() { + // 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.toStdWString()); + 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()); - bool isBlocked = myPathField->blockSignals(true); - myPathField->setText(QString::fromStdString(aStringAttr->value())); - myPathField->blockSignals(isBlocked); + char16_t* aStr = aStringAttr->valueU(); + QString aNewText = QString::fromUtf16(aStr); + if (myPathField->text() != aNewText) { + bool isBlocked = myPathField->blockSignals(true); + myPathField->setText(aNewText); + myPathField->blockSignals(isBlocked); + } return true; } -QWidget* ModuleBase_WidgetFileSelector::getControl() const -{ - return myMainWidget; -} - QList ModuleBase_WidgetFileSelector::getControls() const { QList result; - QPushButton * aButton = myMainWidget->findChild(); - 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(); +} + +bool ModuleBase_WidgetFileSelector::processEscape() +{ + if (myFileDialog) { + myFileDialog->reject(); + return true; + } + return ModuleBase_ModelWidget::processEscape(); } void ModuleBase_WidgetFileSelector::onPathSelectionBtn() { - QString aFilter = formatsString(myFormats); - QString aFileName = QFileDialog::getOpenFileName(myMainWidget, myTitle, myDefaultPath, aFilter); - if (!aFileName.isEmpty()) { - myPathField->setText(aFileName); + QString aDefaultPath = myPathField->text().isEmpty() + ? myDefaultPath + : QFileInfo(myPathField->text()).absolutePath(); + QString aFilter = filterString(); + + // use Option prohibited native dialog using to have both lower/upper extensions of files + // satisfied to dialog filter on Linux(Calibre) Issue #2055 + myFileDialog = new QFileDialog(this, myTitle, aDefaultPath, aFilter); + myFileDialog->setNameFilter(aFilter); + myFileDialog->setOptions(QFileDialog::DontUseNativeDialog); + myFileDialog->setAcceptMode(myType == WFS_SAVE ? QFileDialog::AcceptSave + : QFileDialog::AcceptOpen); + if (myFileDialog->exec() == QDialog::Accepted) + { + mySelectedFilter = myFileDialog->selectedNameFilter(); + QStringList aFileNames = myFileDialog->selectedFiles(); + if (!aFileNames.empty()) { + QString aFileName = aFileNames.first(); + if (!aFileName.isEmpty()) { + if (myType == WFS_SAVE) + aFileName = applyExtension(aFileName, mySelectedFilter); + myPathField->setText(aFileName.toUtf8()); + myDefaultPath = QFileInfo(aFileName).absolutePath(); + emit focusOutWidget(this); + } + } } + myFileDialog = 0; } 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) { - QString aXMLFormat = QString::fromStdString(theData->getProperty("formats")); - aXMLFormat = aXMLFormat.toUpper(); - const QChar kSep = ','; - return aXMLFormat.split(kSep, QString::SkipEmptyParts); + 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) +{ + // 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(); + + ModelAPI_ValidatorsFactory::Validators allValidators; + aFactory->validators(myFeature->getKind(), myAttributeID, allValidators); + + QStringList aResult; + std::list anArgumentList = allValidators.front().second; + std::list::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::formatsString(const QStringList theFormats) const +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(QString(".") + 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; +}