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 <QFileDialog>
21 #include <QGridLayout>
26 #include <QPushButton>
33 ModuleBase_WidgetFileSelector::ModuleBase_WidgetFileSelector(QWidget* theParent,
34 const Config_WidgetAPI* theData,
35 const std::string& theParentId)
36 : ModuleBase_ModelWidget(theParent, theData, theParentId)
38 myTitle = QString::fromStdString(theData->getProperty("title"));
39 myType = (theData->getProperty("type") == "save") ? WFS_SAVE : WFS_OPEN;
40 myDefaultPath = QString::fromStdString(theData->getProperty("path"));
42 QGridLayout* aMainLay = new QGridLayout(this);
43 ModuleBase_Tools::adjustMargins(aMainLay);
44 QLabel* aTitleLabel = new QLabel(myTitle, this);
45 aTitleLabel->setIndent(1);
46 aMainLay->addWidget(aTitleLabel, 0, 0);
47 myPathField = new QLineEdit(this);
48 aMainLay->addWidget(myPathField, 1, 0);
49 QPushButton* aSelectPathBtn = new QPushButton("...", this);
50 aSelectPathBtn->setToolTip(tr("Select file..."));
51 aSelectPathBtn->setMaximumWidth(20);
52 aSelectPathBtn->setMaximumHeight(20);
53 aMainLay->addWidget(aSelectPathBtn, 1, 1);
54 aMainLay->setColumnStretch(0, 1);
55 myPathField->setMinimumHeight(20);
56 aMainLay->setHorizontalSpacing(1);
57 this->setLayout(aMainLay);
59 connect(myPathField, SIGNAL(textChanged(const QString&)),
60 this, SLOT(onPathChanged()));
61 connect(aSelectPathBtn, SIGNAL(clicked()),
62 this, SLOT(onPathSelectionBtn()));
65 ModuleBase_WidgetFileSelector::~ModuleBase_WidgetFileSelector()
69 bool ModuleBase_WidgetFileSelector::storeValueCustom() const
71 // A rare case when plugin was not loaded.
74 DataPtr aData = myFeature->data();
75 AttributeStringPtr aStringAttr = aData->string(attributeID());
76 QString aWidgetValue = myPathField->text();
77 aStringAttr->setValue(aWidgetValue.toStdString());
78 updateObject(myFeature);
82 bool ModuleBase_WidgetFileSelector::restoreValueCustom()
84 // A rare case when plugin was not loaded.
87 DataPtr aData = myFeature->data();
88 AttributeStringPtr aStringAttr = aData->string(attributeID());
90 bool isBlocked = myPathField->blockSignals(true);
91 myPathField->setText(QString::fromStdString(aStringAttr->value()));
92 myPathField->blockSignals(isBlocked);
97 QList<QWidget*> ModuleBase_WidgetFileSelector::getControls() const
99 QList<QWidget*> result;
100 result << myPathField;
104 bool ModuleBase_WidgetFileSelector::isCurrentPathValid()
106 QFileInfo aFile(myPathField->text());
107 return aFile.exists();
110 void ModuleBase_WidgetFileSelector::onPathSelectionBtn()
112 QString aDefaultPath = myPathField->text().isEmpty()
114 : QFileInfo(myPathField->text()).absolutePath();
115 QString aFilter = filterString();
116 QString aFileName = (myType == WFS_SAVE)
117 ? QFileDialog::getSaveFileName(this, myTitle, aDefaultPath, aFilter, &mySelectedFilter)
118 : QFileDialog::getOpenFileName(this, myTitle, aDefaultPath, aFilter, &mySelectedFilter);
119 if (!aFileName.isEmpty()) {
120 if (myType == WFS_SAVE)
121 aFileName = applyExtension(aFileName, mySelectedFilter);
122 myPathField->setText(aFileName);
126 void ModuleBase_WidgetFileSelector::onPathChanged()
128 if (myType == WFS_OPEN && !isCurrentPathValid())
131 emit valuesChanged();
134 QString ModuleBase_WidgetFileSelector::formatToFilter(const QString & theFormat)
136 if (theFormat.isEmpty() && !theFormat.contains(":"))
139 QStringList aExtesionList = theFormat.section(':', 0, 0).split("|");
140 QString aFormat = theFormat.section(':', 1, 1);
141 return QString("%1 files (%2)").arg(aFormat)
142 .arg(QStringList(aExtesionList).replaceInStrings(QRegExp("^(.*)$"), "*.\\1").join(" "));
145 QString ModuleBase_WidgetFileSelector::filterToShortFormat(const QString & theFilter)
147 // Simplified implementation.
148 // It relies on theFilter was made by formatToFilter() function.
149 return theFilter.section(' ', 0, 0);
152 QStringList ModuleBase_WidgetFileSelector::filterToExtensions(const QString & theFilter)
154 // Simplified implementation.
155 // It relies on theFilter was made by formatToFilter() function.
156 QStringList anExtensions = theFilter.section("(", 1, 1).section(")", 0, 0).split(" ");
160 QStringList ModuleBase_WidgetFileSelector::getValidatorFormats() const
162 SessionPtr aMgr = ModelAPI_Session::get();
163 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
165 ModelAPI_ValidatorsFactory::Validators allValidators;
166 aFactory->validators(myFeature->getKind(), myAttributeID, allValidators);
169 std::list<std::string> anArgumentList = allValidators.front().second;
170 std::list<std::string>::const_iterator it = anArgumentList.begin();
171 for (; it != anArgumentList.end(); ++it) {
172 QString aFormat = QString::fromStdString(*it);
173 if (!aFormat.isEmpty())
179 QString ModuleBase_WidgetFileSelector::filterString() const
182 QStringList aValidatorFormats = getValidatorFormats();
184 foreach(const QString & eachFormat, aValidatorFormats) {
185 aResult << formatToFilter(eachFormat);
187 if (myType == WFS_OPEN)
188 aResult << QString("All files (*.*)");
189 return aResult.join(";;");
192 QString ModuleBase_WidgetFileSelector::applyExtension(const QString& theFileName,
193 const QString& theFilter)
195 QString aResult = theFileName;
196 bool hasExtension = false;
197 QStringList anExtensions = filterToExtensions(theFilter);
198 foreach(const QString& anExtension, anExtensions) {
199 if (theFileName.endsWith(anExtension.section(".", 1, 1), Qt::CaseInsensitive)) {
204 if (!hasExtension && !anExtensions.isEmpty())
205 aResult = QString("%1.%2").arg(theFileName).arg(anExtensions[0].section(".", 1, 1));