goat3d

view goatview/src/scenemodel.cc @ 86:6d04caf510ab

forgot to add the new source files
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 14 May 2014 05:34:38 +0300
parents
children 7941e89798e5
line source
1 #include <assert.h>
2 #include "scenemodel.h"
4 SceneModel::SceneModel()
5 {
6 scn = 0;
7 }
9 SceneModel::~SceneModel()
10 {
11 clear_scene();
12 }
14 void SceneModel::set_scene(goat3d *scn)
15 {
16 clear_scene();
17 this->scn = scn;
19 if(!scn) return;
21 // create the SceneNodeData for each node
22 int num_nodes = goat3d_get_node_count(scn);
23 for(int i=0; i<num_nodes; i++) {
24 goat3d_node *node = goat3d_get_node(scn, i);
26 SceneNodeData data;
27 data.visible = true;
29 node_data[node] = data;
30 }
31 }
33 void SceneModel::clear_scene()
34 {
35 node_data.clear();
36 scn = 0;
37 }
39 SceneNodeData *SceneModel::get_node_data(goat3d_node *node) const
40 {
41 auto it = node_data.find(node);
42 if(it == node_data.end()) {
43 return 0;
44 }
45 return (SceneNodeData*)&it->second;
46 }
48 Qt::ItemFlags SceneModel::flags(const QModelIndex &index) const
49 {
50 if(!index.isValid()) {
51 return 0;
52 }
54 Qt::ItemFlags res = Qt::ItemIsEnabled | Qt::ItemIsSelectable;
55 if(index.column() == 1) {
56 res |= Qt::ItemIsUserCheckable | Qt::ItemIsEditable;
57 }
58 return res;
59 }
61 QVariant SceneModel::data(const QModelIndex &index, int role) const
62 {
63 if(!index.isValid()) {
64 return QVariant();
65 }
67 goat3d_node *node = (goat3d_node*)index.internalPointer();
68 SceneNodeData *data = get_node_data(node);
70 switch(index.column()) {
71 case 0:
72 if(role == Qt::DisplayRole) {
73 return QString(goat3d_get_node_name(node));
74 }
75 break;
77 case 1:
78 if(role == Qt::CheckStateRole && data) {
79 return data->visible ? Qt::Checked : Qt::Unchecked;
80 }
81 break;
83 default:
84 break;
85 }
87 return QVariant();
88 }
90 bool SceneModel::setData(const QModelIndex &index, const QVariant &value, int role)
91 {
92 if(!index.isValid()) {
93 return false;
94 }
96 goat3d_node *node = (goat3d_node*)index.internalPointer();
97 SceneNodeData *data = get_node_data(node);
98 assert(data);
100 switch(index.column()) {
101 case 1:
102 if(role == Qt::CheckStateRole) {
103 data->visible = value.toBool();
104 emit dataChanged(index, index);
105 return true;
106 }
107 break;
109 default:
110 break;
111 }
112 return false;
113 }
115 QVariant SceneModel::headerData(int section, Qt::Orientation orient, int role) const
116 {
117 if(orient == Qt::Horizontal && role == Qt::DisplayRole) {
118 switch(section) {
119 case 0:
120 return QString("name");
121 case 1:
122 return QString("vis");
123 default:
124 return QString("???");
125 }
126 }
127 return QVariant();
128 }
130 int SceneModel::rowCount(const QModelIndex &parent) const
131 {
132 if(!scn) return 0;
134 if(!parent.isValid()) {
135 // return the number of root nodes
136 int num_nodes = goat3d_get_node_count(scn);
137 int num_root_nodes = 0;
138 for(int i=0; i<num_nodes; i++) {
139 goat3d_node *node = goat3d_get_node(scn, i);
140 if(!goat3d_get_node_parent(node)) {
141 ++num_root_nodes;
142 }
143 }
144 return num_root_nodes;
145 }
147 goat3d_node *pnode = (goat3d_node*)parent.internalPointer();
148 return goat3d_get_node_child_count(pnode);
149 }
151 int SceneModel::columnCount(const QModelIndex &parent) const
152 {
153 return 2;
154 }
156 bool SceneModel::hasChildren(const QModelIndex &parent) const
157 {
158 if(!parent.isValid()) {
159 return true;
160 }
162 goat3d_node *pnode = (goat3d_node*)parent.internalPointer();
163 return goat3d_get_node_child_count(pnode) > 0;
164 }
166 QModelIndex SceneModel::index(int row, int column, const QModelIndex &parent) const
167 {
168 if(!scn) {
169 return QModelIndex();
170 }
172 goat3d_node *node = 0;
174 if(!parent.isValid()) {
175 int num_nodes = goat3d_get_node_count(scn);
176 int idx = 0;
177 for(int i=0; i<num_nodes; i++) {
178 goat3d_node *n = goat3d_get_node(scn, i);
179 if(!goat3d_get_node_parent(n)) {
180 if(idx == row) {
181 node = n;
182 break;
183 }
184 idx++;
185 }
186 }
188 if(idx != row) {
189 return QModelIndex(); // failed
190 }
191 } else {
192 goat3d_node *pnode = (goat3d_node*)parent.internalPointer();
193 node = goat3d_get_node_child(pnode, row);
194 }
196 if(!node) {
197 return QModelIndex();
198 }
199 return createIndex(row, column, (void*)node);
200 }
202 QModelIndex SceneModel::parent(const QModelIndex &index) const
203 {
204 if(!index.isValid()) {
205 return QModelIndex(); // root node
206 }
208 goat3d_node *node = (goat3d_node*)index.internalPointer();
209 goat3d_node *parent = node ? goat3d_get_node_parent(node) : 0;
211 if(!parent) {
212 return QModelIndex();
213 }
215 // find out which child of its parent is our parent
216 int pidx = -1;
218 goat3d_node *grandparent = goat3d_get_node_parent(parent);
219 if(grandparent) {
220 int num_children = goat3d_get_node_child_count(grandparent);
221 for(int i=0; i<num_children; i++) {
222 if(goat3d_get_node_child(grandparent, i) == parent) {
223 pidx = i;
224 break;
225 }
226 }
227 } else {
228 int idx = 0;
229 int num_nodes = goat3d_get_node_count(scn);
230 for(int i=0; i<num_nodes; i++) {
231 goat3d_node *n = goat3d_get_node(scn, i);
232 if(!goat3d_get_node_parent(n)) {
233 if(n == parent) {
234 pidx = idx;
235 break;
236 }
237 idx++;
238 }
239 }
240 }
242 if(pidx == -1) {
243 fprintf(stderr, "%s: wtf?\n", __FUNCTION__);
244 return QModelIndex(); // failed
245 }
247 return createIndex(pidx, 0, (void*)parent);
248 }