UNIXworkcode

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_spinbox_create(UiObject *obj, UiSpinBoxArgs *args) { 40 UiContainerPrivate *ctn = ui_obj_container(obj); 41 42 double min = args->min; 43 double max = args->max != 0 ? args->max : 100000; 44 45 bool use_double = false; 46 UiVar *var = NULL; 47 UiVarType vartype = UI_VAR_SPECIAL; 48 if(args->varname) { 49 var = uic_get_var(obj->ctx, args->varname); 50 vartype = var->type; 51 if(var->type == UI_VAR_DOUBLE) { 52 use_double = true; 53 } else if(var->type == UI_VAR_RANGE) { 54 use_double = true; 55 } else if(var->type != UI_VAR_INTEGER) { 56 var = NULL; 57 fprintf(stderr, "UI Error: var ''%s'' has wrong type (must be int/double/range)\n", args->varname); 58 } 59 } 60 61 if(!var) { 62 if(args->intvalue) { 63 var = uic_widget_var(obj->ctx, obj->ctx, args->intvalue, NULL, UI_VAR_INTEGER); 64 vartype = UI_VAR_INTEGER; 65 } else if(args->doublevalue) { 66 var = uic_widget_var(obj->ctx, obj->ctx, args->doublevalue, NULL, UI_VAR_DOUBLE); 67 vartype = UI_VAR_DOUBLE; 68 use_double = true; 69 } else if(args->rangevalue) { 70 var = uic_widget_var(obj->ctx, obj->ctx, args->rangevalue, NULL, UI_VAR_RANGE); 71 vartype = UI_VAR_RANGE; 72 use_double = true; 73 } else { 74 if(args->digits > 0) { 75 use_double = true; 76 } 77 } 78 } 79 80 if(vartype == UI_VAR_RANGE) { 81 UiRange *r = (UiRange*)var->value; 82 min = r->min; 83 max = r->max; 84 } 85 86 QAbstractSpinBox *widget = nullptr; 87 if(use_double) { 88 QDoubleSpinBox *spinbox = new QDoubleSpinBox(); 89 spinbox->setDecimals(args->digits); 90 if(args->step != 0) { 91 spinbox->setSingleStep(args->step); 92 } 93 spinbox->setMinimum(min); 94 spinbox->setMaximum(max); 95 widget = spinbox; 96 } else { 97 QSpinBox *spinbox = new QSpinBox(); 98 if(args->step != 0) { 99 spinbox->setSingleStep(args->step); 100 } 101 spinbox->setMinimum((int)min); 102 spinbox->setMaximum((int)max); 103 widget = spinbox; 104 } 105 106 if(var) { 107 if(vartype == UI_VAR_INTEGER) { 108 UiInteger *value = (UiInteger*)var->value; 109 value->obj = widget; 110 if(value->value != 0) { 111 QSpinBox *spinbox = (QSpinBox*)widget; 112 spinbox->setValue(value->value); 113 } 114 value->get = ui_spinbox_int_get; 115 value->set = ui_spinbox_int_set; 116 } else if(vartype == UI_VAR_DOUBLE) { 117 UiDouble *value = (UiDouble*)var->value; 118 value->obj = widget; 119 if(value->value != 0) { 120 QDoubleSpinBox *spinbox = (QDoubleSpinBox*)widget; 121 spinbox->setValue(value->value); 122 } 123 value->get = ui_spinbox_double_get; 124 value->set = ui_spinbox_double_set; 125 } else if(vartype == UI_VAR_RANGE) { 126 UiRange *value = (UiRange*)var->value; 127 value->obj = widget; 128 QDoubleSpinBox *spinbox = (QDoubleSpinBox*)widget; 129 if(value->value != 0) { 130 spinbox->setValue(value->value); 131 } 132 if(value->min != value->max) { 133 spinbox->setRange(value->min, value->max); 134 } 135 if(value->extent != 0) { 136 spinbox->setSingleStep(value->extent); 137 } 138 value->get = ui_spinbox_range_get; 139 value->set = ui_spinbox_range_set; 140 value->setrange = ui_spinbox_range_setrange; 141 value->setextent = ui_spinbox_range_setextent; 142 } 143 } 144 145 UiLayout layout = UI_ARGS2LAYOUT(args); 146 ctn->add(widget, layout); 147 return widget; 148 } 149 150 int64_t ui_spinbox_int_get(UiInteger *i) { 151 QSpinBox *spinbox = (QSpinBox*)i->obj; 152 i->value = spinbox->value(); 153 return i->value; 154 } 155 156 void ui_spinbox_int_set(UiInteger *i, int64_t value) { 157 QSpinBox *spinbox = (QSpinBox*)i->obj; 158 spinbox->setValue(value); 159 i->value = spinbox->value(); 160 } 161 162 double ui_spinbox_double_get(UiDouble *d) { 163 QDoubleSpinBox *spinbox = (QDoubleSpinBox*)d->obj; 164 d->value = spinbox->value(); 165 return d->value; 166 } 167 168 void ui_spinbox_double_set(UiDouble *d, double value) { 169 QDoubleSpinBox *spinbox = (QDoubleSpinBox*)d->obj; 170 spinbox->setValue(value); 171 d->value = spinbox->value(); 172 } 173 174 double ui_spinbox_range_get(UiRange *range) { 175 QDoubleSpinBox *spinbox = (QDoubleSpinBox*)range->obj; 176 range->value = spinbox->value(); 177 return range->value; 178 } 179 180 void ui_spinbox_range_set(UiRange *range, double value) { 181 QDoubleSpinBox *spinbox = (QDoubleSpinBox*)range->obj; 182 spinbox->setValue(value); 183 range->value = spinbox->value(); 184 } 185 186 void ui_spinbox_range_setrange(UiRange *range, double min, double max) { 187 QDoubleSpinBox *spinbox = (QDoubleSpinBox*)range->obj; 188 spinbox->setRange(min, max); 189 range->min = min; 190 range->max = max; 191 } 192 193 void ui_spinbox_range_setextent(UiRange *range, double extent) { 194 QDoubleSpinBox *spinbox = (QDoubleSpinBox*)range->obj; 195 spinbox->setSingleStep(extent); 196 range->extent = extent; 197 } 198