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 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 static void* null_getvalue(UiList *list, void *elm, int row, int col, void *userdata, UiBool *freeResult) { 46 return NULL; 47 } 48 49 UIWIDGET ui_listview_create(UiObject* obj, UiListArgs *args) { 50 UiContainerPrivate *ctn = ui_obj_container(obj); 51 UI_APPLY_LAYOUT(ctn->layout, args); 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 91 ctn->add(view); 92 93 return view; 94 } 95 96 UIWIDGET ui_table_create(UiObject* obj, UiListArgs *args) { 97 UiContainerPrivate *ctn = ui_obj_container(obj); 98 UI_APPLY_LAYOUT(ctn->layout, args); 99 100 QTreeView *view = new QTreeView(); 101 view->setItemsExpandable(false); 102 view->setRootIsDecorated(false); 103 if(args->multiselection) { 104 view->setSelectionMode(QAbstractItemView::ExtendedSelection); 105 } 106 107 UiVar* var = uic_widget_var(obj->ctx, obj->ctx, args->list, args->varname, UI_VAR_LIST); 108 109 ui_getvaluefunc2 getvalue = args->getvalue2; 110 void *getvaluedata = args->getvalue2data; 111 if(!getvalue) { 112 if(args->getvalue) { 113 getvalue = getvalue_wrapper; 114 getvaluedata = (void*)args->getvalue; 115 } else { 116 getvalue = null_getvalue; 117 } 118 } 119 TableModel *model = new TableModel(obj, view, var, args->model, getvalue, getvaluedata); 120 view->setModel(model); 121 122 if(var) { 123 UiList *list = (UiList*)var->value; 124 list->update = ui_listmodel_update; 125 list->getselection = ui_listmodel_getselection; 126 list->setselection = ui_listmodel_setselection; 127 list->obj = model; 128 } 129 130 model->setActivationCallback(args->onactivate, args->onactivatedata); 131 model->setSelectionCallback(args->onselection, args->onselectiondata); 132 133 QItemSelectionModel *s = view->selectionModel(); 134 QObject::connect( 135 s, 136 SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)), 137 model, 138 SLOT(selectionChanged(const QItemSelection &, const QItemSelection &))); 139 140 141 ctn->add(view); 142 143 return view; 144 } 145