# HG changeset patch # User Olaf Wintermann # Date 1420881268 -3600 # Node ID 419c8c3209e8f81867d6c0106f09087fac453fdc # Parent bd9fb6476b80fae26bcf2cd217691b7714eb7bc9 improved table (Qt) diff -r bd9fb6476b80 -r 419c8c3209e8 application/main.c --- 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(); diff -r bd9fb6476b80 -r 419c8c3209e8 ui/gtk/tree.c --- 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); } diff -r bd9fb6476b80 -r 419c8c3209e8 ui/qt/model.cpp --- 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; +} diff -r bd9fb6476b80 -r 419c8c3209e8 ui/qt/model.h --- 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 #include +#include +#include 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 */ diff -r bd9fb6476b80 -r 419c8c3209e8 ui/qt/tree.cpp --- 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 +#include + 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;