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
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   myHasDefaultValue = false;
33
34   myTitle = QString::fromStdString(theData->getProperty("title"));
35   //TODO(sbh): Get them from the feature
36   myFormats = getSupportedFormats(theData);
37   myDefaultPath = QString::fromStdString(theData->getProperty("path"));
38
39   myMainWidget = new QWidget(theParent);
40   QGridLayout* aMainLay = new QGridLayout(myMainWidget);
41   aMainLay->setContentsMargins(0, 0, 0, 0);
42   QLabel* aTitleLabel = new QLabel(myTitle, myMainWidget);
43   aTitleLabel->setIndent(1);
44   aMainLay->addWidget(aTitleLabel, 0, 0);
45   myPathField = new QLineEdit(myMainWidget);
46   aMainLay->addWidget(myPathField, 1, 0);
47   QPushButton* aSelectPathBtn = new QPushButton("...", myMainWidget);
48   aSelectPathBtn->setMaximumWidth(20);
49   aSelectPathBtn->setMaximumHeight(20);
50   aMainLay->addWidget(aSelectPathBtn, 1, 1);
51   aMainLay->setColumnStretch(0, 1);
52   myPathField->setMinimumHeight(20);
53   aMainLay->setHorizontalSpacing(1);
54   myMainWidget->setLayout(aMainLay);
55
56   connect(myPathField, SIGNAL(textChanged(const QString&)),
57           this,        SLOT(onPathChanged()));
58   connect(aSelectPathBtn, SIGNAL(clicked()),
59           this,        SLOT(onPathSelectionBtn()));
60 }
61
62 ModuleBase_WidgetFileSelector::~ModuleBase_WidgetFileSelector()
63 {
64 }
65
66 bool ModuleBase_WidgetFileSelector::storeValue() const
67 {
68   DataPtr aData = myFeature->data();
69   AttributeStringPtr aStringAttr = aData->string(attributeID());
70   QString aModelValue = QString::fromStdString(aStringAttr->value());
71   QString aWidgetValue = myPathField->text();
72   if(aModelValue != aWidgetValue) {
73     aStringAttr->setValue(aWidgetValue.toStdString());
74     updateObject(myFeature);
75   }
76   return true;
77 }
78
79 bool ModuleBase_WidgetFileSelector::restoreValue()
80 {
81   DataPtr aData = myFeature->data();
82   AttributeStringPtr aStringAttr = aData->string(attributeID());
83
84   bool isBlocked = myPathField->blockSignals(true);
85   myPathField->setText(QString::fromStdString(aStringAttr->value()));
86   myPathField->blockSignals(isBlocked);
87
88   return true;
89 }
90
91 QWidget* ModuleBase_WidgetFileSelector::getControl() const
92 {
93   return myMainWidget;
94 }
95
96 QList<QWidget*> ModuleBase_WidgetFileSelector::getControls() const
97 {
98   QList<QWidget*> result;
99   QPushButton * aButton = myMainWidget->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() && myFormats.contains(aFile.suffix(), Qt::CaseInsensitive);
109 }
110
111
112 void ModuleBase_WidgetFileSelector::onPathSelectionBtn()
113 {
114   QString aFilter = formatsString(myFormats);
115   QString aFileName = QFileDialog::getOpenFileName(myMainWidget, 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 QStringList ModuleBase_WidgetFileSelector::getSupportedFormats(const Config_WidgetAPI* theData) const
130 {
131   QString aXMLFormat = QString::fromStdString(theData->getProperty("formats"));
132   aXMLFormat = aXMLFormat.toUpper();
133   const QChar kSep = ',';
134   return aXMLFormat.split(kSep, QString::SkipEmptyParts);
135 }
136
137 QString ModuleBase_WidgetFileSelector::formatsString(const QStringList theFormats) const
138 {
139   QStringList aResult;
140   foreach(QString eachFormat, theFormats)  {
141     aResult << QString("%1 files (*.%1)").arg(eachFormat);
142   }
143   return aResult.join(";;");
144 }
145