Salome HOME
Copyright update 2022
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetFileSelector.cpp
1 // Copyright (C) 2014-2022  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 email : webmaster.salome@opencascade.com
18 //
19
20 #include <ModelAPI_AttributeString.h>
21 #include <ModelAPI_Data.h>
22 #include <ModelAPI_Object.h>
23 #include <ModelAPI_Validator.h>
24 #include <ModelAPI_Session.h>
25 #include <ModuleBase_WidgetFileSelector.h>
26 #include <ModuleBase_Tools.h>
27
28 #include <Config_PropManager.h>
29 #include <Config_WidgetAPI.h>
30
31 #include <QFileDialog>
32 #include <QGridLayout>
33 #include <QLabel>
34 #include <QLineEdit>
35 #include <QList>
36 #include <QObject>
37 #include <QPushButton>
38 #include <QRegExp>
39 #include <QString>
40
41 #include <memory>
42 #include <string>
43
44
45 /// Default path
46 static QString myDefaultPath;
47
48
49 ModuleBase_WidgetFileSelector::ModuleBase_WidgetFileSelector(QWidget* theParent,
50                                                              const Config_WidgetAPI* theData)
51 : ModuleBase_ModelWidget(theParent, theData), myFileDialog(0)
52 {
53   myTitle = translate(theData->getProperty("title"));
54   myType = (theData->getProperty("type") == "save") ? WFS_SAVE : WFS_OPEN;
55   if (myDefaultPath.isNull() || myDefaultPath.isEmpty())
56     myDefaultPath = QString::fromStdString(theData->getProperty("path"));
57
58   if (myDefaultPath.isEmpty())
59     myDefaultPath = Config_PropManager::string("Plugins", "import_initial_path").c_str();
60
61   QGridLayout* aMainLay = new QGridLayout(this);
62   ModuleBase_Tools::adjustMargins(aMainLay);
63   QLabel* aTitleLabel = new QLabel(myTitle, this);
64   aTitleLabel->setIndent(1);
65   aMainLay->addWidget(aTitleLabel, 0, 0);
66   myPathField = new QLineEdit(this);
67   aMainLay->addWidget(myPathField, 1, 0);
68
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.toStdWString());
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   char16_t* aStr = aStringAttr->valueU();
111   QString aNewText = QString::fromUtf16(aStr);
112   if (myPathField->text() != aNewText) {
113     bool isBlocked = myPathField->blockSignals(true);
114     myPathField->setText(aNewText);
115     myPathField->blockSignals(isBlocked);
116   }
117
118   return true;
119 }
120
121 QList<QWidget*> ModuleBase_WidgetFileSelector::getControls() const
122 {
123   QList<QWidget*> result;
124   result << myPathField;
125   return result;
126 }
127
128 bool ModuleBase_WidgetFileSelector::isCurrentPathValid()
129 {
130   QFileInfo aFile(myPathField->text());
131   return aFile.exists();
132 }
133
134 bool ModuleBase_WidgetFileSelector::processEscape()
135 {
136   if (myFileDialog) {
137     myFileDialog->reject();
138     return true;
139   }
140   return ModuleBase_ModelWidget::processEscape();
141 }
142
143
144 void ModuleBase_WidgetFileSelector::onPathSelectionBtn()
145 {
146   QString aDefaultPath = myPathField->text().isEmpty()
147       ? myDefaultPath
148       : QFileInfo(myPathField->text()).absolutePath();
149   QString aFilter = filterString();
150
151   // use Option prohibited native dialog using to have both lower/upper extensions of files
152   // satisfied to dialog filter on Linux(Calibre) Issue #2055
153   myFileDialog = new QFileDialog(this, myTitle, aDefaultPath, aFilter);
154   myFileDialog->setNameFilter(aFilter);
155   myFileDialog->setOptions(QFileDialog::DontUseNativeDialog);
156   myFileDialog->setAcceptMode(myType == WFS_SAVE ? QFileDialog::AcceptSave
157                                                  : QFileDialog::AcceptOpen);
158   if (myFileDialog->exec() == QDialog::Accepted)
159   {
160     mySelectedFilter = myFileDialog->selectedNameFilter();
161     QStringList aFileNames = myFileDialog->selectedFiles();
162     if (!aFileNames.empty()) {
163       QString aFileName = aFileNames.first();
164       if (!aFileName.isEmpty()) {
165         if (myType == WFS_SAVE)
166           aFileName = applyExtension(aFileName, mySelectedFilter);
167         myPathField->setText(aFileName.toUtf8());
168         myDefaultPath = QFileInfo(aFileName).absolutePath();
169         emit focusOutWidget(this);
170       }
171     }
172   }
173   myFileDialog = 0;
174 }
175
176 void ModuleBase_WidgetFileSelector::onPathChanged()
177 {
178   if (myType == WFS_OPEN && !isCurrentPathValid())
179     return;
180   storeValue();
181   emit valuesChanged();
182 }
183
184 QString ModuleBase_WidgetFileSelector::formatToFilter(const QString & theFormat)
185 {
186   if (theFormat.isEmpty() && !theFormat.contains(":"))
187     return QString();
188
189   QStringList aExtesionList = theFormat.section(':', 0, 0).split("|");
190   QString aFormat = theFormat.section(':', 1, 1);
191   return QString("%1 files (%2)").arg(aFormat)
192       .arg(QStringList(aExtesionList).replaceInStrings(QRegExp("^(.*)$"), "*.\\1").join(" "));
193 }
194
195 QString ModuleBase_WidgetFileSelector::filterToShortFormat(const QString & theFilter)
196 {
197   // Simplified implementation.
198   // It relies on theFilter was made by formatToFilter() function.
199   return theFilter.section(' ', 0, 0);
200 }
201
202 QStringList ModuleBase_WidgetFileSelector::filterToExtensions(const QString & theFilter)
203 {
204   // Simplified implementation.
205   // It relies on theFilter was made by formatToFilter() function.
206   QStringList anExtensions = theFilter.section("(", 1, 1).section(")", 0, 0).split(" ");
207   return anExtensions;
208 }
209
210 QStringList ModuleBase_WidgetFileSelector::getValidatorFormats() const
211 {
212   SessionPtr aMgr = ModelAPI_Session::get();
213   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
214
215   ModelAPI_ValidatorsFactory::Validators allValidators;
216   aFactory->validators(myFeature->getKind(), myAttributeID, allValidators);
217
218   QStringList aResult;
219   std::list<std::string> anArgumentList = allValidators.front().second;
220   std::list<std::string>::const_iterator it = anArgumentList.begin();
221   for (; it != anArgumentList.end(); ++it) {
222     QString aFormat = QString::fromStdString(*it);
223     if (!aFormat.isEmpty())
224       aResult << aFormat;
225   }
226   return aResult;
227 }
228
229 QString ModuleBase_WidgetFileSelector::filterString() const
230 {
231   QStringList aResult;
232   QStringList aValidatorFormats = getValidatorFormats();
233
234   foreach(const QString & eachFormat, aValidatorFormats) {
235     aResult << formatToFilter(eachFormat);
236   }
237   if (myType == WFS_OPEN)
238     aResult << QString("All files (*.*)");
239   return aResult.join(";;");
240 }
241
242 QString ModuleBase_WidgetFileSelector::applyExtension(const QString& theFileName,
243                                                       const QString& theFilter)
244 {
245   QString aResult = theFileName;
246   bool hasExtension = false;
247   QStringList anExtensions = filterToExtensions(theFilter);
248   foreach(const QString& anExtension, anExtensions) {
249     if (theFileName.endsWith(QString(".") + anExtension.section(".", 1, 1), Qt::CaseInsensitive)) {
250       hasExtension = true;
251       break;
252     }
253   }
254   if (!hasExtension && !anExtensions.isEmpty())
255     aResult = QString("%1.%2").arg(theFileName).arg(anExtensions[0].section(".", 1, 1));
256   return aResult;
257 }