| |
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 QDoubleSpinBox *spinbox = (QDoubleSpinBox*)widget; |
| |
112 if(value->value != 0) { |
| |
113 spinbox->setValue(value->value); |
| |
114 } |
| |
115 if(value->min != value->max) { |
| |
116 spinbox->setRange(value->min, value->max); |
| |
117 } |
| |
118 if(value->extent != 0) { |
| |
119 spinbox->setSingleStep(value->extent); |
| |
120 } |
| |
121 value->get = ui_spinbox_range_get; |
| |
122 value->set = ui_spinbox_range_set; |
| |
123 value->setrange = ui_spinbox_range_setrange; |
| |
124 value->setextent = ui_spinbox_range_setextent; |
| |
125 } |
| |
126 } |
| |
127 |
| |
128 |
| |
129 ctn->add(widget); |
| |
130 return widget; |
| |
131 } |
| |
132 |
| |
133 int64_t ui_spinbox_int_get(UiInteger *i) { |
| |
134 QSpinBox *spinbox = (QSpinBox*)i->obj; |
| |
135 i->value = spinbox->value(); |
| |
136 return i->value; |
| |
137 } |
| |
138 |
| |
139 void ui_spinbox_int_set(UiInteger *i, int64_t value) { |
| |
140 QSpinBox *spinbox = (QSpinBox*)i->obj; |
| |
141 spinbox->setValue(value); |
| |
142 i->value = spinbox->value(); |
| |
143 } |
| |
144 |
| |
145 double ui_spinbox_double_get(UiDouble *d) { |
| |
146 QDoubleSpinBox *spinbox = (QDoubleSpinBox*)d->obj; |
| |
147 d->value = spinbox->value(); |
| |
148 return d->value; |
| |
149 } |
| |
150 |
| |
151 void ui_spinbox_double_set(UiDouble *d, double value) { |
| |
152 QDoubleSpinBox *spinbox = (QDoubleSpinBox*)d->obj; |
| |
153 spinbox->setValue(value); |
| |
154 d->value = spinbox->value(); |
| |
155 } |
| |
156 |
| |
157 double ui_spinbox_range_get(UiRange *range) { |
| |
158 QDoubleSpinBox *spinbox = (QDoubleSpinBox*)range->obj; |
| |
159 range->value = spinbox->value(); |
| |
160 return range->value; |
| |
161 } |
| |
162 |
| |
163 void ui_spinbox_range_set(UiRange *range, double value) { |
| |
164 QDoubleSpinBox *spinbox = (QDoubleSpinBox*)range->obj; |
| |
165 spinbox->setValue(value); |
| |
166 range->value = spinbox->value(); |
| |
167 } |
| |
168 |
| |
169 void ui_spinbox_range_setrange(UiRange *range, double min, double max) { |
| |
170 QDoubleSpinBox *spinbox = (QDoubleSpinBox*)range->obj; |
| |
171 spinbox->setRange(min, max); |
| |
172 range->min = min; |
| |
173 range->max = max; |
| |
174 } |
| |
175 |
| |
176 void ui_spinbox_range_setextent(UiRange *range, double extent) { |
| |
177 QDoubleSpinBox *spinbox = (QDoubleSpinBox*)range->obj; |
| |
178 spinbox->setSingleStep(extent); |
| |
179 range->extent = extent; |
| |
180 } |