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 email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 * ModuleBase_WidgetFileSelector.cpp
23 * Created on: Aug 28, 2014
27 #include <ModelAPI_AttributeString.h>
28 #include <ModelAPI_Data.h>
29 #include <ModelAPI_Object.h>
30 #include <ModelAPI_Validator.h>
31 #include <ModelAPI_Session.h>
32 #include <ModuleBase_WidgetFileSelector.h>
33 #include <ModuleBase_Tools.h>
35 #include <Config_WidgetAPI.h>
37 #include <QFileDialog>
38 #include <QGridLayout>
43 #include <QPushButton>
50 ModuleBase_WidgetFileSelector::ModuleBase_WidgetFileSelector(QWidget* theParent,
51 const Config_WidgetAPI* theData)
52 : ModuleBase_ModelWidget(theParent, theData)
54 myTitle = translate(theData->getProperty("title"));
55 myType = (theData->getProperty("type") == "save") ? WFS_SAVE : WFS_OPEN;
56 myDefaultPath = QString::fromStdString(theData->getProperty("path"));
58 QGridLayout* aMainLay = new QGridLayout(this);
59 ModuleBase_Tools::adjustMargins(aMainLay);
60 QLabel* aTitleLabel = new QLabel(myTitle, this);
61 aTitleLabel->setIndent(1);
62 aMainLay->addWidget(aTitleLabel, 0, 0);
63 myPathField = new QLineEdit(this);
64 aMainLay->addWidget(myPathField, 1, 0);
65 QPushButton* aSelectPathBtn = new QPushButton("...", this);
66 aSelectPathBtn->setToolTip(tr("Select file..."));
67 aSelectPathBtn->setMaximumWidth(20);
68 aSelectPathBtn->setMaximumHeight(20);
69 aMainLay->addWidget(aSelectPathBtn, 1, 1);
70 aMainLay->setColumnStretch(0, 1);
71 myPathField->setMinimumHeight(20);
72 aMainLay->setHorizontalSpacing(1);
73 this->setLayout(aMainLay);
75 connect(myPathField, SIGNAL(textChanged(const QString&)),
76 this, SLOT(onPathChanged()));
77 connect(aSelectPathBtn, SIGNAL(clicked()),
78 this, SLOT(onPathSelectionBtn()));
81 ModuleBase_WidgetFileSelector::~ModuleBase_WidgetFileSelector()
85 bool ModuleBase_WidgetFileSelector::storeValueCustom()
87 // A rare case when plugin was not loaded.
90 DataPtr aData = myFeature->data();
91 AttributeStringPtr aStringAttr = aData->string(attributeID());
92 QString aWidgetValue = myPathField->text();
93 aStringAttr->setValue(aWidgetValue.toStdString());
94 updateObject(myFeature);
98 bool ModuleBase_WidgetFileSelector::restoreValueCustom()
100 // A rare case when plugin was not loaded.
103 DataPtr aData = myFeature->data();
104 AttributeStringPtr aStringAttr = aData->string(attributeID());
106 bool isBlocked = myPathField->blockSignals(true);
107 QString aNewText = QString::fromStdString(aStringAttr->value());
108 if( myPathField->text() != aNewText )
109 myPathField->setText( aNewText );
110 myPathField->blockSignals(isBlocked);
115 QList<QWidget*> ModuleBase_WidgetFileSelector::getControls() const
117 QList<QWidget*> result;
118 result << myPathField;
122 bool ModuleBase_WidgetFileSelector::isCurrentPathValid()
124 QFileInfo aFile(myPathField->text());
125 return aFile.exists();
128 void ModuleBase_WidgetFileSelector::onPathSelectionBtn()
130 QString aDefaultPath = myPathField->text().isEmpty()
132 : QFileInfo(myPathField->text()).absolutePath();
133 QString aFilter = filterString();
134 // use Option prohibited native dialog using to have both lower/upper extensions of files
135 // satisfied to dialog filter on Linux(Calibre) Issue #2055
136 QString aFileName = (myType == WFS_SAVE)
137 ? QFileDialog::getSaveFileName(this, myTitle, aDefaultPath, aFilter, &mySelectedFilter,
138 QFileDialog::DontUseNativeDialog)
139 : QFileDialog::getOpenFileName(this, myTitle, aDefaultPath, aFilter, &mySelectedFilter,
140 QFileDialog::DontUseNativeDialog);
141 if (!aFileName.isEmpty()) {
142 if (myType == WFS_SAVE)
143 aFileName = applyExtension(aFileName, mySelectedFilter);
144 myPathField->setText(aFileName);
145 emit focusOutWidget(this);
149 void ModuleBase_WidgetFileSelector::onPathChanged()
151 if (myType == WFS_OPEN && !isCurrentPathValid())
154 emit valuesChanged();
157 QString ModuleBase_WidgetFileSelector::formatToFilter(const QString & theFormat)
159 if (theFormat.isEmpty() && !theFormat.contains(":"))
162 QStringList aExtesionList = theFormat.section(':', 0, 0).split("|");
163 QString aFormat = theFormat.section(':', 1, 1);
164 return QString("%1 files (%2)").arg(aFormat)
165 .arg(QStringList(aExtesionList).replaceInStrings(QRegExp("^(.*)$"), "*.\\1").join(" "));
168 QString ModuleBase_WidgetFileSelector::filterToShortFormat(const QString & theFilter)
170 // Simplified implementation.
171 // It relies on theFilter was made by formatToFilter() function.
172 return theFilter.section(' ', 0, 0);
175 QStringList ModuleBase_WidgetFileSelector::filterToExtensions(const QString & theFilter)
177 // Simplified implementation.
178 // It relies on theFilter was made by formatToFilter() function.
179 QStringList anExtensions = theFilter.section("(", 1, 1).section(")", 0, 0).split(" ");
183 QStringList ModuleBase_WidgetFileSelector::getValidatorFormats() const
185 SessionPtr aMgr = ModelAPI_Session::get();
186 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
188 ModelAPI_ValidatorsFactory::Validators allValidators;
189 aFactory->validators(myFeature->getKind(), myAttributeID, allValidators);
192 std::list<std::string> anArgumentList = allValidators.front().second;
193 std::list<std::string>::const_iterator it = anArgumentList.begin();
194 for (; it != anArgumentList.end(); ++it) {
195 QString aFormat = QString::fromStdString(*it);
196 if (!aFormat.isEmpty())
202 QString ModuleBase_WidgetFileSelector::filterString() const
205 QStringList aValidatorFormats = getValidatorFormats();
207 foreach(const QString & eachFormat, aValidatorFormats) {
208 aResult << formatToFilter(eachFormat);
210 if (myType == WFS_OPEN)
211 aResult << QString("All files (*.*)");
212 return aResult.join(";;");
215 QString ModuleBase_WidgetFileSelector::applyExtension(const QString& theFileName,
216 const QString& theFilter)
218 QString aResult = theFileName;
219 bool hasExtension = false;
220 QStringList anExtensions = filterToExtensions(theFilter);
221 foreach(const QString& anExtension, anExtensions) {
222 if (theFileName.endsWith(anExtension.section(".", 1, 1), Qt::CaseInsensitive)) {
227 if (!hasExtension && !anExtensions.isEmpty())
228 aResult = QString("%1.%2").arg(theFileName).arg(anExtensions[0].section(".", 1, 1));