implement menu radiobutton (QT)

2 days ago

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 30 Mar 2025 14:19:55 +0200 (2 days ago)
changeset 537
81e46644f1aa
parent 536
4a1c2eae4bcd
child 538
5004b4a9b16d

implement menu radiobutton (QT)

application/main.c file | annotate | diff | comparison | revisions
ui/qt/menu.cpp file | annotate | diff | comparison | revisions
ui/qt/menu.h file | annotate | diff | comparison | revisions
--- a/application/main.c	Sun Mar 30 12:51:28 2025 +0200
+++ b/application/main.c	Sun Mar 30 14:19:55 2025 +0200
@@ -803,6 +803,11 @@
         ui_menuitem(.label = "Undo");
         ui_menu_toggleitem(.label = "Checkbox");
         ui_menuseparator();
+        ui_menu_radioitem(.label = "Option 1", .varname = "menuoption");
+        ui_menu_radioitem(.label = "Option 2", .varname = "menuoption");
+        ui_menu_radioitem(.label = "Option 3", .varname = "menuoption");
+        ui_menu_radioitem(.label = "Option 4", .varname = "menuoption");
+        ui_menuseparator();
         ui_menu("Submenu") {
             ui_menuitem(.label = "Subitem");
         }
--- a/ui/qt/menu.cpp	Sun Mar 30 12:51:28 2025 +0200
+++ b/ui/qt/menu.cpp	Sun Mar 30 14:19:55 2025 +0200
@@ -124,14 +124,71 @@
         
         action->setChecked((bool)value->value);
     }
+    action->var = var;
 }
 
 void add_radioitem_widget(QMenu *parent, int index, UiMenuItemI *item, UiObject *obj) {
+    UiMenuRadioItem *it = (UiMenuRadioItem*)item;
     
+    UiAction *action = create_action(obj, it->icon, it->label, it->callback, it->userdata, it->groups);
+    parent->addAction(action);
+    action->setCheckable(true);
+    action->prepare_event = ui_actiongroup_prepare_event;
+    
+    UiVar* var = uic_widget_var(obj->ctx, obj->ctx, NULL, it->varname, UI_VAR_INTEGER);
+    if(var) {
+        UiInteger *value = (UiInteger*)var->value;
+        QActionGroup *group = (QActionGroup*)value->obj;
+        if(!group) {
+            group = new QActionGroup(parent);
+            value->obj = group;
+        }
+        group->addAction(action);
+        value->get = ui_actiongroup_get;
+        value->set = ui_actiongroup_set;
+        if(value->value != 0) {
+            ui_actiongroup_set(value, value->value);
+        }
+    }
+    action->var;
 }
 
-void add_checkitemnv_widget(QMenu *parent, int i, UiMenuItemI *item, UiObject *obj) {
-    
+void ui_actiongroup_prepare_event(UiEvent *event, UiAction *action) {
+    if(action->var) {
+        UiInteger *value = (UiInteger*)action->var->value;
+        event->eventdata = value;
+        event->intval = value->get(value);
+    }
+}
+
+int64_t ui_actiongroup_get(UiInteger *value) {
+    QActionGroup *group = (QActionGroup*)value->obj;
+    auto actions = group->actions();
+    int i = 1;
+    foreach(const QAction *action, actions) {
+        if(action->isChecked()) {
+            value->value = i;
+            break;
+        }
+        i++;
+    }
+    return value->value;
+}
+
+void ui_actiongroup_set(UiInteger *value, int64_t i) {
+    QActionGroup *group = (QActionGroup*)value->obj;
+    auto actions = group->actions();
+    if(i > 0) {
+        if(i-1 < actions.size()) {
+            actions[i]->setEnabled(true);
+        }
+        value->value = i;
+    } else {
+        foreach(QAction *action, actions) {
+            action->setEnabled(false);
+        }
+        value->value = 0;
+    }
 }
 
 void add_menuitem_list_widget(QMenu *parent, int i, UiMenuItemI *item, UiObject *obj) {
--- a/ui/qt/menu.h	Sun Mar 30 12:51:28 2025 +0200
+++ b/ui/qt/menu.h	Sun Mar 30 14:19:55 2025 +0200
@@ -55,5 +55,9 @@
 int64_t ui_checkableaction_get(UiInteger *value);
 void ui_checkableaction_set(UiInteger *value, int64_t i);
 
+void ui_actiongroup_prepare_event(UiEvent *event, UiAction *action);
+int64_t ui_actiongroup_get(UiInteger *value);
+void ui_actiongroup_set(UiInteger *value, int64_t i);
+
 #endif	/* MENU_H */
 

mercurial