]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModuleBase/ModuleBase_WidgetFileSelector.cpp
Salome HOME
Bugfix: treat attribute as valid and initialized if it's default value is equual...
[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 <ModuleBase_WidgetFileSelector.h>
12
13 #include <Config_WidgetAPI.h>
14
15 #include <QGridLayout>
16 #include <QFileDialog>
17 #include <QLineEdit>
18 #include <QList>
19 #include <QObject>
20 #include <QPushButton>
21 #include <QString>
22 #include <QLabel>
23
24 #include <boost/smart_ptr/shared_ptr.hpp>
25 #include <string>
26
27 ModuleBase_WidgetFileSelector::ModuleBase_WidgetFileSelector(QWidget* theParent,
28                                                              const Config_WidgetAPI* theData,
29                                                              const std::string& theParentId)
30     : ModuleBase_ModelWidget(theParent, theData, theParentId)
31 {
32   myTitle = QString::fromStdString(theData->getProperty("title"));
33   //TODO(sbh): Get them from the feature
34   myFormats = getSupportedFormats(theData);
35   myDefaultPath = QString::fromStdString(theData->getProperty("path"));
36
37   myMainWidget = new QWidget(theParent);
38   QGridLayout* aMainLay = new QGridLayout(myMainWidget);
39   aMainLay->setContentsMargins(0, 0, 0, 0);
40   QLabel* aTitleLabel = new QLabel(myTitle, myMainWidget);
41   aTitleLabel->setIndent(1);
42   aMainLay->addWidget(aTitleLabel, 0, 0);
43   myPathField = new QLineEdit(myMainWidget);
44   aMainLay->addWidget(myPathField, 1, 0);
45   QPushButton* aSelectPathBtn = new QPushButton("...", myMainWidget);
46   aSelectPathBtn->setMaximumWidth(20);
47   aSelectPathBtn->setMaximumHeight(20);
48   aMainLay->addWidget(aSelectPathBtn, 1, 1);
49   aMainLay->setColumnStretch(0, 1);
50   myPathField->setMinimumHeight(20);
51   aMainLay->setHorizontalSpacing(1);
52   myMainWidget->setLayout(aMainLay);
53
54   connect(myPathField, SIGNAL(textChanged(const QString&)),
55           this,        SLOT(onPathChanged()));
56   connect(aSelectPathBtn, SIGNAL(clicked()),
57           this,        SLOT(onPathSelectionBtn()));
58 }
59
60 ModuleBase_WidgetFileSelector::~ModuleBase_WidgetFileSelector()
61 {
62 }
63
64 bool ModuleBase_WidgetFileSelector::storeValue() const
65 {
66   DataPtr aData = myFeature->data();
67   AttributeStringPtr aStringAttr = aData->string(attributeID());
68   QString aWidgetValue = myPathField->text();
69   aStringAttr->setValue(aWidgetValue.toStdString());
70   updateObject(myFeature);
71   return true;
72 }
73
74 bool ModuleBase_WidgetFileSelector::restoreValue()
75 {
76   DataPtr aData = myFeature->data();
77   AttributeStringPtr aStringAttr = aData->string(attributeID());
78
79   bool isBlocked = myPathField->blockSignals(true);
80   myPathField->setText(QString::fromStdString(aStringAttr->value()));
81   myPathField->blockSignals(isBlocked);
82
83   return true;
84 }
85
86 QWidget* ModuleBase_WidgetFileSelector::getControl() const
87 {
88   return myMainWidget;
89 }
90
91 QList<QWidget*> ModuleBase_WidgetFileSelector::getControls() const
92 {
93   QList<QWidget*> result;
94   QPushButton * aButton = myMainWidget->findChild<QPushButton *>();
95   result << aButton;
96   result << myPathField;
97   return result;
98 }
99
100 bool ModuleBase_WidgetFileSelector::isCurrentPathValid()
101 {
102   QFileInfo aFile (myPathField->text());
103   return aFile.exists() && myFormats.contains(aFile.suffix(), Qt::CaseInsensitive);
104 }
105
106
107 void ModuleBase_WidgetFileSelector::onPathSelectionBtn()
108 {
109   QString aFilter = formatsString(myFormats);
110   QString aFileName = QFileDialog::getOpenFileName(myMainWidget, myTitle, myDefaultPath, aFilter);
111   if (!aFileName.isEmpty()) {
112     myPathField->setText(aFileName);
113   }
114 }
115
116 void ModuleBase_WidgetFileSelector::onPathChanged()
117 {
118   if(!isCurrentPathValid())
119     return;
120   storeValue();
121   emit valuesChanged();
122 }
123
124 QStringList ModuleBase_WidgetFileSelector::getSupportedFormats(const Config_WidgetAPI* theData) const
125 {
126   QString aXMLFormat = QString::fromStdString(theData->getProperty("formats"));
127   aXMLFormat = aXMLFormat.toUpper();
128   const QChar kSep = ',';
129   return aXMLFormat.split(kSep, QString::SkipEmptyParts);
130 }
131
132 QString ModuleBase_WidgetFileSelector::formatsString(const QStringList theFormats) const
133 {
134   QStringList aResult;
135   foreach(QString eachFormat, theFormats)  {
136     aResult << QString("%1 files (*.%1)").arg(eachFormat);
137   }
138   return aResult.join(";;");
139 }
140