Salome HOME
Key_Escape processing in File selector widget. Otherwise, crash happens if file dialo...
[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 ModuleBase_WidgetFileSelector::ModuleBase_WidgetFileSelector(QWidget* theParent,
46                                                              const Config_WidgetAPI* theData)
47 : ModuleBase_ModelWidget(theParent, theData), myFileDialog(0)
48 {
49   myTitle = translate(theData->getProperty("title"));
50   myType = (theData->getProperty("type") == "save") ? WFS_SAVE : WFS_OPEN;
51   myDefaultPath = QString::fromStdString(theData->getProperty("path"));
52
53   if (myDefaultPath.isEmpty())
54     myDefaultPath = Config_PropManager::string("Plugins", "import_initial_path").c_str();
55
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);
72
73   connect(myPathField, SIGNAL(textChanged(const QString&)),
74           this,        SLOT(onPathChanged()));
75   connect(aSelectPathBtn, SIGNAL(clicked()),
76           this,        SLOT(onPathSelectionBtn()));
77 }
78
79 ModuleBase_WidgetFileSelector::~ModuleBase_WidgetFileSelector()
80 {
81 }
82
83 bool ModuleBase_WidgetFileSelector::storeValueCustom()
84 {
85   // A rare case when plugin was not loaded.
86   if (!myFeature)
87     return false;
88   DataPtr aData = myFeature->data();
89   AttributeStringPtr aStringAttr = aData->string(attributeID());
90   QString aWidgetValue = myPathField->text();
91   aStringAttr->setValue(aWidgetValue.toStdString());
92   updateObject(myFeature);
93   return true;
94 }
95
96 bool ModuleBase_WidgetFileSelector::restoreValueCustom()
97 {
98   // A rare case when plugin was not loaded.
99   if (!myFeature)
100     return false;
101   DataPtr aData = myFeature->data();
102   AttributeStringPtr aStringAttr = aData->string(attributeID());
103
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);
109
110   return true;
111 }
112
113 QList<QWidget*> ModuleBase_WidgetFileSelector::getControls() const
114 {
115   QList<QWidget*> result;
116   result << myPathField;
117   return result;
118 }
119
120 bool ModuleBase_WidgetFileSelector::isCurrentPathValid()
121 {
122   QFileInfo aFile(myPathField->text());
123   return aFile.exists();
124 }
125
126 bool ModuleBase_WidgetFileSelector::processEscape()
127 {
128   if (myFileDialog) {
129     myFileDialog->reject();
130     return true;
131   }
132   return ModuleBase_ModelWidget::processEscape();
133 }
134
135
136 void ModuleBase_WidgetFileSelector::onPathSelectionBtn()
137 {
138   QString aDefaultPath = myPathField->text().isEmpty()
139       ? myDefaultPath
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)
150   {
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);
160       }
161     }
162   }
163   myFileDialog = 0;
164 }
165
166 void ModuleBase_WidgetFileSelector::onPathChanged()
167 {
168   if (myType == WFS_OPEN && !isCurrentPathValid())
169     return;
170   storeValue();
171   emit valuesChanged();
172 }
173
174 QString ModuleBase_WidgetFileSelector::formatToFilter(const QString & theFormat)
175 {
176   if (theFormat.isEmpty() && !theFormat.contains(":"))
177     return QString();
178
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(" "));
183 }
184
185 QString ModuleBase_WidgetFileSelector::filterToShortFormat(const QString & theFilter)
186 {
187   // Simplified implementation.
188   // It relies on theFilter was made by formatToFilter() function.
189   return theFilter.section(' ', 0, 0);
190 }
191
192 QStringList ModuleBase_WidgetFileSelector::filterToExtensions(const QString & theFilter)
193 {
194   // Simplified implementation.
195   // It relies on theFilter was made by formatToFilter() function.
196   QStringList anExtensions = theFilter.section("(", 1, 1).section(")", 0, 0).split(" ");
197   return anExtensions;
198 }
199
200 QStringList ModuleBase_WidgetFileSelector::getValidatorFormats() const
201 {
202   SessionPtr aMgr = ModelAPI_Session::get();
203   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
204
205   ModelAPI_ValidatorsFactory::Validators allValidators;
206   aFactory->validators(myFeature->getKind(), myAttributeID, allValidators);
207
208   QStringList aResult;
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())
214       aResult << aFormat;
215   }
216   return aResult;
217 }
218
219 QString ModuleBase_WidgetFileSelector::filterString() const
220 {
221   QStringList aResult;
222   QStringList aValidatorFormats = getValidatorFormats();
223
224   foreach(const QString & eachFormat, aValidatorFormats) {
225     aResult << formatToFilter(eachFormat);
226   }
227   if (myType == WFS_OPEN)
228     aResult << QString("All files (*.*)");
229   return aResult.join(";;");
230 }
231
232 QString ModuleBase_WidgetFileSelector::applyExtension(const QString& theFileName,
233                                                       const QString& theFilter)
234 {
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)) {
240       hasExtension = true;
241       break;
242     }
243   }
244   if (!hasExtension && !anExtensions.isEmpty())
245     aResult = QString("%1.%2").arg(theFileName).arg(anExtensions[0].section(".", 1, 1));
246   return aResult;
247 }