ui/qt/button.cpp

9 days ago

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 30 Mar 2025 12:00:26 +0200 (9 days ago)
changeset 535
ede57f5b6178
parent 526
f6a6b0b08641
child 550
0412c4c1048e
permissions
-rw-r--r--

add QT implementation for menubars, sub-menus and simple menu items

/*
 * 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);
    }
}

mercurial