Salome HOME
Merge branch 'master' of newgeom:newgeom.git into BR_PYTHON_PLUGIN
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetFileSelector.cpp
1 /*
2  * ModuleBase_WidgetFileSelector.cpp
3  *
4  *  Created on: Aug 28, 2014
5  *      Author: sbh
6  */
7
8 #include <ModelAPI_AttributeString.h>
9 #include <ModelAPI_Data.h>
10 #include <ModelAPI_Object.h>
11 #include <ModelAPI_Validator.h>
12 #include <ModelAPI_Session.h>
13 #include <ModuleBase_WidgetFileSelector.h>
14 #include <ModuleBase_Tools.h>
15
16 #include <Config_WidgetAPI.h>
17
18 #include <QGridLayout>
19 #include <QFileDialog>
20 #include <QLineEdit>
21 #include <QList>
22 #include <QObject>
23 #include <QPushButton>
24 #include <QString>
25 #include <QLabel>
26
27 #include <memory>
28 #include <string>
29
30 ModuleBase_WidgetFileSelector::ModuleBase_WidgetFileSelector(QWidget* theParent,
31                                                              const Config_WidgetAPI* theData,
32                                                              const std::string& theParentId)
33     : ModuleBase_ModelWidget(theParent, theData, theParentId)
34 {
35   myTitle = QString::fromStdString(theData->getProperty("title"));
36   myDefaultPath = QString::fromStdString(theData->getProperty("path"));
37
38   myMainWidget = new QWidget(theParent);
39   QGridLayout* aMainLay = new QGridLayout(myMainWidget);
40   ModuleBase_Tools::adjustMargins(aMainLay);
41   QLabel* aTitleLabel = new QLabel(myTitle, myMainWidget);
42   aTitleLabel->setIndent(1);
43   aMainLay->addWidget(aTitleLabel, 0, 0);
44   myPathField = new QLineEdit(myMainWidget);
45   aMainLay->addWidget(myPathField, 1, 0);
46   QPushButton* aSelectPathBtn = new QPushButton("...", myMainWidget);
47   aSelectPathBtn->setMaximumWidth(20);
48   aSelectPathBtn->setMaximumHeight(20);
49   aMainLay->addWidget(aSelectPathBtn, 1, 1);
50   aMainLay->setColumnStretch(0, 1);
51   myPathField->setMinimumHeight(20);
52   aMainLay->setHorizontalSpacing(1);
53   myMainWidget->setLayout(aMainLay);
54
55   connect(myPathField, SIGNAL(textChanged(const QString&)),
56           this,        SLOT(onPathChanged()));
57   connect(aSelectPathBtn, SIGNAL(clicked()),
58           this,        SLOT(onPathSelectionBtn()));
59 }
60
61 ModuleBase_WidgetFileSelector::~ModuleBase_WidgetFileSelector()
62 {
63 }
64
65 bool ModuleBase_WidgetFileSelector::storeValue() const
66 {
67   // A rare case when plugin was not loaded. 
68   if(!myFeature)
69     return false;
70   DataPtr aData = myFeature->data();
71   AttributeStringPtr aStringAttr = aData->string(attributeID());
72   QString aWidgetValue = myPathField->text();
73   aStringAttr->setValue(aWidgetValue.toStdString());
74   updateObject(myFeature);
75   return true;
76 }
77
78 bool ModuleBase_WidgetFileSelector::restoreValue()
79 {
80   // A rare case when plugin was not loaded. 
81   if(!myFeature)
82     return false;
83   DataPtr aData = myFeature->data();
84   AttributeStringPtr aStringAttr = aData->string(attributeID());
85
86   bool isBlocked = myPathField->blockSignals(true);
87   myPathField->setText(QString::fromStdString(aStringAttr->value()));
88   myPathField->blockSignals(isBlocked);
89
90   return true;
91 }
92
93 QWidget* ModuleBase_WidgetFileSelector::getControl() const
94 {
95   return myMainWidget;
96 }
97
98 QList<QWidget*> ModuleBase_WidgetFileSelector::getControls() const
99 {
100   QList<QWidget*> result;
101   //QPushButton * aButton = myMainWidget->findChild<QPushButton *>();
102   //result << aButton;
103   result << myPathField;
104   return result;
105 }
106
107 bool ModuleBase_WidgetFileSelector::isCurrentPathValid()
108 {
109   QFileInfo aFile (myPathField->text());
110   return aFile.exists();
111 }
112
113
114 void ModuleBase_WidgetFileSelector::onPathSelectionBtn()
115 {
116   QString aFilter = formatsString();
117   QString aFileName = QFileDialog::getOpenFileName(myMainWidget, myTitle, myDefaultPath, aFilter);
118   if (!aFileName.isEmpty()) {
119     myPathField->setText(aFileName);
120   }
121 }
122
123 void ModuleBase_WidgetFileSelector::onPathChanged()
124 {
125   if(!isCurrentPathValid())
126     return;
127   storeValue();
128   emit valuesChanged();
129 }
130
131 QString ModuleBase_WidgetFileSelector::formatsString() const
132 {
133   QStringList aResult;
134   QStringList aValidatorFormats = getValidatorFormats();
135
136   foreach(QString eachFormat, aValidatorFormats)  {
137     aResult << QString("%1 files (*.%1)").arg(eachFormat);
138   }
139   aResult << QString("All files (*.*)");
140   return aResult.join(";;");
141 }
142
143 QStringList ModuleBase_WidgetFileSelector::getValidatorFormats() const
144 {
145   SessionPtr aMgr = ModelAPI_Session::get();
146   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
147   std::list<ModelAPI_Validator*> allValidators;
148   std::list<std::list<std::string> > allArguments;
149   aFactory->validators(myFeature->getKind(), myAttributeID, allValidators, allArguments);
150   //TODO(sbh): extract as separate method
151   if(allArguments.empty())
152     return QStringList();
153   std::list<std::string> anArgumentList = allArguments.front();
154   std::list<std::string>::const_iterator it = anArgumentList.begin();
155   QStringList aResult;
156   for (; it != anArgumentList.end(); ++it) {
157     std::string anArg = *it;
158     int aSepPos = anArg.find(":");
159     if (aSepPos == std::string::npos) {
160       continue;
161     }
162     QString aFormat = QString::fromStdString(anArg.substr(0, aSepPos));
163     aFormat = aFormat.toUpper();
164     aResult.append(aFormat);
165   }
166   return aResult;
167 }