Salome HOME
Add copyright header according to request of CEA from 06.06.2017
[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 email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
18 //
19
20 /*
21  * ModuleBase_WidgetFileSelector.cpp
22  *
23  *  Created on: Aug 28, 2014
24  *      Author: sbh
25  */
26
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>
34
35 #include <Config_WidgetAPI.h>
36
37 #include <QFileDialog>
38 #include <QGridLayout>
39 #include <QLabel>
40 #include <QLineEdit>
41 #include <QList>
42 #include <QObject>
43 #include <QPushButton>
44 #include <QRegExp>
45 #include <QString>
46
47 #include <memory>
48 #include <string>
49
50 ModuleBase_WidgetFileSelector::ModuleBase_WidgetFileSelector(QWidget* theParent,
51                                                              const Config_WidgetAPI* theData)
52 : ModuleBase_ModelWidget(theParent, theData)
53 {
54   myTitle = translate(theData->getProperty("title"));
55   myType = (theData->getProperty("type") == "save") ? WFS_SAVE : WFS_OPEN;
56   myDefaultPath = QString::fromStdString(theData->getProperty("path"));
57
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);
74
75   connect(myPathField, SIGNAL(textChanged(const QString&)),
76           this,        SLOT(onPathChanged()));
77   connect(aSelectPathBtn, SIGNAL(clicked()),
78           this,        SLOT(onPathSelectionBtn()));
79 }
80
81 ModuleBase_WidgetFileSelector::~ModuleBase_WidgetFileSelector()
82 {
83 }
84
85 bool ModuleBase_WidgetFileSelector::storeValueCustom()
86 {
87   // A rare case when plugin was not loaded.
88   if (!myFeature)
89     return false;
90   DataPtr aData = myFeature->data();
91   AttributeStringPtr aStringAttr = aData->string(attributeID());
92   QString aWidgetValue = myPathField->text();
93   aStringAttr->setValue(aWidgetValue.toStdString());
94   updateObject(myFeature);
95   return true;
96 }
97
98 bool ModuleBase_WidgetFileSelector::restoreValueCustom()
99 {
100   // A rare case when plugin was not loaded.
101   if (!myFeature)
102     return false;
103   DataPtr aData = myFeature->data();
104   AttributeStringPtr aStringAttr = aData->string(attributeID());
105
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);
111
112   return true;
113 }
114
115 QList<QWidget*> ModuleBase_WidgetFileSelector::getControls() const
116 {
117   QList<QWidget*> result;
118   result << myPathField;
119   return result;
120 }
121
122 bool ModuleBase_WidgetFileSelector::isCurrentPathValid()
123 {
124   QFileInfo aFile(myPathField->text());
125   return aFile.exists();
126 }
127
128 void ModuleBase_WidgetFileSelector::onPathSelectionBtn()
129 {
130   QString aDefaultPath = myPathField->text().isEmpty()
131       ? myDefaultPath
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);
146   }
147 }
148
149 void ModuleBase_WidgetFileSelector::onPathChanged()
150 {
151   if (myType == WFS_OPEN && !isCurrentPathValid())
152     return;
153   storeValue();
154   emit valuesChanged();
155 }
156
157 QString ModuleBase_WidgetFileSelector::formatToFilter(const QString & theFormat)
158 {
159   if (theFormat.isEmpty() && !theFormat.contains(":"))
160     return QString();
161
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(" "));
166 }
167
168 QString ModuleBase_WidgetFileSelector::filterToShortFormat(const QString & theFilter)
169 {
170   // Simplified implementation.
171   // It relies on theFilter was made by formatToFilter() function.
172   return theFilter.section(' ', 0, 0);
173 }
174
175 QStringList ModuleBase_WidgetFileSelector::filterToExtensions(const QString & theFilter)
176 {
177   // Simplified implementation.
178   // It relies on theFilter was made by formatToFilter() function.
179   QStringList anExtensions = theFilter.section("(", 1, 1).section(")", 0, 0).split(" ");
180   return anExtensions;
181 }
182
183 QStringList ModuleBase_WidgetFileSelector::getValidatorFormats() const
184 {
185   SessionPtr aMgr = ModelAPI_Session::get();
186   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
187
188   ModelAPI_ValidatorsFactory::Validators allValidators;
189   aFactory->validators(myFeature->getKind(), myAttributeID, allValidators);
190
191   QStringList aResult;
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())
197       aResult << aFormat;
198   }
199   return aResult;
200 }
201
202 QString ModuleBase_WidgetFileSelector::filterString() const
203 {
204   QStringList aResult;
205   QStringList aValidatorFormats = getValidatorFormats();
206
207   foreach(const QString & eachFormat, aValidatorFormats) {
208     aResult << formatToFilter(eachFormat);
209   }
210   if (myType == WFS_OPEN)
211     aResult << QString("All files (*.*)");
212   return aResult.join(";;");
213 }
214
215 QString ModuleBase_WidgetFileSelector::applyExtension(const QString& theFileName,
216                                                       const QString& theFilter)
217 {
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)) {
223       hasExtension = true;
224       break;
225     }
226   }
227   if (!hasExtension && !anExtensions.isEmpty())
228     aResult = QString("%1.%2").arg(theFileName).arg(anExtensions[0].section(".", 1, 1));
229   return aResult;
230 }