]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModuleBase/ModuleBase_WidgetFileSelector.cpp
Salome HOME
Issue #273: Add copyright string
[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->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   myMainWidget->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::storeValue() 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 QWidget* ModuleBase_WidgetFileSelector::getControl() const
96 {
97   return myMainWidget;
98 }
99
100 QList<QWidget*> ModuleBase_WidgetFileSelector::getControls() const
101 {
102   QList<QWidget*> result;
103   //QPushButton * aButton = myMainWidget->findChild<QPushButton *>();
104   //result << aButton;
105   result << myPathField;
106   return result;
107 }
108
109 bool ModuleBase_WidgetFileSelector::isCurrentPathValid()
110 {
111   QFileInfo aFile (myPathField->text());
112   return aFile.exists();
113 }
114
115
116 void ModuleBase_WidgetFileSelector::onPathSelectionBtn()
117 {
118   QString aFilter = formatsString();
119   QString aFileName = QFileDialog::getOpenFileName(myMainWidget, myTitle, myDefaultPath, aFilter);
120   if (!aFileName.isEmpty()) {
121     myPathField->setText(aFileName);
122   }
123 }
124
125 void ModuleBase_WidgetFileSelector::onPathChanged()
126 {
127   if(!isCurrentPathValid())
128     return;
129   storeValue();
130   emit valuesChanged();
131 }
132
133 QString ModuleBase_WidgetFileSelector::formatsString() const
134 {
135   QStringList aResult;
136   QStringList aValidatorFormats = getValidatorFormats();
137
138   foreach(QString eachFormat, aValidatorFormats)  {
139     aResult << QString("%1 files (*.%1)").arg(eachFormat);
140   }
141   aResult << QString("All files (*.*)");
142   return aResult.join(";;");
143 }
144
145 QStringList ModuleBase_WidgetFileSelector::getValidatorFormats() const
146 {
147   SessionPtr aMgr = ModelAPI_Session::get();
148   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
149   std::list<ModelAPI_Validator*> allValidators;
150   std::list<std::list<std::string> > allArguments;
151   aFactory->validators(myFeature->getKind(), myAttributeID, allValidators, allArguments);
152   //TODO(sbh): extract as separate method
153   if(allArguments.empty())
154     return QStringList();
155   std::list<std::string> anArgumentList = allArguments.front();
156   std::list<std::string>::const_iterator it = anArgumentList.begin();
157   QStringList aResult;
158   for (; it != anArgumentList.end(); ++it) {
159     std::string anArg = *it;
160     int aSepPos = anArg.find(":");
161     if (aSepPos == std::string::npos) {
162       continue;
163     }
164     QString aFormat = QString::fromStdString(anArg.substr(0, aSepPos));
165     aFormat = aFormat.toUpper();
166     aResult.append(aFormat);
167   }
168   return aResult;
169 }