]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModuleBase/ModuleBase_WidgetFileSelector.cpp
Salome HOME
ModuleBase_WidgetChoice is extended to use in a combo box a list of values read from...
[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)
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 void ModuleBase_WidgetFileSelector::onPathSelectionBtn()
127 {
128   QString aDefaultPath = myPathField->text().isEmpty()
129       ? myDefaultPath
130       : QFileInfo(myPathField->text()).absolutePath();
131   QString aFilter = filterString();
132   // use Option prohibited native dialog using to have both lower/upper extensions of files
133   // satisfied to dialog filter on Linux(Calibre) Issue #2055
134   QString aFileName = (myType == WFS_SAVE)
135       ? QFileDialog::getSaveFileName(this, myTitle, aDefaultPath, aFilter, &mySelectedFilter,
136                                      QFileDialog::DontUseNativeDialog)
137       : QFileDialog::getOpenFileName(this, myTitle, aDefaultPath, aFilter, &mySelectedFilter,
138                                      QFileDialog::DontUseNativeDialog);
139   if (!aFileName.isEmpty()) {
140     if (myType == WFS_SAVE)
141       aFileName = applyExtension(aFileName, mySelectedFilter);
142     myPathField->setText(aFileName);
143     emit focusOutWidget(this);
144   }
145 }
146
147 void ModuleBase_WidgetFileSelector::onPathChanged()
148 {
149   if (myType == WFS_OPEN && !isCurrentPathValid())
150     return;
151   storeValue();
152   emit valuesChanged();
153 }
154
155 QString ModuleBase_WidgetFileSelector::formatToFilter(const QString & theFormat)
156 {
157   if (theFormat.isEmpty() && !theFormat.contains(":"))
158     return QString();
159
160   QStringList aExtesionList = theFormat.section(':', 0, 0).split("|");
161   QString aFormat = theFormat.section(':', 1, 1);
162   return QString("%1 files (%2)").arg(aFormat)
163       .arg(QStringList(aExtesionList).replaceInStrings(QRegExp("^(.*)$"), "*.\\1").join(" "));
164 }
165
166 QString ModuleBase_WidgetFileSelector::filterToShortFormat(const QString & theFilter)
167 {
168   // Simplified implementation.
169   // It relies on theFilter was made by formatToFilter() function.
170   return theFilter.section(' ', 0, 0);
171 }
172
173 QStringList ModuleBase_WidgetFileSelector::filterToExtensions(const QString & theFilter)
174 {
175   // Simplified implementation.
176   // It relies on theFilter was made by formatToFilter() function.
177   QStringList anExtensions = theFilter.section("(", 1, 1).section(")", 0, 0).split(" ");
178   return anExtensions;
179 }
180
181 QStringList ModuleBase_WidgetFileSelector::getValidatorFormats() const
182 {
183   SessionPtr aMgr = ModelAPI_Session::get();
184   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
185
186   ModelAPI_ValidatorsFactory::Validators allValidators;
187   aFactory->validators(myFeature->getKind(), myAttributeID, allValidators);
188
189   QStringList aResult;
190   std::list<std::string> anArgumentList = allValidators.front().second;
191   std::list<std::string>::const_iterator it = anArgumentList.begin();
192   for (; it != anArgumentList.end(); ++it) {
193     QString aFormat = QString::fromStdString(*it);
194     if (!aFormat.isEmpty())
195       aResult << aFormat;
196   }
197   return aResult;
198 }
199
200 QString ModuleBase_WidgetFileSelector::filterString() const
201 {
202   QStringList aResult;
203   QStringList aValidatorFormats = getValidatorFormats();
204
205   foreach(const QString & eachFormat, aValidatorFormats) {
206     aResult << formatToFilter(eachFormat);
207   }
208   if (myType == WFS_OPEN)
209     aResult << QString("All files (*.*)");
210   return aResult.join(";;");
211 }
212
213 QString ModuleBase_WidgetFileSelector::applyExtension(const QString& theFileName,
214                                                       const QString& theFilter)
215 {
216   QString aResult = theFileName;
217   bool hasExtension = false;
218   QStringList anExtensions = filterToExtensions(theFilter);
219   foreach(const QString& anExtension, anExtensions) {
220     if (theFileName.endsWith(anExtension.section(".", 1, 1), Qt::CaseInsensitive)) {
221       hasExtension = true;
222       break;
223     }
224   }
225   if (!hasExtension && !anExtensions.isEmpty())
226     aResult = QString("%1.%2").arg(theFileName).arg(anExtensions[0].section(".", 1, 1));
227   return aResult;
228 }