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->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 myMainWidget->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 QWidget* ModuleBase_WidgetFileSelector::getControl() const
101 QList<QWidget*> ModuleBase_WidgetFileSelector::getControls() const
103 QList<QWidget*> result;
104 //QPushButton * aButton = myMainWidget->findChild<QPushButton *>();
106 result << myPathField;
110 bool ModuleBase_WidgetFileSelector::isCurrentPathValid()
112 QFileInfo aFile (myPathField->text());
113 return aFile.exists();
117 void ModuleBase_WidgetFileSelector::onPathSelectionBtn()
119 QString aFilter = formatsString();
120 QString aFileName = QFileDialog::getOpenFileName(myMainWidget, myTitle, myDefaultPath, aFilter);
121 if (!aFileName.isEmpty()) {
122 myPathField->setText(aFileName);
126 void ModuleBase_WidgetFileSelector::onPathChanged()
128 if(!isCurrentPathValid())
131 emit valuesChanged();
134 QString ModuleBase_WidgetFileSelector::formatsString() const
137 QStringList aValidatorFormats = getValidatorFormats();
139 foreach(QString eachFormat, aValidatorFormats) {
140 aResult << QString("%1 files (*.%1)").arg(eachFormat);
142 aResult << QString("All files (*.*)");
143 return aResult.join(";;");
146 QStringList ModuleBase_WidgetFileSelector::getValidatorFormats() const
148 SessionPtr aMgr = ModelAPI_Session::get();
149 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
150 std::list<ModelAPI_Validator*> allValidators;
151 std::list<std::list<std::string> > allArguments;
152 aFactory->validators(myFeature->getKind(), myAttributeID, allValidators, allArguments);
153 //TODO(sbh): extract as separate method
154 if(allArguments.empty())
155 return QStringList();
156 std::list<std::string> anArgumentList = allArguments.front();
157 std::list<std::string>::const_iterator it = anArgumentList.begin();
159 for (; it != anArgumentList.end(); ++it) {
160 std::string anArg = *it;
161 int aSepPos = anArg.find(":");
162 if (aSepPos == std::string::npos) {
165 QString aFormat = QString::fromStdString(anArg.substr(0, aSepPos));
166 aFormat = aFormat.toUpper();
167 aResult.append(aFormat);