goat3d

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