ui/qt/tree.cpp

Thu, 14 Nov 2024 17:25:23 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Thu, 14 Nov 2024 17:25:23 +0100
changeset 83
a612adaee43d
parent 0
2483f517c562
permissions
-rw-r--r--

fix pathbar elmfunc creating an element for an empty string

/*
 * 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 "tree.h"
#include "container.h"

#include <QTreeView>
#include <QTreeWidgetItem>
#include <QListView>


extern "C" void* ui_strmodel_getvalue(void *elm, int column) {
    return column == 0 ? elm : NULL;
}

UIWIDGET ui_listview_str(UiObject *obj, UiList *list, ui_callback f, void *udata) {
    return ui_listview(obj, list, ui_strmodel_getvalue, f, udata);
}
UIWIDGET ui_listview_var(UiObject *obj, UiListPtr *list, ui_model_getvalue_f getvalue, ui_callback f, void *udata) {
    QListView *view = new QListView();
    ListModel *model = new ListModel(obj, view, list, getvalue, f, udata);
    view->setModel(model);
    
    // TODO: observer update
    
    QItemSelectionModel *s = view->selectionModel();
    QObject::connect(
            s,
            SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)),
            model,
            SLOT(selectionChanged(const QItemSelection &, const QItemSelection &)));
    
    UiContainer *ct = uic_get_current_container(obj); 
    ct->add(view, true);
    return view;
}

UIWIDGET ui_listview(UiObject *obj, UiList *list, ui_model_getvalue_f getvalue, ui_callback f, void *udata) {
    UiListPtr *listptr = (UiListPtr*)ucx_mempool_malloc(obj->ctx->mempool, sizeof(UiListPtr));
    listptr->list = list;
    return ui_listview_var(obj, listptr, getvalue, f, udata);
}

UIWIDGET ui_listview_nv(UiObject *obj, char *varname, ui_model_getvalue_f getvalue, ui_callback f, void *udata) {
    UiVar *var = uic_connect_var(obj->ctx, varname, UI_VAR_LIST);
    if(var) {
        UiListVar *value = (UiListVar*)var->value;
        return ui_listview_var(obj, value->listptr, getvalue, f, udata);
    } else {
        // TODO: error
    }
    return NULL;
}


UIWIDGET ui_table_var(UiObject *obj, UiListPtr *list, UiModelInfo *modelinfo) {
    QTreeView *view = new QTreeView();
    TableModel *model = new TableModel(obj, view, list, modelinfo);
    view->setModel(model);
    
    view->setItemsExpandable(false);
    view->setRootIsDecorated(false);   
    
    // TODO: observer update
    UiTableView *u = new UiTableView();
    u->widget = view;
    u->model = model;
    list->list->observers = ui_add_observer(
            list->list->observers,
            (ui_callback)ui_table_update,
            u);
    
    view->setSelectionMode(QAbstractItemView::ExtendedSelection);
    QItemSelectionModel *s = view->selectionModel();
    QObject::connect(
            s,
            SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)),
            model,
            SLOT(selectionChanged(const QItemSelection &, const QItemSelection &)));
    QObject::connect(
            view,
            SIGNAL(doubleClicked(const QModelIndex &)),
            model,
            SLOT(activate(const QModelIndex &)));
    
    
    UiContainer *ct = uic_get_current_container(obj); 
    ct->add(view, true);
    return view;
}

void ui_table_update(UiEvent *event, UiTableView *view) {
    // TODO
    printf("update\n");
    
    //view->model->update();
    view->widget->setModel(NULL);
    view->widget->setModel(view->model);
}

UIWIDGET ui_table(UiObject *obj, UiList *list, UiModelInfo *modelinfo) {
    UiListPtr *listptr = (UiListPtr*)ucx_mempool_malloc(obj->ctx->mempool, sizeof(UiListPtr));
    listptr->list = list;
    return ui_table_var(obj, listptr, modelinfo);
}

UIWIDGET ui_table_nv(UiObject *obj, char *varname, UiModelInfo *modelinfo) {
    UiVar *var = uic_connect_var(obj->ctx, varname, UI_VAR_LIST);
    if(var) {
        UiListVar *value = (UiListVar*)var->value;
        return ui_table_var(obj, value->listptr, modelinfo);
    } else {
        // TODO: error
    }
    return NULL;
}

mercurial