5 days ago
add togglebutton and checkbox (QT)
application/main.c | file | annotate | diff | comparison | revisions | |
ui/qt/button.cpp | file | annotate | diff | comparison | revisions | |
ui/qt/button.h | file | annotate | diff | comparison | revisions | |
ui/qt/container.cpp | file | annotate | diff | comparison | revisions | |
ui/qt/toolkit.cpp | file | annotate | diff | comparison | revisions | |
ui/qt/toolkit.h | file | annotate | diff | comparison | revisions |
--- a/application/main.c Thu Mar 27 21:28:04 2025 +0100 +++ b/application/main.c Thu Mar 27 22:06:19 2025 +0100 @@ -785,6 +785,7 @@ ui_newline(obj); ui_button(obj, .label = "Button Y"); + ui_checkbox(obj, .label = "Checkbox"); } ui_show(obj); }
--- a/ui/qt/button.cpp Thu Mar 27 21:28:04 2025 +0100 +++ b/ui/qt/button.cpp Thu Mar 27 22:06:19 2025 +0100 @@ -40,6 +40,51 @@ if(args.onclick) { UiEventWrapper *event = new UiEventWrapper(obj, args.onclick, args.onclickdata); button->connect(button, SIGNAL(clicked()), event, SLOT(slot())); + button->connect(button, SIGNAL(destroyed()), event, SLOT(destroy())); + } + + ctn->add(button, false); + + return button; +} + +static void togglebutton_event(UiEvent *event, UiEventWrapper *wrapper) { + QPushButton *button = (QPushButton*)wrapper->customdata1; + event->intval = button->isChecked(); + if(wrapper->var) { + event->eventdata = wrapper->var->value; + } +} + +UIWIDGET ui_togglebutton_create(UiObject* obj, UiToggleArgs args) { + UiContainerPrivate *ctn = ui_obj_container(obj); + UI_APPLY_LAYOUT(ctn->layout, args); + + QString str = QString::fromUtf8(args.label); + QPushButton *button = new QPushButton(str); + button->setCheckable(true); + + UiVar* var = uic_widget_var(obj->ctx, obj->ctx, args.value, args.varname, UI_VAR_INTEGER); + + if(args.onchange) { + UiEventWrapper *event = new UiEventWrapper(obj, args.onchange, args.onchangedata); + event->var = var; + event->customdata1 = button; + event->prepare_event = togglebutton_event; + button->connect(button, SIGNAL(clicked()), event, SLOT(slot())); + button->connect(button, SIGNAL(destroyed()), event, SLOT(destroy())); + } + + if(var) { + UiInteger *i = (UiInteger*)var->value; + + if(i->value) { + button->setChecked(true); + } + + i->obj = button; + i->get = ui_togglebutton_get; + i->set = ui_togglebutton_set; } ctn->add(button, false); @@ -47,3 +92,74 @@ return button; } +int64_t ui_togglebutton_get(UiInteger *value) { + QPushButton *button = (QPushButton*)value->obj; + value->value = button->isChecked(); + return value->value; +} + +void ui_togglebutton_set(UiInteger *value, int64_t i) { + QPushButton *button = (QPushButton*)value->obj; + value->value = i; + if(i != 0) { + button->setChecked(true); + } +} + +static void checkbox_event(UiEvent *event, UiEventWrapper *wrapper) { + QPushButton *button = (QPushButton*)wrapper->customdata1; + event->intval = button->isChecked(); + if(wrapper->var) { + event->eventdata = wrapper->var->value; + } +} + + +UIWIDGET ui_checkbox_create(UiObject* obj, UiToggleArgs args) { + UiContainerPrivate *ctn = ui_obj_container(obj); + UI_APPLY_LAYOUT(ctn->layout, args); + + QString str = QString::fromUtf8(args.label); + QCheckBox *checkbox = new QCheckBox(str); + + UiVar* var = uic_widget_var(obj->ctx, obj->ctx, args.value, args.varname, UI_VAR_INTEGER); + + if(args.onchange) { + UiEventWrapper *event = new UiEventWrapper(obj, args.onchange, args.onchangedata); + event->var = var; + event->customdata1 = checkbox; + event->prepare_event = checkbox_event; + checkbox->connect(checkbox, SIGNAL(clicked()), event, SLOT(slot())); + checkbox->connect(checkbox, SIGNAL(destroyed()), event, SLOT(destroy())); + } + + if(var) { + UiInteger *i = (UiInteger*)var->value; + + if(i->value) { + checkbox->setChecked(true); + } + + i->obj = checkbox; + i->get = ui_checkbox_get; + i->set = ui_checkbox_set; + } + + ctn->add(checkbox, false); + + return checkbox; +} + +int64_t ui_checkbox_get(UiInteger *value) { + QPushButton *button = (QPushButton*)value->obj; + value->value = button->isChecked(); + return value->value; +} + +void ui_checkbox_set(UiInteger *value, int64_t i) { + QPushButton *button = (QPushButton*)value->obj; + value->value = i; + if(i != 0) { + button->setChecked(true); + } +}
--- a/ui/qt/button.h Thu Mar 27 21:28:04 2025 +0100 +++ b/ui/qt/button.h Thu Mar 27 22:06:19 2025 +0100 @@ -34,11 +34,18 @@ #include <QPushButton> #include <QRadioButton> #include <QButtonGroup> +#include <QCheckBox> extern "C" { + +int64_t ui_togglebutton_get(UiInteger *value); +void ui_togglebutton_set(UiInteger *value, int64_t i); + +int64_t ui_checkbox_get(UiInteger *value); +void ui_checkbox_set(UiInteger *value, int64_t i); + int64_t ui_radiobutton_get(UiInteger *value); - void ui_radiobutton_set(UiInteger *value, int64_t i); }
--- a/ui/qt/container.cpp Thu Mar 27 21:28:04 2025 +0100 +++ b/ui/qt/container.cpp Thu Mar 27 22:06:19 2025 +0100 @@ -220,16 +220,16 @@ void UiGridContainer::end() { if(!col_expanding) { - QLabel *filler = new QLabel(""); + QSpacerItem *filler = new QSpacerItem(0, 0); x = max_x + 1; grid->setColumnStretch(x, 1); - grid->addWidget(filler, 0, x, 1, 1, 0); + grid->addItem(filler, 0, x, 1, 1, 0); } if(!row_expanding) { - QLabel *filler = new QLabel(""); + QSpacerItem *filler = new QSpacerItem(0, 0); y++; grid->setRowStretch(y, 1); - grid->addWidget(filler, y, 0, 1, 1, 0); + grid->addItem(filler, y, 0, 1, 1, 0); } }
--- a/ui/qt/toolkit.cpp Thu Mar 27 21:28:04 2025 +0100 +++ b/ui/qt/toolkit.cpp Thu Mar 27 22:06:19 2025 +0100 @@ -129,5 +129,15 @@ e.document = obj->ctx->document; e.eventdata = NULL; e.intval = 0; + e.set = ui_get_setop(); + if(prepare_event) { + prepare_event(&e, this); + } callback(&e, userdata); + + // TODO: notify var observers } + +void UiEventWrapper::destroy() { + delete this; +}
--- a/ui/qt/toolkit.h Thu Mar 27 21:28:04 2025 +0100 +++ b/ui/qt/toolkit.h Thu Mar 27 22:06:19 2025 +0100 @@ -35,18 +35,32 @@ #include <QApplication> +class UiEventWrapper; + +typedef void (*ui_prepare_event_func)(UiEvent *event, UiEventWrapper *wrapper); + class UiEventWrapper : public QObject { Q_OBJECT UiObject *obj; ui_callback callback; void *userdata; - + public: + UiVar *var; + + void *customdata1 = NULL; + void *customdata2 = NULL; + int customvalue1 = 0; + int customvalue2 = 0; + + ui_prepare_event_func prepare_event = NULL; + UiEventWrapper(UiObject *obj, ui_callback f, void *userdata); public slots: void slot(); + void destroy(); };