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 myMainWidget = new QWidget(theParent);
41 QGridLayout* aMainLay = new QGridLayout(myMainWidget);
42 ModuleBase_Tools::adjustMargins(aMainLay);
43 QLabel* aTitleLabel = new QLabel(myTitle, myMainWidget);
44 aTitleLabel->setIndent(1);
45 aMainLay->addWidget(aTitleLabel, 0, 0);
46 myPathField = new QLineEdit(myMainWidget);
47 aMainLay->addWidget(myPathField, 1, 0);
48 QPushButton* aSelectPathBtn = new QPushButton("...", myMainWidget);
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 myMainWidget->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::storeValue() 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 QWidget* ModuleBase_WidgetFileSelector::getControl() const
100 QList<QWidget*> ModuleBase_WidgetFileSelector::getControls() const
102 QList<QWidget*> result;
103 //QPushButton * aButton = myMainWidget->findChild<QPushButton *>();
105 result << myPathField;
109 bool ModuleBase_WidgetFileSelector::isCurrentPathValid()
111 QFileInfo aFile (myPathField->text());
112 return aFile.exists();
116 void ModuleBase_WidgetFileSelector::onPathSelectionBtn()
118 QString aFilter = formatsString();
119 QString aFileName = QFileDialog::getOpenFileName(myMainWidget, myTitle, myDefaultPath, aFilter);
120 if (!aFileName.isEmpty()) {
121 myPathField->setText(aFileName);
125 void ModuleBase_WidgetFileSelector::onPathChanged()
127 if(!isCurrentPathValid())
130 emit valuesChanged();
133 QString ModuleBase_WidgetFileSelector::formatsString() const
136 QStringList aValidatorFormats = getValidatorFormats();
138 foreach(QString eachFormat, aValidatorFormats) {
139 aResult << QString("%1 files (*.%1)").arg(eachFormat);
141 aResult << QString("All files (*.*)");
142 return aResult.join(";;");
145 QStringList ModuleBase_WidgetFileSelector::getValidatorFormats() const
147 SessionPtr aMgr = ModelAPI_Session::get();
148 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
149 std::list<ModelAPI_Validator*> allValidators;
150 std::list<std::list<std::string> > allArguments;
151 aFactory->validators(myFeature->getKind(), myAttributeID, allValidators, allArguments);
152 //TODO(sbh): extract as separate method
153 if(allArguments.empty())
154 return QStringList();
155 std::list<std::string> anArgumentList = allArguments.front();
156 std::list<std::string>::const_iterator it = anArgumentList.begin();
158 for (; it != anArgumentList.end(); ++it) {
159 std::string anArg = *it;
160 int aSepPos = anArg.find(":");
161 if (aSepPos == std::string::npos) {
164 QString aFormat = QString::fromStdString(anArg.substr(0, aSepPos));
165 aFormat = aFormat.toUpper();
166 aResult.append(aFormat);