ui/qt/entry.cpp

changeset 560
9e302b8a6c37
child 561
5798e3a28c59
equal deleted inserted replaced
559:dc5ed4c10d83 560:9e302b8a6c37
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2025 Olaf Wintermann. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include "entry.h"
30
31 #include "container.h"
32 #include "../common/context.h"
33
34 #include <QDoubleSpinBox>
35 #include <QSpinBox>
36
37
38
39 UIWIDGET ui_spinner_create(UiObject *obj, UiSpinnerArgs args) {
40 UiContainerPrivate *ctn = ui_obj_container(obj);
41 UI_APPLY_LAYOUT(ctn->layout, args);
42
43 bool use_double = false;
44 UiVar *var = NULL;
45 if(args.varname) {
46 var = uic_get_var(obj->ctx, args.varname);
47 if(var->type == UI_VAR_DOUBLE) {
48 use_double = true;
49 } else if(var->type == UI_VAR_RANGE) {
50 use_double = true;
51 } else if(var->type != UI_VAR_INTEGER) {
52 var = NULL;
53 fprintf(stderr, "UI Error: var '%s' has wrong type (must be int/double/range)\n", args.varname);
54 }
55 }
56
57 if(!var) {
58 if(args.intvalue) {
59 var = uic_widget_var(obj->ctx, obj->ctx, args.intvalue, NULL, UI_VAR_INTEGER);
60 } else if(args.doublevalue) {
61 var = uic_widget_var(obj->ctx, obj->ctx, args.doublevalue, NULL, UI_VAR_DOUBLE);
62 use_double = true;
63 } else if(args.rangevalue) {
64 var = uic_widget_var(obj->ctx, obj->ctx, args.rangevalue, NULL, UI_VAR_RANGE);
65 use_double = true;
66 } else {
67 if(args.digits > 0) {
68 use_double = true;
69 }
70 }
71 }
72
73 QAbstractSpinBox *widget = nullptr;
74 if(use_double) {
75 QDoubleSpinBox *spinbox = new QDoubleSpinBox();
76 spinbox->setDecimals(args.digits);
77 if(args.step != 0) {
78 spinbox->setSingleStep(args.step);
79 }
80 widget = spinbox;
81 } else {
82 QSpinBox *spinbox = new QSpinBox();
83 if(args.step != 0) {
84 spinbox->setSingleStep(args.step);
85 }
86 widget = spinbox;
87 }
88
89 if(var) {
90 if(var->type == UI_VAR_INTEGER) {
91 UiInteger *value = (UiInteger*)var->value;
92 value->obj = widget;
93 if(value->value != 0) {
94 QSpinBox *spinbox = (QSpinBox*)widget;
95 spinbox->setValue(value->value);
96 }
97 value->get = ui_spinbox_int_get;
98 value->set = ui_spinbox_int_set;
99 } else if(var->type == UI_VAR_DOUBLE) {
100 UiDouble *value = (UiDouble*)var->value;
101 value->obj = widget;
102 if(value->value != 0) {
103 QDoubleSpinBox *spinbox = (QDoubleSpinBox*)widget;
104 spinbox->setValue(value->value);
105 }
106 value->get = ui_spinbox_double_get;
107 value->set = ui_spinbox_double_set;
108 } else if(var->type == UI_VAR_RANGE) {
109 UiRange *value = (UiRange*)var->value;
110 value->obj = widget;
111 // TODO
112 }
113 }
114
115
116 ctn->add(widget, false);
117 return widget;
118 }
119
120 int64_t ui_spinbox_int_get(UiInteger *i) {
121 QSpinBox *spinbox = (QSpinBox*)i->obj;
122 i->value = spinbox->value();
123 return i->value;
124 }
125
126 void ui_spinbox_int_set(UiInteger *i, int64_t value) {
127 QSpinBox *spinbox = (QSpinBox*)i->obj;
128 spinbox->setValue(value);
129 i->value = spinbox->value();
130 }
131
132 double ui_spinbox_double_get(UiDouble *d) {
133 QDoubleSpinBox *spinbox = (QDoubleSpinBox*)d->obj;
134 d->value = spinbox->value();
135 return d->value;
136 }
137
138 void ui_spinbox_double_set(UiDouble *d, double value) {
139 QDoubleSpinBox *spinbox = (QDoubleSpinBox*)d->obj;
140 spinbox->setValue(value);
141 d->value = spinbox->value();
142 }

mercurial