From 302d3f5b95030b81d1a4b45f40fc8d18f1ecbe49 Mon Sep 17 00:00:00 2001 From: wmayer Date: Wed, 7 Dec 2022 12:58:07 +0100 Subject: [PATCH] Core: allow to set file filter from PropertyFileIncluded --- src/App/PropertyFile.cpp | 51 +++++++++++++++++++++---- src/App/PropertyFile.h | 9 ++++- src/Gui/propertyeditor/PropertyItem.cpp | 10 +++++ 3 files changed, 61 insertions(+), 9 deletions(-) diff --git a/src/App/PropertyFile.cpp b/src/App/PropertyFile.cpp index 60870f1b79d6..9944d637fb60 100644 --- a/src/App/PropertyFile.cpp +++ b/src/App/PropertyFile.cpp @@ -278,15 +278,17 @@ bool isIOFile(PyObject* file) void PropertyFileIncluded::setPyObject(PyObject *value) { - std::string string; if (PyUnicode_Check(value)) { - string = PyUnicode_AsUTF8(value); + std::string string = PyUnicode_AsUTF8(value); + setValue(string.c_str()); } else if (PyBytes_Check(value)) { - string = PyBytes_AsString(value); + std::string string = PyBytes_AsString(value); + setValue(string.c_str()); } else if (isIOFile(value)){ - string = getNameFromFile(value); + std::string string = getNameFromFile(value); + setValue(string.c_str()); } else if (PyTuple_Check(value)) { if (PyTuple_Size(value) != 2) @@ -329,16 +331,22 @@ void PropertyFileIncluded::setPyObject(PyObject *value) } setValue(fileStr.c_str(),nameStr.c_str()); - return; + } + else if (PyDict_Check(value)) { + Py::Dict dict(value); + if (dict.hasKey("filter")) { + setFilter(Py::String(dict.getItem("filter"))); + } + if (dict.hasKey("filename")) { + std::string string = static_cast(Py::String(dict.getItem("filename"))); + setValue(string.c_str()); + } } else { std::string error = std::string("Type must be string or file, not "); error += value->ob_type->tp_name; throw Base::TypeError(error); } - - // assign the string - setValue(string.c_str()); } void PropertyFileIncluded::Save (Base::Writer &writer) const @@ -569,6 +577,16 @@ unsigned int PropertyFileIncluded::getMemSize () const return mem; } +void PropertyFileIncluded::setFilter(std::string filter) +{ + m_filter = std::move(filter); +} + +std::string PropertyFileIncluded::getFilter() const +{ + return m_filter; +} + //************************************************************************** // PropertyFile //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ @@ -592,3 +610,20 @@ std::string PropertyFile::getFilter() const return m_filter; } +void PropertyFile::setPyObject(PyObject *value) +{ + if (PyDict_Check(value)) { + Py::Dict dict(value); + if (dict.hasKey("filter")) { + setFilter(Py::String(dict.getItem("filter"))); + } + if (dict.hasKey("filename")) { + std::string string = static_cast(Py::String(dict.getItem("filename"))); + setValue(string.c_str()); + } + } + else { + PropertyString::setPyObject(value); + } +} + diff --git a/src/App/PropertyFile.h b/src/App/PropertyFile.h index e593f5cf1925..3806b09bf81e 100644 --- a/src/App/PropertyFile.h +++ b/src/App/PropertyFile.h @@ -50,6 +50,7 @@ class AppExport PropertyFile : public PropertyString const char* getEditorName() const override { return "Gui::PropertyEditor::PropertyFileItem"; } + void setPyObject(PyObject *) override; virtual void setFilter(const std::string filter); virtual std::string getFilter() const; @@ -86,7 +87,7 @@ class AppExport PropertyFileIncluded : public Property { return "Gui::PropertyEditor::PropertyTransientFileItem"; } PyObject *getPyObject() override; void setPyObject(PyObject *) override; - + void Save (Base::Writer &writer) const override; void Restore(Base::XMLReader &reader) override; @@ -116,6 +117,9 @@ class AppExport PropertyFileIncluded : public Property bool isEmpty() const {return _cValue.empty();} + void setFilter(std::string filter); + std::string getFilter() const; + protected: // get the transient path if the property is in a DocumentObject std::string getDocTransientPath() const; @@ -126,6 +130,9 @@ class AppExport PropertyFileIncluded : public Property mutable std::string _cValue; mutable std::string _BaseFileName; mutable std::string _OriginalName; + +private: + std::string m_filter; }; diff --git a/src/Gui/propertyeditor/PropertyItem.cpp b/src/Gui/propertyeditor/PropertyItem.cpp index ea4c74f608c5..63f1495ee781 100644 --- a/src/Gui/propertyeditor/PropertyItem.cpp +++ b/src/Gui/propertyeditor/PropertyItem.cpp @@ -4247,6 +4247,16 @@ void PropertyTransientFileItem::setEditorData(QWidget *editor, const QVariant& d { auto fc = qobject_cast(editor); fc->setFileName(data.toString()); + + const auto prop = static_cast + (getFirstProperty()); + + if (prop) { + std::string filter = prop->getFilter(); + if (!filter.empty()) { + fc->setFilter(QString::fromStdString(filter)); + } + } } QVariant PropertyTransientFileItem::editorData(QWidget *editor) const