ui/qt/list.cpp

changeset 108
77254bd6dccb
child 109
c3dfcb8f0be7
equal deleted inserted replaced
107:b34bd1557c6c 108:77254bd6dccb
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 "list.h"
30 #include "container.h"
31
32 #include <QTreeView>
33 #include <QTreeWidgetItem>
34 #include <QListView>
35
36 extern "C" void* ui_strmodel_getvalue(void *elm, int column) {
37 return column == 0 ? elm : NULL;
38 }
39
40 static void* getvalue_wrapper(UiList *list, void *elm, int row, int col, void *userdata, UiBool *freeResult) {
41 ui_getvaluefunc getvalue = (ui_getvaluefunc)userdata;
42 return getvalue(elm, col);
43 }
44
45 UIWIDGET ui_listview_create(UiObject* obj, UiListArgs *args) {
46 UiContainerPrivate *ctn = ui_obj_container(obj);
47 UI_APPLY_LAYOUT(ctn->layout, args);
48
49 QListView *view = new QListView();
50 ui_getvaluefunc2 getvalue = nullptr;
51 void *getvaluedata = nullptr;
52 if(args->getvalue2) {
53 getvalue = args->getvalue2;
54 getvaluedata = args->getvalue2data;
55 } else if(args->getvalue) {
56 getvalue = getvalue_wrapper;
57 getvaluedata = (void*)args->getvalue;
58 } else {
59 getvalue = getvalue_wrapper;
60 getvaluedata = (void*)ui_strmodel_getvalue;
61 }
62
63 UiVar* var = uic_widget_var(obj->ctx, obj->ctx, args->list, args->varname, UI_VAR_LIST);
64
65 ListModel *model = new ListModel(obj, view, var, getvalue, getvaluedata);
66 view->setModel(model);
67
68 if(var) {
69 UiList *list = (UiList*)var->value;
70 list->update = ui_listmodel_update;
71 list->getselection = ui_listmodel_getselection;
72 list->setselection = ui_listmodel_setselection;
73 list->obj = model;
74 }
75
76 model->setActivationCallback(args->onactivate, args->onactivatedata);
77 model->setSelectionCallback(args->onselection, args->onselectiondata);
78
79 QItemSelectionModel *s = view->selectionModel();
80 QObject::connect(
81 s,
82 SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)),
83 model,
84 SLOT(selectionChanged(const QItemSelection &, const QItemSelection &)));
85
86
87 ctn->add(view);
88
89 return view;
90 }
91
92 UIWIDGET ui_table_create(UiObject* obj, UiListArgs *args) {
93 UiContainerPrivate *ctn = ui_obj_container(obj);
94 UI_APPLY_LAYOUT(ctn->layout, args);
95
96 QTreeView *view = new QTreeView();
97 view->setItemsExpandable(false);
98 view->setRootIsDecorated(false);
99 if(args->multiselection) {
100 view->setSelectionMode(QAbstractItemView::ExtendedSelection);
101 }
102
103 UiVar* var = uic_widget_var(obj->ctx, obj->ctx, args->list, args->varname, UI_VAR_LIST);
104
105 TableModel *model = new TableModel(obj, view, var, args->model);
106 view->setModel(model);
107
108 if(var) {
109 UiList *list = (UiList*)var->value;
110 list->update = ui_listmodel_update;
111 list->getselection = ui_listmodel_getselection;
112 list->setselection = ui_listmodel_setselection;
113 list->obj = model;
114 }
115
116 model->setActivationCallback(args->onactivate, args->onactivatedata);
117 model->setSelectionCallback(args->onselection, args->onselectiondata);
118
119 QItemSelectionModel *s = view->selectionModel();
120 QObject::connect(
121 s,
122 SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)),
123 model,
124 SLOT(selectionChanged(const QItemSelection &, const QItemSelection &)));
125
126
127 ctn->add(view);
128
129 return view;
130 }

mercurial