]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModuleBase/ModuleBase_WidgetFileSelector.cpp
Salome HOME
Issue #539: import step - *.stp files
[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   myDefaultPath = QString::fromStdString(theData->getProperty("path"));
40
41   QGridLayout* aMainLay = new QGridLayout(this);
42   ModuleBase_Tools::adjustMargins(aMainLay);
43   QLabel* aTitleLabel = new QLabel(myTitle, this);
44   aTitleLabel->setIndent(1);
45   aMainLay->addWidget(aTitleLabel, 0, 0);
46   myPathField = new QLineEdit(this);
47   aMainLay->addWidget(myPathField, 1, 0);
48   QPushButton* aSelectPathBtn = new QPushButton("...", this);
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   this->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 QList<QWidget*> ModuleBase_WidgetFileSelector::getControls() const
97 {
98   QList<QWidget*> result;
99   //QPushButton * aButton = this->findChild<QPushButton *>();
100   //result << aButton;
101   result << myPathField;
102   return result;
103 }
104
105 bool ModuleBase_WidgetFileSelector::isCurrentPathValid()
106 {
107   QFileInfo aFile (myPathField->text());
108   return aFile.exists();
109 }
110
111
112 void ModuleBase_WidgetFileSelector::onPathSelectionBtn()
113 {
114   QString aFilter = formatsString();
115   QString aFileName = QFileDialog::getOpenFileName(this, myTitle, myDefaultPath, aFilter);
116   if (!aFileName.isEmpty()) {
117     myPathField->setText(aFileName);
118   }
119 }
120
121 void ModuleBase_WidgetFileSelector::onPathChanged()
122 {
123   if(!isCurrentPathValid())
124     return;
125   storeValue();
126   emit valuesChanged();
127 }
128
129 QString ModuleBase_WidgetFileSelector::formatsString() const
130 {
131   QStringList aResult;
132   QStringList aValidatorFormats = getValidatorFormats();
133
134   foreach(QString eachFormat, aValidatorFormats)  {
135     QStringList aFormatList = eachFormat.split("|");
136     aResult << QString("%1 files (%2)").arg(aFormatList.value(0))
137         .arg(QStringList(aFormatList).replaceInStrings(QRegExp("^(.*)$"), "*.\\1").join(" "));
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 }