ui/qt/toolbar.cpp

Sun, 20 Jul 2025 22:04:39 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 20 Jul 2025 22:04:39 +0200
changeset 108
77254bd6dccb
parent 103
6606616eca9f
permissions
-rw-r--r--

update toolkit, adjust UI code

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2014 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 <inttypes.h>

#include "toolbar.h"
#include "menu.h"
#include "stock.h"

static void add_items(UiObject *obj, QToolBar *toolbar, CxList *defaults, CxMap *items);
static void create_item(UiObject *obj, QToolBar *toolbar, UiToolbarItemI *i);

QToolBar* ui_create_toolbar(UiObject *obj) {
    CxMap *items = uic_get_toolbar_items();
    CxList *left_defaults = uic_get_toolbar_defaults(UI_TOOLBAR_LEFT);
    CxList *center_defaults = uic_get_toolbar_defaults(UI_TOOLBAR_CENTER);
    CxList *right_defaults = uic_get_toolbar_defaults(UI_TOOLBAR_RIGHT);
    
    if(!items || cxMapSize(items) == 0) {
        return nullptr;
    }
    
    QToolBar *toolbar = new QToolBar();
    add_items(obj, toolbar, left_defaults, items);
    add_items(obj, toolbar, center_defaults, items);
    add_items(obj, toolbar, right_defaults, items);
    
    
    return toolbar;
}

static void add_items(UiObject *obj, QToolBar *toolbar, CxList *defaults, CxMap *items) {
    CxIterator i = cxListIterator(defaults);
    cx_foreach(char *, name, i) {
        UiToolbarItemI *item = (UiToolbarItemI*)cxMapGet(items, name);
        if(item) {
            create_item(obj, toolbar, item);
        } else {
            fprintf(stderr, "UI Error: unknown toolbar item '%s'\n", name);
        }
    }
}

static void create_item(UiObject *obj, QToolBar *toolbar, UiToolbarItemI *i) {
    switch(i->type) {
        case UI_TOOLBAR_ITEM: {
            ui_toolbar_add_item(obj, toolbar, (UiToolbarItem*)i);
            break;
        }
        case UI_TOOLBAR_TOGGLEITEM: {
            ui_toolbar_add_toggleitem(obj, toolbar, (UiToolbarToggleItem*)i);
            break;
        }
        case UI_TOOLBAR_MENU: {
            ui_toolbar_add_menu(obj, toolbar, (UiToolbarMenuItem*)i);
            break;
        }
        default: fprintf(stderr, "toolbar item type unimplemented: %d\n", (int)i->type);
    }
}

void ui_toolbar_add_item(UiObject *obj, QToolBar *toolbar, UiToolbarItem *item) {
    QAction *action = new QAction();
    if(item->args.label) {
        action->setText(item->args.label);
    }
    if(item->args.icon) {
        action->setIcon(QIcon::fromTheme(item->args.icon));
    }
    toolbar->addAction(action);
    
    UiEventWrapper *event = new UiEventWrapper(obj, item->args.onclick, item->args.onclickdata);
    action->connect(action, SIGNAL(triggered()), event, SLOT(slot()));
    action->connect(action, SIGNAL(destroyed()), event, SLOT(destroy()));
}

static void toolbar_togglebutton_event(UiEvent *event, UiEventWrapper *wrapper) {
    QAction *action = (QAction*)wrapper->customdata1;
    event->intval = action->isChecked();
    if(wrapper->var) {
        event->eventdata = wrapper->var->value;
        event->eventdatatype = UI_EVENT_DATA_INTEGER_VALUE;
    }
}

void ui_toolbar_add_toggleitem(UiObject *obj, QToolBar *toolbar, UiToolbarToggleItem *item) {
    QAction *action = new QAction();
    action->setCheckable(true);
    if(item->args.label) {
        action->setText(item->args.label);
    }
    if(item->args.icon) {
        action->setIcon(QIcon::fromTheme(item->args.icon));
    }
    toolbar->addAction(action);
    
    UiVar* var = uic_widget_var(obj->ctx, obj->ctx, nullptr, item->args.varname, UI_VAR_INTEGER);
    UiEventWrapper *event = new UiEventWrapper(obj, item->args.onchange, item->args.onchangedata);
    event->var = var;
    event->customdata1 = action;
    event->prepare_event = toolbar_togglebutton_event;
    action->connect(action, SIGNAL(triggered()), event, SLOT(slot()));
    action->connect(action, SIGNAL(destroyed()), event, SLOT(destroy()));
}

void ui_toolbar_add_menu(UiObject *obj, QToolBar *toolbar, UiToolbarMenuItem *item) {
    
}

mercurial