2 * ModuleBase_WidgetFileSelector.cpp
4 * Created on: Aug 28, 2014
8 #include <ModelAPI_AttributeString.h>
9 #include <ModelAPI_Data.h>
10 #include <ModelAPI_Object.h>
11 #include <ModelAPI_Validator.h>
12 #include <ModelAPI_Session.h>
13 #include <ModuleBase_WidgetFileSelector.h>
14 #include <ModuleBase_Tools.h>
16 #include <Config_WidgetAPI.h>
18 #include <QGridLayout>
19 #include <QFileDialog>
23 #include <QPushButton>
30 ModuleBase_WidgetFileSelector::ModuleBase_WidgetFileSelector(QWidget* theParent,
31 const Config_WidgetAPI* theData,
32 const std::string& theParentId)
33 : ModuleBase_ModelWidget(theParent, theData, theParentId)
35 myTitle = QString::fromStdString(theData->getProperty("title"));
36 myDefaultPath = QString::fromStdString(theData->getProperty("path"));
38 myMainWidget = new QWidget(theParent);
39 QGridLayout* aMainLay = new QGridLayout(myMainWidget);
40 ModuleBase_Tools::adjustMargins(aMainLay);
41 QLabel* aTitleLabel = new QLabel(myTitle, myMainWidget);
42 aTitleLabel->setIndent(1);
43 aMainLay->addWidget(aTitleLabel, 0, 0);
44 myPathField = new QLineEdit(myMainWidget);
45 aMainLay->addWidget(myPathField, 1, 0);
46 QPushButton* aSelectPathBtn = new QPushButton("...", myMainWidget);
47 aSelectPathBtn->setMaximumWidth(20);
48 aSelectPathBtn->setMaximumHeight(20);
49 aMainLay->addWidget(aSelectPathBtn, 1, 1);
50 aMainLay->setColumnStretch(0, 1);
51 myPathField->setMinimumHeight(20);
52 aMainLay->setHorizontalSpacing(1);
53 myMainWidget->setLayout(aMainLay);
55 connect(myPathField, SIGNAL(textChanged(const QString&)),
56 this, SLOT(onPathChanged()));
57 connect(aSelectPathBtn, SIGNAL(clicked()),
58 this, SLOT(onPathSelectionBtn()));
61 ModuleBase_WidgetFileSelector::~ModuleBase_WidgetFileSelector()
65 bool ModuleBase_WidgetFileSelector::storeValue() const
67 // A rare case when plugin was not loaded.
70 DataPtr aData = myFeature->data();
71 AttributeStringPtr aStringAttr = aData->string(attributeID());
72 QString aWidgetValue = myPathField->text();
73 aStringAttr->setValue(aWidgetValue.toStdString());
74 updateObject(myFeature);
78 bool ModuleBase_WidgetFileSelector::restoreValue()
80 // A rare case when plugin was not loaded.
83 DataPtr aData = myFeature->data();
84 AttributeStringPtr aStringAttr = aData->string(attributeID());
86 bool isBlocked = myPathField->blockSignals(true);
87 myPathField->setText(QString::fromStdString(aStringAttr->value()));
88 myPathField->blockSignals(isBlocked);
93 QWidget* ModuleBase_WidgetFileSelector::getControl() const
98 QList<QWidget*> ModuleBase_WidgetFileSelector::getControls() const
100 QList<QWidget*> result;
101 //QPushButton * aButton = myMainWidget->findChild<QPushButton *>();
103 result << myPathField;
107 bool ModuleBase_WidgetFileSelector::isCurrentPathValid()
109 QFileInfo aFile (myPathField->text());
110 return aFile.exists();
114 void ModuleBase_WidgetFileSelector::onPathSelectionBtn()
116 QString aFilter = formatsString();
117 QString aFileName = QFileDialog::getOpenFileName(myMainWidget, myTitle, myDefaultPath, aFilter);
118 if (!aFileName.isEmpty()) {
119 myPathField->setText(aFileName);
123 void ModuleBase_WidgetFileSelector::onPathChanged()
125 if(!isCurrentPathValid())
128 emit valuesChanged();
131 QString ModuleBase_WidgetFileSelector::formatsString() const
134 QStringList aValidatorFormats = getValidatorFormats();
136 foreach(QString eachFormat, aValidatorFormats) {
137 aResult << QString("%1 files (*.%1)").arg(eachFormat);
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);