#include "entry.h"
#include "container.h"
#include "../common/context.h"
#include <QDoubleSpinBox>
#include <QSpinBox>
UIWIDGET ui_spinbox_create(UiObject *obj, UiSpinBoxArgs *args) {
UiContainerPrivate *ctn = ui_obj_container(obj);
double min = args->min;
double max = args->max !=
0 ? args->max :
100000;
bool use_double = false;
UiVar *var =
NULL;
UiVarType vartype =
UI_VAR_SPECIAL;
if(args->varname) {
var = uic_get_var(obj->ctx, args->varname);
vartype = var->type;
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);
vartype =
UI_VAR_INTEGER;
}
else if(args->doublevalue) {
var = uic_widget_var(obj->ctx, obj->ctx, args->doublevalue,
NULL,
UI_VAR_DOUBLE);
vartype =
UI_VAR_DOUBLE;
use_double = true;
}
else if(args->rangevalue) {
var = uic_widget_var(obj->ctx, obj->ctx, args->rangevalue,
NULL,
UI_VAR_RANGE);
vartype =
UI_VAR_RANGE;
use_double = true;
}
else {
if(args->digits >
0) {
use_double = true;
}
}
}
if(vartype ==
UI_VAR_RANGE) {
UiRange *r = (UiRange*)var->value;
min = r->min;
max = r->max;
}
QAbstractSpinBox *widget = nullptr;
if(use_double) {
QDoubleSpinBox *spinbox = new QDoubleSpinBox();
spinbox->setDecimals(args->digits);
if(args->step !=
0) {
spinbox->setSingleStep(args->step);
}
spinbox->setMinimum(min);
spinbox->setMaximum(max);
widget = spinbox;
}
else {
QSpinBox *spinbox = new QSpinBox();
if(args->step !=
0) {
spinbox->setSingleStep(args->step);
}
spinbox->setMinimum((
int)min);
spinbox->setMaximum((
int)max);
widget = spinbox;
}
if(var) {
if(vartype ==
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(vartype ==
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(vartype ==
UI_VAR_RANGE) {
UiRange *value = (UiRange*)var->value;
value->obj = widget;
QDoubleSpinBox *spinbox = (QDoubleSpinBox*)widget;
if(value->value !=
0) {
spinbox->setValue(value->value);
}
if(value->min != value->max) {
spinbox->setRange(value->min, value->max);
}
if(value->extent !=
0) {
spinbox->setSingleStep(value->extent);
}
value->get = ui_spinbox_range_get;
value->set = ui_spinbox_range_set;
value->setrange = ui_spinbox_range_setrange;
value->setextent = ui_spinbox_range_setextent;
}
}
UiLayout layout =
UI_ARGS2LAYOUT(args);
ctn->add(widget, layout);
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();
}
double ui_spinbox_range_get(UiRange *range) {
QDoubleSpinBox *spinbox = (QDoubleSpinBox*)range->obj;
range->value = spinbox->value();
return range->value;
}
void ui_spinbox_range_set(UiRange *range,
double value) {
QDoubleSpinBox *spinbox = (QDoubleSpinBox*)range->obj;
spinbox->setValue(value);
range->value = spinbox->value();
}
void ui_spinbox_range_setrange(UiRange *range,
double min,
double max) {
QDoubleSpinBox *spinbox = (QDoubleSpinBox*)range->obj;
spinbox->setRange(min, max);
range->min = min;
range->max = max;
}
void ui_spinbox_range_setextent(UiRange *range,
double extent) {
QDoubleSpinBox *spinbox = (QDoubleSpinBox*)range->obj;
spinbox->setSingleStep(extent);
range->extent = extent;
}