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 : ModuleBase_ModelWidget(theParent, theData)
37 myTitle = translate(theData->getProperty("title"));
38 myType = (theData->getProperty("type") == "save") ? WFS_SAVE : WFS_OPEN;
39 myDefaultPath = QString::fromStdString(theData->getProperty("path"));
41 QGridLayout* aMainLay = new QGridLayout(this);
42 ModuleBase_Tools::adjustMargins(aMainLay);
43 QLabel* aTitleLabel = new QLabel(myTitle, this);
44 aTitleLabel->setIndent(1);
45 aMainLay->addWidget(aTitleLabel, 0, 0);
46 myPathField = new QLineEdit(this);
47 aMainLay->addWidget(myPathField, 1, 0);
48 QPushButton* aSelectPathBtn = new QPushButton("...", this);
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 this->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()
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::restoreValueCustom()
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 QString aNewText = QString::fromStdString(aStringAttr->value());
91 if( myPathField->text() != aNewText )
92 myPathField->setText( aNewText );
93 myPathField->blockSignals(isBlocked);
98 QList<QWidget*> ModuleBase_WidgetFileSelector::getControls() const
100 QList<QWidget*> result;
101 result << myPathField;
105 bool ModuleBase_WidgetFileSelector::isCurrentPathValid()
107 QFileInfo aFile(myPathField->text());
108 return aFile.exists();
111 void ModuleBase_WidgetFileSelector::onPathSelectionBtn()
113 QString aDefaultPath = myPathField->text().isEmpty()
115 : QFileInfo(myPathField->text()).absolutePath();
116 QString aFilter = filterString();
117 QString aFileName = (myType == WFS_SAVE)
118 ? QFileDialog::getSaveFileName(this, myTitle, aDefaultPath, aFilter, &mySelectedFilter)
119 : QFileDialog::getOpenFileName(this, myTitle, aDefaultPath, aFilter, &mySelectedFilter);
120 if (!aFileName.isEmpty()) {
121 if (myType == WFS_SAVE)
122 aFileName = applyExtension(aFileName, mySelectedFilter);
123 myPathField->setText(aFileName);
124 emit focusOutWidget(this);
128 void ModuleBase_WidgetFileSelector::onPathChanged()
130 if (myType == WFS_OPEN && !isCurrentPathValid())
133 emit valuesChanged();
136 QString ModuleBase_WidgetFileSelector::formatToFilter(const QString & theFormat)
138 if (theFormat.isEmpty() && !theFormat.contains(":"))
141 QStringList aExtesionList = theFormat.section(':', 0, 0).split("|");
142 QString aFormat = theFormat.section(':', 1, 1);
143 return QString("%1 files (%2)").arg(aFormat)
144 .arg(QStringList(aExtesionList).replaceInStrings(QRegExp("^(.*)$"), "*.\\1").join(" "));
147 QString ModuleBase_WidgetFileSelector::filterToShortFormat(const QString & theFilter)
149 // Simplified implementation.
150 // It relies on theFilter was made by formatToFilter() function.
151 return theFilter.section(' ', 0, 0);
154 QStringList ModuleBase_WidgetFileSelector::filterToExtensions(const QString & theFilter)
156 // Simplified implementation.
157 // It relies on theFilter was made by formatToFilter() function.
158 QStringList anExtensions = theFilter.section("(", 1, 1).section(")", 0, 0).split(" ");
162 QStringList ModuleBase_WidgetFileSelector::getValidatorFormats() const
164 SessionPtr aMgr = ModelAPI_Session::get();
165 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
167 ModelAPI_ValidatorsFactory::Validators allValidators;
168 aFactory->validators(myFeature->getKind(), myAttributeID, allValidators);
171 std::list<std::string> anArgumentList = allValidators.front().second;
172 std::list<std::string>::const_iterator it = anArgumentList.begin();
173 for (; it != anArgumentList.end(); ++it) {
174 QString aFormat = QString::fromStdString(*it);
175 if (!aFormat.isEmpty())
181 QString ModuleBase_WidgetFileSelector::filterString() const
184 QStringList aValidatorFormats = getValidatorFormats();
186 foreach(const QString & eachFormat, aValidatorFormats) {
187 aResult << formatToFilter(eachFormat);
189 if (myType == WFS_OPEN)
190 aResult << QString("All files (*.*)");
191 return aResult.join(";;");
194 QString ModuleBase_WidgetFileSelector::applyExtension(const QString& theFileName,
195 const QString& theFilter)
197 QString aResult = theFileName;
198 bool hasExtension = false;
199 QStringList anExtensions = filterToExtensions(theFilter);
200 foreach(const QString& anExtension, anExtensions) {
201 if (theFileName.endsWith(anExtension.section(".", 1, 1), Qt::CaseInsensitive)) {
206 if (!hasExtension && !anExtensions.isEmpty())
207 aResult = QString("%1.%2").arg(theFileName).arg(anExtensions[0].section(".", 1, 1));