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 <QGridLayout>
21 #include <QFileDialog>
25 #include <QPushButton>
32 ModuleBase_WidgetFileSelector::ModuleBase_WidgetFileSelector(QWidget* theParent,
33 const Config_WidgetAPI* theData,
34 const std::string& theParentId)
35 : ModuleBase_ModelWidget(theParent, theData, theParentId)
37 myTitle = QString::fromStdString(theData->getProperty("title"));
38 myDefaultPath = QString::fromStdString(theData->getProperty("path"));
40 QGridLayout* aMainLay = new QGridLayout(this);
41 ModuleBase_Tools::adjustMargins(aMainLay);
42 QLabel* aTitleLabel = new QLabel(myTitle, this);
43 aTitleLabel->setIndent(1);
44 aMainLay->addWidget(aTitleLabel, 0, 0);
45 myPathField = new QLineEdit(this);
46 aMainLay->addWidget(myPathField, 1, 0);
47 QPushButton* aSelectPathBtn = new QPushButton("...", this);
48 aSelectPathBtn->setToolTip(tr("Select file..."));
49 aSelectPathBtn->setMaximumWidth(20);
50 aSelectPathBtn->setMaximumHeight(20);
51 aMainLay->addWidget(aSelectPathBtn, 1, 1);
52 aMainLay->setColumnStretch(0, 1);
53 myPathField->setMinimumHeight(20);
54 aMainLay->setHorizontalSpacing(1);
55 this->setLayout(aMainLay);
57 connect(myPathField, SIGNAL(textChanged(const QString&)),
58 this, SLOT(onPathChanged()));
59 connect(aSelectPathBtn, SIGNAL(clicked()),
60 this, SLOT(onPathSelectionBtn()));
63 ModuleBase_WidgetFileSelector::~ModuleBase_WidgetFileSelector()
67 bool ModuleBase_WidgetFileSelector::storeValueCustom() const
69 // A rare case when plugin was not loaded.
72 DataPtr aData = myFeature->data();
73 AttributeStringPtr aStringAttr = aData->string(attributeID());
74 QString aWidgetValue = myPathField->text();
75 aStringAttr->setValue(aWidgetValue.toStdString());
76 updateObject(myFeature);
80 bool ModuleBase_WidgetFileSelector::restoreValue()
82 // A rare case when plugin was not loaded.
85 DataPtr aData = myFeature->data();
86 AttributeStringPtr aStringAttr = aData->string(attributeID());
88 bool isBlocked = myPathField->blockSignals(true);
89 myPathField->setText(QString::fromStdString(aStringAttr->value()));
90 myPathField->blockSignals(isBlocked);
95 QList<QWidget*> ModuleBase_WidgetFileSelector::getControls() const
97 QList<QWidget*> result;
98 //QPushButton * aButton = this->findChild<QPushButton *>();
100 result << myPathField;
104 bool ModuleBase_WidgetFileSelector::isCurrentPathValid()
106 QFileInfo aFile (myPathField->text());
107 return aFile.exists();
111 void ModuleBase_WidgetFileSelector::onPathSelectionBtn()
113 QString aFilter = formatsString();
114 QString aFileName = QFileDialog::getOpenFileName(this, myTitle, myDefaultPath, aFilter);
115 if (!aFileName.isEmpty()) {
116 myPathField->setText(aFileName);
120 void ModuleBase_WidgetFileSelector::onPathChanged()
122 if(!isCurrentPathValid())
125 emit valuesChanged();
128 QString ModuleBase_WidgetFileSelector::formatsString() const
131 QStringList aValidatorFormats = getValidatorFormats();
133 foreach(QString eachFormat, aValidatorFormats) {
134 aResult << QString("%1 files (*.%1)").arg(eachFormat);
136 aResult << QString("All files (*.*)");
137 return aResult.join(";;");
140 QStringList ModuleBase_WidgetFileSelector::getValidatorFormats() const
142 SessionPtr aMgr = ModelAPI_Session::get();
143 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
144 std::list<ModelAPI_Validator*> allValidators;
145 std::list<std::list<std::string> > allArguments;
146 aFactory->validators(myFeature->getKind(), myAttributeID, allValidators, allArguments);
147 //TODO(sbh): extract as separate method
148 if(allArguments.empty())
149 return QStringList();
150 std::list<std::string> anArgumentList = allArguments.front();
151 std::list<std::string>::const_iterator it = anArgumentList.begin();
153 for (; it != anArgumentList.end(); ++it) {
154 std::string anArg = *it;
155 int aSepPos = anArg.find(":");
156 if (aSepPos == std::string::npos) {
159 QString aFormat = QString::fromStdString(anArg.substr(0, aSepPos));
160 aFormat = aFormat.toUpper();
161 aResult.append(aFormat);