2 * ModuleBase_WidgetFileSelector.cpp
4 * Created on: Aug 28, 2014
8 #include <ModelAPI_AttributeString.h>
9 #include <ModelAPI_Data.h>
10 #include <ModelAPI_Object.h>
11 #include <ModuleBase_WidgetFileSelector.h>
13 #include <Config_WidgetAPI.h>
15 #include <QGridLayout>
16 #include <QFileDialog>
20 #include <QPushButton>
24 #include <boost/smart_ptr/shared_ptr.hpp>
27 ModuleBase_WidgetFileSelector::ModuleBase_WidgetFileSelector(QWidget* theParent,
28 const Config_WidgetAPI* theData,
29 const std::string& theParentId)
30 : ModuleBase_ModelWidget(theParent, theData, theParentId)
32 myHasDefaultValue = false;
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"));
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);
56 connect(myPathField, SIGNAL(textChanged(const QString&)),
57 this, SLOT(onPathChanged()));
58 connect(aSelectPathBtn, SIGNAL(clicked()),
59 this, SLOT(onPathSelectionBtn()));
62 ModuleBase_WidgetFileSelector::~ModuleBase_WidgetFileSelector()
66 bool ModuleBase_WidgetFileSelector::storeValue() const
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);
79 bool ModuleBase_WidgetFileSelector::restoreValue()
81 DataPtr aData = myFeature->data();
82 AttributeStringPtr aStringAttr = aData->string(attributeID());
84 bool isBlocked = myPathField->blockSignals(true);
85 myPathField->setText(QString::fromStdString(aStringAttr->value()));
86 myPathField->blockSignals(isBlocked);
91 QWidget* ModuleBase_WidgetFileSelector::getControl() const
96 QList<QWidget*> ModuleBase_WidgetFileSelector::getControls() const
98 QList<QWidget*> result;
99 QPushButton * aButton = myMainWidget->findChild<QPushButton *>();
101 result << myPathField;
105 bool ModuleBase_WidgetFileSelector::isCurrentPathValid()
107 QFileInfo aFile (myPathField->text());
108 return aFile.exists() && myFormats.contains(aFile.suffix(), Qt::CaseInsensitive);
112 void ModuleBase_WidgetFileSelector::onPathSelectionBtn()
114 QString aFilter = formatsString(myFormats);
115 QString aFileName = QFileDialog::getOpenFileName(myMainWidget, myTitle, myDefaultPath, aFilter);
116 if (!aFileName.isEmpty()) {
117 myPathField->setText(aFileName);
121 void ModuleBase_WidgetFileSelector::onPathChanged()
123 if(!isCurrentPathValid())
126 emit valuesChanged();
129 QStringList ModuleBase_WidgetFileSelector::getSupportedFormats(const Config_WidgetAPI* theData) const
131 QString aXMLFormat = QString::fromStdString(theData->getProperty("formats"));
132 aXMLFormat = aXMLFormat.toUpper();
133 const QChar kSep = ',';
134 return aXMLFormat.split(kSep, QString::SkipEmptyParts);
137 QString ModuleBase_WidgetFileSelector::formatsString(const QStringList theFormats) const
140 foreach(QString eachFormat, theFormats) {
141 aResult << QString("%1 files (*.%1)").arg(eachFormat);
143 return aResult.join(";;");