void QtBaseWidget::mousePressEvent(QMouseEvent* ev) { QWidget::mousePressEvent(ev); PyScopedGIL gil; PyQtGuiObject* control = getControl(); if(control) control->handleCurSelectedSong(); Py_XDECREF(control); }
void QtBaseWidget::resizeEvent(QResizeEvent* ev) { if(handleResize) return; // avoid infinite recursion handleResize = true; QWidget::resizeEvent(ev); PyScopedGIL gil; PyQtGuiObject* control = getControl(); if(control) control->layout(); Py_XDECREF(control); handleResize = false; }
void QtActionWidget::onClick() { PyScopedGIL gil; PyQtGuiObject* control = getControl(); if(!control) return; control->updateSubjectObject(); if(control->subjectObject) { PyObject* ret = PyObject_CallFunction(control->subjectObject, NULL); if(!ret) { if(PyErr_Occurred()) PyErr_Print(); } Py_XDECREF(ret); } Py_DECREF(control); }
void QtOneLineTextWidget::updateContent() { PyQtGuiObject* control = NULL; std::string s = "???"; { PyScopedGIL gil; control = getControl(); if(!control) return; control->updateSubjectObject(); { PyObject* labelContent = getTextObj(); if(!labelContent && PyErr_Occurred()) PyErr_Print(); if(labelContent) { if(!pyStr(labelContent, s)) { if(PyErr_Occurred()) PyErr_Print(); } } } } WeakRef selfRefCopy(*this); // Note: We had this async before. But I think other code wants to know the actual size // and we only get it after we set the text. execInMainThread_sync([=]() { PyScopedGIL gil; ScopedRef selfRef(selfRefCopy.scoped()); if(selfRef) { auto self = dynamic_cast<QtOneLineTextWidget*>(selfRef.get()); assert(self); assert(self->lineEditWidget); self->lineEditWidget->setText(QString::fromStdString(s)); PyScopedGIL gil; /* NSColor* color = backgroundColor(control); if(color) { [self setDrawsBackground:YES]; [self setBackgroundColor:color]; } */ //[self setTextColor:foregroundColor(control)]; bool autosizeWidth = attrChain_bool_default(control->attr, "autosizeWidth", false); if(autosizeWidth) { QFontMetrics metrics(self->lineEditWidget->fontMetrics()); int w = metrics.boundingRect(self->lineEditWidget->text()).width(); w += 5; // TODO: margin size? self->resize(w, self->height()); PyObject* res = PyObject_CallMethod((PyObject*) control, (char*)"layoutLine", NULL); if(!res && PyErr_Occurred()) PyErr_Print(); Py_XDECREF(res); } } Py_DECREF(control); }); }
bool QtApp::openWindow(const std::string& name) { assert(QThread::currentThread() == qApp->thread()); PyScopedGIL gil; PyObject* rootObj = handleModuleCommand("gui", "RootObjs.__getitem__", "(s)", name.c_str()); if(!rootObj) return false; // Python errs already handled in handleModuleCommand PyQtGuiObject* control = NULL; control = (PyQtGuiObject*) PyObject_GetAttrString(rootObj, "guiObj"); if(!control) { if(PyErr_Occurred()) PyErr_Print(); Py_DECREF(rootObj); return false; } if((PyObject*) control == Py_None) Py_CLEAR(control); if(control) { if(PyType_IsSubtype(Py_TYPE(control), &QtGuiObject_Type)) { QtBaseWidget::ScopedRef win(control->widget); if(win) { win->show(); return true; } // continue with existing control but create new window } else { printf("Qt open window: existing rootObj.guiObj is of wrong type\n"); // reset and continue with new control creation Py_CLEAR(control); } } if(!control) { control = (PyQtGuiObject*) PyObject_CallFunction((PyObject*) &QtGuiObject_Type, NULL); if(!control) { if(PyErr_Occurred()) PyErr_Print(); Py_DECREF(rootObj); return false; } assert(control->root == NULL); control->root = control; Py_XINCREF(control->root); assert(control->subjectObject == NULL); control->subjectObject = PyObject_GetAttrString(rootObj, "obj"); if(!control->subjectObject) { if(PyErr_Occurred()) PyErr_Print(); Py_DECREF(rootObj); Py_DECREF(control); return false; } } if(PyObject_SetAttrString(rootObj, "guiObj", (PyObject*) control) < 0) { if(PyErr_Occurred()) PyErr_Print(); Py_DECREF(rootObj); Py_DECREF(control); return false; } // check subjectObject { PyObject* subjectObject = PyObject_GetAttrString(rootObj, "obj"); if(!subjectObject) { if(PyErr_Occurred()) PyErr_Print(); // continue, maybe it doesn't matter } else { if(subjectObject != control->subjectObject) { printf("Qt open window: got new subject object\n"); // strange, but just overtake and continue Py_CLEAR(control->subjectObject); control->subjectObject = subjectObject; subjectObject = NULL; } } Py_XDECREF(subjectObject); } QtBaseWidget* win = new QtBaseWidget(control); win->setAttribute(Qt::WA_DeleteOnClose); // set title { PyObject* title = PyObject_GetAttrString(rootObj, "title"); std::string titleStr; if(!title || !pyStr(title, titleStr)) { if(PyErr_Occurred()) PyErr_Print(); win->setWindowTitle(QString::fromStdString(name)); } else { win->setWindowTitle(QString::fromStdString(titleStr)); } Py_XDECREF(title); } Vec size = control->setupChilds(); win->setMinimumSize(size.x, size.y); // ... win->show(); control->layout(); Py_DECREF(rootObj); Py_DECREF(control); return true; }