added list view (Qt)

Mon, 12 Jan 2015 12:03:05 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Mon, 12 Jan 2015 12:03:05 +0100
changeset 72
a00b46d92c54
parent 71
3e021c5f18a0
child 73
473acef47ddd

added list view (Qt)

application/main.c file | annotate | diff | comparison | revisions
ui/qt/model.cpp file | annotate | diff | comparison | revisions
ui/qt/model.h file | annotate | diff | comparison | revisions
ui/qt/tree.cpp file | annotate | diff | comparison | revisions
--- a/application/main.c	Sun Jan 11 16:33:28 2015 +0100
+++ b/application/main.c	Mon Jan 12 12:03:05 2015 +0100
@@ -111,7 +111,29 @@
     printf("create window\n");
     UiObject *window = ui_window("Mod0", NULL);
     
+    UiModelInfo *model = ui_model_info(window->ctx, UI_STRING, "Name", UI_STRING, "Email", -1);
+    model->getvalue = (ui_model_getvalue_f)person_getvalue;
+    model->activate = action_activate;
+    model->selection = action_select;
+    UiList *list = ui_list_new();
+    Person *p1 = ui_malloc(window->ctx, sizeof(Person));
+    Person *p2 = ui_malloc(window->ctx, sizeof(Person));
+    Person *p3 = ui_malloc(window->ctx, sizeof(Person));
+    Person *p4 = ui_malloc(window->ctx, sizeof(Person));
+    p1->name = "Some Näme";
+    p1->mail = "mail@host.com";
+    p2->name = "押井守";
+    p2->mail = "other.person@provider.com";
+    p3->name = "My Self";
+    p3->mail = "my@self.org";
+    p4->name = "Gregory House";
+    p4->mail = "greg@pp";
+    ui_list_append(list, p1);
+    ui_list_append(list, p2);
+    ui_list_append(list, p3);
+    ui_list_append(list, p4);
     
+    ui_listview(window, list, (ui_model_getvalue_f)person_getvalue, action_activate, NULL);
     
     ui_show(window);
     ui_main();
--- a/ui/qt/model.cpp	Sun Jan 11 16:33:28 2015 +0100
+++ b/ui/qt/model.cpp	Mon Jan 12 12:03:05 2015 +0100
@@ -28,6 +28,66 @@
 
 #include "model.h"
 
+UiListSelection* listSelection(QItemSelectionModel *s) {
+    UiListSelection *selection = new UiListSelection();
+    
+    QModelIndexList list = s->selectedRows();
+    selection->count = list.count();
+    if(selection->count > 0) {
+        selection->rows = new int[selection->count];
+    }
+    
+    QModelIndex index;
+    int i=0;
+    foreach(index, list) {
+        selection->rows[i] = index.row();
+        i++;
+    }
+    return selection;
+}
+
+ListModel::ListModel(UiObject* obj, QListView* view, UiListPtr* list, ui_model_getvalue_f getvalue, ui_callback f, void* userdata) {
+    this->obj = obj;
+    this->view = view;
+    this->list = list;
+    this->getvalue = getvalue;
+    this->callback = f;
+    this->userdata = userdata;
+}
+
+int ListModel::rowCount(const QModelIndex& parent) const {
+    return list->list->count(list->list);
+}
+
+QVariant ListModel::data(const QModelIndex &index, int role) const {
+    if(role == Qt::DisplayRole) {
+        UiList *ls = list->list;
+        void *rowData = ls->get(ls, index.row());
+        if(rowData && getvalue) {
+            void *value = getvalue(rowData, 0);
+            return QString::fromUtf8((char*)value); 
+        }
+    }
+    return QVariant();
+}
+
+void ListModel::selectionChanged(const QItemSelection& selected, const QItemSelection& deselected) {
+    UiListSelection *selection = listSelection(view->selectionModel());
+    
+    UiEvent e;
+    e.obj = obj;
+    e.window = obj->window;
+    e.document = obj->ctx->document;
+    e.eventdata = selection;
+    e.intval = selection->count > 0 ? selection->rows[0] : -1;
+    callback(&e, userdata);
+    
+    if(selection->count > 0) {
+        delete selection->rows;
+    }
+    delete selection;
+}
+
 TableModel::TableModel(UiObject *obj, QTreeView *view, UiListPtr *list, UiModelInfo *info) {
     this->obj = obj;
     this->list = list;
@@ -71,26 +131,8 @@
     return QVariant();
 }
 
-UiListSelection* TableModel::listSelection() {
-    UiListSelection *selection = new UiListSelection();
-    
-    QModelIndexList list = view->selectionModel()->selectedRows();
-    selection->count = list.count();
-    if(selection->count > 0) {
-        selection->rows = new int[selection->count];
-    }
-    
-    QModelIndex index;
-    int i=0;
-    foreach(index, list) {
-        selection->rows[i] = index.row();
-        i++;
-    }
-    return selection;
-}
-
 void TableModel::selectionChanged(const QItemSelection& selected, const QItemSelection& deselected) {
-    UiListSelection *selection = listSelection();
+    UiListSelection *selection = listSelection(view->selectionModel());
     
     UiEvent e;
     e.obj = obj;
@@ -107,7 +149,7 @@
 }
 
 void TableModel::activate(const QModelIndex &) {
-    UiListSelection *selection = listSelection();
+    UiListSelection *selection = listSelection(view->selectionModel());
     
     UiEvent e;
     e.obj = obj;
--- a/ui/qt/model.h	Sun Jan 11 16:33:28 2015 +0100
+++ b/ui/qt/model.h	Mon Jan 12 12:03:05 2015 +0100
@@ -32,11 +32,35 @@
 #include "toolkit.h"
 #include "../ui/tree.h"
 #include "../common/context.h"
+#include <QListView>
 #include <QTreeView>
+#include <QAbstractListModel>
 #include <QAbstractTableModel>
 #include <QAbstractItemModel>
 #include <QItemSelectionModel>
 
+class ListModel : public QAbstractListModel {
+    Q_OBJECT
+    
+    UiObject    *obj;
+    UiListPtr   *list;
+    ui_model_getvalue_f getvalue;
+    ui_callback callback;
+    void        *userdata;
+    QListView   *view;
+    
+public:
+    ListModel(UiObject *obj, QListView *view, UiListPtr *list, ui_model_getvalue_f getvalue, ui_callback f, void *userdata);
+    
+    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 QAbstractTableModel {
     Q_OBJECT
     
@@ -47,13 +71,10 @@
 public:
     TableModel(UiObject *obj, QTreeView *view, UiListPtr *list, UiModelInfo *info);
     
-    int rowCount(const QModelIndex &parent = QModelIndex()) const ;
+    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;
-
-private:
-    UiListSelection* listSelection();
     
 public slots:
     void selectionChanged(
@@ -62,5 +83,7 @@
     void activate(const QModelIndex &);
 };
 
+UiListSelection* listSelection(QItemSelectionModel *s);
+
 #endif	/* MODEL_H */
 
--- a/ui/qt/tree.cpp	Sun Jan 11 16:33:28 2015 +0100
+++ b/ui/qt/tree.cpp	Mon Jan 12 12:03:05 2015 +0100
@@ -31,6 +31,50 @@
 
 #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);
+    
+    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();

mercurial