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>
47 static QString myDefaultPath;
50 ModuleBase_WidgetFileSelector::ModuleBase_WidgetFileSelector(QWidget* theParent,
51 const Config_WidgetAPI* theData)
52 : ModuleBase_ModelWidget(theParent, theData), myFileDialog(0)
54 myTitle = translate(theData->getProperty("title"));
55 myType = (theData->getProperty("type") == "save") ? WFS_SAVE : WFS_OPEN;
56 if (myDefaultPath.isNull() || myDefaultPath.isEmpty())
57 myDefaultPath = QString::fromStdString(theData->getProperty("path"));
59 if (myDefaultPath.isEmpty())
60 myDefaultPath = Config_PropManager::string("Plugins", "import_initial_path").c_str();
62 QGridLayout* aMainLay = new QGridLayout(this);
63 ModuleBase_Tools::adjustMargins(aMainLay);
64 QLabel* aTitleLabel = new QLabel(myTitle, this);
65 aTitleLabel->setIndent(1);
66 aMainLay->addWidget(aTitleLabel, 0, 0);
67 myPathField = new QLineEdit(this);
68 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.toStdString());
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 bool isBlocked = myPathField->blockSignals(true);
111 QString aNewText = QString::fromStdString(aStringAttr->value());
112 if( myPathField->text() != aNewText )
113 myPathField->setText( aNewText );
114 myPathField->blockSignals(isBlocked);
119 QList<QWidget*> ModuleBase_WidgetFileSelector::getControls() const
121 QList<QWidget*> result;
122 result << myPathField;
126 bool ModuleBase_WidgetFileSelector::isCurrentPathValid()
128 QFileInfo aFile(myPathField->text());
129 return aFile.exists();
132 bool ModuleBase_WidgetFileSelector::processEscape()
135 myFileDialog->reject();
138 return ModuleBase_ModelWidget::processEscape();
142 void ModuleBase_WidgetFileSelector::onPathSelectionBtn()
144 QString aDefaultPath = myPathField->text().isEmpty()
146 : QFileInfo(myPathField->text()).absolutePath();
147 QString aFilter = filterString();
149 // use Option prohibited native dialog using to have both lower/upper extensions of files
150 // satisfied to dialog filter on Linux(Calibre) Issue #2055
151 myFileDialog = new QFileDialog(this, myTitle, aDefaultPath, aFilter);
152 myFileDialog->setNameFilter(aFilter);
153 myFileDialog->setOptions(QFileDialog::DontUseNativeDialog);
154 myFileDialog->setAcceptMode(myType == WFS_SAVE ? QFileDialog::AcceptSave
155 : QFileDialog::AcceptOpen);
156 if (myFileDialog->exec() == QDialog::Accepted)
158 mySelectedFilter = myFileDialog->selectedNameFilter();
159 QStringList aFileNames = myFileDialog->selectedFiles();
160 if (!aFileNames.empty()) {
161 QString aFileName = aFileNames.first();
162 if (!aFileName.isEmpty()) {
163 if (myType == WFS_SAVE)
164 aFileName = applyExtension(aFileName, mySelectedFilter);
165 myPathField->setText(aFileName);
166 myDefaultPath = QFileInfo(aFileName).absolutePath();
167 emit focusOutWidget(this);
174 void ModuleBase_WidgetFileSelector::onPathChanged()
176 if (myType == WFS_OPEN && !isCurrentPathValid())
179 emit valuesChanged();
182 QString ModuleBase_WidgetFileSelector::formatToFilter(const QString & theFormat)
184 if (theFormat.isEmpty() && !theFormat.contains(":"))
187 QStringList aExtesionList = theFormat.section(':', 0, 0).split("|");
188 QString aFormat = theFormat.section(':', 1, 1);
189 return QString("%1 files (%2)").arg(aFormat)
190 .arg(QStringList(aExtesionList).replaceInStrings(QRegExp("^(.*)$"), "*.\\1").join(" "));
193 QString ModuleBase_WidgetFileSelector::filterToShortFormat(const QString & theFilter)
195 // Simplified implementation.
196 // It relies on theFilter was made by formatToFilter() function.
197 return theFilter.section(' ', 0, 0);
200 QStringList ModuleBase_WidgetFileSelector::filterToExtensions(const QString & theFilter)
202 // Simplified implementation.
203 // It relies on theFilter was made by formatToFilter() function.
204 QStringList anExtensions = theFilter.section("(", 1, 1).section(")", 0, 0).split(" ");
208 QStringList ModuleBase_WidgetFileSelector::getValidatorFormats() const
210 SessionPtr aMgr = ModelAPI_Session::get();
211 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
213 ModelAPI_ValidatorsFactory::Validators allValidators;
214 aFactory->validators(myFeature->getKind(), myAttributeID, allValidators);
217 std::list<std::string> anArgumentList = allValidators.front().second;
218 std::list<std::string>::const_iterator it = anArgumentList.begin();
219 for (; it != anArgumentList.end(); ++it) {
220 QString aFormat = QString::fromStdString(*it);
221 if (!aFormat.isEmpty())
227 QString ModuleBase_WidgetFileSelector::filterString() const
230 QStringList aValidatorFormats = getValidatorFormats();
232 foreach(const QString & eachFormat, aValidatorFormats) {
233 aResult << formatToFilter(eachFormat);
235 if (myType == WFS_OPEN)
236 aResult << QString("All files (*.*)");
237 return aResult.join(";;");
240 QString ModuleBase_WidgetFileSelector::applyExtension(const QString& theFileName,
241 const QString& theFilter)
243 QString aResult = theFileName;
244 bool hasExtension = false;
245 QStringList anExtensions = filterToExtensions(theFilter);
246 foreach(const QString& anExtension, anExtensions) {
247 if (theFileName.endsWith(anExtension.section(".", 1, 1), Qt::CaseInsensitive)) {
252 if (!hasExtension && !anExtensions.isEmpty())
253 aResult = QString("%1.%2").arg(theFileName).arg(anExtensions[0].section(".", 1, 1));