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>
12 #include <ModuleBase_Tools.h>
14 #include <Config_WidgetAPI.h>
16 #include <QGridLayout>
17 #include <QFileDialog>
21 #include <QPushButton>
25 #include <boost/smart_ptr/shared_ptr.hpp>
28 ModuleBase_WidgetFileSelector::ModuleBase_WidgetFileSelector(QWidget* theParent,
29 const Config_WidgetAPI* theData,
30 const std::string& theParentId)
31 : ModuleBase_ModelWidget(theParent, theData, theParentId)
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"));
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);
55 connect(myPathField, SIGNAL(textChanged(const QString&)),
56 this, SLOT(onPathChanged()));
57 connect(aSelectPathBtn, SIGNAL(clicked()),
58 this, SLOT(onPathSelectionBtn()));
61 ModuleBase_WidgetFileSelector::~ModuleBase_WidgetFileSelector()
65 bool ModuleBase_WidgetFileSelector::storeValue() const
67 DataPtr aData = myFeature->data();
68 AttributeStringPtr aStringAttr = aData->string(attributeID());
69 QString aWidgetValue = myPathField->text();
70 aStringAttr->setValue(aWidgetValue.toStdString());
71 updateObject(myFeature);
75 bool ModuleBase_WidgetFileSelector::restoreValue()
77 DataPtr aData = myFeature->data();
78 AttributeStringPtr aStringAttr = aData->string(attributeID());
80 bool isBlocked = myPathField->blockSignals(true);
81 myPathField->setText(QString::fromStdString(aStringAttr->value()));
82 myPathField->blockSignals(isBlocked);
87 QWidget* ModuleBase_WidgetFileSelector::getControl() const
92 QList<QWidget*> ModuleBase_WidgetFileSelector::getControls() const
94 QList<QWidget*> result;
95 QPushButton * aButton = myMainWidget->findChild<QPushButton *>();
97 result << myPathField;
101 bool ModuleBase_WidgetFileSelector::isCurrentPathValid()
103 QFileInfo aFile (myPathField->text());
104 return aFile.exists() && myFormats.contains(aFile.suffix(), Qt::CaseInsensitive);
108 void ModuleBase_WidgetFileSelector::onPathSelectionBtn()
110 QString aFilter = formatsString(myFormats);
111 QString aFileName = QFileDialog::getOpenFileName(myMainWidget, myTitle, myDefaultPath, aFilter);
112 if (!aFileName.isEmpty()) {
113 myPathField->setText(aFileName);
117 void ModuleBase_WidgetFileSelector::onPathChanged()
119 if(!isCurrentPathValid())
122 emit valuesChanged();
125 QStringList ModuleBase_WidgetFileSelector::getSupportedFormats(const Config_WidgetAPI* theData) const
127 QString aXMLFormat = QString::fromStdString(theData->getProperty("formats"));
128 aXMLFormat = aXMLFormat.toUpper();
129 const QChar kSep = ',';
130 return aXMLFormat.split(kSep, QString::SkipEmptyParts);
133 QString ModuleBase_WidgetFileSelector::formatsString(const QStringList theFormats) const
136 foreach(QString eachFormat, theFormats) {
137 aResult << QString("%1 files (*.%1)").arg(eachFormat);
139 return aResult.join(";;");