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_WidgetAPI.h>
31 #include <QFileDialog>
32 #include <QGridLayout>
37 #include <QPushButton>
44 ModuleBase_WidgetFileSelector::ModuleBase_WidgetFileSelector(QWidget* theParent,
45 const Config_WidgetAPI* theData)
46 : ModuleBase_ModelWidget(theParent, theData)
48 myTitle = translate(theData->getProperty("title"));
49 myType = (theData->getProperty("type") == "save") ? WFS_SAVE : WFS_OPEN;
50 myDefaultPath = QString::fromStdString(theData->getProperty("path"));
52 QGridLayout* aMainLay = new QGridLayout(this);
53 ModuleBase_Tools::adjustMargins(aMainLay);
54 QLabel* aTitleLabel = new QLabel(myTitle, this);
55 aTitleLabel->setIndent(1);
56 aMainLay->addWidget(aTitleLabel, 0, 0);
57 myPathField = new QLineEdit(this);
58 aMainLay->addWidget(myPathField, 1, 0);
59 QPushButton* aSelectPathBtn = new QPushButton("...", this);
60 aSelectPathBtn->setToolTip(tr("Select file..."));
61 aSelectPathBtn->setMaximumWidth(20);
62 aSelectPathBtn->setMaximumHeight(20);
63 aMainLay->addWidget(aSelectPathBtn, 1, 1);
64 aMainLay->setColumnStretch(0, 1);
65 myPathField->setMinimumHeight(20);
66 aMainLay->setHorizontalSpacing(1);
67 this->setLayout(aMainLay);
69 connect(myPathField, SIGNAL(textChanged(const QString&)),
70 this, SLOT(onPathChanged()));
71 connect(aSelectPathBtn, SIGNAL(clicked()),
72 this, SLOT(onPathSelectionBtn()));
75 ModuleBase_WidgetFileSelector::~ModuleBase_WidgetFileSelector()
79 bool ModuleBase_WidgetFileSelector::storeValueCustom()
81 // A rare case when plugin was not loaded.
84 DataPtr aData = myFeature->data();
85 AttributeStringPtr aStringAttr = aData->string(attributeID());
86 QString aWidgetValue = myPathField->text();
87 aStringAttr->setValue(aWidgetValue.toStdString());
88 updateObject(myFeature);
92 bool ModuleBase_WidgetFileSelector::restoreValueCustom()
94 // A rare case when plugin was not loaded.
97 DataPtr aData = myFeature->data();
98 AttributeStringPtr aStringAttr = aData->string(attributeID());
100 bool isBlocked = myPathField->blockSignals(true);
101 QString aNewText = QString::fromStdString(aStringAttr->value());
102 if( myPathField->text() != aNewText )
103 myPathField->setText( aNewText );
104 myPathField->blockSignals(isBlocked);
109 QList<QWidget*> ModuleBase_WidgetFileSelector::getControls() const
111 QList<QWidget*> result;
112 result << myPathField;
116 bool ModuleBase_WidgetFileSelector::isCurrentPathValid()
118 QFileInfo aFile(myPathField->text());
119 return aFile.exists();
122 void ModuleBase_WidgetFileSelector::onPathSelectionBtn()
124 QString aDefaultPath = myPathField->text().isEmpty()
126 : QFileInfo(myPathField->text()).absolutePath();
127 QString aFilter = filterString();
128 // use Option prohibited native dialog using to have both lower/upper extensions of files
129 // satisfied to dialog filter on Linux(Calibre) Issue #2055
130 QString aFileName = (myType == WFS_SAVE)
131 ? QFileDialog::getSaveFileName(this, myTitle, aDefaultPath, aFilter, &mySelectedFilter,
132 QFileDialog::DontUseNativeDialog)
133 : QFileDialog::getOpenFileName(this, myTitle, aDefaultPath, aFilter, &mySelectedFilter,
134 QFileDialog::DontUseNativeDialog);
135 if (!aFileName.isEmpty()) {
136 if (myType == WFS_SAVE)
137 aFileName = applyExtension(aFileName, mySelectedFilter);
138 myPathField->setText(aFileName);
139 emit focusOutWidget(this);
143 void ModuleBase_WidgetFileSelector::onPathChanged()
145 if (myType == WFS_OPEN && !isCurrentPathValid())
148 emit valuesChanged();
151 QString ModuleBase_WidgetFileSelector::formatToFilter(const QString & theFormat)
153 if (theFormat.isEmpty() && !theFormat.contains(":"))
156 QStringList aExtesionList = theFormat.section(':', 0, 0).split("|");
157 QString aFormat = theFormat.section(':', 1, 1);
158 return QString("%1 files (%2)").arg(aFormat)
159 .arg(QStringList(aExtesionList).replaceInStrings(QRegExp("^(.*)$"), "*.\\1").join(" "));
162 QString ModuleBase_WidgetFileSelector::filterToShortFormat(const QString & theFilter)
164 // Simplified implementation.
165 // It relies on theFilter was made by formatToFilter() function.
166 return theFilter.section(' ', 0, 0);
169 QStringList ModuleBase_WidgetFileSelector::filterToExtensions(const QString & theFilter)
171 // Simplified implementation.
172 // It relies on theFilter was made by formatToFilter() function.
173 QStringList anExtensions = theFilter.section("(", 1, 1).section(")", 0, 0).split(" ");
177 QStringList ModuleBase_WidgetFileSelector::getValidatorFormats() const
179 SessionPtr aMgr = ModelAPI_Session::get();
180 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
182 ModelAPI_ValidatorsFactory::Validators allValidators;
183 aFactory->validators(myFeature->getKind(), myAttributeID, allValidators);
186 std::list<std::string> anArgumentList = allValidators.front().second;
187 std::list<std::string>::const_iterator it = anArgumentList.begin();
188 for (; it != anArgumentList.end(); ++it) {
189 QString aFormat = QString::fromStdString(*it);
190 if (!aFormat.isEmpty())
196 QString ModuleBase_WidgetFileSelector::filterString() const
199 QStringList aValidatorFormats = getValidatorFormats();
201 foreach(const QString & eachFormat, aValidatorFormats) {
202 aResult << formatToFilter(eachFormat);
204 if (myType == WFS_OPEN)
205 aResult << QString("All files (*.*)");
206 return aResult.join(";;");
209 QString ModuleBase_WidgetFileSelector::applyExtension(const QString& theFileName,
210 const QString& theFilter)
212 QString aResult = theFileName;
213 bool hasExtension = false;
214 QStringList anExtensions = filterToExtensions(theFilter);
215 foreach(const QString& anExtension, anExtensions) {
216 if (theFileName.endsWith(anExtension.section(".", 1, 1), Qt::CaseInsensitive)) {
221 if (!hasExtension && !anExtensions.isEmpty())
222 aResult = QString("%1.%2").arg(theFileName).arg(anExtensions[0].section(".", 1, 1));