ui/qt/model.cpp

changeset 72
a00b46d92c54
parent 69
419c8c3209e8
child 130
212b63dd61be
equal deleted inserted replaced
71:3e021c5f18a0 72:a00b46d92c54
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE. 26 * POSSIBILITY OF SUCH DAMAGE.
27 */ 27 */
28 28
29 #include "model.h" 29 #include "model.h"
30
31 UiListSelection* listSelection(QItemSelectionModel *s) {
32 UiListSelection *selection = new UiListSelection();
33
34 QModelIndexList list = s->selectedRows();
35 selection->count = list.count();
36 if(selection->count > 0) {
37 selection->rows = new int[selection->count];
38 }
39
40 QModelIndex index;
41 int i=0;
42 foreach(index, list) {
43 selection->rows[i] = index.row();
44 i++;
45 }
46 return selection;
47 }
48
49 ListModel::ListModel(UiObject* obj, QListView* view, UiListPtr* list, ui_model_getvalue_f getvalue, ui_callback f, void* userdata) {
50 this->obj = obj;
51 this->view = view;
52 this->list = list;
53 this->getvalue = getvalue;
54 this->callback = f;
55 this->userdata = userdata;
56 }
57
58 int ListModel::rowCount(const QModelIndex& parent) const {
59 return list->list->count(list->list);
60 }
61
62 QVariant ListModel::data(const QModelIndex &index, int role) const {
63 if(role == Qt::DisplayRole) {
64 UiList *ls = list->list;
65 void *rowData = ls->get(ls, index.row());
66 if(rowData && getvalue) {
67 void *value = getvalue(rowData, 0);
68 return QString::fromUtf8((char*)value);
69 }
70 }
71 return QVariant();
72 }
73
74 void ListModel::selectionChanged(const QItemSelection& selected, const QItemSelection& deselected) {
75 UiListSelection *selection = listSelection(view->selectionModel());
76
77 UiEvent e;
78 e.obj = obj;
79 e.window = obj->window;
80 e.document = obj->ctx->document;
81 e.eventdata = selection;
82 e.intval = selection->count > 0 ? selection->rows[0] : -1;
83 callback(&e, userdata);
84
85 if(selection->count > 0) {
86 delete selection->rows;
87 }
88 delete selection;
89 }
30 90
31 TableModel::TableModel(UiObject *obj, QTreeView *view, UiListPtr *list, UiModelInfo *info) { 91 TableModel::TableModel(UiObject *obj, QTreeView *view, UiListPtr *list, UiModelInfo *info) {
32 this->obj = obj; 92 this->obj = obj;
33 this->list = list; 93 this->list = list;
34 this->info = info; 94 this->info = info;
69 return QString::fromUtf8(label); 129 return QString::fromUtf8(label);
70 } 130 }
71 return QVariant(); 131 return QVariant();
72 } 132 }
73 133
74 UiListSelection* TableModel::listSelection() {
75 UiListSelection *selection = new UiListSelection();
76
77 QModelIndexList list = view->selectionModel()->selectedRows();
78 selection->count = list.count();
79 if(selection->count > 0) {
80 selection->rows = new int[selection->count];
81 }
82
83 QModelIndex index;
84 int i=0;
85 foreach(index, list) {
86 selection->rows[i] = index.row();
87 i++;
88 }
89 return selection;
90 }
91
92 void TableModel::selectionChanged(const QItemSelection& selected, const QItemSelection& deselected) { 134 void TableModel::selectionChanged(const QItemSelection& selected, const QItemSelection& deselected) {
93 UiListSelection *selection = listSelection(); 135 UiListSelection *selection = listSelection(view->selectionModel());
94 136
95 UiEvent e; 137 UiEvent e;
96 e.obj = obj; 138 e.obj = obj;
97 e.window = obj->window; 139 e.window = obj->window;
98 e.document = obj->ctx->document; 140 e.document = obj->ctx->document;
105 } 147 }
106 delete selection; 148 delete selection;
107 } 149 }
108 150
109 void TableModel::activate(const QModelIndex &) { 151 void TableModel::activate(const QModelIndex &) {
110 UiListSelection *selection = listSelection(); 152 UiListSelection *selection = listSelection(view->selectionModel());
111 153
112 UiEvent e; 154 UiEvent e;
113 e.obj = obj; 155 e.obj = obj;
114 e.window = obj->window; 156 e.window = obj->window;
115 e.document = obj->ctx->document; 157 e.document = obj->ctx->document;

mercurial