ui/qt/menu.cpp

changeset 536
4a1c2eae4bcd
parent 535
ede57f5b6178
child 537
81e46644f1aa
--- a/ui/qt/menu.cpp	Sun Mar 30 12:00:26 2025 +0200
+++ b/ui/qt/menu.cpp	Sun Mar 30 12:51:28 2025 +0200
@@ -53,6 +53,9 @@
     /* UI_MENU_SEPARATOR       */ add_menuseparator_widget
 };
 
+/*
+ * create all menu child items
+ */
 static void add_menu_items(QMenu *parent, int i, UiMenu *menu, UiObject *obj) {
     UiMenuItemI *it = menu->items_begin;
     int index = 0;
@@ -69,12 +72,33 @@
     add_menu_items(menu, i, m, obj);
 }
 
+static UiAction* create_action(
+        UiObject *obj,
+        const char *icon,
+        const char *label,
+        ui_callback callback,
+        void *userdata,
+        int *states)
+{
+    QString str = QString::fromUtf8(label);
+    UiAction *action = new UiAction(obj, str, callback, userdata);
+    if(icon) {
+        action->setIcon(QIcon::fromTheme(icon));
+        action->setIconVisibleInMenu(true);
+    }
+    
+    if(states) {
+        size_t nstates = uic_group_array_size(states);
+        uic_add_group_widget_i(obj->ctx, action, (ui_enablefunc)ui_action_enable, states, nstates);
+        action->setEnabled(false);
+    }
+    
+    return action;
+}
+
 void add_menuitem_widget(QMenu *parent, int i, UiMenuItemI *item, UiObject *obj) {
     UiMenuItem *it = (UiMenuItem*)item;
-    QString str = QString::fromUtf8(it->label);
-    UiAction *action = new UiAction(obj, str, it->callback, it->userdata);
-    action->setIcon(QIcon::fromTheme(it->icon));
-    action->setIconVisibleInMenu(true);
+    UiAction *action = create_action(obj, it->icon, it->label, it->callback, it->userdata, it->groups);
     parent->addAction(action);
     QObject::connect(action, SIGNAL(triggered()), action, SLOT(trigger()));
 }
@@ -84,7 +108,22 @@
 }
 
 void add_checkitem_widget(QMenu *parent, int i, UiMenuItemI *item, UiObject *obj) {
+    UiMenuCheckItem *it = (UiMenuCheckItem*)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_checkableaction_prepare_event;
+    
+    UiVar* var = uic_widget_var(obj->ctx, obj->ctx, NULL, it->varname, UI_VAR_INTEGER);
+    if(var) {
+        UiInteger *value = (UiInteger*)var->value;
+        value->obj = action;
+        value->get = ui_checkableaction_get;
+        value->set = ui_checkableaction_set;
+        
+        action->setChecked((bool)value->value);
+    }
 }
 
 void add_radioitem_widget(QMenu *parent, int index, UiMenuItemI *item, UiObject *obj) {
@@ -118,3 +157,24 @@
     }
 }
 
+void ui_checkableaction_prepare_event(UiEvent *event, UiAction *action) {
+    if(action->var) {
+        event->eventdata = action->var->value;
+    }
+    event->intval = action->isChecked();
+}
+
+int64_t ui_checkableaction_get(UiInteger *value) {
+    UiAction *action= (UiAction*)value->obj;
+    value->value = action->isChecked();
+    return value->value;
+}
+
+void ui_checkableaction_set(UiInteger *value, int64_t i) {
+    UiAction *action = (UiAction*)value->obj;
+    value->value = i;
+    if(i != 0) {
+        action->setChecked((bool)i);
+    }
+}
+

mercurial