ui/qt/model.cpp

changeset 579
810b848e2e4f
parent 577
c2e854820922
child 581
7486347f73cf
equal deleted inserted replaced
578:950d2e38957b 579:810b848e2e4f
27 */ 27 */
28 28
29 #include "model.h" 29 #include "model.h"
30 30
31 31
32 ListModel::ListModel(UiObject *obj, QListView *view, UiVar *var, ui_getvaluefunc getvalue, ui_callback f, void *userdata){ 32 ListModel::ListModel(UiObject *obj, QListView *view, UiVar *var, ui_getvaluefunc getvalue){
33 this->obj = obj; 33 this->obj = obj;
34 this->view = view; 34 this->view = view;
35 this->var = var; 35 this->var = var;
36 this->getvalue = getvalue; 36 this->getvalue = getvalue;
37 this->callback = f; 37 this->onactivate = nullptr;
38 this->userdata = userdata; 38 this->onactivatedata = nullptr;
39 this->onselection = nullptr;
40 this->onselectiondata = nullptr;
41 }
42
43 void ListModel::setActivationCallback(ui_callback f, void *userdata) {
44 onactivate = f;
45 onactivatedata = userdata;
46 }
47
48 void ListModel::setSelectionCallback(ui_callback f, void *userdata) {
49 onselection = f;
50 onselectiondata = userdata;
51 }
52
53 void ListModel::update(int row) {
54 if(row >= 0) {
55 this->update(row);
56 } else {
57 this->beginResetModel();
58 this->endResetModel();
59 }
39 } 60 }
40 61
41 int ListModel::rowCount(const QModelIndex& parent) const { 62 int ListModel::rowCount(const QModelIndex& parent) const {
42 UiList *list = (UiList*)var->value; 63 UiList *list = (UiList*)var->value;
43 return ui_list_count(list); 64 return ui_list_count(list);
54 } 75 }
55 return QVariant(); 76 return QVariant();
56 } 77 }
57 78
58 void ListModel::selectionChanged(const QItemSelection& selected, const QItemSelection& deselected) { 79 void ListModel::selectionChanged(const QItemSelection& selected, const QItemSelection& deselected) {
80 UiListSelection sel = ui_selection_model_to_selection(view->selectionModel());
59 81
82 UiEvent event;
83 event.obj = obj;
84 event.window = obj->window;
85 event.document = obj->ctx->document;
86 event.eventdata = &sel;
87 event.intval = sel.count;
88 event.set = ui_get_setop();
89
90 if(onactivate) {
91 onactivate(&event, onactivatedata);
92 }
93 if(onselection) {
94 onselection(&event, onselectiondata);
95 }
96
97 free(sel.rows);
60 } 98 }
61 99
62 100
101
102
103 UiListSelection ui_selection_model_to_selection(QItemSelectionModel *model) {
104 UiListSelection sel;
105 sel.rows = NULL;
106 sel.count = 0;
107
108 if(model->hasSelection()) {
109 QModelIndexList indices = model->selectedIndexes();
110 sel.count = indices.count();
111 sel.rows = (int*)calloc(sel.count, sizeof(int));
112
113 int i = 0;
114 for (const QModelIndex &index : indices) {
115 sel.rows[i++] = index.row();
116 }
117 }
118
119 return sel;
120 }
121
122 /* ---------------------- UiList implementation -----------------------------*/
123
124 void ui_listmodel_update(UiList *list, int row) {
125 ListModel *model = (ListModel*)list->obj;
126 model->update(row);
127 }
128
129 void ui_listmodel_setselection(UiList *list, UiListSelection sel) {
130 ListModel *model = (ListModel*)list->obj;
131 QItemSelection selection;
132 for (int i=0;i<sel.count;i++) {
133 QModelIndex index = model->index(sel.rows[i]);
134 selection.select(index, index);
135 }
136 model->view->selectionModel()->select(selection, QItemSelectionModel::ClearAndSelect);
137 }
138
139 UiListSelection ui_listmodel_getselection(UiList *list) {
140 ListModel *model = (ListModel*)list->obj;
141 return ui_selection_model_to_selection(model->view->selectionModel());
142 }

mercurial