ui/qt/model.cpp

changeset 581
7486347f73cf
parent 579
810b848e2e4f
child 582
6c86efe60b37
equal deleted inserted replaced
580:9b8d61227169 581:7486347f73cf
97 free(sel.rows); 97 free(sel.rows);
98 } 98 }
99 99
100 100
101 101
102 TableModel::TableModel(UiObject *obj, QTreeView *view, UiVar *var, ui_getvaluefunc getvalue){
103 this->obj = obj;
104 this->view = view;
105 this->var = var;
106 this->getvalue = getvalue;
107 this->onactivate = nullptr;
108 this->onactivatedata = nullptr;
109 this->onselection = nullptr;
110 this->onselectiondata = nullptr;
111 }
112
113 void TableModel::setActivationCallback(ui_callback f, void *userdata) {
114 onactivate = f;
115 onactivatedata = userdata;
116 }
117
118 void TableModel::setSelectionCallback(ui_callback f, void *userdata) {
119 onselection = f;
120 onselectiondata = userdata;
121 }
122
123 void TableModel::update(int row) {
124 if(row >= 0) {
125 this->update(row);
126 } else {
127 this->beginResetModel();
128 this->endResetModel();
129 }
130 }
131
132 int TableModel::rowCount(const QModelIndex& parent) const {
133 UiList *list = (UiList*)var->value;
134 return ui_list_count(list);
135 }
136
137 QVariant TableModel::data(const QModelIndex &index, int role) const {
138 if(role == Qt::DisplayRole) {
139 UiList *ls = (UiList*)var->value;
140 void *rowData = ls->get(ls, index.row());
141 if(rowData && getvalue) {
142 void *value = getvalue(rowData, 0);
143 return QString::fromUtf8((char*)value);
144 }
145 }
146 return QVariant();
147 }
148
149 void TableModel::selectionChanged(const QItemSelection& selected, const QItemSelection& deselected) {
150 UiListSelection sel = ui_selection_model_to_selection(view->selectionModel());
151
152 UiEvent event;
153 event.obj = obj;
154 event.window = obj->window;
155 event.document = obj->ctx->document;
156 event.eventdata = &sel;
157 event.intval = sel.count;
158 event.set = ui_get_setop();
159
160 if(onactivate) {
161 onactivate(&event, onactivatedata);
162 }
163 if(onselection) {
164 onselection(&event, onselectiondata);
165 }
166
167 free(sel.rows);
168 }
169
170
102 171
103 UiListSelection ui_selection_model_to_selection(QItemSelectionModel *model) { 172 UiListSelection ui_selection_model_to_selection(QItemSelectionModel *model) {
104 UiListSelection sel; 173 UiListSelection sel;
105 sel.rows = NULL; 174 sel.rows = NULL;
106 sel.count = 0; 175 sel.count = 0;

mercurial