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