ui/qt/button.cpp

changeset 108
77254bd6dccb
parent 103
6606616eca9f
child 109
c3dfcb8f0be7
equal deleted inserted replaced
107:b34bd1557c6c 108:77254bd6dccb
1 /* 1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 * 3 *
4 * Copyright 2015 Olaf Wintermann. All rights reserved. 4 * Copyright 2025 Olaf Wintermann. All rights reserved.
5 * 5 *
6 * Redistribution and use in source and binary forms, with or without 6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met: 7 * modification, are permitted provided that the following conditions are met:
8 * 8 *
9 * 1. Redistributions of source code must retain the above copyright 9 * 1. Redistributions of source code must retain the above copyright
28 28
29 #include "button.h" 29 #include "button.h"
30 #include "container.h" 30 #include "container.h"
31 #include "toolkit.h" 31 #include "toolkit.h"
32 32
33 UIWIDGET ui_button_create(UiObject* obj, UiButtonArgs args) { 33 UIWIDGET ui_button_create(UiObject* obj, UiButtonArgs *args) {
34 UiContainerPrivate *ctn = ui_obj_container(obj); 34 UiContainerPrivate *ctn = ui_obj_container(obj);
35 UI_APPLY_LAYOUT(ctn->layout, args); 35 UI_APPLY_LAYOUT(ctn->layout, args);
36 36
37 QString str = QString::fromUtf8(args.label); 37 QString str = QString::fromUtf8(args->label);
38 QPushButton *button = new QPushButton(str); 38 QPushButton *button = new QPushButton(str);
39 39
40 if(args.onclick) { 40 if(args->onclick) {
41 UiEventWrapper *event = new UiEventWrapper(obj, args.onclick, args.onclickdata); 41 UiEventWrapper *event = new UiEventWrapper(obj, args->onclick, args->onclickdata);
42 button->connect(button, SIGNAL(clicked()), event, SLOT(slot())); 42 button->connect(button, SIGNAL(clicked()), event, SLOT(slot()));
43 button->connect(button, SIGNAL(destroyed()), event, SLOT(destroy())); 43 button->connect(button, SIGNAL(destroyed()), event, SLOT(destroy()));
44 } 44 }
45 45
46 ctn->add(button, false); 46 ctn->add(button);
47 47
48 return button; 48 return button;
49 } 49 }
50 50
51 static void togglebutton_event(UiEvent *event, UiEventWrapper *wrapper) { 51 static void togglebutton_event(UiEvent *event, UiEventWrapper *wrapper) {
52 QPushButton *button = (QPushButton*)wrapper->customdata1; 52 QPushButton *button = (QPushButton*)wrapper->customdata1;
53 event->intval = button->isChecked(); 53 event->intval = button->isChecked();
54 if(wrapper->var) { 54 if(wrapper->var) {
55 event->eventdata = wrapper->var->value; 55 event->eventdata = wrapper->var->value;
56 } 56 event->eventdatatype = UI_EVENT_DATA_INTEGER_VALUE;
57 } 57 }
58 58 }
59 UIWIDGET ui_togglebutton_create(UiObject* obj, UiToggleArgs args) { 59
60 UiContainerPrivate *ctn = ui_obj_container(obj); 60 UIWIDGET ui_togglebutton_create(UiObject* obj, UiToggleArgs *args) {
61 UI_APPLY_LAYOUT(ctn->layout, args); 61 UiContainerPrivate *ctn = ui_obj_container(obj);
62 62 UI_APPLY_LAYOUT(ctn->layout, args);
63 QString str = QString::fromUtf8(args.label); 63
64 QString str = QString::fromUtf8(args->label);
64 QPushButton *button = new QPushButton(str); 65 QPushButton *button = new QPushButton(str);
65 button->setCheckable(true); 66 button->setCheckable(true);
66 67
67 UiVar* var = uic_widget_var(obj->ctx, obj->ctx, args.value, args.varname, UI_VAR_INTEGER); 68 UiVar* var = uic_widget_var(obj->ctx, obj->ctx, args->value, args->varname, UI_VAR_INTEGER);
68 69
69 if(args.onchange) { 70 if(args->onchange) {
70 UiEventWrapper *event = new UiEventWrapper(obj, args.onchange, args.onchangedata); 71 UiEventWrapper *event = new UiEventWrapper(obj, args->onchange, args->onchangedata);
71 event->var = var; 72 event->var = var;
72 event->customdata1 = button; 73 event->customdata1 = button;
73 event->prepare_event = togglebutton_event; 74 event->prepare_event = togglebutton_event;
74 button->connect(button, SIGNAL(clicked()), event, SLOT(slot())); 75 button->connect(button, SIGNAL(clicked()), event, SLOT(slot()));
75 button->connect(button, SIGNAL(destroyed()), event, SLOT(destroy())); 76 button->connect(button, SIGNAL(destroyed()), event, SLOT(destroy()));
85 i->obj = button; 86 i->obj = button;
86 i->get = ui_togglebutton_get; 87 i->get = ui_togglebutton_get;
87 i->set = ui_togglebutton_set; 88 i->set = ui_togglebutton_set;
88 } 89 }
89 90
90 ctn->add(button, false); 91 ctn->add(button);
91 92
92 return button; 93 return button;
93 } 94 }
94 95
95 int64_t ui_togglebutton_get(UiInteger *value) { 96 int64_t ui_togglebutton_get(UiInteger *value) {
109 static void checkbox_event(UiEvent *event, UiEventWrapper *wrapper) { 110 static void checkbox_event(UiEvent *event, UiEventWrapper *wrapper) {
110 QPushButton *button = (QPushButton*)wrapper->customdata1; 111 QPushButton *button = (QPushButton*)wrapper->customdata1;
111 event->intval = button->isChecked(); 112 event->intval = button->isChecked();
112 if(wrapper->var) { 113 if(wrapper->var) {
113 event->eventdata = wrapper->var->value; 114 event->eventdata = wrapper->var->value;
114 } 115 event->eventdatatype = UI_EVENT_DATA_INTEGER_VALUE;
115 } 116 }
116 117 }
117 118
118 UIWIDGET ui_checkbox_create(UiObject* obj, UiToggleArgs args) { 119
119 UiContainerPrivate *ctn = ui_obj_container(obj); 120 UIWIDGET ui_checkbox_create(UiObject* obj, UiToggleArgs *args) {
120 UI_APPLY_LAYOUT(ctn->layout, args); 121 UiContainerPrivate *ctn = ui_obj_container(obj);
121 122 UI_APPLY_LAYOUT(ctn->layout, args);
122 QString str = QString::fromUtf8(args.label); 123
124 QString str = QString::fromUtf8(args->label);
123 QCheckBox *checkbox = new QCheckBox(str); 125 QCheckBox *checkbox = new QCheckBox(str);
124 126
125 UiVar* var = uic_widget_var(obj->ctx, obj->ctx, args.value, args.varname, UI_VAR_INTEGER); 127 UiVar* var = uic_widget_var(obj->ctx, obj->ctx, args->value, args->varname, UI_VAR_INTEGER);
126 128
127 if(args.onchange) { 129 if(args->onchange) {
128 UiEventWrapper *event = new UiEventWrapper(obj, args.onchange, args.onchangedata); 130 UiEventWrapper *event = new UiEventWrapper(obj, args->onchange, args->onchangedata);
129 event->var = var; 131 event->var = var;
130 event->customdata1 = checkbox; 132 event->customdata1 = checkbox;
131 event->prepare_event = checkbox_event; 133 event->prepare_event = checkbox_event;
132 checkbox->connect(checkbox, SIGNAL(clicked()), event, SLOT(slot())); 134 checkbox->connect(checkbox, SIGNAL(clicked()), event, SLOT(slot()));
133 checkbox->connect(checkbox, SIGNAL(destroyed()), event, SLOT(destroy())); 135 checkbox->connect(checkbox, SIGNAL(destroyed()), event, SLOT(destroy()));
143 i->obj = checkbox; 145 i->obj = checkbox;
144 i->get = ui_checkbox_get; 146 i->get = ui_checkbox_get;
145 i->set = ui_checkbox_set; 147 i->set = ui_checkbox_set;
146 } 148 }
147 149
148 ctn->add(checkbox, false); 150 ctn->add(checkbox);
149 151
150 return checkbox; 152 return checkbox;
151 } 153 }
152 154
153 int64_t ui_checkbox_get(UiInteger *value) { 155 int64_t ui_checkbox_get(UiInteger *value) {
165 } 167 }
166 168
167 169
168 static void radiobutton_event(UiEvent *event, UiEventWrapper *wrapper) { 170 static void radiobutton_event(UiEvent *event, UiEventWrapper *wrapper) {
169 if(wrapper->var) { 171 if(wrapper->var) {
170 UiInteger *value = wrapper->var->value; 172 UiInteger *value = (UiInteger*)wrapper->var->value;
171 event->eventdata = value; 173 event->eventdata = value;
172 event->intval = ui_get(value); 174 event->eventdatatype = UI_EVENT_DATA_INTEGER_VALUE;
173 } 175 event->intval = value->get(value);
174 } 176 }
175 177 }
176 UIWIDGET ui_radiobutton_create(UiObject *obj, UiToggleArgs args) { 178
177 UiContainerPrivate *ctn = ui_obj_container(obj); 179 UIWIDGET ui_radiobutton_create(UiObject *obj, UiToggleArgs *args) {
178 UI_APPLY_LAYOUT(ctn->layout, args); 180 UiContainerPrivate *ctn = ui_obj_container(obj);
179 181 UI_APPLY_LAYOUT(ctn->layout, args);
180 QString str = QString::fromUtf8(args.label); 182
183 QString str = QString::fromUtf8(args->label);
181 QRadioButton *button = new QRadioButton(str); 184 QRadioButton *button = new QRadioButton(str);
182 button->setAutoExclusive(false); 185 button->setAutoExclusive(false);
183 186
184 UiVar* var = uic_widget_var(obj->ctx, obj->ctx, args.value, args.varname, UI_VAR_INTEGER); 187 UiVar* var = uic_widget_var(obj->ctx, obj->ctx, args->value, args->varname, UI_VAR_INTEGER);
185 if(var) { 188 if(var) {
186 UiInteger *value = (UiInteger*)var->value; 189 UiInteger *value = (UiInteger*)var->value;
187 QButtonGroup *buttonGroup = (QButtonGroup*)value->obj; 190 QButtonGroup *buttonGroup = (QButtonGroup*)value->obj;
188 if(!buttonGroup) { 191 if(!buttonGroup) {
189 buttonGroup = new QButtonGroup(); 192 buttonGroup = new QButtonGroup();
196 } 199 }
197 value->get = ui_radiobutton_get; 200 value->get = ui_radiobutton_get;
198 value->set = ui_radiobutton_set; 201 value->set = ui_radiobutton_set;
199 } 202 }
200 203
201 UiEventWrapper *event = new UiEventWrapper(obj, args.onchange, args.onchangedata); 204 UiEventWrapper *event = new UiEventWrapper(obj, args->onchange, args->onchangedata);
202 event->var = var; 205 event->var = var;
203 event->customdata1 = button; 206 event->customdata1 = button;
204 event->prepare_event = togglebutton_event; 207 event->prepare_event = togglebutton_event;
205 button->connect(button, SIGNAL(clicked()), event, SLOT(slot())); 208 button->connect(button, SIGNAL(clicked()), event, SLOT(slot()));
206 button->connect(button, SIGNAL(destroyed()), event, SLOT(destroy())); 209 button->connect(button, SIGNAL(destroyed()), event, SLOT(destroy()));
207 210
208 ctn->add(button, false); 211 ctn->add(button);
209 212
210 return button; 213 return button;
211 } 214 }
212 215
213 int64_t ui_radiobutton_get(UiInteger *value) { 216 int64_t ui_radiobutton_get(UiInteger *value) {

mercurial