add table model (QT) default tip

41 hours ago

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Tue, 22 Apr 2025 20:40:01 +0200 (41 hours ago)
changeset 581
7486347f73cf
parent 580
9b8d61227169

add table model (QT)

ui/qt/model.cpp file | annotate | diff | comparison | revisions
ui/qt/model.h file | annotate | diff | comparison | revisions
--- a/ui/qt/model.cpp	Mon Apr 21 19:32:57 2025 +0200
+++ b/ui/qt/model.cpp	Tue Apr 22 20:40:01 2025 +0200
@@ -99,6 +99,75 @@
 
 
 
+TableModel::TableModel(UiObject *obj, QTreeView *view, UiVar *var, ui_getvaluefunc getvalue){
+    this->obj = obj;
+    this->view = view;
+    this->var = var;
+    this->getvalue = getvalue;
+    this->onactivate = nullptr;
+    this->onactivatedata = nullptr;
+    this->onselection = nullptr;
+    this->onselectiondata = nullptr;
+}
+
+void TableModel::setActivationCallback(ui_callback f, void *userdata) {
+    onactivate = f;
+    onactivatedata = userdata;
+}
+
+void TableModel::setSelectionCallback(ui_callback f, void *userdata) {
+    onselection = f;
+    onselectiondata = userdata;
+}
+
+void TableModel::update(int row) {
+    if(row >= 0) {
+        this->update(row);
+    } else {
+        this->beginResetModel();
+        this->endResetModel();
+    }
+}
+
+int TableModel::rowCount(const QModelIndex& parent) const {
+    UiList *list = (UiList*)var->value;
+    return ui_list_count(list);
+}
+
+QVariant TableModel::data(const QModelIndex &index, int role) const {
+    if(role == Qt::DisplayRole) {
+        UiList *ls = (UiList*)var->value;
+        void *rowData = ls->get(ls, index.row());
+        if(rowData && getvalue) {
+            void *value = getvalue(rowData, 0);
+            return QString::fromUtf8((char*)value); 
+        }
+    }
+    return QVariant();
+}
+
+void TableModel::selectionChanged(const QItemSelection& selected, const QItemSelection& deselected) {
+    UiListSelection sel = ui_selection_model_to_selection(view->selectionModel());
+    
+    UiEvent event;
+    event.obj = obj;
+    event.window = obj->window;
+    event.document = obj->ctx->document;
+    event.eventdata = &sel;
+    event.intval = sel.count;
+    event.set = ui_get_setop();
+    
+    if(onactivate) {
+        onactivate(&event, onactivatedata);
+    }
+    if(onselection) {
+        onselection(&event, onselectiondata);
+    }
+    
+    free(sel.rows);
+}
+
+
 
 UiListSelection ui_selection_model_to_selection(QItemSelectionModel *model) {
     UiListSelection sel;
--- a/ui/qt/model.h	Mon Apr 21 19:32:57 2025 +0200
+++ b/ui/qt/model.h	Tue Apr 22 20:40:01 2025 +0200
@@ -71,6 +71,36 @@
         const QItemSelection & deselected);
 };
 
+class TableModel : public QAbstractListModel {
+    Q_OBJECT
+    
+    ui_getvaluefunc getvalue;
+    ui_callback onactivate;
+    void        *onactivatedata;
+    ui_callback onselection;
+    void        *onselectiondata;
+    
+public:
+    UiObject    *obj;
+    UiVar       *var;
+    QTreeView   *view;
+    
+    TableModel(UiObject *obj, QTreeView *view, UiVar *var, ui_getvaluefunc getvalue);
+    
+    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);
+};
+
 
 UiListSelection ui_selection_model_to_selection(QItemSelectionModel *model);
 

mercurial