python: fix support of UI callbacks

This commit is contained in:
Alexander Alekhin 2018-04-19 18:42:01 +03:00
parent 7ea5029ae5
commit 21026bf7cd

View File

@ -27,6 +27,7 @@
#include "pycompat.hpp"
#include <map>
static PyObject* opencv_error = 0;
@ -1621,13 +1622,19 @@ static PyObject *pycvSetMouseCallback(PyObject*, PyObject *args, PyObject *kw)
if (param == NULL) {
param = Py_None;
}
static PyObject* last_param = NULL;
if (last_param) {
Py_DECREF(last_param);
last_param = NULL;
PyObject* py_callback_info = Py_BuildValue("OO", on_mouse, param);
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;
}
last_param = Py_BuildValue("OO", on_mouse, param);
ERRWRAP2(setMouseCallback(name, OnMouse, last_param));
else
{
registered_callbacks.insert(std::pair<std::string, PyObject*>(std::string(name), py_callback_info));
}
ERRWRAP2(setMouseCallback(name, OnMouse, py_callback_info));
Py_RETURN_NONE;
}
#endif
@ -1663,13 +1670,20 @@ static PyObject *pycvCreateTrackbar(PyObject*, PyObject *args)
PyErr_SetString(PyExc_TypeError, "on_change must be callable");
return NULL;
}
static PyObject* last_param = NULL;
if (last_param) {
Py_DECREF(last_param);
last_param = NULL;
PyObject* py_callback_info = Py_BuildValue("OO", on_change, Py_None);
std::string name = std::string(window_name) + ":" + std::string(trackbar_name);
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;
}
last_param = Py_BuildValue("OO", on_change, Py_None);
ERRWRAP2(createTrackbar(trackbar_name, window_name, value, count, OnChange, last_param));
else
{
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;
}
@ -1717,13 +1731,21 @@ static PyObject *pycvCreateButton(PyObject*, PyObject *args, PyObject *kw)
userdata = Py_None;
}
static PyObject* last_param = NULL;
if (last_param) {
Py_DECREF(last_param);
last_param = NULL;
PyObject* py_callback_info = Py_BuildValue("OO", on_change, userdata);
std::string name(button_name);
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;
}
last_param = Py_BuildValue("OO", on_change, userdata);
ERRWRAP2(createButton(button_name, OnButtonChange, last_param, button_type, initial_button_state != 0));
else
{
registered_callbacks.insert(std::pair<std::string, PyObject*>(name, py_callback_info));
}
ERRWRAP2(createButton(button_name, OnButtonChange, py_callback_info, button_type, initial_button_state != 0));
Py_RETURN_NONE;
}
#endif