ui/qt/model.h

Sun, 07 Dec 2025 15:50:20 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 07 Dec 2025 15:50:20 +0100
changeset 971
0224108bd8c2
parent 958
749a8a36d74b
permissions
-rw-r--r--

rename tree.h to list.h

/*
 * 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.
 */

#ifndef MODEL_H
#define	MODEL_H

#include "toolkit.h"
#include "../ui/list.h"
#include "../common/context.h"
#include <QListView>
#include <QTreeView>
#include <QComboBox>
#include <QAbstractListModel>
#include <QAbstractTableModel>
#include <QAbstractItemModel>
#include <QItemSelectionModel>



class ListModel : public QAbstractListModel {
    Q_OBJECT
    
    ui_getvaluefunc2 getvalue;
    void        *getvaluedata;
    ui_callback onactivate;
    void        *onactivatedata;
    ui_callback onselection;
    void        *onselectiondata;
    
public:
    UiObject    *obj;
    UiVar       *var;
    QListView   *listview;
    QComboBox   *combobox;
    
    ListModel(UiObject *obj, QListView *view, UiVar *var, ui_getvaluefunc2 getvalue, void *getvaluedata);
    ListModel(UiObject *obj, QComboBox *view, UiVar *var, ui_getvaluefunc2 getvalue, void *getvaluedata);
    
    void setActivationCallback(ui_callback f, void *userdata);
    void setSelectionCallback(ui_callback f, void *userdata);
    
    void update(int row);
    
    int rowCount(const QModelIndex &parent = QModelIndex()) const;
    QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
    
public slots:
    void selectionChanged(
        const QItemSelection & selected,
        const QItemSelection & deselected);
};

class TableModel : public QAbstractListModel {
    Q_OBJECT
    
    UiModel          *model;
    ui_getvaluefunc2 getvalue;
    void             *getvaluedata;
    ui_callback      onactivate;
    void             *onactivatedata;
    ui_callback      onselection;
    void             *onselectiondata;
    
public:
    UiObject         *obj;
    UiVar            *var;
    QTreeView        *view;
    
    TableModel(UiObject *obj, QTreeView *view, UiVar *var, UiModel *model, ui_getvaluefunc2 getvalue, void *getvaluedata);
    
    void setActivationCallback(ui_callback f, void *userdata);
    void setSelectionCallback(ui_callback f, void *userdata);
    
    void update(int row);
    
    int rowCount(const QModelIndex &parent = QModelIndex()) const;
    int columnCount(const QModelIndex &parent = QModelIndex()) const;
    QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
    QVariant headerData(int section, Qt::Orientation orientation, int role) const;
    
public slots:
    void selectionChanged(
        const QItemSelection & selected,
        const QItemSelection & deselected);
};


UiListSelection ui_selection_model_to_selection(QItemSelectionModel *model);

extern "C" {
    
    void ui_listmodel_update(UiList *list, int row);
    void ui_listmodel_setselection(UiList *list, UiListSelection sel);
    UiListSelection ui_listmodel_getselection(UiList *list);

}

#endif	/* MODEL_H */

mercurial