2 weeks ago
add spinner entry (QT)
application/main.c | file | annotate | diff | comparison | revisions | |
ui/qt/button.cpp | file | annotate | diff | comparison | revisions | |
ui/qt/button.h | file | annotate | diff | comparison | revisions | |
ui/qt/entry.cpp | file | annotate | diff | comparison | revisions | |
ui/qt/entry.h | file | annotate | diff | comparison | revisions | |
ui/qt/qt5.pro | file | annotate | diff | comparison | revisions |
--- a/application/main.c Wed Apr 09 20:49:27 2025 +0200 +++ b/application/main.c Wed Apr 09 21:17:26 2025 +0200 @@ -928,6 +928,7 @@ ui_button(obj, .label = "Button Y"); ui_checkbox(obj, .label = "Checkbox"); + ui_spinner(obj, .digits = 2); ui_newline(obj); ui_hbox(obj, .colspan = 3) {
--- a/ui/qt/button.cpp Wed Apr 09 20:49:27 2025 +0200 +++ b/ui/qt/button.cpp Wed Apr 09 21:17:26 2025 +0200 @@ -1,7 +1,7 @@ /* * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. * - * Copyright 2015 Olaf Wintermann. All rights reserved. + * Copyright 2025 Olaf Wintermann. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met:
--- a/ui/qt/button.h Wed Apr 09 20:49:27 2025 +0200 +++ b/ui/qt/button.h Wed Apr 09 21:17:26 2025 +0200 @@ -1,7 +1,7 @@ /* * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. * - * Copyright 2015 Olaf Wintermann. All rights reserved. + * Copyright 2025 Olaf Wintermann. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met:
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ui/qt/entry.cpp Wed Apr 09 21:17:26 2025 +0200 @@ -0,0 +1,142 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2025 Olaf Wintermann. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include "entry.h" + +#include "container.h" +#include "../common/context.h" + +#include <QDoubleSpinBox> +#include <QSpinBox> + + + +UIWIDGET ui_spinner_create(UiObject *obj, UiSpinnerArgs args) { + UiContainerPrivate *ctn = ui_obj_container(obj); + UI_APPLY_LAYOUT(ctn->layout, args); + + bool use_double = false; + UiVar *var = NULL; + if(args.varname) { + var = uic_get_var(obj->ctx, args.varname); + if(var->type == UI_VAR_DOUBLE) { + use_double = true; + } else if(var->type == UI_VAR_RANGE) { + use_double = true; + } else if(var->type != UI_VAR_INTEGER) { + var = NULL; + fprintf(stderr, "UI Error: var '%s' has wrong type (must be int/double/range)\n", args.varname); + } + } + + if(!var) { + if(args.intvalue) { + var = uic_widget_var(obj->ctx, obj->ctx, args.intvalue, NULL, UI_VAR_INTEGER); + } else if(args.doublevalue) { + var = uic_widget_var(obj->ctx, obj->ctx, args.doublevalue, NULL, UI_VAR_DOUBLE); + use_double = true; + } else if(args.rangevalue) { + var = uic_widget_var(obj->ctx, obj->ctx, args.rangevalue, NULL, UI_VAR_RANGE); + use_double = true; + } else { + if(args.digits > 0) { + use_double = true; + } + } + } + + QAbstractSpinBox *widget = nullptr; + if(use_double) { + QDoubleSpinBox *spinbox = new QDoubleSpinBox(); + spinbox->setDecimals(args.digits); + if(args.step != 0) { + spinbox->setSingleStep(args.step); + } + widget = spinbox; + } else { + QSpinBox *spinbox = new QSpinBox(); + if(args.step != 0) { + spinbox->setSingleStep(args.step); + } + widget = spinbox; + } + + if(var) { + if(var->type == UI_VAR_INTEGER) { + UiInteger *value = (UiInteger*)var->value; + value->obj = widget; + if(value->value != 0) { + QSpinBox *spinbox = (QSpinBox*)widget; + spinbox->setValue(value->value); + } + value->get = ui_spinbox_int_get; + value->set = ui_spinbox_int_set; + } else if(var->type == UI_VAR_DOUBLE) { + UiDouble *value = (UiDouble*)var->value; + value->obj = widget; + if(value->value != 0) { + QDoubleSpinBox *spinbox = (QDoubleSpinBox*)widget; + spinbox->setValue(value->value); + } + value->get = ui_spinbox_double_get; + value->set = ui_spinbox_double_set; + } else if(var->type == UI_VAR_RANGE) { + UiRange *value = (UiRange*)var->value; + value->obj = widget; + // TODO + } + } + + + ctn->add(widget, false); + return widget; +} + +int64_t ui_spinbox_int_get(UiInteger *i) { + QSpinBox *spinbox = (QSpinBox*)i->obj; + i->value = spinbox->value(); + return i->value; +} + +void ui_spinbox_int_set(UiInteger *i, int64_t value) { + QSpinBox *spinbox = (QSpinBox*)i->obj; + spinbox->setValue(value); + i->value = spinbox->value(); +} + +double ui_spinbox_double_get(UiDouble *d) { + QDoubleSpinBox *spinbox = (QDoubleSpinBox*)d->obj; + d->value = spinbox->value(); + return d->value; +} + +void ui_spinbox_double_set(UiDouble *d, double value) { + QDoubleSpinBox *spinbox = (QDoubleSpinBox*)d->obj; + spinbox->setValue(value); + d->value = spinbox->value(); +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ui/qt/entry.h Wed Apr 09 21:17:26 2025 +0200 @@ -0,0 +1,51 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2025 Olaf Wintermann. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef ENTRY_H +#define ENTRY_H + +#include "../ui/entry.h" +#include "toolkit.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int64_t ui_spinbox_int_get(UiInteger *i); +void ui_spinbox_int_set(UiInteger *i, int64_t value); + +double ui_spinbox_double_get(UiDouble *d); +void ui_spinbox_double_set(UiDouble *d, double value); + + +#ifdef __cplusplus +} +#endif + +#endif /* ENTRY_H */ +
--- a/ui/qt/qt5.pro Wed Apr 09 20:49:27 2025 +0200 +++ b/ui/qt/qt5.pro Wed Apr 09 21:17:26 2025 +0200 @@ -50,6 +50,7 @@ SOURCES += label.cpp SOURCES += graphics.cpp SOURCES += widget.cpp +SOURCES += entry.cpp HEADERS += toolkit.h HEADERS += window.h @@ -64,4 +65,5 @@ HEADERS += label.h HEADERS += graphics.h HEADERS += widget.h +HEADERS += entry.h