Salome HOME
Issue #390 Selection restore problems during edit operation
[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   myMainWidget = new QWidget(theParent);
41   QGridLayout* aMainLay = new QGridLayout(myMainWidget);
42   ModuleBase_Tools::adjustMargins(aMainLay);
43   QLabel* aTitleLabel = new QLabel(myTitle, myMainWidget);
44   aTitleLabel->setIndent(1);
45   aMainLay->addWidget(aTitleLabel, 0, 0);
46   myPathField = new QLineEdit(myMainWidget);
47   aMainLay->addWidget(myPathField, 1, 0);
48   QPushButton* aSelectPathBtn = new QPushButton("...", myMainWidget);
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   myMainWidget->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::restoreValue()
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   myPathField->setText(QString::fromStdString(aStringAttr->value()));
91   myPathField->blockSignals(isBlocked);
92
93   return true;
94 }
95
96 QWidget* ModuleBase_WidgetFileSelector::getControl() const
97 {
98   return myMainWidget;
99 }
100
101 QList<QWidget*> ModuleBase_WidgetFileSelector::getControls() const
102 {
103   QList<QWidget*> result;
104   //QPushButton * aButton = myMainWidget->findChild<QPushButton *>();
105   //result << aButton;
106   result << myPathField;
107   return result;
108 }
109
110 bool ModuleBase_WidgetFileSelector::isCurrentPathValid()
111 {
112   QFileInfo aFile (myPathField->text());
113   return aFile.exists();
114 }
115
116
117 void ModuleBase_WidgetFileSelector::onPathSelectionBtn()
118 {
119   QString aFilter = formatsString();
120   QString aFileName = QFileDialog::getOpenFileName(myMainWidget, myTitle, myDefaultPath, aFilter);
121   if (!aFileName.isEmpty()) {
122     myPathField->setText(aFileName);
123   }
124 }
125
126 void ModuleBase_WidgetFileSelector::onPathChanged()
127 {
128   if(!isCurrentPathValid())
129     return;
130   storeValue();
131   emit valuesChanged();
132 }
133
134 QString ModuleBase_WidgetFileSelector::formatsString() const
135 {
136   QStringList aResult;
137   QStringList aValidatorFormats = getValidatorFormats();
138
139   foreach(QString eachFormat, aValidatorFormats)  {
140     aResult << QString("%1 files (*.%1)").arg(eachFormat);
141   }
142   aResult << QString("All files (*.*)");
143   return aResult.join(";;");
144 }
145
146 QStringList ModuleBase_WidgetFileSelector::getValidatorFormats() const
147 {
148   SessionPtr aMgr = ModelAPI_Session::get();
149   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
150   std::list<ModelAPI_Validator*> allValidators;
151   std::list<std::list<std::string> > allArguments;
152   aFactory->validators(myFeature->getKind(), myAttributeID, allValidators, allArguments);
153   //TODO(sbh): extract as separate method
154   if(allArguments.empty())
155     return QStringList();
156   std::list<std::string> anArgumentList = allArguments.front();
157   std::list<std::string>::const_iterator it = anArgumentList.begin();
158   QStringList aResult;
159   for (; it != anArgumentList.end(); ++it) {
160     std::string anArg = *it;
161     int aSepPos = anArg.find(":");
162     if (aSepPos == std::string::npos) {
163       continue;
164     }
165     QString aFormat = QString::fromStdString(anArg.substr(0, aSepPos));
166     aFormat = aFormat.toUpper();
167     aResult.append(aFormat);
168   }
169   return aResult;
170 }