ui/qt/button.cpp

changeset 526
f6a6b0b08641
parent 520
ea1bba55de44
child 550
0412c4c1048e
--- 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);
+    }
+}

mercurial