Commit 21026bf7 authored by Alexander Alekhin's avatar Alexander Alekhin

python: fix support of UI callbacks

parent 7ea5029a
...@@ -27,6 +27,7 @@ ...@@ -27,6 +27,7 @@
#include "pycompat.hpp" #include "pycompat.hpp"
#include <map>
static PyObject* opencv_error = 0; static PyObject* opencv_error = 0;
...@@ -1621,13 +1622,19 @@ static PyObject *pycvSetMouseCallback(PyObject*, PyObject *args, PyObject *kw) ...@@ -1621,13 +1622,19 @@ static PyObject *pycvSetMouseCallback(PyObject*, PyObject *args, PyObject *kw)
if (param == NULL) { if (param == NULL) {
param = Py_None; param = Py_None;
} }
static PyObject* last_param = NULL; PyObject* py_callback_info = Py_BuildValue("OO", on_mouse, param);
if (last_param) { static std::map<std::string, PyObject*> registered_callbacks;
Py_DECREF(last_param); std::map<std::string, PyObject*>::iterator i = registered_callbacks.find(name);
last_param = NULL; if (i != registered_callbacks.end())
{
Py_DECREF(i->second);
i->second = py_callback_info;
}
else
{
registered_callbacks.insert(std::pair<std::string, PyObject*>(std::string(name), py_callback_info));
} }
last_param = Py_BuildValue("OO", on_mouse, param); ERRWRAP2(setMouseCallback(name, OnMouse, py_callback_info));
ERRWRAP2(setMouseCallback(name, OnMouse, last_param));
Py_RETURN_NONE; Py_RETURN_NONE;
} }
#endif #endif
...@@ -1663,13 +1670,20 @@ static PyObject *pycvCreateTrackbar(PyObject*, PyObject *args) ...@@ -1663,13 +1670,20 @@ static PyObject *pycvCreateTrackbar(PyObject*, PyObject *args)
PyErr_SetString(PyExc_TypeError, "on_change must be callable"); PyErr_SetString(PyExc_TypeError, "on_change must be callable");
return NULL; return NULL;
} }
static PyObject* last_param = NULL; PyObject* py_callback_info = Py_BuildValue("OO", on_change, Py_None);
if (last_param) { std::string name = std::string(window_name) + ":" + std::string(trackbar_name);
Py_DECREF(last_param); static std::map<std::string, PyObject*> registered_callbacks;
last_param = NULL; std::map<std::string, PyObject*>::iterator i = registered_callbacks.find(name);
if (i != registered_callbacks.end())
{
Py_DECREF(i->second);
i->second = py_callback_info;
} }
last_param = Py_BuildValue("OO", on_change, Py_None); else
ERRWRAP2(createTrackbar(trackbar_name, window_name, value, count, OnChange, last_param)); {
registered_callbacks.insert(std::pair<std::string, PyObject*>(name, py_callback_info));
}
ERRWRAP2(createTrackbar(trackbar_name, window_name, value, count, OnChange, py_callback_info));
Py_RETURN_NONE; Py_RETURN_NONE;
} }
...@@ -1717,13 +1731,21 @@ static PyObject *pycvCreateButton(PyObject*, PyObject *args, PyObject *kw) ...@@ -1717,13 +1731,21 @@ static PyObject *pycvCreateButton(PyObject*, PyObject *args, PyObject *kw)
userdata = Py_None; userdata = Py_None;
} }
static PyObject* last_param = NULL; PyObject* py_callback_info = Py_BuildValue("OO", on_change, userdata);
if (last_param) { std::string name(button_name);
Py_DECREF(last_param);
last_param = NULL; static std::map<std::string, PyObject*> registered_callbacks;
std::map<std::string, PyObject*>::iterator i = registered_callbacks.find(name);
if (i != registered_callbacks.end())
{
Py_DECREF(i->second);
i->second = py_callback_info;
}
else
{
registered_callbacks.insert(std::pair<std::string, PyObject*>(name, py_callback_info));
} }
last_param = Py_BuildValue("OO", on_change, userdata); ERRWRAP2(createButton(button_name, OnButtonChange, py_callback_info, button_type, initial_button_state != 0));
ERRWRAP2(createButton(button_name, OnButtonChange, last_param, button_type, initial_button_state != 0));
Py_RETURN_NONE; Py_RETURN_NONE;
} }
#endif #endif
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment