]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModuleBase/ModuleBase_WidgetFileSelector.cpp
Salome HOME
Merge branch 'Dev_1.1.1' of newgeom:newgeom into Dev_1.2.0
[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                                                              const std::string& theParentId)
36     : ModuleBase_ModelWidget(theParent, theData, theParentId)
37 {
38   myTitle = QString::fromStdString(theData->getProperty("title"));
39   myType = (theData->getProperty("type") == "save") ? WFS_SAVE : WFS_OPEN;
40   myDefaultPath = QString::fromStdString(theData->getProperty("path"));
41
42   QGridLayout* aMainLay = new QGridLayout(this);
43   ModuleBase_Tools::adjustMargins(aMainLay);
44   QLabel* aTitleLabel = new QLabel(myTitle, this);
45   aTitleLabel->setIndent(1);
46   aMainLay->addWidget(aTitleLabel, 0, 0);
47   myPathField = new QLineEdit(this);
48   aMainLay->addWidget(myPathField, 1, 0);
49   QPushButton* aSelectPathBtn = new QPushButton("...", this);
50   aSelectPathBtn->setToolTip(tr("Select file..."));
51   aSelectPathBtn->setMaximumWidth(20);
52   aSelectPathBtn->setMaximumHeight(20);
53   aMainLay->addWidget(aSelectPathBtn, 1, 1);
54   aMainLay->setColumnStretch(0, 1);
55   myPathField->setMinimumHeight(20);
56   aMainLay->setHorizontalSpacing(1);
57   this->setLayout(aMainLay);
58
59   connect(myPathField, SIGNAL(textChanged(const QString&)),
60           this,        SLOT(onPathChanged()));
61   connect(aSelectPathBtn, SIGNAL(clicked()),
62           this,        SLOT(onPathSelectionBtn()));
63 }
64
65 ModuleBase_WidgetFileSelector::~ModuleBase_WidgetFileSelector()
66 {
67 }
68
69 bool ModuleBase_WidgetFileSelector::storeValueCustom() const
70 {
71   // A rare case when plugin was not loaded. 
72   if (!myFeature)
73     return false;
74   DataPtr aData = myFeature->data();
75   AttributeStringPtr aStringAttr = aData->string(attributeID());
76   QString aWidgetValue = myPathField->text();
77   aStringAttr->setValue(aWidgetValue.toStdString());
78   updateObject(myFeature);
79   return true;
80 }
81
82 bool ModuleBase_WidgetFileSelector::restoreValue()
83 {
84   // A rare case when plugin was not loaded. 
85   if (!myFeature)
86     return false;
87   DataPtr aData = myFeature->data();
88   AttributeStringPtr aStringAttr = aData->string(attributeID());
89
90   bool isBlocked = myPathField->blockSignals(true);
91   myPathField->setText(QString::fromStdString(aStringAttr->value()));
92   myPathField->blockSignals(isBlocked);
93
94   return true;
95 }
96
97 QList<QWidget*> ModuleBase_WidgetFileSelector::getControls() const
98 {
99   QList<QWidget*> result;
100   //QPushButton * aButton = this->findChild<QPushButton *>();
101   //result << aButton;
102   result << myPathField;
103   return result;
104 }
105
106 bool ModuleBase_WidgetFileSelector::isCurrentPathValid()
107 {
108   QFileInfo aFile(myPathField->text());
109   return aFile.exists();
110 }
111
112 void ModuleBase_WidgetFileSelector::onPathSelectionBtn()
113 {
114   QString aDefaultPath = myPathField->text().isEmpty()
115       ? myDefaultPath
116       : QFileInfo(myPathField->text()).absolutePath();
117   QString aFilter = filterString();
118   QString aFileName = (myType == WFS_SAVE)
119       ? QFileDialog::getSaveFileName(this, myTitle, aDefaultPath, aFilter, &mySelectedFilter)
120       : QFileDialog::getOpenFileName(this, myTitle, aDefaultPath, aFilter, &mySelectedFilter);
121   if (!aFileName.isEmpty()) {
122     myPathField->setText(aFileName);
123   }
124 }
125
126 void ModuleBase_WidgetFileSelector::onPathChanged()
127 {
128   if (myType == WFS_OPEN && !isCurrentPathValid())
129     return;
130   storeValue();
131   emit valuesChanged();
132 }
133
134 QString ModuleBase_WidgetFileSelector::formatToFilter( const QString & theFormat )
135 {
136   if (theFormat.isEmpty() && !theFormat.contains(":"))
137     return QString();
138
139   QStringList aExtesionList = theFormat.section(':', 0, 0).split("|");
140   QString aFormat = theFormat.section(':', 1, 1);
141   return QString("%1 files (%2)").arg(aFormat)
142       .arg(QStringList(aExtesionList).replaceInStrings(QRegExp("^(.*)$"), "*.\\1").join(" "));
143 }
144
145 QStringList ModuleBase_WidgetFileSelector::getValidatorFormats() const
146 {
147   SessionPtr aMgr = ModelAPI_Session::get();
148   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
149
150   std::list<ModelAPI_Validator*> allValidators;
151   std::list<std::list<std::string> > allArguments;
152   aFactory->validators(myFeature->getKind(), myAttributeID, allValidators, allArguments);
153
154   QStringList aResult;
155   std::list<std::string> anArgumentList = allArguments.front();
156   std::list<std::string>::const_iterator it = anArgumentList.begin();
157   for (; it != anArgumentList.end(); ++it) {
158     QString aFormat = QString::fromStdString(*it);
159     if (!aFormat.isEmpty())
160       aResult << aFormat;
161   }
162   return aResult;
163 }
164
165 QString ModuleBase_WidgetFileSelector::filterString() const
166 {
167   QStringList aResult;
168   QStringList aValidatorFormats = getValidatorFormats();
169
170   foreach(const QString & eachFormat, aValidatorFormats) {
171     aResult << formatToFilter(eachFormat);
172   }
173   if (myType == WFS_OPEN)
174     aResult << QString("All files (*.*)");
175   return aResult.join(";;");
176 }