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