+// Copyright (C) 2014-20xx CEA/DEN, EDF R&D
+
/*
* ModuleBase_WidgetFileSelector.cpp
*
#include <ModelAPI_AttributeString.h>
#include <ModelAPI_Data.h>
#include <ModelAPI_Object.h>
+#include <ModelAPI_Validator.h>
+#include <ModelAPI_Session.h>
#include <ModuleBase_WidgetFileSelector.h>
+#include <ModuleBase_Tools.h>
#include <Config_WidgetAPI.h>
#include <QString>
#include <QLabel>
-#include <boost/smart_ptr/shared_ptr.hpp>
+#include <memory>
#include <string>
ModuleBase_WidgetFileSelector::ModuleBase_WidgetFileSelector(QWidget* theParent,
const std::string& theParentId)
: ModuleBase_ModelWidget(theParent, theData, theParentId)
{
- myHasDefaultValue = false;
-
myTitle = QString::fromStdString(theData->getProperty("title"));
- //TODO(sbh): Get them from the feature
- myFormats = getSupportedFormats(theData);
myDefaultPath = QString::fromStdString(theData->getProperty("path"));
- myMainWidget = new QWidget(theParent);
- QGridLayout* aMainLay = new QGridLayout(myMainWidget);
- aMainLay->setContentsMargins(0, 0, 0, 0);
- QLabel* aTitleLabel = new QLabel(myTitle, myMainWidget);
+ QGridLayout* aMainLay = new QGridLayout(this);
+ ModuleBase_Tools::adjustMargins(aMainLay);
+ QLabel* aTitleLabel = new QLabel(myTitle, this);
aTitleLabel->setIndent(1);
aMainLay->addWidget(aTitleLabel, 0, 0);
- myPathField = new QLineEdit(myMainWidget);
+ myPathField = new QLineEdit(this);
aMainLay->addWidget(myPathField, 1, 0);
- QPushButton* aSelectPathBtn = new QPushButton("...", myMainWidget);
+ QPushButton* aSelectPathBtn = new QPushButton("...", this);
+ aSelectPathBtn->setToolTip(tr("Select file..."));
aSelectPathBtn->setMaximumWidth(20);
aSelectPathBtn->setMaximumHeight(20);
aMainLay->addWidget(aSelectPathBtn, 1, 1);
aMainLay->setColumnStretch(0, 1);
myPathField->setMinimumHeight(20);
aMainLay->setHorizontalSpacing(1);
- myMainWidget->setLayout(aMainLay);
+ this->setLayout(aMainLay);
connect(myPathField, SIGNAL(textChanged(const QString&)),
this, SLOT(onPathChanged()));
{
}
-bool ModuleBase_WidgetFileSelector::storeValue() const
+bool ModuleBase_WidgetFileSelector::storeValueCustom() const
{
+ // A rare case when plugin was not loaded.
+ if(!myFeature)
+ return false;
DataPtr aData = myFeature->data();
AttributeStringPtr aStringAttr = aData->string(attributeID());
- QString aModelValue = QString::fromStdString(aStringAttr->value());
QString aWidgetValue = myPathField->text();
- if(aModelValue != aWidgetValue) {
- aStringAttr->setValue(aWidgetValue.toStdString());
- updateObject(myFeature);
- }
+ aStringAttr->setValue(aWidgetValue.toStdString());
+ updateObject(myFeature);
return true;
}
bool ModuleBase_WidgetFileSelector::restoreValue()
{
+ // A rare case when plugin was not loaded.
+ if(!myFeature)
+ return false;
DataPtr aData = myFeature->data();
AttributeStringPtr aStringAttr = aData->string(attributeID());
return true;
}
-QWidget* ModuleBase_WidgetFileSelector::getControl() const
-{
- return myMainWidget;
-}
-
QList<QWidget*> ModuleBase_WidgetFileSelector::getControls() const
{
QList<QWidget*> result;
- QPushButton * aButton = myMainWidget->findChild<QPushButton *>();
- result << aButton;
+ //QPushButton * aButton = this->findChild<QPushButton *>();
+ //result << aButton;
result << myPathField;
return result;
}
bool ModuleBase_WidgetFileSelector::isCurrentPathValid()
{
QFileInfo aFile (myPathField->text());
- return aFile.exists() && myFormats.contains(aFile.suffix(), Qt::CaseInsensitive);
+ return aFile.exists();
}
void ModuleBase_WidgetFileSelector::onPathSelectionBtn()
{
- QString aFilter = formatsString(myFormats);
- QString aFileName = QFileDialog::getOpenFileName(myMainWidget, myTitle, myDefaultPath, aFilter);
+ QString aFilter = formatsString();
+ QString aFileName = QFileDialog::getOpenFileName(this, myTitle, myDefaultPath, aFilter);
if (!aFileName.isEmpty()) {
myPathField->setText(aFileName);
}
emit valuesChanged();
}
-QStringList ModuleBase_WidgetFileSelector::getSupportedFormats(const Config_WidgetAPI* theData) const
-{
- QString aXMLFormat = QString::fromStdString(theData->getProperty("formats"));
- aXMLFormat = aXMLFormat.toUpper();
- const QChar kSep = ',';
- return aXMLFormat.split(kSep, QString::SkipEmptyParts);
-}
-
-QString ModuleBase_WidgetFileSelector::formatsString(const QStringList theFormats) const
+QString ModuleBase_WidgetFileSelector::formatsString() const
{
QStringList aResult;
- foreach(QString eachFormat, theFormats) {
+ QStringList aValidatorFormats = getValidatorFormats();
+
+ foreach(QString eachFormat, aValidatorFormats) {
aResult << QString("%1 files (*.%1)").arg(eachFormat);
}
+ aResult << QString("All files (*.*)");
return aResult.join(";;");
}
+QStringList ModuleBase_WidgetFileSelector::getValidatorFormats() const
+{
+ SessionPtr aMgr = ModelAPI_Session::get();
+ ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
+ std::list<ModelAPI_Validator*> allValidators;
+ std::list<std::list<std::string> > allArguments;
+ aFactory->validators(myFeature->getKind(), myAttributeID, allValidators, allArguments);
+ //TODO(sbh): extract as separate method
+ if(allArguments.empty())
+ return QStringList();
+ std::list<std::string> anArgumentList = allArguments.front();
+ std::list<std::string>::const_iterator it = anArgumentList.begin();
+ QStringList aResult;
+ for (; it != anArgumentList.end(); ++it) {
+ std::string anArg = *it;
+ int aSepPos = anArg.find(":");
+ if (aSepPos == std::string::npos) {
+ continue;
+ }
+ QString aFormat = QString::fromStdString(anArg.substr(0, aSepPos));
+ aFormat = aFormat.toUpper();
+ aResult.append(aFormat);
+ }
+ return aResult;
+}