Skip to content

Commit 302d3f5

Browse files
committed
Core: allow to set file filter from PropertyFileIncluded
1 parent ace6d5a commit 302d3f5

File tree

3 files changed

+61
-9
lines changed

3 files changed

+61
-9
lines changed

src/App/PropertyFile.cpp

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -278,15 +278,17 @@ bool isIOFile(PyObject* file)
278278

279279
void PropertyFileIncluded::setPyObject(PyObject *value)
280280
{
281-
std::string string;
282281
if (PyUnicode_Check(value)) {
283-
string = PyUnicode_AsUTF8(value);
282+
std::string string = PyUnicode_AsUTF8(value);
283+
setValue(string.c_str());
284284
}
285285
else if (PyBytes_Check(value)) {
286-
string = PyBytes_AsString(value);
286+
std::string string = PyBytes_AsString(value);
287+
setValue(string.c_str());
287288
}
288289
else if (isIOFile(value)){
289-
string = getNameFromFile(value);
290+
std::string string = getNameFromFile(value);
291+
setValue(string.c_str());
290292
}
291293
else if (PyTuple_Check(value)) {
292294
if (PyTuple_Size(value) != 2)
@@ -329,16 +331,22 @@ void PropertyFileIncluded::setPyObject(PyObject *value)
329331
}
330332

331333
setValue(fileStr.c_str(),nameStr.c_str());
332-
return;
334+
}
335+
else if (PyDict_Check(value)) {
336+
Py::Dict dict(value);
337+
if (dict.hasKey("filter")) {
338+
setFilter(Py::String(dict.getItem("filter")));
339+
}
340+
if (dict.hasKey("filename")) {
341+
std::string string = static_cast<std::string>(Py::String(dict.getItem("filename")));
342+
setValue(string.c_str());
343+
}
333344
}
334345
else {
335346
std::string error = std::string("Type must be string or file, not ");
336347
error += value->ob_type->tp_name;
337348
throw Base::TypeError(error);
338349
}
339-
340-
// assign the string
341-
setValue(string.c_str());
342350
}
343351

344352
void PropertyFileIncluded::Save (Base::Writer &writer) const
@@ -569,6 +577,16 @@ unsigned int PropertyFileIncluded::getMemSize () const
569577
return mem;
570578
}
571579

580+
void PropertyFileIncluded::setFilter(std::string filter)
581+
{
582+
m_filter = std::move(filter);
583+
}
584+
585+
std::string PropertyFileIncluded::getFilter() const
586+
{
587+
return m_filter;
588+
}
589+
572590
//**************************************************************************
573591
// PropertyFile
574592
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
@@ -592,3 +610,20 @@ std::string PropertyFile::getFilter() const
592610
return m_filter;
593611
}
594612

613+
void PropertyFile::setPyObject(PyObject *value)
614+
{
615+
if (PyDict_Check(value)) {
616+
Py::Dict dict(value);
617+
if (dict.hasKey("filter")) {
618+
setFilter(Py::String(dict.getItem("filter")));
619+
}
620+
if (dict.hasKey("filename")) {
621+
std::string string = static_cast<std::string>(Py::String(dict.getItem("filename")));
622+
setValue(string.c_str());
623+
}
624+
}
625+
else {
626+
PropertyString::setPyObject(value);
627+
}
628+
}
629+

src/App/PropertyFile.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ class AppExport PropertyFile : public PropertyString
5050
const char* getEditorName() const override
5151
{ return "Gui::PropertyEditor::PropertyFileItem"; }
5252

53+
void setPyObject(PyObject *) override;
5354
virtual void setFilter(const std::string filter);
5455
virtual std::string getFilter() const;
5556

@@ -86,7 +87,7 @@ class AppExport PropertyFileIncluded : public Property
8687
{ return "Gui::PropertyEditor::PropertyTransientFileItem"; }
8788
PyObject *getPyObject() override;
8889
void setPyObject(PyObject *) override;
89-
90+
9091
void Save (Base::Writer &writer) const override;
9192
void Restore(Base::XMLReader &reader) override;
9293

@@ -116,6 +117,9 @@ class AppExport PropertyFileIncluded : public Property
116117

117118
bool isEmpty() const {return _cValue.empty();}
118119

120+
void setFilter(std::string filter);
121+
std::string getFilter() const;
122+
119123
protected:
120124
// get the transient path if the property is in a DocumentObject
121125
std::string getDocTransientPath() const;
@@ -126,6 +130,9 @@ class AppExport PropertyFileIncluded : public Property
126130
mutable std::string _cValue;
127131
mutable std::string _BaseFileName;
128132
mutable std::string _OriginalName;
133+
134+
private:
135+
std::string m_filter;
129136
};
130137

131138

src/Gui/propertyeditor/PropertyItem.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4247,6 +4247,16 @@ void PropertyTransientFileItem::setEditorData(QWidget *editor, const QVariant& d
42474247
{
42484248
auto fc = qobject_cast<Gui::FileChooser*>(editor);
42494249
fc->setFileName(data.toString());
4250+
4251+
const auto prop = static_cast
4252+
<const App::PropertyFileIncluded*>(getFirstProperty());
4253+
4254+
if (prop) {
4255+
std::string filter = prop->getFilter();
4256+
if (!filter.empty()) {
4257+
fc->setFilter(QString::fromStdString(filter));
4258+
}
4259+
}
42504260
}
42514261

42524262
QVariant PropertyTransientFileItem::editorData(QWidget *editor) const

0 commit comments

Comments
 (0)