1 // Copyright (C) 2014-2017 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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include <ModelAPI_AttributeString.h>
22 #include <ModelAPI_Data.h>
23 #include <ModelAPI_Object.h>
24 #include <ModelAPI_Validator.h>
25 #include <ModelAPI_Session.h>
26 #include <ModuleBase_WidgetFileSelector.h>
27 #include <ModuleBase_Tools.h>
29 #include <Config_PropManager.h>
30 #include <Config_WidgetAPI.h>
32 #include <QFileDialog>
33 #include <QGridLayout>
38 #include <QPushButton>
45 ModuleBase_WidgetFileSelector::ModuleBase_WidgetFileSelector(QWidget* theParent,
46 const Config_WidgetAPI* theData)
47 : ModuleBase_ModelWidget(theParent, theData), myFileDialog(0)
49 myTitle = translate(theData->getProperty("title"));
50 myType = (theData->getProperty("type") == "save") ? WFS_SAVE : WFS_OPEN;
51 myDefaultPath = QString::fromStdString(theData->getProperty("path"));
53 if (myDefaultPath.isEmpty())
54 myDefaultPath = Config_PropManager::string("Plugins", "import_initial_path").c_str();
56 QGridLayout* aMainLay = new QGridLayout(this);
57 ModuleBase_Tools::adjustMargins(aMainLay);
58 QLabel* aTitleLabel = new QLabel(myTitle, this);
59 aTitleLabel->setIndent(1);
60 aMainLay->addWidget(aTitleLabel, 0, 0);
61 myPathField = new QLineEdit(this);
62 aMainLay->addWidget(myPathField, 1, 0);
63 QPushButton* aSelectPathBtn = new QPushButton("...", this);
64 aSelectPathBtn->setToolTip(tr("Select file..."));
65 aSelectPathBtn->setMaximumWidth(20);
66 aSelectPathBtn->setMaximumHeight(20);
67 aMainLay->addWidget(aSelectPathBtn, 1, 1);
68 aMainLay->setColumnStretch(0, 1);
69 myPathField->setMinimumHeight(20);
70 aMainLay->setHorizontalSpacing(1);
71 this->setLayout(aMainLay);
73 connect(myPathField, SIGNAL(textChanged(const QString&)),
74 this, SLOT(onPathChanged()));
75 connect(aSelectPathBtn, SIGNAL(clicked()),
76 this, SLOT(onPathSelectionBtn()));
79 ModuleBase_WidgetFileSelector::~ModuleBase_WidgetFileSelector()
83 bool ModuleBase_WidgetFileSelector::storeValueCustom()
85 // A rare case when plugin was not loaded.
88 DataPtr aData = myFeature->data();
89 AttributeStringPtr aStringAttr = aData->string(attributeID());
90 QString aWidgetValue = myPathField->text();
91 aStringAttr->setValue(aWidgetValue.toStdString());
92 updateObject(myFeature);
96 bool ModuleBase_WidgetFileSelector::restoreValueCustom()
98 // A rare case when plugin was not loaded.
101 DataPtr aData = myFeature->data();
102 AttributeStringPtr aStringAttr = aData->string(attributeID());
104 bool isBlocked = myPathField->blockSignals(true);
105 QString aNewText = QString::fromStdString(aStringAttr->value());
106 if( myPathField->text() != aNewText )
107 myPathField->setText( aNewText );
108 myPathField->blockSignals(isBlocked);
113 QList<QWidget*> ModuleBase_WidgetFileSelector::getControls() const
115 QList<QWidget*> result;
116 result << myPathField;
120 bool ModuleBase_WidgetFileSelector::isCurrentPathValid()
122 QFileInfo aFile(myPathField->text());
123 return aFile.exists();
126 bool ModuleBase_WidgetFileSelector::processEscape()
129 myFileDialog->reject();
132 return ModuleBase_ModelWidget::processEscape();
136 void ModuleBase_WidgetFileSelector::onPathSelectionBtn()
138 QString aDefaultPath = myPathField->text().isEmpty()
140 : QFileInfo(myPathField->text()).absolutePath();
141 QString aFilter = filterString();
142 // use Option prohibited native dialog using to have both lower/upper extensions of files
143 // satisfied to dialog filter on Linux(Calibre) Issue #2055
144 myFileDialog = new QFileDialog(this, myTitle, aDefaultPath, aFilter);
145 myFileDialog->setNameFilter(aFilter);
146 myFileDialog->setOptions(QFileDialog::DontUseNativeDialog);
147 myFileDialog->setAcceptMode(myType == WFS_SAVE ? QFileDialog::AcceptSave
148 : QFileDialog::AcceptOpen);
149 if (myFileDialog->exec() == QDialog::Accepted)
151 mySelectedFilter = myFileDialog->selectedNameFilter();
152 QStringList aFileNames = myFileDialog->selectedFiles();
153 if (!aFileNames.empty()) {
154 QString aFileName = aFileNames.first();
155 if (!aFileName.isEmpty()) {
156 if (myType == WFS_SAVE)
157 aFileName = applyExtension(aFileName, mySelectedFilter);
158 myPathField->setText(aFileName);
159 emit focusOutWidget(this);
166 void ModuleBase_WidgetFileSelector::onPathChanged()
168 if (myType == WFS_OPEN && !isCurrentPathValid())
171 emit valuesChanged();
174 QString ModuleBase_WidgetFileSelector::formatToFilter(const QString & theFormat)
176 if (theFormat.isEmpty() && !theFormat.contains(":"))
179 QStringList aExtesionList = theFormat.section(':', 0, 0).split("|");
180 QString aFormat = theFormat.section(':', 1, 1);
181 return QString("%1 files (%2)").arg(aFormat)
182 .arg(QStringList(aExtesionList).replaceInStrings(QRegExp("^(.*)$"), "*.\\1").join(" "));
185 QString ModuleBase_WidgetFileSelector::filterToShortFormat(const QString & theFilter)
187 // Simplified implementation.
188 // It relies on theFilter was made by formatToFilter() function.
189 return theFilter.section(' ', 0, 0);
192 QStringList ModuleBase_WidgetFileSelector::filterToExtensions(const QString & theFilter)
194 // Simplified implementation.
195 // It relies on theFilter was made by formatToFilter() function.
196 QStringList anExtensions = theFilter.section("(", 1, 1).section(")", 0, 0).split(" ");
200 QStringList ModuleBase_WidgetFileSelector::getValidatorFormats() const
202 SessionPtr aMgr = ModelAPI_Session::get();
203 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
205 ModelAPI_ValidatorsFactory::Validators allValidators;
206 aFactory->validators(myFeature->getKind(), myAttributeID, allValidators);
209 std::list<std::string> anArgumentList = allValidators.front().second;
210 std::list<std::string>::const_iterator it = anArgumentList.begin();
211 for (; it != anArgumentList.end(); ++it) {
212 QString aFormat = QString::fromStdString(*it);
213 if (!aFormat.isEmpty())
219 QString ModuleBase_WidgetFileSelector::filterString() const
222 QStringList aValidatorFormats = getValidatorFormats();
224 foreach(const QString & eachFormat, aValidatorFormats) {
225 aResult << formatToFilter(eachFormat);
227 if (myType == WFS_OPEN)
228 aResult << QString("All files (*.*)");
229 return aResult.join(";;");
232 QString ModuleBase_WidgetFileSelector::applyExtension(const QString& theFileName,
233 const QString& theFilter)
235 QString aResult = theFileName;
236 bool hasExtension = false;
237 QStringList anExtensions = filterToExtensions(theFilter);
238 foreach(const QString& anExtension, anExtensions) {
239 if (theFileName.endsWith(anExtension.section(".", 1, 1), Qt::CaseInsensitive)) {
244 if (!hasExtension && !anExtensions.isEmpty())
245 aResult = QString("%1.%2").arg(theFileName).arg(anExtensions[0].section(".", 1, 1));