1 // Copyright (C) 2014-2019 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include <ModelAPI_AttributeString.h>
21 #include <ModelAPI_Data.h>
22 #include <ModelAPI_Object.h>
23 #include <ModelAPI_Validator.h>
24 #include <ModelAPI_Session.h>
25 #include <ModuleBase_WidgetFileSelector.h>
26 #include <ModuleBase_Tools.h>
28 #include <Config_PropManager.h>
29 #include <Config_WidgetAPI.h>
31 #include <QFileDialog>
32 #include <QGridLayout>
37 #include <QPushButton>
46 static QString myDefaultPath;
49 ModuleBase_WidgetFileSelector::ModuleBase_WidgetFileSelector(QWidget* theParent,
50 const Config_WidgetAPI* theData)
51 : ModuleBase_ModelWidget(theParent, theData), myFileDialog(0)
53 myTitle = translate(theData->getProperty("title"));
54 myType = (theData->getProperty("type") == "save") ? WFS_SAVE : WFS_OPEN;
55 if (myDefaultPath.isNull() || myDefaultPath.isEmpty())
56 myDefaultPath = QString::fromStdString(theData->getProperty("path"));
58 if (myDefaultPath.isEmpty())
59 myDefaultPath = Config_PropManager::string("Plugins", "import_initial_path").c_str();
61 QGridLayout* aMainLay = new QGridLayout(this);
62 ModuleBase_Tools::adjustMargins(aMainLay);
63 QLabel* aTitleLabel = new QLabel(myTitle, this);
64 aTitleLabel->setIndent(1);
65 aMainLay->addWidget(aTitleLabel, 0, 0);
66 myPathField = new QLineEdit(this);
67 aMainLay->addWidget(myPathField, 1, 0);
69 QPushButton* aSelectPathBtn = new QPushButton("...", this);
70 aSelectPathBtn->setToolTip(tr("Select file..."));
71 aSelectPathBtn->setMaximumWidth(20);
72 aSelectPathBtn->setMaximumHeight(20);
73 aMainLay->addWidget(aSelectPathBtn, 1, 1);
74 aMainLay->setColumnStretch(0, 1);
75 myPathField->setMinimumHeight(20);
76 aMainLay->setHorizontalSpacing(1);
77 this->setLayout(aMainLay);
79 connect(myPathField, SIGNAL(textChanged(const QString&)),
80 this, SLOT(onPathChanged()));
81 connect(aSelectPathBtn, SIGNAL(clicked()),
82 this, SLOT(onPathSelectionBtn()));
85 ModuleBase_WidgetFileSelector::~ModuleBase_WidgetFileSelector()
89 bool ModuleBase_WidgetFileSelector::storeValueCustom()
91 // A rare case when plugin was not loaded.
94 DataPtr aData = myFeature->data();
95 AttributeStringPtr aStringAttr = aData->string(attributeID());
96 QString aWidgetValue = myPathField->text();
97 aStringAttr->setValue(aWidgetValue.toStdWString());
98 updateObject(myFeature);
102 bool ModuleBase_WidgetFileSelector::restoreValueCustom()
104 // A rare case when plugin was not loaded.
107 DataPtr aData = myFeature->data();
108 AttributeStringPtr aStringAttr = aData->string(attributeID());
110 char16_t* aStr = aStringAttr->valueU();
111 QString aNewText = QString::fromUtf16(aStr);
112 if (myPathField->text() != aNewText) {
113 bool isBlocked = myPathField->blockSignals(true);
114 myPathField->setText(aNewText);
115 myPathField->blockSignals(isBlocked);
121 QList<QWidget*> ModuleBase_WidgetFileSelector::getControls() const
123 QList<QWidget*> result;
124 result << myPathField;
128 bool ModuleBase_WidgetFileSelector::isCurrentPathValid()
130 QFileInfo aFile(myPathField->text());
131 return aFile.exists();
134 bool ModuleBase_WidgetFileSelector::processEscape()
137 myFileDialog->reject();
140 return ModuleBase_ModelWidget::processEscape();
144 void ModuleBase_WidgetFileSelector::onPathSelectionBtn()
146 QString aDefaultPath = myPathField->text().isEmpty()
148 : QFileInfo(myPathField->text()).absolutePath();
149 QString aFilter = filterString();
151 // use Option prohibited native dialog using to have both lower/upper extensions of files
152 // satisfied to dialog filter on Linux(Calibre) Issue #2055
153 myFileDialog = new QFileDialog(this, myTitle, aDefaultPath, aFilter);
154 myFileDialog->setNameFilter(aFilter);
155 myFileDialog->setOptions(QFileDialog::DontUseNativeDialog);
156 myFileDialog->setAcceptMode(myType == WFS_SAVE ? QFileDialog::AcceptSave
157 : QFileDialog::AcceptOpen);
158 if (myFileDialog->exec() == QDialog::Accepted)
160 mySelectedFilter = myFileDialog->selectedNameFilter();
161 QStringList aFileNames = myFileDialog->selectedFiles();
162 if (!aFileNames.empty()) {
163 QString aFileName = aFileNames.first();
164 if (!aFileName.isEmpty()) {
165 if (myType == WFS_SAVE)
166 aFileName = applyExtension(aFileName, mySelectedFilter);
167 myPathField->setText(aFileName.toUtf8());
168 myDefaultPath = QFileInfo(aFileName).absolutePath();
169 emit focusOutWidget(this);
176 void ModuleBase_WidgetFileSelector::onPathChanged()
178 if (myType == WFS_OPEN && !isCurrentPathValid())
181 emit valuesChanged();
184 QString ModuleBase_WidgetFileSelector::formatToFilter(const QString & theFormat)
186 if (theFormat.isEmpty() && !theFormat.contains(":"))
189 QStringList aExtesionList = theFormat.section(':', 0, 0).split("|");
190 QString aFormat = theFormat.section(':', 1, 1);
191 return QString("%1 files (%2)").arg(aFormat)
192 .arg(QStringList(aExtesionList).replaceInStrings(QRegExp("^(.*)$"), "*.\\1").join(" "));
195 QString ModuleBase_WidgetFileSelector::filterToShortFormat(const QString & theFilter)
197 // Simplified implementation.
198 // It relies on theFilter was made by formatToFilter() function.
199 return theFilter.section(' ', 0, 0);
202 QStringList ModuleBase_WidgetFileSelector::filterToExtensions(const QString & theFilter)
204 // Simplified implementation.
205 // It relies on theFilter was made by formatToFilter() function.
206 QStringList anExtensions = theFilter.section("(", 1, 1).section(")", 0, 0).split(" ");
210 QStringList ModuleBase_WidgetFileSelector::getValidatorFormats() const
212 SessionPtr aMgr = ModelAPI_Session::get();
213 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
215 ModelAPI_ValidatorsFactory::Validators allValidators;
216 aFactory->validators(myFeature->getKind(), myAttributeID, allValidators);
219 std::list<std::string> anArgumentList = allValidators.front().second;
220 std::list<std::string>::const_iterator it = anArgumentList.begin();
221 for (; it != anArgumentList.end(); ++it) {
222 QString aFormat = QString::fromStdString(*it);
223 if (!aFormat.isEmpty())
229 QString ModuleBase_WidgetFileSelector::filterString() const
232 QStringList aValidatorFormats = getValidatorFormats();
234 foreach(const QString & eachFormat, aValidatorFormats) {
235 aResult << formatToFilter(eachFormat);
237 if (myType == WFS_OPEN)
238 aResult << QString("All files (*.*)");
239 return aResult.join(";;");
242 QString ModuleBase_WidgetFileSelector::applyExtension(const QString& theFileName,
243 const QString& theFilter)
245 QString aResult = theFileName;
246 bool hasExtension = false;
247 QStringList anExtensions = filterToExtensions(theFilter);
248 foreach(const QString& anExtension, anExtensions) {
249 if (theFileName.endsWith(QString(".") + anExtension.section(".", 1, 1), Qt::CaseInsensitive)) {
254 if (!hasExtension && !anExtensions.isEmpty())
255 aResult = QString("%1.%2").arg(theFileName).arg(anExtensions[0].section(".", 1, 1));