Skip to content

Commit

Permalink
Core: allow to set file filter from PropertyFileIncluded
Browse files Browse the repository at this point in the history
  • Loading branch information
wwmayer committed Dec 7, 2022
1 parent ace6d5a commit 302d3f5
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 9 deletions.
51 changes: 43 additions & 8 deletions src/App/PropertyFile.cpp
Expand Up @@ -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)
Expand Down Expand Up @@ -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<std::string>(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
Expand Down Expand Up @@ -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
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Expand All @@ -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<std::string>(Py::String(dict.getItem("filename")));
setValue(string.c_str());
}
}
else {
PropertyString::setPyObject(value);
}
}

9 changes: 8 additions & 1 deletion src/App/PropertyFile.h
Expand Up @@ -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;

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand All @@ -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;
};


Expand Down
10 changes: 10 additions & 0 deletions src/Gui/propertyeditor/PropertyItem.cpp
Expand Up @@ -4247,6 +4247,16 @@ void PropertyTransientFileItem::setEditorData(QWidget *editor, const QVariant& d
{
auto fc = qobject_cast<Gui::FileChooser*>(editor);
fc->setFileName(data.toString());

const auto prop = static_cast
<const App::PropertyFileIncluded*>(getFirstProperty());

if (prop) {
std::string filter = prop->getFilter();
if (!filter.empty()) {
fc->setFilter(QString::fromStdString(filter));
}
}
}

QVariant PropertyTransientFileItem::editorData(QWidget *editor) const
Expand Down

0 comments on commit 302d3f5

Please sign in to comment.