Core: allow to set file filter from PropertyFileIncluded

This commit is contained in:
wmayer
2022-12-07 12:58:07 +01:00
parent ace6d5a9ce
commit 302d3f5b95
3 changed files with 61 additions and 9 deletions

View File

@@ -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<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
@@ -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<std::string>(Py::String(dict.getItem("filename")));
setValue(string.c_str());
}
}
else {
PropertyString::setPyObject(value);
}
}

View File

@@ -50,6 +50,7 @@ public:
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 @@ public:
{ 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 @@ public:
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 @@ protected:
mutable std::string _cValue;
mutable std::string _BaseFileName;
mutable std::string _OriginalName;
private:
std::string m_filter;
};

View File

@@ -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