ui/qt/tree.cpp

changeset 0
804d8803eade
equal deleted inserted replaced
-1:000000000000 0:804d8803eade
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2014 Olaf Wintermann. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include "tree.h"
30 #include "container.h"
31
32 #include <QTreeView>
33 #include <QTreeWidgetItem>
34 #include <QListView>
35
36
37 extern "C" void* ui_strmodel_getvalue(void *elm, int column) {
38 return column == 0 ? elm : NULL;
39 }
40
41 UIWIDGET ui_listview_str(UiObject *obj, UiList *list, ui_callback f, void *udata) {
42 return ui_listview(obj, list, ui_strmodel_getvalue, f, udata);
43 }
44 UIWIDGET ui_listview_var(UiObject *obj, UiListPtr *list, ui_model_getvalue_f getvalue, ui_callback f, void *udata) {
45 QListView *view = new QListView();
46 ListModel *model = new ListModel(obj, view, list, getvalue, f, udata);
47 view->setModel(model);
48
49 // TODO: observer update
50
51 QItemSelectionModel *s = view->selectionModel();
52 QObject::connect(
53 s,
54 SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)),
55 model,
56 SLOT(selectionChanged(const QItemSelection &, const QItemSelection &)));
57
58 UiContainer *ct = uic_get_current_container(obj);
59 ct->add(view, true);
60 return view;
61 }
62
63 UIWIDGET ui_listview(UiObject *obj, UiList *list, ui_model_getvalue_f getvalue, ui_callback f, void *udata) {
64 UiListPtr *listptr = (UiListPtr*)ucx_mempool_malloc(obj->ctx->mempool, sizeof(UiListPtr));
65 listptr->list = list;
66 return ui_listview_var(obj, listptr, getvalue, f, udata);
67 }
68
69 UIWIDGET ui_listview_nv(UiObject *obj, char *varname, ui_model_getvalue_f getvalue, ui_callback f, void *udata) {
70 UiVar *var = uic_connect_var(obj->ctx, varname, UI_VAR_LIST);
71 if(var) {
72 UiListVar *value = (UiListVar*)var->value;
73 return ui_listview_var(obj, value->listptr, getvalue, f, udata);
74 } else {
75 // TODO: error
76 }
77 return NULL;
78 }
79
80
81 UIWIDGET ui_table_var(UiObject *obj, UiListPtr *list, UiModelInfo *modelinfo) {
82 QTreeView *view = new QTreeView();
83 TableModel *model = new TableModel(obj, view, list, modelinfo);
84 view->setModel(model);
85
86 view->setItemsExpandable(false);
87 view->setRootIsDecorated(false);
88
89 // TODO: observer update
90 UiTableView *u = new UiTableView();
91 u->widget = view;
92 u->model = model;
93 list->list->observers = ui_add_observer(
94 list->list->observers,
95 (ui_callback)ui_table_update,
96 u);
97
98 view->setSelectionMode(QAbstractItemView::ExtendedSelection);
99 QItemSelectionModel *s = view->selectionModel();
100 QObject::connect(
101 s,
102 SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)),
103 model,
104 SLOT(selectionChanged(const QItemSelection &, const QItemSelection &)));
105 QObject::connect(
106 view,
107 SIGNAL(doubleClicked(const QModelIndex &)),
108 model,
109 SLOT(activate(const QModelIndex &)));
110
111
112 UiContainer *ct = uic_get_current_container(obj);
113 ct->add(view, true);
114 return view;
115 }
116
117 void ui_table_update(UiEvent *event, UiTableView *view) {
118 // TODO
119 printf("update\n");
120
121 //view->model->update();
122 view->widget->setModel(NULL);
123 view->widget->setModel(view->model);
124 }
125
126 UIWIDGET ui_table(UiObject *obj, UiList *list, UiModelInfo *modelinfo) {
127 UiListPtr *listptr = (UiListPtr*)ucx_mempool_malloc(obj->ctx->mempool, sizeof(UiListPtr));
128 listptr->list = list;
129 return ui_table_var(obj, listptr, modelinfo);
130 }
131
132 UIWIDGET ui_table_nv(UiObject *obj, char *varname, UiModelInfo *modelinfo) {
133 UiVar *var = uic_connect_var(obj->ctx, varname, UI_VAR_LIST);
134 if(var) {
135 UiListVar *value = (UiListVar*)var->value;
136 return ui_table_var(obj, value->listptr, modelinfo);
137 } else {
138 // TODO: error
139 }
140 return NULL;
141 }
142

mercurial