diff --git a/src/App/Application.cpp b/src/App/Application.cpp index e534de2387..5a7d1e80b0 100644 --- a/src/App/Application.cpp +++ b/src/App/Application.cpp @@ -512,6 +512,11 @@ Document* Application::newDocument(const char * proposedName, const char * propo return doc; } +bool Application::closeDocument(const Document* doc) +{ + return closeDocument(doc->getName()); +} + bool Application::closeDocument(const char* name) { const auto pos = DocMap.find( name ); diff --git a/src/App/Application.h b/src/App/Application.h index 27245d6a38..fd5c73a801 100644 --- a/src/App/Application.h +++ b/src/App/Application.h @@ -128,6 +128,15 @@ public: App::Document* newDocument(const char* proposedName = nullptr, const char* proposedLabel = nullptr, DocumentInitFlags CreateFlags = DocumentInitFlags()); + + /** + * @brief Closes the document and removes it from the application. + * + * @param[in] doc The document to close. + * @return Returns true if the document was found and closed, false otherwise. + */ + bool closeDocument(const Document* doc); + /** * @brief Closes the document and removes it from the application. * diff --git a/src/App/ApplicationPy.cpp b/src/App/ApplicationPy.cpp index c4500201e2..50477ac3c0 100644 --- a/src/App/ApplicationPy.cpp +++ b/src/App/ApplicationPy.cpp @@ -439,26 +439,49 @@ PyObject* ApplicationPy::sSetActiveDocument(PyObject* /*self*/, PyObject* args) PyObject* ApplicationPy::sCloseDocument(PyObject* /*self*/, PyObject* args) { char* pstr = nullptr; - if (!PyArg_ParseTuple(args, "s", &pstr)) { - return nullptr; + if (PyArg_ParseTuple(args, "s", &pstr)) { + Document* doc = GetApplication().getDocument(pstr); + if (!doc) { + PyErr_Format(PyExc_NameError, "Unknown document '%s'", pstr); + return nullptr; + } + if (!doc->isClosable()) { + PyErr_Format(PyExc_RuntimeError, "The document '%s' is not closable for the moment", pstr); + return nullptr; + } + + if (!GetApplication().closeDocument(pstr)) { + PyErr_Format(PyExc_RuntimeError, "Closing the document '%s' failed", pstr); + return nullptr; + } + + Py_Return; } - Document* doc = GetApplication().getDocument(pstr); - if (!doc) { - PyErr_Format(PyExc_NameError, "Unknown document '%s'", pstr); - return nullptr; - } - if (!doc->isClosable()) { - PyErr_Format(PyExc_RuntimeError, "The document '%s' is not closable for the moment", pstr); - return nullptr; + PyErr_Clear(); + PyObject* docpy {}; + if (PyArg_ParseTuple(args, "O!", &App::DocumentPy::Type, &docpy)) { + Document* doc = static_cast(docpy)->getDocumentPtr(); + if (!doc) { + PyErr_Format(PyExc_RuntimeError, "Invalid document"); + return nullptr; + } + + if (!doc->isClosable()) { + PyErr_Format(PyExc_RuntimeError, "The document '%s' is not closable for the moment", doc->getName()); + return nullptr; + } + + if (!GetApplication().closeDocument(doc)) { + PyErr_Format(PyExc_RuntimeError, "Closing the document '%s' failed", doc->getName()); + return nullptr; + } + + Py_Return; } - if (!GetApplication().closeDocument(pstr)) { - PyErr_Format(PyExc_RuntimeError, "Closing the document '%s' failed", pstr); - return nullptr; - } - - Py_Return; + PyErr_SetString(PyExc_TypeError, "Expect str or Document"); + return nullptr; } PyObject* ApplicationPy::sSaveDocument(PyObject* /*self*/, PyObject* args)