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