improved table (Qt)

Sat, 10 Jan 2015 10:14:28 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sat, 10 Jan 2015 10:14:28 +0100
changeset 69
419c8c3209e8
parent 68
bd9fb6476b80
child 70
3d801e8dda3a

improved table (Qt)

application/main.c file | annotate | diff | comparison | revisions
ui/gtk/tree.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	Fri Jan 09 14:33:40 2015 +0100
+++ b/application/main.c	Sat Jan 10 10:14:28 2015 +0100
@@ -80,11 +80,11 @@
     printf("{%s}\n", s);
     //printf("name: {%s}\n", ui_getval(name));
     //printf("mail: {%s}\n", ui_getval(mail));
-    ui_select_tab(tabview, 1);
+    //ui_select_tab(tabview, 1);
 }
 
 void action_test(UiEvent *event, void *data) {
-    ui_select_tab(tabview, 0);
+    //ui_select_tab(tabview, 0);
 }
 
 int main(int argc, char** argv) { 
@@ -105,23 +105,30 @@
     printf("create window\n");
     UiObject *window = ui_window("Mod0", NULL);
     
-    tabview = ui_tabview(window);
+    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_tab(window, "1");
-    ui_grid(window);
-    ui_button(window, "Test1________________", action_button, NULL);
-    ui_button(window, "Test2", action_button, NULL);
-    ui_newline(window);
-    ui_button(window, "Test1", action_button, NULL);
-    ui_button(window, "Test2", action_button, NULL);
-    ui_end(window);
-    ui_end(window);
+    ui_table(window, list, model);
     
-    ui_tab(window, "2");
-    ui_textarea_nv(window, "text");
-    ui_button(window, "Zurück", action_test, NULL);
-    
-    ui_end(window);
     
     ui_show(window);
     ui_main();
--- a/ui/gtk/tree.c	Fri Jan 09 14:33:40 2015 +0100
+++ b/ui/gtk/tree.c	Sat Jan 10 10:14:28 2015 +0100
@@ -244,6 +244,9 @@
     e.intval = selection->count > 0 ? selection->rows[0] : -1;
     event->activate(&e, event->userdata);
     
+    if(selection->count > 0) {
+        free(selection->rows);
+    }
     free(selection);
 }
 
@@ -261,6 +264,9 @@
     e.intval = selection->count > 0 ? selection->rows[0] : -1;
     event->selection(&e, event->userdata);
     
+    if(selection->count > 0) {
+        free(selection->rows);
+    }
     free(selection);
 }
 
--- a/ui/qt/model.cpp	Fri Jan 09 14:33:40 2015 +0100
+++ b/ui/qt/model.cpp	Sat Jan 10 10:14:28 2015 +0100
@@ -28,9 +28,11 @@
 
 #include "model.h"
 
-TableModel::TableModel(UiListPtr *list, UiModelInfo *info) {
+TableModel::TableModel(UiObject *obj, QTreeView *view, UiListPtr *list, UiModelInfo *info) {
+    this->obj = obj;
     this->list = list;
     this->info = info;
+    this->view = view;
 }
 
 int TableModel::rowCount(const QModelIndex &parent) const {
@@ -61,4 +63,62 @@
     return QVariant();
 }
 
+QVariant TableModel::headerData(int section, Qt::Orientation orientation, int role) const {
+    if(role == Qt::DisplayRole) {
+        char *label = info->titles[section];
+        return QString::fromUtf8(label);
+    }
+    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();
+    
+    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;
+    info->selection(&e, info->userdata);
+    
+    if(selection->count > 0) {
+        delete selection->rows;
+    }
+    delete selection;
+}
+
+void TableModel::activate(const QModelIndex &) {
+    UiListSelection *selection = listSelection();
+    
+    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;
+    info->activate(&e, info->userdata);
+    
+    if(selection->count > 0) {
+        delete selection->rows;
+    }
+    delete selection;
+}
--- a/ui/qt/model.h	Fri Jan 09 14:33:40 2015 +0100
+++ b/ui/qt/model.h	Sat Jan 10 10:14:28 2015 +0100
@@ -32,19 +32,34 @@
 #include "toolkit.h"
 #include "../ui/tree.h"
 #include "../common/context.h"
+#include <QTreeView>
 #include <QAbstractTableModel>
+#include <QAbstractItemModel>
+#include <QItemSelectionModel>
 
 class TableModel : public QAbstractTableModel {
     Q_OBJECT
     
+    UiObject    *obj;
     UiListPtr   *list;
     UiModelInfo *info;
+    QTreeView   *view;
 public:
-    TableModel(UiListPtr *list, UiModelInfo *info);
+    TableModel(UiObject *obj, QTreeView *view, UiListPtr *list, UiModelInfo *info);
     
     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(
+        const QItemSelection & selected,
+        const QItemSelection & deselected);
+    void activate(const QModelIndex &);
 };
 
 #endif	/* MODEL_H */
--- a/ui/qt/tree.cpp	Fri Jan 09 14:33:40 2015 +0100
+++ b/ui/qt/tree.cpp	Sat Jan 10 10:14:28 2015 +0100
@@ -29,11 +29,31 @@
 #include "tree.h"
 #include "container.h"
 
+#include <QTreeView>
+#include <QTreeWidgetItem>
+
 UIWIDGET ui_table_var(UiObject *obj, UiListPtr *list, UiModelInfo *modelinfo) {
-    QTableView *view = new QTableView();
-    TableModel *model = new TableModel(list, modelinfo);
+    QTreeView *view = new QTreeView();
+    TableModel *model = new TableModel(obj, view, list, modelinfo);
     view->setModel(model);
     
+    view->setItemsExpandable(false);
+    view->setRootIsDecorated(false);   
+    
+    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;

mercurial