1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
4 * ModuleBase_WidgetFileSelector.cpp
6 * Created on: Aug 28, 2014
10 #include <ModelAPI_AttributeString.h>
11 #include <ModelAPI_Data.h>
12 #include <ModelAPI_Object.h>
13 #include <ModelAPI_Validator.h>
14 #include <ModelAPI_Session.h>
15 #include <ModuleBase_WidgetFileSelector.h>
16 #include <ModuleBase_Tools.h>
18 #include <Config_WidgetAPI.h>
20 #include <QFileDialog>
21 #include <QGridLayout>
26 #include <QPushButton>
33 ModuleBase_WidgetFileSelector::ModuleBase_WidgetFileSelector(QWidget* theParent,
34 const Config_WidgetAPI* theData,
35 const std::string& theParentId)
36 : ModuleBase_ModelWidget(theParent, theData, theParentId)
38 myTitle = QString::fromStdString(theData->getProperty("title"));
39 myDefaultPath = QString::fromStdString(theData->getProperty("path"));
41 QGridLayout* aMainLay = new QGridLayout(this);
42 ModuleBase_Tools::adjustMargins(aMainLay);
43 QLabel* aTitleLabel = new QLabel(myTitle, this);
44 aTitleLabel->setIndent(1);
45 aMainLay->addWidget(aTitleLabel, 0, 0);
46 myPathField = new QLineEdit(this);
47 aMainLay->addWidget(myPathField, 1, 0);
48 QPushButton* aSelectPathBtn = new QPushButton("...", this);
49 aSelectPathBtn->setToolTip(tr("Select file..."));
50 aSelectPathBtn->setMaximumWidth(20);
51 aSelectPathBtn->setMaximumHeight(20);
52 aMainLay->addWidget(aSelectPathBtn, 1, 1);
53 aMainLay->setColumnStretch(0, 1);
54 myPathField->setMinimumHeight(20);
55 aMainLay->setHorizontalSpacing(1);
56 this->setLayout(aMainLay);
58 connect(myPathField, SIGNAL(textChanged(const QString&)),
59 this, SLOT(onPathChanged()));
60 connect(aSelectPathBtn, SIGNAL(clicked()),
61 this, SLOT(onPathSelectionBtn()));
64 ModuleBase_WidgetFileSelector::~ModuleBase_WidgetFileSelector()
68 bool ModuleBase_WidgetFileSelector::storeValueCustom() const
70 // A rare case when plugin was not loaded.
73 DataPtr aData = myFeature->data();
74 AttributeStringPtr aStringAttr = aData->string(attributeID());
75 QString aWidgetValue = myPathField->text();
76 aStringAttr->setValue(aWidgetValue.toStdString());
77 updateObject(myFeature);
81 bool ModuleBase_WidgetFileSelector::restoreValue()
83 // A rare case when plugin was not loaded.
86 DataPtr aData = myFeature->data();
87 AttributeStringPtr aStringAttr = aData->string(attributeID());
89 bool isBlocked = myPathField->blockSignals(true);
90 myPathField->setText(QString::fromStdString(aStringAttr->value()));
91 myPathField->blockSignals(isBlocked);
96 QList<QWidget*> ModuleBase_WidgetFileSelector::getControls() const
98 QList<QWidget*> result;
99 //QPushButton * aButton = this->findChild<QPushButton *>();
101 result << myPathField;
105 bool ModuleBase_WidgetFileSelector::isCurrentPathValid()
107 QFileInfo aFile (myPathField->text());
108 return aFile.exists();
112 void ModuleBase_WidgetFileSelector::onPathSelectionBtn()
114 QString aFilter = formatsString();
115 QString aFileName = QFileDialog::getOpenFileName(this, myTitle, myDefaultPath, aFilter);
116 if (!aFileName.isEmpty()) {
117 myPathField->setText(aFileName);
121 void ModuleBase_WidgetFileSelector::onPathChanged()
123 if(!isCurrentPathValid())
126 emit valuesChanged();
129 QString ModuleBase_WidgetFileSelector::formatsString() const
132 QStringList aValidatorFormats = getValidatorFormats();
134 foreach(QString eachFormat, aValidatorFormats) {
135 QStringList aFormatList = eachFormat.split("|");
136 aResult << QString("%1 files (%2)").arg(aFormatList.value(0))
137 .arg(QStringList(aFormatList).replaceInStrings(QRegExp("^(.*)$"), "*.\\1").join(" "));
139 aResult << QString("All files (*.*)");
140 return aResult.join(";;");
143 QStringList ModuleBase_WidgetFileSelector::getValidatorFormats() const
145 SessionPtr aMgr = ModelAPI_Session::get();
146 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
147 std::list<ModelAPI_Validator*> allValidators;
148 std::list<std::list<std::string> > allArguments;
149 aFactory->validators(myFeature->getKind(), myAttributeID, allValidators, allArguments);
150 //TODO(sbh): extract as separate method
151 if(allArguments.empty())
152 return QStringList();
153 std::list<std::string> anArgumentList = allArguments.front();
154 std::list<std::string>::const_iterator it = anArgumentList.begin();
156 for (; it != anArgumentList.end(); ++it) {
157 std::string anArg = *it;
158 int aSepPos = anArg.find(":");
159 if (aSepPos == std::string::npos) {
162 QString aFormat = QString::fromStdString(anArg.substr(0, aSepPos));
163 aFormat = aFormat.toUpper();
164 aResult.append(aFormat);