Salome HOME
Issue #2436: Remember last visited directory as default path for saving files
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetFileSelector.cpp
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
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>
28
29 #include <Config_PropManager.h>
30 #include <Config_WidgetAPI.h>
31
32 #include <QFileDialog>
33 #include <QGridLayout>
34 #include <QLabel>
35 #include <QLineEdit>
36 #include <QList>
37 #include <QObject>
38 #include <QPushButton>
39 #include <QRegExp>
40 #include <QString>
41
42 #include <memory>
43 #include <string>
44
45
46 /// Default path
47 static QString myDefaultPath;
48
49
50 ModuleBase_WidgetFileSelector::ModuleBase_WidgetFileSelector(QWidget* theParent,
51                                                              const Config_WidgetAPI* theData)
52 : ModuleBase_ModelWidget(theParent, theData), myFileDialog(0)
53 {
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"));
58
59   if (myDefaultPath.isEmpty())
60     myDefaultPath = Config_PropManager::string("Plugins", "import_initial_path").c_str();
61
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);
78
79   connect(myPathField, SIGNAL(textChanged(const QString&)),
80           this,        SLOT(onPathChanged()));
81   connect(aSelectPathBtn, SIGNAL(clicked()),
82           this,        SLOT(onPathSelectionBtn()));
83 }
84
85 ModuleBase_WidgetFileSelector::~ModuleBase_WidgetFileSelector()
86 {
87 }
88
89 bool ModuleBase_WidgetFileSelector::storeValueCustom()
90 {
91   // A rare case when plugin was not loaded.
92   if (!myFeature)
93     return false;
94   DataPtr aData = myFeature->data();
95   AttributeStringPtr aStringAttr = aData->string(attributeID());
96   QString aWidgetValue = myPathField->text();
97   aStringAttr->setValue(aWidgetValue.toStdString());
98   updateObject(myFeature);
99   return true;
100 }
101
102 bool ModuleBase_WidgetFileSelector::restoreValueCustom()
103 {
104   // A rare case when plugin was not loaded.
105   if (!myFeature)
106     return false;
107   DataPtr aData = myFeature->data();
108   AttributeStringPtr aStringAttr = aData->string(attributeID());
109
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);
115
116   return true;
117 }
118
119 QList<QWidget*> ModuleBase_WidgetFileSelector::getControls() const
120 {
121   QList<QWidget*> result;
122   result << myPathField;
123   return result;
124 }
125
126 bool ModuleBase_WidgetFileSelector::isCurrentPathValid()
127 {
128   QFileInfo aFile(myPathField->text());
129   return aFile.exists();
130 }
131
132 bool ModuleBase_WidgetFileSelector::processEscape()
133 {
134   if (myFileDialog) {
135     myFileDialog->reject();
136     return true;
137   }
138   return ModuleBase_ModelWidget::processEscape();
139 }
140
141
142 void ModuleBase_WidgetFileSelector::onPathSelectionBtn()
143 {
144   QString aDefaultPath = myPathField->text().isEmpty()
145       ? myDefaultPath
146       : QFileInfo(myPathField->text()).absolutePath();
147   QString aFilter = filterString();
148
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)
157   {
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);
168       }
169     }
170   }
171   myFileDialog = 0;
172 }
173
174 void ModuleBase_WidgetFileSelector::onPathChanged()
175 {
176   if (myType == WFS_OPEN && !isCurrentPathValid())
177     return;
178   storeValue();
179   emit valuesChanged();
180 }
181
182 QString ModuleBase_WidgetFileSelector::formatToFilter(const QString & theFormat)
183 {
184   if (theFormat.isEmpty() && !theFormat.contains(":"))
185     return QString();
186
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(" "));
191 }
192
193 QString ModuleBase_WidgetFileSelector::filterToShortFormat(const QString & theFilter)
194 {
195   // Simplified implementation.
196   // It relies on theFilter was made by formatToFilter() function.
197   return theFilter.section(' ', 0, 0);
198 }
199
200 QStringList ModuleBase_WidgetFileSelector::filterToExtensions(const QString & theFilter)
201 {
202   // Simplified implementation.
203   // It relies on theFilter was made by formatToFilter() function.
204   QStringList anExtensions = theFilter.section("(", 1, 1).section(")", 0, 0).split(" ");
205   return anExtensions;
206 }
207
208 QStringList ModuleBase_WidgetFileSelector::getValidatorFormats() const
209 {
210   SessionPtr aMgr = ModelAPI_Session::get();
211   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
212
213   ModelAPI_ValidatorsFactory::Validators allValidators;
214   aFactory->validators(myFeature->getKind(), myAttributeID, allValidators);
215
216   QStringList aResult;
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())
222       aResult << aFormat;
223   }
224   return aResult;
225 }
226
227 QString ModuleBase_WidgetFileSelector::filterString() const
228 {
229   QStringList aResult;
230   QStringList aValidatorFormats = getValidatorFormats();
231
232   foreach(const QString & eachFormat, aValidatorFormats) {
233     aResult << formatToFilter(eachFormat);
234   }
235   if (myType == WFS_OPEN)
236     aResult << QString("All files (*.*)");
237   return aResult.join(";;");
238 }
239
240 QString ModuleBase_WidgetFileSelector::applyExtension(const QString& theFileName,
241                                                       const QString& theFilter)
242 {
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)) {
248       hasExtension = true;
249       break;
250     }
251   }
252   if (!hasExtension && !anExtensions.isEmpty())
253     aResult = QString("%1.%2").arg(theFileName).arg(anExtensions[0].section(".", 1, 1));
254   return aResult;
255 }