ui/qt/button.cpp

2 weeks ago

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sat, 05 Apr 2025 11:21:02 +0200 (2 weeks ago)
changeset 551
0e543e1d060e
parent 550
0412c4c1048e
child 559
dc5ed4c10d83
permissions
-rw-r--r--

handle radiobutton click event (QT)

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2015 Olaf Wintermann. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *   1. Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 *
 *   2. Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in the
 *      documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include "button.h"
#include "container.h"
#include "toolkit.h"

UIWIDGET ui_button_create(UiObject* obj, UiButtonArgs args) {
    UiContainerPrivate *ctn = ui_obj_container(obj);
    UI_APPLY_LAYOUT(ctn->layout, args);
    
    QString str = QString::fromUtf8(args.label);
    QPushButton *button = new QPushButton(str);
    
    if(args.onclick) {
        UiEventWrapper *event = new UiEventWrapper(obj, args.onclick, args.onclickdata);
        button->connect(button, SIGNAL(clicked()), event, SLOT(slot()));
        button->connect(button, SIGNAL(destroyed()), event, SLOT(destroy()));
    }
    
    ctn->add(button, false);
    
    return button;
}

static void togglebutton_event(UiEvent *event, UiEventWrapper *wrapper) {
    QPushButton *button = (QPushButton*)wrapper->customdata1;
    event->intval = button->isChecked();
    if(wrapper->var) {
        event->eventdata = wrapper->var->value;
    }
}

UIWIDGET ui_togglebutton_create(UiObject* obj, UiToggleArgs args) {
    UiContainerPrivate *ctn = ui_obj_container(obj);
    UI_APPLY_LAYOUT(ctn->layout, args);
    
    QString str = QString::fromUtf8(args.label);
    QPushButton *button = new QPushButton(str);
    button->setCheckable(true);
    
    UiVar* var = uic_widget_var(obj->ctx, obj->ctx, args.value, args.varname, UI_VAR_INTEGER);
    
    if(args.onchange) {
        UiEventWrapper *event = new UiEventWrapper(obj, args.onchange, args.onchangedata);
        event->var = var;
        event->customdata1 = button;
        event->prepare_event = togglebutton_event;
        button->connect(button, SIGNAL(clicked()), event, SLOT(slot()));
        button->connect(button, SIGNAL(destroyed()), event, SLOT(destroy()));
    }
    
    if(var) {
        UiInteger *i = (UiInteger*)var->value;
        
        if(i->value) {
            button->setChecked(true);
        }
        
        i->obj = button;
        i->get = ui_togglebutton_get;
        i->set = ui_togglebutton_set;
    }
    
    ctn->add(button, false);
    
    return button;
}

int64_t ui_togglebutton_get(UiInteger *value) {
    QPushButton *button = (QPushButton*)value->obj;
    value->value = button->isChecked();
    return value->value;
}

void ui_togglebutton_set(UiInteger *value, int64_t i) {
    QPushButton *button = (QPushButton*)value->obj;
    value->value = i;
    if(i != 0) {
        button->setChecked(true);
    }
}

static void checkbox_event(UiEvent *event, UiEventWrapper *wrapper) {
    QPushButton *button = (QPushButton*)wrapper->customdata1;
    event->intval = button->isChecked();
    if(wrapper->var) {
        event->eventdata = wrapper->var->value;
    }
}


UIWIDGET ui_checkbox_create(UiObject* obj, UiToggleArgs args) {
    UiContainerPrivate *ctn = ui_obj_container(obj);
    UI_APPLY_LAYOUT(ctn->layout, args);
    
    QString str = QString::fromUtf8(args.label);
    QCheckBox *checkbox = new QCheckBox(str);
    
    UiVar* var = uic_widget_var(obj->ctx, obj->ctx, args.value, args.varname, UI_VAR_INTEGER);
    
    if(args.onchange) {
        UiEventWrapper *event = new UiEventWrapper(obj, args.onchange, args.onchangedata);
        event->var = var;
        event->customdata1 = checkbox;
        event->prepare_event = checkbox_event;
        checkbox->connect(checkbox, SIGNAL(clicked()), event, SLOT(slot()));
        checkbox->connect(checkbox, SIGNAL(destroyed()), event, SLOT(destroy()));
    }
    
    if(var) {
        UiInteger *i = (UiInteger*)var->value;
        
        if(i->value) {
            checkbox->setChecked(true);
        }
        
        i->obj = checkbox;
        i->get = ui_checkbox_get;
        i->set = ui_checkbox_set;
    }
    
    ctn->add(checkbox, false);
    
    return checkbox;
}

int64_t ui_checkbox_get(UiInteger *value) {
    QPushButton *button = (QPushButton*)value->obj;
    value->value = button->isChecked();
    return value->value;
}

void ui_checkbox_set(UiInteger *value, int64_t i) {
    QPushButton *button = (QPushButton*)value->obj;
    value->value = i;
    if(i != 0) {
        button->setChecked(true);
    }
}


static void radiobutton_event(UiEvent *event, UiEventWrapper *wrapper) {
    if(wrapper->var) {
        UiInteger *value = wrapper->var->value;
        event->eventdata = value;
        event->intval = ui_get(value);
    }
}

UIWIDGET ui_radiobutton_create(UiObject *obj, UiToggleArgs args) {
    UiContainerPrivate *ctn = ui_obj_container(obj);
    UI_APPLY_LAYOUT(ctn->layout, args);
    
    QString str = QString::fromUtf8(args.label);
    QRadioButton *button = new QRadioButton(str);
    button->setAutoExclusive(false);
    
    UiVar* var = uic_widget_var(obj->ctx, obj->ctx, args.value, args.varname, UI_VAR_INTEGER);
    if(var) {
        UiInteger *value = (UiInteger*)var->value;
        QButtonGroup *buttonGroup = (QButtonGroup*)value->obj;
        if(!buttonGroup) {
            buttonGroup = new QButtonGroup();
            value->obj = buttonGroup;
        }
        int id = buttonGroup->buttons().size()+1;
        buttonGroup->addButton(button, id);
        if(value->value == id) {
            button->setChecked(true);
        }
        value->get = ui_radiobutton_get;
        value->set = ui_radiobutton_set;
    }
    
    UiEventWrapper *event = new UiEventWrapper(obj, args.onchange, args.onchangedata);
    event->var = var;
    event->customdata1 = button;
    event->prepare_event = togglebutton_event;
    button->connect(button, SIGNAL(clicked()), event, SLOT(slot()));
    button->connect(button, SIGNAL(destroyed()), event, SLOT(destroy()));
    
    ctn->add(button, false);
    
    return button;
}

int64_t ui_radiobutton_get(UiInteger *value) {
    QButtonGroup *buttonGroup = (QButtonGroup*)value->obj;
    value->value = buttonGroup->checkedId();
    return value->value;
}

void ui_radiobutton_set(UiInteger *value, int64_t i) {
    QButtonGroup *buttonGroup = (QButtonGroup*)value->obj;
    QAbstractButton *button = buttonGroup->button(i);
    if(button) {
        button->setChecked(true);
        value->value = i;
    }
}

mercurial