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 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"));
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);
54 connect(myPathField, SIGNAL(textChanged(const QString&)),
55 this, SLOT(onPathChanged()));
56 connect(aSelectPathBtn, SIGNAL(clicked()),
57 this, SLOT(onPathSelectionBtn()));
60 ModuleBase_WidgetFileSelector::~ModuleBase_WidgetFileSelector()
64 bool ModuleBase_WidgetFileSelector::storeValue() const
66 DataPtr aData = myFeature->data();
67 AttributeStringPtr aStringAttr = aData->string(attributeID());
68 QString aWidgetValue = myPathField->text();
69 aStringAttr->setValue(aWidgetValue.toStdString());
70 updateObject(myFeature);
74 bool ModuleBase_WidgetFileSelector::restoreValue()
76 DataPtr aData = myFeature->data();
77 AttributeStringPtr aStringAttr = aData->string(attributeID());
79 bool isBlocked = myPathField->blockSignals(true);
80 myPathField->setText(QString::fromStdString(aStringAttr->value()));
81 myPathField->blockSignals(isBlocked);
86 QWidget* ModuleBase_WidgetFileSelector::getControl() const
91 QList<QWidget*> ModuleBase_WidgetFileSelector::getControls() const
93 QList<QWidget*> result;
94 QPushButton * aButton = myMainWidget->findChild<QPushButton *>();
96 result << myPathField;
100 bool ModuleBase_WidgetFileSelector::isCurrentPathValid()
102 QFileInfo aFile (myPathField->text());
103 return aFile.exists() && myFormats.contains(aFile.suffix(), Qt::CaseInsensitive);
107 void ModuleBase_WidgetFileSelector::onPathSelectionBtn()
109 QString aFilter = formatsString(myFormats);
110 QString aFileName = QFileDialog::getOpenFileName(myMainWidget, myTitle, myDefaultPath, aFilter);
111 if (!aFileName.isEmpty()) {
112 myPathField->setText(aFileName);
116 void ModuleBase_WidgetFileSelector::onPathChanged()
118 if(!isCurrentPathValid())
121 emit valuesChanged();
124 QStringList ModuleBase_WidgetFileSelector::getSupportedFormats(const Config_WidgetAPI* theData) const
126 QString aXMLFormat = QString::fromStdString(theData->getProperty("formats"));
127 aXMLFormat = aXMLFormat.toUpper();
128 const QChar kSep = ',';
129 return aXMLFormat.split(kSep, QString::SkipEmptyParts);
132 QString ModuleBase_WidgetFileSelector::formatsString(const QStringList theFormats) const
135 foreach(QString eachFormat, theFormats) {
136 aResult << QString("%1 files (*.%1)").arg(eachFormat);
138 return aResult.join(";;");