Salome HOME
Merge branch 'Pre_2.8.0_development'
[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_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 ModuleBase_WidgetFileSelector::ModuleBase_WidgetFileSelector(QWidget* theParent,
45                                                              const Config_WidgetAPI* theData)
46 : ModuleBase_ModelWidget(theParent, theData)
47 {
48   myTitle = translate(theData->getProperty("title"));
49   myType = (theData->getProperty("type") == "save") ? WFS_SAVE : WFS_OPEN;
50   myDefaultPath = QString::fromStdString(theData->getProperty("path"));
51
52   QGridLayout* aMainLay = new QGridLayout(this);
53   ModuleBase_Tools::adjustMargins(aMainLay);
54   QLabel* aTitleLabel = new QLabel(myTitle, this);
55   aTitleLabel->setIndent(1);
56   aMainLay->addWidget(aTitleLabel, 0, 0);
57   myPathField = new QLineEdit(this);
58   aMainLay->addWidget(myPathField, 1, 0);
59   QPushButton* aSelectPathBtn = new QPushButton("...", this);
60   aSelectPathBtn->setToolTip(tr("Select file..."));
61   aSelectPathBtn->setMaximumWidth(20);
62   aSelectPathBtn->setMaximumHeight(20);
63   aMainLay->addWidget(aSelectPathBtn, 1, 1);
64   aMainLay->setColumnStretch(0, 1);
65   myPathField->setMinimumHeight(20);
66   aMainLay->setHorizontalSpacing(1);
67   this->setLayout(aMainLay);
68
69   connect(myPathField, SIGNAL(textChanged(const QString&)),
70           this,        SLOT(onPathChanged()));
71   connect(aSelectPathBtn, SIGNAL(clicked()),
72           this,        SLOT(onPathSelectionBtn()));
73 }
74
75 ModuleBase_WidgetFileSelector::~ModuleBase_WidgetFileSelector()
76 {
77 }
78
79 bool ModuleBase_WidgetFileSelector::storeValueCustom()
80 {
81   // A rare case when plugin was not loaded.
82   if (!myFeature)
83     return false;
84   DataPtr aData = myFeature->data();
85   AttributeStringPtr aStringAttr = aData->string(attributeID());
86   QString aWidgetValue = myPathField->text();
87   aStringAttr->setValue(aWidgetValue.toStdString());
88   updateObject(myFeature);
89   return true;
90 }
91
92 bool ModuleBase_WidgetFileSelector::restoreValueCustom()
93 {
94   // A rare case when plugin was not loaded.
95   if (!myFeature)
96     return false;
97   DataPtr aData = myFeature->data();
98   AttributeStringPtr aStringAttr = aData->string(attributeID());
99
100   bool isBlocked = myPathField->blockSignals(true);
101   QString aNewText = QString::fromStdString(aStringAttr->value());
102   if( myPathField->text() != aNewText )
103     myPathField->setText( aNewText );
104   myPathField->blockSignals(isBlocked);
105
106   return true;
107 }
108
109 QList<QWidget*> ModuleBase_WidgetFileSelector::getControls() const
110 {
111   QList<QWidget*> result;
112   result << myPathField;
113   return result;
114 }
115
116 bool ModuleBase_WidgetFileSelector::isCurrentPathValid()
117 {
118   QFileInfo aFile(myPathField->text());
119   return aFile.exists();
120 }
121
122 void ModuleBase_WidgetFileSelector::onPathSelectionBtn()
123 {
124   QString aDefaultPath = myPathField->text().isEmpty()
125       ? myDefaultPath
126       : QFileInfo(myPathField->text()).absolutePath();
127   QString aFilter = filterString();
128   // use Option prohibited native dialog using to have both lower/upper extensions of files
129   // satisfied to dialog filter on Linux(Calibre) Issue #2055
130   QString aFileName = (myType == WFS_SAVE)
131       ? QFileDialog::getSaveFileName(this, myTitle, aDefaultPath, aFilter, &mySelectedFilter,
132                                      QFileDialog::DontUseNativeDialog)
133       : QFileDialog::getOpenFileName(this, myTitle, aDefaultPath, aFilter, &mySelectedFilter,
134                                      QFileDialog::DontUseNativeDialog);
135   if (!aFileName.isEmpty()) {
136     if (myType == WFS_SAVE)
137       aFileName = applyExtension(aFileName, mySelectedFilter);
138     myPathField->setText(aFileName);
139     emit focusOutWidget(this);
140   }
141 }
142
143 void ModuleBase_WidgetFileSelector::onPathChanged()
144 {
145   if (myType == WFS_OPEN && !isCurrentPathValid())
146     return;
147   storeValue();
148   emit valuesChanged();
149 }
150
151 QString ModuleBase_WidgetFileSelector::formatToFilter(const QString & theFormat)
152 {
153   if (theFormat.isEmpty() && !theFormat.contains(":"))
154     return QString();
155
156   QStringList aExtesionList = theFormat.section(':', 0, 0).split("|");
157   QString aFormat = theFormat.section(':', 1, 1);
158   return QString("%1 files (%2)").arg(aFormat)
159       .arg(QStringList(aExtesionList).replaceInStrings(QRegExp("^(.*)$"), "*.\\1").join(" "));
160 }
161
162 QString ModuleBase_WidgetFileSelector::filterToShortFormat(const QString & theFilter)
163 {
164   // Simplified implementation.
165   // It relies on theFilter was made by formatToFilter() function.
166   return theFilter.section(' ', 0, 0);
167 }
168
169 QStringList ModuleBase_WidgetFileSelector::filterToExtensions(const QString & theFilter)
170 {
171   // Simplified implementation.
172   // It relies on theFilter was made by formatToFilter() function.
173   QStringList anExtensions = theFilter.section("(", 1, 1).section(")", 0, 0).split(" ");
174   return anExtensions;
175 }
176
177 QStringList ModuleBase_WidgetFileSelector::getValidatorFormats() const
178 {
179   SessionPtr aMgr = ModelAPI_Session::get();
180   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
181
182   ModelAPI_ValidatorsFactory::Validators allValidators;
183   aFactory->validators(myFeature->getKind(), myAttributeID, allValidators);
184
185   QStringList aResult;
186   std::list<std::string> anArgumentList = allValidators.front().second;
187   std::list<std::string>::const_iterator it = anArgumentList.begin();
188   for (; it != anArgumentList.end(); ++it) {
189     QString aFormat = QString::fromStdString(*it);
190     if (!aFormat.isEmpty())
191       aResult << aFormat;
192   }
193   return aResult;
194 }
195
196 QString ModuleBase_WidgetFileSelector::filterString() const
197 {
198   QStringList aResult;
199   QStringList aValidatorFormats = getValidatorFormats();
200
201   foreach(const QString & eachFormat, aValidatorFormats) {
202     aResult << formatToFilter(eachFormat);
203   }
204   if (myType == WFS_OPEN)
205     aResult << QString("All files (*.*)");
206   return aResult.join(";;");
207 }
208
209 QString ModuleBase_WidgetFileSelector::applyExtension(const QString& theFileName,
210                                                       const QString& theFilter)
211 {
212   QString aResult = theFileName;
213   bool hasExtension = false;
214   QStringList anExtensions = filterToExtensions(theFilter);
215   foreach(const QString& anExtension, anExtensions) {
216     if (theFileName.endsWith(anExtension.section(".", 1, 1), Qt::CaseInsensitive)) {
217       hasExtension = true;
218       break;
219     }
220   }
221   if (!hasExtension && !anExtensions.isEmpty())
222     aResult = QString("%1.%2").arg(theFileName).arg(anExtensions[0].section(".", 1, 1));
223   return aResult;
224 }