Salome HOME
f3238f222744a1468ebe337d9aad48fc8b7fb791
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetFileSelector.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 /*
4  * ModuleBase_WidgetFileSelector.cpp
5  *
6  *  Created on: Aug 28, 2014
7  *      Author: sbh
8  */
9
10 #include <ModelAPI_AttributeString.h>
11 #include <ModelAPI_Data.h>
12 #include <ModelAPI_Object.h>
13 #include <ModelAPI_Validator.h>
14 #include <ModelAPI_Session.h>
15 #include <ModuleBase_WidgetFileSelector.h>
16 #include <ModuleBase_Tools.h>
17
18 #include <Config_WidgetAPI.h>
19
20 #include <QFileDialog>
21 #include <QGridLayout>
22 #include <QLabel>
23 #include <QLineEdit>
24 #include <QList>
25 #include <QObject>
26 #include <QPushButton>
27 #include <QRegExp>
28 #include <QString>
29
30 #include <memory>
31 #include <string>
32
33 ModuleBase_WidgetFileSelector::ModuleBase_WidgetFileSelector(QWidget* theParent,
34                                                              const Config_WidgetAPI* theData)
35 : ModuleBase_ModelWidget(theParent, theData)
36 {
37   myTitle = QString::fromStdString(theData->getProperty("title"));
38   myType = (theData->getProperty("type") == "save") ? WFS_SAVE : WFS_OPEN;
39   myDefaultPath = QString::fromStdString(theData->getProperty("path"));
40
41   QGridLayout* aMainLay = new QGridLayout(this);
42   ModuleBase_Tools::adjustMargins(aMainLay);
43   QLabel* aTitleLabel = new QLabel(myTitle, this);
44   aTitleLabel->setIndent(1);
45   aMainLay->addWidget(aTitleLabel, 0, 0);
46   myPathField = new QLineEdit(this);
47   aMainLay->addWidget(myPathField, 1, 0);
48   QPushButton* aSelectPathBtn = new QPushButton("...", this);
49   aSelectPathBtn->setToolTip(tr("Select file..."));
50   aSelectPathBtn->setMaximumWidth(20);
51   aSelectPathBtn->setMaximumHeight(20);
52   aMainLay->addWidget(aSelectPathBtn, 1, 1);
53   aMainLay->setColumnStretch(0, 1);
54   myPathField->setMinimumHeight(20);
55   aMainLay->setHorizontalSpacing(1);
56   this->setLayout(aMainLay);
57
58   connect(myPathField, SIGNAL(textChanged(const QString&)),
59           this,        SLOT(onPathChanged()));
60   connect(aSelectPathBtn, SIGNAL(clicked()),
61           this,        SLOT(onPathSelectionBtn()));
62 }
63
64 ModuleBase_WidgetFileSelector::~ModuleBase_WidgetFileSelector()
65 {
66 }
67
68 bool ModuleBase_WidgetFileSelector::storeValueCustom() const
69 {
70   // A rare case when plugin was not loaded. 
71   if (!myFeature)
72     return false;
73   DataPtr aData = myFeature->data();
74   AttributeStringPtr aStringAttr = aData->string(attributeID());
75   QString aWidgetValue = myPathField->text();
76   aStringAttr->setValue(aWidgetValue.toStdString());
77   updateObject(myFeature);
78   return true;
79 }
80
81 bool ModuleBase_WidgetFileSelector::restoreValueCustom()
82 {
83   // A rare case when plugin was not loaded. 
84   if (!myFeature)
85     return false;
86   DataPtr aData = myFeature->data();
87   AttributeStringPtr aStringAttr = aData->string(attributeID());
88
89   bool isBlocked = myPathField->blockSignals(true);
90   QString aNewText = QString::fromStdString(aStringAttr->value());
91   if( myPathField->text() != aNewText )
92     myPathField->setText( aNewText );
93   myPathField->blockSignals(isBlocked);
94
95   return true;
96 }
97
98 QList<QWidget*> ModuleBase_WidgetFileSelector::getControls() const
99 {
100   QList<QWidget*> result;
101   result << myPathField;
102   return result;
103 }
104
105 bool ModuleBase_WidgetFileSelector::isCurrentPathValid()
106 {
107   QFileInfo aFile(myPathField->text());
108   return aFile.exists();
109 }
110
111 void ModuleBase_WidgetFileSelector::onPathSelectionBtn()
112 {
113   QString aDefaultPath = myPathField->text().isEmpty()
114       ? myDefaultPath
115       : QFileInfo(myPathField->text()).absolutePath();
116   QString aFilter = filterString();
117   QString aFileName = (myType == WFS_SAVE)
118       ? QFileDialog::getSaveFileName(this, myTitle, aDefaultPath, aFilter, &mySelectedFilter)
119       : QFileDialog::getOpenFileName(this, myTitle, aDefaultPath, aFilter, &mySelectedFilter);
120   if (!aFileName.isEmpty()) {
121     if (myType == WFS_SAVE)
122       aFileName = applyExtension(aFileName, mySelectedFilter);
123     myPathField->setText(aFileName);
124   }
125 }
126
127 void ModuleBase_WidgetFileSelector::onPathChanged()
128 {
129   if (myType == WFS_OPEN && !isCurrentPathValid())
130     return;
131   storeValue();
132   emit valuesChanged();
133 }
134
135 QString ModuleBase_WidgetFileSelector::formatToFilter(const QString & theFormat)
136 {
137   if (theFormat.isEmpty() && !theFormat.contains(":"))
138     return QString();
139
140   QStringList aExtesionList = theFormat.section(':', 0, 0).split("|");
141   QString aFormat = theFormat.section(':', 1, 1);
142   return QString("%1 files (%2)").arg(aFormat)
143       .arg(QStringList(aExtesionList).replaceInStrings(QRegExp("^(.*)$"), "*.\\1").join(" "));
144 }
145
146 QString ModuleBase_WidgetFileSelector::filterToShortFormat(const QString & theFilter)
147 {
148   // Simplified implementation.
149   // It relies on theFilter was made by formatToFilter() function.
150   return theFilter.section(' ', 0, 0);
151 }
152
153 QStringList ModuleBase_WidgetFileSelector::filterToExtensions(const QString & theFilter)
154 {
155   // Simplified implementation.
156   // It relies on theFilter was made by formatToFilter() function.
157   QStringList anExtensions = theFilter.section("(", 1, 1).section(")", 0, 0).split(" ");
158   return anExtensions;
159 }
160
161 QStringList ModuleBase_WidgetFileSelector::getValidatorFormats() const
162 {
163   SessionPtr aMgr = ModelAPI_Session::get();
164   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
165
166   ModelAPI_ValidatorsFactory::Validators allValidators;
167   aFactory->validators(myFeature->getKind(), myAttributeID, allValidators);
168
169   QStringList aResult;
170   std::list<std::string> anArgumentList = allValidators.front().second;
171   std::list<std::string>::const_iterator it = anArgumentList.begin();
172   for (; it != anArgumentList.end(); ++it) {
173     QString aFormat = QString::fromStdString(*it);
174     if (!aFormat.isEmpty())
175       aResult << aFormat;
176   }
177   return aResult;
178 }
179
180 QString ModuleBase_WidgetFileSelector::filterString() const
181 {
182   QStringList aResult;
183   QStringList aValidatorFormats = getValidatorFormats();
184
185   foreach(const QString & eachFormat, aValidatorFormats) {
186     aResult << formatToFilter(eachFormat);
187   }
188   if (myType == WFS_OPEN)
189     aResult << QString("All files (*.*)");
190   return aResult.join(";;");
191 }
192
193 QString ModuleBase_WidgetFileSelector::applyExtension(const QString& theFileName,
194                                                       const QString& theFilter)
195 {
196   QString aResult = theFileName;
197   bool hasExtension = false;
198   QStringList anExtensions = filterToExtensions(theFilter);
199   foreach(const QString& anExtension, anExtensions) {
200     if (theFileName.endsWith(anExtension.section(".", 1, 1), Qt::CaseInsensitive)) {
201       hasExtension = true;
202       break;
203     }
204   }
205   if (!hasExtension && !anExtensions.isEmpty())
206     aResult = QString("%1.%2").arg(theFileName).arg(anExtensions[0].section(".", 1, 1));
207   return aResult;
208 }