[Gui] Introducing user edit mode

This commit is contained in:
0penBrain
2021-05-09 09:58:11 +02:00
parent bb434d3ff2
commit ef22c6ba1e
3 changed files with 108 additions and 0 deletions

View File

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

View File

@@ -27,6 +27,7 @@
#include <QPixmap>
#include <string>
#include <vector>
#include <map>
#define putpix()
@@ -133,6 +134,8 @@ public:
boost::signals2::signal<void (const Gui::ViewProviderDocumentObject&)> signalInEdit;
/// signal on leaving edit mode
boost::signals2::signal<void (const Gui::ViewProviderDocumentObject&)> signalResetEdit;
/// signal on changing user edit mode
boost::signals2::signal<void (int)> 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 <int, std::string> userEditModes {
{0, "Default"},
{1, "Transform"},
{2, "Cutting"},
{3, "Color"}
};
int userEditMode = userEditModes.begin()->first;
public:
std::map <int, std::string> 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[];

View File

@@ -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));
}