implement toolbar icons and events (QT)

Fri, 18 Apr 2025 17:22:28 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Fri, 18 Apr 2025 17:22:28 +0200
changeset 575
50da9696a865
parent 574
19de5292f08f
child 576
dd38b170f9a8

implement toolbar icons and events (QT)

application/main.c file | annotate | diff | comparison | revisions
ui/qt/toolbar.cpp file | annotate | diff | comparison | revisions
--- a/application/main.c	Fri Apr 18 14:56:12 2025 +0200
+++ b/application/main.c	Fri Apr 18 17:22:28 2025 +0200
@@ -968,11 +968,13 @@
     ui_toolbar_item("add", .label = "Add");
     ui_toolbar_item("copy", .label = "Copy");
     ui_toolbar_item("paste", .label = "Paste");
+    ui_toolbar_toggleitem("toggle", .label = "Toggle");
     
     ui_toolbar_add_default("new", UI_TOOLBAR_LEFT);
     ui_toolbar_add_default("add", UI_TOOLBAR_LEFT);
     ui_toolbar_add_default("copy", UI_TOOLBAR_LEFT);
     ui_toolbar_add_default("paste", UI_TOOLBAR_LEFT);
+    ui_toolbar_add_default("toggle", UI_TOOLBAR_LEFT);
 
     ui_main();
 
--- a/ui/qt/toolbar.cpp	Fri Apr 18 14:56:12 2025 +0200
+++ b/ui/qt/toolbar.cpp	Fri Apr 18 17:22:28 2025 +0200
@@ -86,12 +86,45 @@
 
 void ui_toolbar_add_item(UiObject *obj, QToolBar *toolbar, UiToolbarItem *item) {
     QAction *action = new QAction();
-    action->setText(item->args.label);
+    if(item->args.label) {
+        action->setText(item->args.label);
+    }
+    if(item->args.icon) {
+        action->setIcon(QIcon::fromTheme(item->args.icon));
+    }
     toolbar->addAction(action);
+    
+    UiEventWrapper *event = new UiEventWrapper(obj, item->args.onclick, item->args.onclickdata);
+    action->connect(action, SIGNAL(clicked()), event, SLOT(slot()));
+    action->connect(action, SIGNAL(destroyed()), event, SLOT(destroy()));
+}
+
+static void toolbar_togglebutton_event(UiEvent *event, UiEventWrapper *wrapper) {
+    QAction *action = (QAction*)wrapper->customdata1;
+    event->intval = action->isChecked();
+    if(wrapper->var) {
+        event->eventdata = wrapper->var->value;
+    }
 }
 
 void ui_toolbar_add_toggleitem(UiObject *obj, QToolBar *toolbar, UiToolbarToggleItem *item) {
+    QAction *action = new QAction();
+    action->setCheckable(true);
+    if(item->args.label) {
+        action->setText(item->args.label);
+    }
+    if(item->args.icon) {
+        action->setIcon(QIcon::fromTheme(item->args.icon));
+    }
+    toolbar->addAction(action);
     
+    UiVar* var = uic_widget_var(obj->ctx, obj->ctx, nullptr, item->args.varname, UI_VAR_INTEGER);
+    UiEventWrapper *event = new UiEventWrapper(obj, item->args.onchange, item->args.onchangedata);
+    event->var = var;
+    event->customdata1 = action;
+    event->prepare_event = toolbar_togglebutton_event;
+    action->connect(action, SIGNAL(clicked()), event, SLOT(slot()));
+    action->connect(action, SIGNAL(destroyed()), event, SLOT(destroy()));
 }
 
 void ui_toolbar_add_menu(UiObject *obj, QToolBar *toolbar, UiToolbarMenuItem *item) {

mercurial