26 * POSSIBILITY OF SUCH DAMAGE. |
26 * POSSIBILITY OF SUCH DAMAGE. |
27 */ |
27 */ |
28 |
28 |
29 #include "model.h" |
29 #include "model.h" |
30 |
30 |
31 TableModel::TableModel(UiListPtr *list, UiModelInfo *info) { |
31 TableModel::TableModel(UiObject *obj, QTreeView *view, UiListPtr *list, UiModelInfo *info) { |
|
32 this->obj = obj; |
32 this->list = list; |
33 this->list = list; |
33 this->info = info; |
34 this->info = info; |
|
35 this->view = view; |
34 } |
36 } |
35 |
37 |
36 int TableModel::rowCount(const QModelIndex &parent) const { |
38 int TableModel::rowCount(const QModelIndex &parent) const { |
37 return list->list->count(list->list); |
39 return list->list->count(list->list); |
38 } |
40 } |
59 } |
61 } |
60 } |
62 } |
61 return QVariant(); |
63 return QVariant(); |
62 } |
64 } |
63 |
65 |
|
66 QVariant TableModel::headerData(int section, Qt::Orientation orientation, int role) const { |
|
67 if(role == Qt::DisplayRole) { |
|
68 char *label = info->titles[section]; |
|
69 return QString::fromUtf8(label); |
|
70 } |
|
71 return QVariant(); |
|
72 } |
64 |
73 |
|
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) { |
|
93 UiListSelection *selection = listSelection(); |
|
94 |
|
95 UiEvent e; |
|
96 e.obj = obj; |
|
97 e.window = obj->window; |
|
98 e.document = obj->ctx->document; |
|
99 e.eventdata = selection; |
|
100 e.intval = selection->count > 0 ? selection->rows[0] : -1; |
|
101 info->selection(&e, info->userdata); |
|
102 |
|
103 if(selection->count > 0) { |
|
104 delete selection->rows; |
|
105 } |
|
106 delete selection; |
|
107 } |
|
108 |
|
109 void TableModel::activate(const QModelIndex &) { |
|
110 UiListSelection *selection = listSelection(); |
|
111 |
|
112 UiEvent e; |
|
113 e.obj = obj; |
|
114 e.window = obj->window; |
|
115 e.document = obj->ctx->document; |
|
116 e.eventdata = selection; |
|
117 e.intval = selection->count > 0 ? selection->rows[0] : -1; |
|
118 info->activate(&e, info->userdata); |
|
119 |
|
120 if(selection->count > 0) { |
|
121 delete selection->rows; |
|
122 } |
|
123 delete selection; |
|
124 } |