UNIXworkcode

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 #include <QComboBox> 36 37 extern "C" void* ui_strmodel_getvalue(void *elm, int column) { 38 return column == 0 ? elm : NULL; 39 } 40 41 static void* getvalue_wrapper(UiList *list, void *elm, int row, int col, void *userdata, UiBool *freeResult) { 42 ui_getvaluefunc getvalue = (ui_getvaluefunc)userdata; 43 return getvalue(elm, col); 44 } 45 46 static void* null_getvalue(UiList *list, void *elm, int row, int col, void *userdata, UiBool *freeResult) { 47 return NULL; 48 } 49 50 UIWIDGET ui_listview_create(UiObject *obj, UiListArgs *args) { 51 UiContainerPrivate *ctn = ui_obj_container(obj); 52 53 QListView *view = new QListView(); 54 ui_getvaluefunc2 getvalue = nullptr; 55 void *getvaluedata = nullptr; 56 if(args->getvalue2) { 57 getvalue = args->getvalue2; 58 getvaluedata = args->getvalue2data; 59 } else if(args->getvalue) { 60 getvalue = getvalue_wrapper; 61 getvaluedata = (void*)args->getvalue; 62 } else { 63 getvalue = getvalue_wrapper; 64 getvaluedata = (void*)ui_strmodel_getvalue; 65 } 66 67 UiVar* var = uic_widget_var(obj->ctx, obj->ctx, args->list, args->varname, UI_VAR_LIST); 68 69 ListModel *model = new ListModel(obj, view, var, getvalue, getvaluedata); 70 view->setModel(model); 71 72 if(var) { 73 UiList *list = (UiList*)var->value; 74 list->update = ui_listmodel_update; 75 list->getselection = ui_listmodel_getselection; 76 list->setselection = ui_listmodel_setselection; 77 list->obj = model; 78 } 79 80 model->setActivationCallback(args->onactivate, args->onactivatedata); 81 model->setSelectionCallback(args->onselection, args->onselectiondata); 82 83 QItemSelectionModel *s = view->selectionModel(); 84 QObject::connect( 85 s, 86 SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)), 87 model, 88 SLOT(selectionChanged(const QItemSelection &, const QItemSelection &))); 89 90 UiLayout layout = UI_ARGS2LAYOUT(args); 91 ctn->add(view, layout); 92 93 return view; 94 } 95 96 UIWIDGET ui_table_create(UiObject *obj, UiListArgs *args) { 97 UiContainerPrivate *ctn = ui_obj_container(obj); 98 99 QTreeView *view = new QTreeView(); 100 view->setItemsExpandable(false); 101 view->setRootIsDecorated(false); 102 if(args->multiselection) { 103 view->setSelectionMode(QAbstractItemView::ExtendedSelection); 104 } 105 106 UiVar* var = uic_widget_var(obj->ctx, obj->ctx, args->list, args->varname, UI_VAR_LIST); 107 108 ui_getvaluefunc2 getvalue = args->getvalue2; 109 void *getvaluedata = args->getvalue2data; 110 if(!getvalue) { 111 if(args->getvalue) { 112 getvalue = getvalue_wrapper; 113 getvaluedata = (void*)args->getvalue; 114 } else { 115 getvalue = null_getvalue; 116 } 117 } 118 TableModel *model = new TableModel(obj, view, var, args->model, getvalue, getvaluedata); 119 view->setModel(model); 120 121 if(var) { 122 UiList *list = (UiList*)var->value; 123 list->update = ui_listmodel_update; 124 list->getselection = ui_listmodel_getselection; 125 list->setselection = ui_listmodel_setselection; 126 list->obj = model; 127 } 128 129 model->setActivationCallback(args->onactivate, args->onactivatedata); 130 model->setSelectionCallback(args->onselection, args->onselectiondata); 131 132 QItemSelectionModel *s = view->selectionModel(); 133 QObject::connect( 134 s, 135 SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)), 136 model, 137 SLOT(selectionChanged(const QItemSelection &, const QItemSelection &))); 138 139 140 UiLayout layout = UI_ARGS2LAYOUT(args); 141 ctn->add(view, layout); 142 143 return view; 144 } 145 146 UIWIDGET ui_dropdown_create(UiObject *obj, UiListArgs *args) { 147 UiContainerPrivate *ctn = ui_obj_container(obj); 148 149 QComboBox *view = new QComboBox(); 150 UiLayout layout = UI_ARGS2LAYOUT(args); 151 ctn->add(view, layout); 152 153 ui_getvaluefunc2 getvalue = nullptr; 154 void *getvaluedata = nullptr; 155 if(args->getvalue2) { 156 getvalue = args->getvalue2; 157 getvaluedata = args->getvalue2data; 158 } else if(args->getvalue) { 159 getvalue = getvalue_wrapper; 160 getvaluedata = (void*)args->getvalue; 161 } else { 162 getvalue = getvalue_wrapper; 163 getvaluedata = (void*)ui_strmodel_getvalue; 164 } 165 166 UiVar* var = uic_widget_var(obj->ctx, obj->ctx, args->list, args->varname, UI_VAR_LIST); 167 168 ListModel *model = new ListModel(obj, view, var, getvalue, getvaluedata); 169 view->setModel(model); 170 171 if(var) { 172 UiList *list = (UiList*)var->value; 173 list->update = ui_listmodel_update; 174 list->getselection = ui_listmodel_getselection; 175 list->setselection = ui_listmodel_setselection; 176 list->obj = model; 177 } 178 179 model->setActivationCallback(args->onactivate, args->onactivatedata); 180 model->setSelectionCallback(args->onselection, args->onselectiondata); 181 182 return view; 183 } 184