3 days ago
implement menu toggleitem (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 | |
ui/qt/toolkit.cpp | file | annotate | diff | comparison | revisions | |
ui/qt/toolkit.h | file | annotate | diff | comparison | revisions |
--- a/application/main.c Sun Mar 30 12:00:26 2025 +0200 +++ b/application/main.c Sun Mar 30 12:51:28 2025 +0200 @@ -801,6 +801,7 @@ ui_menu("Edit") { ui_menuitem(.label = "Undo"); + ui_menu_toggleitem(.label = "Checkbox"); ui_menuseparator(); ui_menu("Submenu") { ui_menuitem(.label = "Subitem");
--- 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); + } +} +
--- a/ui/qt/menu.h Sun Mar 30 12:00:26 2025 +0200 +++ b/ui/qt/menu.h Sun Mar 30 12:51:28 2025 +0200 @@ -32,6 +32,8 @@ #include "../ui/menu.h" #include "../common/menu.h" +#include "toolkit.h" + #include <QMainWindow> #include <QMenu> #include <QMenuBar> @@ -49,5 +51,9 @@ void add_checkitemnv_widget(QMenu *parent, int i, UiMenuItemI *item, UiObject *obj); void add_menuitem_list_widget(QMenu *parent, int i, UiMenuItemI *item, UiObject *obj); +void ui_checkableaction_prepare_event(UiEvent *event, UiAction *action); +int64_t ui_checkableaction_get(UiInteger *value); +void ui_checkableaction_set(UiInteger *value, int64_t i); + #endif /* MENU_H */
--- a/ui/qt/toolkit.cpp Sun Mar 30 12:00:26 2025 +0200 +++ b/ui/qt/toolkit.cpp Sun Mar 30 12:51:28 2025 +0200 @@ -176,7 +176,16 @@ 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 } + +// ui_enablefunc for UiAction +void ui_action_enable(UiAction *action, int enable) { + action->setEnabled((bool)enable); +} +
--- a/ui/qt/toolkit.h Sun Mar 30 12:00:26 2025 +0200 +++ b/ui/qt/toolkit.h Sun Mar 30 12:51:28 2025 +0200 @@ -49,12 +49,12 @@ public: UiVar *var; - void *customdata1 = NULL; - void *customdata2 = NULL; + void *customdata1 = nullptr; + void *customdata2 = nullptr; int customvalue1 = 0; int customvalue2 = 0; - ui_prepare_event_func prepare_event = NULL; + ui_prepare_event_func prepare_event = nullptr; UiEventWrapper(UiObject *obj, ui_callback f, void *userdata); @@ -63,6 +63,10 @@ void destroy(); }; +class UiAction; + +typedef void (*ui_prepare_action_event_func)(UiEvent *event, UiAction *action); + class UiAction : public QAction { Q_OBJECT @@ -73,6 +77,12 @@ public: UiVar *var; + ui_prepare_action_event_func prepare_event = nullptr; + void *customdata1 = nullptr; + void *customdata2 = nullptr; + int customvalue1 = 0; + int customvalue2 = 0; + UiAction(UiObject *obj, QString &label, ui_callback f, void *userdata); ~UiAction(); @@ -80,6 +90,7 @@ void trigger(); }; +void ui_action_enable(UiAction *action, int enable); #endif /* TOOLKIT_H */