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 <ModuleBase_WidgetFileSelector.h>
13 #include <ModuleBase_Tools.h>
15 #include <Config_WidgetAPI.h>
17 #include <QGridLayout>
18 #include <QFileDialog>
22 #include <QPushButton>
26 #include <boost/smart_ptr/shared_ptr.hpp>
29 ModuleBase_WidgetFileSelector::ModuleBase_WidgetFileSelector(QWidget* theParent,
30 const Config_WidgetAPI* theData,
31 const std::string& theParentId)
32 : ModuleBase_ModelWidget(theParent, theData, theParentId)
34 myTitle = QString::fromStdString(theData->getProperty("title"));
35 myDefaultPath = QString::fromStdString(theData->getProperty("path"));
37 myMainWidget = new QWidget(theParent);
38 QGridLayout* aMainLay = new QGridLayout(myMainWidget);
39 ModuleBase_Tools::adjustMargins(aMainLay);
40 QLabel* aTitleLabel = new QLabel(myTitle, myMainWidget);
41 aTitleLabel->setIndent(1);
42 aMainLay->addWidget(aTitleLabel, 0, 0);
43 myPathField = new QLineEdit(myMainWidget);
44 aMainLay->addWidget(myPathField, 1, 0);
45 QPushButton* aSelectPathBtn = new QPushButton("...", myMainWidget);
46 aSelectPathBtn->setMaximumWidth(20);
47 aSelectPathBtn->setMaximumHeight(20);
48 aMainLay->addWidget(aSelectPathBtn, 1, 1);
49 aMainLay->setColumnStretch(0, 1);
50 myPathField->setMinimumHeight(20);
51 aMainLay->setHorizontalSpacing(1);
52 myMainWidget->setLayout(aMainLay);
54 connect(myPathField, SIGNAL(textChanged(const QString&)),
55 this, SLOT(onPathChanged()));
56 connect(aSelectPathBtn, SIGNAL(clicked()),
57 this, SLOT(onPathSelectionBtn()));
60 ModuleBase_WidgetFileSelector::~ModuleBase_WidgetFileSelector()
64 bool ModuleBase_WidgetFileSelector::storeValue() const
66 // A rare case when plugin was not loaded.
69 DataPtr aData = myFeature->data();
70 AttributeStringPtr aStringAttr = aData->string(attributeID());
71 QString aWidgetValue = myPathField->text();
72 aStringAttr->setValue(aWidgetValue.toStdString());
73 updateObject(myFeature);
77 bool ModuleBase_WidgetFileSelector::restoreValue()
79 // A rare case when plugin was not loaded.
82 DataPtr aData = myFeature->data();
83 AttributeStringPtr aStringAttr = aData->string(attributeID());
85 bool isBlocked = myPathField->blockSignals(true);
86 myPathField->setText(QString::fromStdString(aStringAttr->value()));
87 myPathField->blockSignals(isBlocked);
92 QWidget* ModuleBase_WidgetFileSelector::getControl() const
97 QList<QWidget*> ModuleBase_WidgetFileSelector::getControls() const
99 QList<QWidget*> result;
100 QPushButton * aButton = myMainWidget->findChild<QPushButton *>();
102 result << myPathField;
106 bool ModuleBase_WidgetFileSelector::isCurrentPathValid()
108 QFileInfo aFile (myPathField->text());
109 return aFile.exists();
113 void ModuleBase_WidgetFileSelector::onPathSelectionBtn()
115 QString aFilter = formatsString();
116 QString aFileName = QFileDialog::getOpenFileName(myMainWidget, myTitle, myDefaultPath, aFilter);
117 if (!aFileName.isEmpty()) {
118 myPathField->setText(aFileName);
122 void ModuleBase_WidgetFileSelector::onPathChanged()
124 if(!isCurrentPathValid())
127 emit valuesChanged();
130 QString ModuleBase_WidgetFileSelector::formatsString() const
133 QStringList aValidatorFormats = getValidatorFormats();
135 foreach(QString eachFormat, aValidatorFormats) {
136 aResult << QString("%1 files (*.%1)").arg(eachFormat);
138 aResult << QString("All files (*.*)");
139 return aResult.join(";;");
142 QStringList ModuleBase_WidgetFileSelector::getValidatorFormats() const
144 SessionPtr aMgr = ModelAPI_Session::get();
145 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
146 std::list<ModelAPI_Validator*> allValidators;
147 std::list<std::list<std::string> > allArguments;
148 aFactory->validators(myFeature->getKind(), myAttributeID, allValidators, allArguments);
149 //TODO(sbh): extract as separate method
150 if(allArguments.empty())
151 return QStringList();
152 std::list<std::string> anArgumentList = allArguments.front();
153 std::list<std::string>::const_iterator it = anArgumentList.begin();
155 for (; it != anArgumentList.end(); ++it) {
156 std::string anArg = *it;
157 int aSepPos = anArg.find(":");
158 if (aSepPos == std::string::npos) {
161 QString aFormat = QString::fromStdString(anArg.substr(0, aSepPos));
162 aFormat = aFormat.toUpper();
163 aResult.append(aFormat);