From ef22c6ba1e72e745bd85ceb4f66cfb009135cf44 Mon Sep 17 00:00:00 2001 From: 0penBrain <48731257+0penBrain@users.noreply.github.com> Date: Sun, 9 May 2021 09:58:11 +0200 Subject: [PATCH] [Gui] Introducing user edit mode --- src/Gui/Application.cpp | 44 +++++++++++++++++++++++++++++++++++++++ src/Gui/Application.h | 26 +++++++++++++++++++++++ src/Gui/ApplicationPy.cpp | 38 +++++++++++++++++++++++++++++++++ 3 files changed, 108 insertions(+) diff --git a/src/Gui/Application.cpp b/src/Gui/Application.cpp index d59831672b..a0f2d10742 100644 --- a/src/Gui/Application.cpp +++ b/src/Gui/Application.cpp @@ -1244,6 +1244,50 @@ void Application::tryClose(QCloseEvent * e) } } +int Application::getUserEditMode(const std::string &mode) const +{ + if (mode.empty()) { + return userEditMode; + } + for (auto const &uem : userEditModes) { + if (uem.second == mode) { + return uem.first; + } + } + return -1; +} + +std::string Application::getUserEditModeName(int mode) const +{ + if (mode == -1) { + return userEditModes.at(userEditMode); + } + if (userEditModes.find(mode) != userEditModes.end()) { + return userEditModes.at(mode); + } + return ""; +} + +bool Application::setUserEditMode(int mode) +{ + if (userEditModes.find(mode) != userEditModes.end() && userEditMode != mode) { + userEditMode = mode; + this->signalUserEditModeChanged(userEditMode); + return true; + } + return false; +} + +bool Application::setUserEditMode(const std::string &mode) +{ + for (auto const &uem : userEditModes) { + if (uem.second == mode) { + return setUserEditMode(uem.first); + } + } + return false; +} + /** * Activate the matching workbench to the registered workbench handler with name \a name. * The handler must be an instance of a class written in Python. diff --git a/src/Gui/Application.h b/src/Gui/Application.h index 0f596d3276..bab862f4f0 100644 --- a/src/Gui/Application.h +++ b/src/Gui/Application.h @@ -27,6 +27,7 @@ #include #include #include +#include #define putpix() @@ -133,6 +134,8 @@ public: boost::signals2::signal signalInEdit; /// signal on leaving edit mode boost::signals2::signal signalResetEdit; + /// signal on changing user edit mode + boost::signals2::signal signalUserEditModeChanged; //@} /** @name methods for Document handling */ @@ -227,6 +230,25 @@ public: static void runApplication(void); void tryClose( QCloseEvent * e ); //@} + + /** @name User edit mode */ + //@{ +protected: + const std::map userEditModes { + {0, "Default"}, + {1, "Transform"}, + {2, "Cutting"}, + {3, "Color"} + }; + int userEditMode = userEditModes.begin()->first; + +public: + std::map listUserEditModes() const { return userEditModes; } + int getUserEditMode(const std::string &mode = "") const; + std::string getUserEditModeName(int mode = -1) const; + bool setUserEditMode(int mode); + bool setUserEditMode(const std::string &mode); + //@} public: //--------------------------------------------------------------------- @@ -293,6 +315,10 @@ public: static PyObject* sAddDocObserver (PyObject *self,PyObject *args); static PyObject* sRemoveDocObserver (PyObject *self,PyObject *args); + + static PyObject* sListUserEditModes (PyObject *self,PyObject *args); + static PyObject* sGetUserEditMode (PyObject *self,PyObject *args); + static PyObject* sSetUserEditMode (PyObject *self,PyObject *args); static PyMethodDef Methods[]; diff --git a/src/Gui/ApplicationPy.cpp b/src/Gui/ApplicationPy.cpp index 13016c977f..61d636a63b 100644 --- a/src/Gui/ApplicationPy.cpp +++ b/src/Gui/ApplicationPy.cpp @@ -205,6 +205,18 @@ PyMethodDef Application::Methods[] = { {"removeDocumentObserver", (PyCFunction) Application::sRemoveDocObserver, METH_VARARGS, "removeDocumentObserver() -> None\n\n" "Remove an added document observer."}, + + {"listUserEditModes", (PyCFunction) Application::sListUserEditModes, METH_VARARGS, + "listUserEditModes() -> list\n\n" + "List available user edit modes"}, + + {"getUserEditMode", (PyCFunction) Application::sGetUserEditMode, METH_VARARGS, + "getUserEditMode() -> string\n\n" + "Get current user edit mode"}, + + {"setUserEditMode", (PyCFunction) Application::sSetUserEditMode, METH_VARARGS, + "setUserEditMode(string=mode) -> Bool\n\n" + "Set user edit mode to 'mode', returns True if exists, false otherwise"}, {"reload", (PyCFunction) Application::sReload, METH_VARARGS, "reload(name) -> doc\n\n" @@ -1485,3 +1497,29 @@ PyObject* Application::sCoinRemoveAllChildren(PyObject * /*self*/, PyObject *arg }PY_CATCH; } +PyObject* Application::sListUserEditModes(PyObject * /*self*/, PyObject *args) +{ + Py::List ret; + if (!PyArg_ParseTuple(args, "")) + return NULL; + for (auto const &uem : Instance->listUserEditModes()) { + ret.append(Py::String(uem.second)); + } + return Py::new_reference_to(ret); +} + +PyObject* Application::sGetUserEditMode(PyObject * /*self*/, PyObject *args) +{ + if (!PyArg_ParseTuple(args, "")) + return NULL; + return Py::new_reference_to(Py::String(Instance->getUserEditModeName())); +} + +PyObject* Application::sSetUserEditMode(PyObject * /*self*/, PyObject *args) +{ + char *mode = ""; + if (!PyArg_ParseTuple(args, "s", &mode)) + return NULL; + bool ok = Instance->setUserEditMode(std::string(mode)); + return Py::new_reference_to(Py::Boolean(ok)); +}