goat3d

changeset 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 91e3aa1a60c3
children f0277d348c2c
files goatview/src/scenemodel.cc goatview/src/scenemodel.h
diffstat 2 files changed, 287 insertions(+), 0 deletions(-) [+]
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/goatview/src/scenemodel.cc	Wed May 14 05:34:38 2014 +0300
     1.3 @@ -0,0 +1,248 @@
     1.4 +#include <assert.h>
     1.5 +#include "scenemodel.h"
     1.6 +
     1.7 +SceneModel::SceneModel()
     1.8 +{
     1.9 +	scn = 0;
    1.10 +}
    1.11 +
    1.12 +SceneModel::~SceneModel()
    1.13 +{
    1.14 +	clear_scene();
    1.15 +}
    1.16 +
    1.17 +void SceneModel::set_scene(goat3d *scn)
    1.18 +{
    1.19 +	clear_scene();
    1.20 +	this->scn = scn;
    1.21 +
    1.22 +	if(!scn) return;
    1.23 +
    1.24 +	// create the SceneNodeData for each node
    1.25 +	int num_nodes = goat3d_get_node_count(scn);
    1.26 +	for(int i=0; i<num_nodes; i++) {
    1.27 +		goat3d_node *node = goat3d_get_node(scn, i);
    1.28 +
    1.29 +		SceneNodeData data;
    1.30 +		data.visible = true;
    1.31 +
    1.32 +		node_data[node] = data;
    1.33 +	}
    1.34 +}
    1.35 +
    1.36 +void SceneModel::clear_scene()
    1.37 +{
    1.38 +	node_data.clear();
    1.39 +	scn = 0;
    1.40 +}
    1.41 +
    1.42 +SceneNodeData *SceneModel::get_node_data(goat3d_node *node) const
    1.43 +{
    1.44 +	auto it = node_data.find(node);
    1.45 +	if(it == node_data.end()) {
    1.46 +		return 0;
    1.47 +	}
    1.48 +	return (SceneNodeData*)&it->second;
    1.49 +}
    1.50 +
    1.51 +Qt::ItemFlags SceneModel::flags(const QModelIndex &index) const
    1.52 +{
    1.53 +	if(!index.isValid()) {
    1.54 +		return 0;
    1.55 +	}
    1.56 +
    1.57 +	Qt::ItemFlags res = Qt::ItemIsEnabled | Qt::ItemIsSelectable;
    1.58 +	if(index.column() == 1) {
    1.59 +		res |= Qt::ItemIsUserCheckable | Qt::ItemIsEditable;
    1.60 +	}
    1.61 +	return res;
    1.62 +}
    1.63 +
    1.64 +QVariant SceneModel::data(const QModelIndex &index, int role) const
    1.65 +{
    1.66 +	if(!index.isValid()) {
    1.67 +		return QVariant();
    1.68 +	}
    1.69 +
    1.70 +	goat3d_node *node = (goat3d_node*)index.internalPointer();
    1.71 +	SceneNodeData *data = get_node_data(node);
    1.72 +
    1.73 +	switch(index.column()) {
    1.74 +	case 0:
    1.75 +		if(role == Qt::DisplayRole) {
    1.76 +			return QString(goat3d_get_node_name(node));
    1.77 +		}
    1.78 +		break;
    1.79 +
    1.80 +	case 1:
    1.81 +		if(role == Qt::CheckStateRole && data) {
    1.82 +			return data->visible ? Qt::Checked : Qt::Unchecked;
    1.83 +		}
    1.84 +		break;
    1.85 +
    1.86 +	default:
    1.87 +		break;
    1.88 +	}
    1.89 +
    1.90 +	return QVariant();
    1.91 +}
    1.92 +
    1.93 +bool SceneModel::setData(const QModelIndex &index, const QVariant &value, int role)
    1.94 +{
    1.95 +	if(!index.isValid()) {
    1.96 +		return false;
    1.97 +	}
    1.98 +
    1.99 +	goat3d_node *node = (goat3d_node*)index.internalPointer();
   1.100 +	SceneNodeData *data = get_node_data(node);
   1.101 +	assert(data);
   1.102 +
   1.103 +	switch(index.column()) {
   1.104 +	case 1:
   1.105 +		if(role == Qt::CheckStateRole) {
   1.106 +			data->visible = value.toBool();
   1.107 +			emit dataChanged(index, index);
   1.108 +			return true;
   1.109 +		}
   1.110 +		break;
   1.111 +
   1.112 +	default:
   1.113 +		break;
   1.114 +	}
   1.115 +	return false;
   1.116 +}
   1.117 +
   1.118 +QVariant SceneModel::headerData(int section, Qt::Orientation orient, int role) const
   1.119 +{
   1.120 +	if(orient == Qt::Horizontal && role == Qt::DisplayRole) {
   1.121 +		switch(section) {
   1.122 +		case 0:
   1.123 +			return QString("name");
   1.124 +		case 1:
   1.125 +			return QString("vis");
   1.126 +		default:
   1.127 +			return QString("???");
   1.128 +		}
   1.129 +	}
   1.130 +	return QVariant();
   1.131 +}
   1.132 +
   1.133 +int SceneModel::rowCount(const QModelIndex &parent) const
   1.134 +{
   1.135 +	if(!scn) return 0;
   1.136 +
   1.137 +	if(!parent.isValid()) {
   1.138 +		// return the number of root nodes
   1.139 +		int num_nodes = goat3d_get_node_count(scn);
   1.140 +		int num_root_nodes = 0;
   1.141 +		for(int i=0; i<num_nodes; i++) {
   1.142 +			goat3d_node *node = goat3d_get_node(scn, i);
   1.143 +			if(!goat3d_get_node_parent(node)) {
   1.144 +				++num_root_nodes;
   1.145 +			}
   1.146 +		}
   1.147 +		return num_root_nodes;
   1.148 +	}
   1.149 +
   1.150 +	goat3d_node *pnode = (goat3d_node*)parent.internalPointer();
   1.151 +	return goat3d_get_node_child_count(pnode);
   1.152 +}
   1.153 +
   1.154 +int SceneModel::columnCount(const QModelIndex &parent) const
   1.155 +{
   1.156 +	return 2;
   1.157 +}
   1.158 +
   1.159 +bool SceneModel::hasChildren(const QModelIndex &parent) const
   1.160 +{
   1.161 +	if(!parent.isValid()) {
   1.162 +		return true;
   1.163 +	}
   1.164 +
   1.165 +	goat3d_node *pnode = (goat3d_node*)parent.internalPointer();
   1.166 +	return goat3d_get_node_child_count(pnode) > 0;
   1.167 +}
   1.168 +
   1.169 +QModelIndex SceneModel::index(int row, int column, const QModelIndex &parent) const
   1.170 +{
   1.171 +	if(!scn) {
   1.172 +		return QModelIndex();
   1.173 +	}
   1.174 +
   1.175 +	goat3d_node *node = 0;
   1.176 +
   1.177 +	if(!parent.isValid()) {
   1.178 +		int num_nodes = goat3d_get_node_count(scn);
   1.179 +		int idx = 0;
   1.180 +		for(int i=0; i<num_nodes; i++) {
   1.181 +			goat3d_node *n = goat3d_get_node(scn, i);
   1.182 +			if(!goat3d_get_node_parent(n)) {
   1.183 +				if(idx == row) {
   1.184 +					node = n;
   1.185 +					break;
   1.186 +				}
   1.187 +				idx++;
   1.188 +			}
   1.189 +		}
   1.190 +
   1.191 +		if(idx != row) {
   1.192 +			return QModelIndex();	// failed
   1.193 +		}
   1.194 +	} else {
   1.195 +		goat3d_node *pnode = (goat3d_node*)parent.internalPointer();
   1.196 +		node = goat3d_get_node_child(pnode, row);
   1.197 +	}
   1.198 +
   1.199 +	if(!node) {
   1.200 +		return QModelIndex();
   1.201 +	}
   1.202 +	return createIndex(row, column, (void*)node);
   1.203 +}
   1.204 +
   1.205 +QModelIndex SceneModel::parent(const QModelIndex &index) const
   1.206 +{
   1.207 +	if(!index.isValid()) {
   1.208 +		return QModelIndex();	// root node
   1.209 +	}
   1.210 +
   1.211 +	goat3d_node *node = (goat3d_node*)index.internalPointer();
   1.212 +	goat3d_node *parent = node ? goat3d_get_node_parent(node) : 0;
   1.213 +
   1.214 +	if(!parent) {
   1.215 +		return QModelIndex();
   1.216 +	}
   1.217 +
   1.218 +	// find out which child of its parent is our parent
   1.219 +	int pidx = -1;
   1.220 +
   1.221 +	goat3d_node *grandparent = goat3d_get_node_parent(parent);
   1.222 +	if(grandparent) {
   1.223 +		int num_children = goat3d_get_node_child_count(grandparent);
   1.224 +		for(int i=0; i<num_children; i++) {
   1.225 +			if(goat3d_get_node_child(grandparent, i) == parent) {
   1.226 +				pidx = i;
   1.227 +				break;
   1.228 +			}
   1.229 +		}
   1.230 +	} else {
   1.231 +		int idx = 0;
   1.232 +		int num_nodes = goat3d_get_node_count(scn);
   1.233 +		for(int i=0; i<num_nodes; i++) {
   1.234 +			goat3d_node *n = goat3d_get_node(scn, i);
   1.235 +			if(!goat3d_get_node_parent(n)) {
   1.236 +				if(n == parent) {
   1.237 +					pidx = idx;
   1.238 +					break;
   1.239 +				}
   1.240 +				idx++;
   1.241 +			}
   1.242 +		}
   1.243 +	}
   1.244 +
   1.245 +	if(pidx == -1) {
   1.246 +		fprintf(stderr, "%s: wtf?\n", __FUNCTION__);
   1.247 +		return QModelIndex();	// failed
   1.248 +	}
   1.249 +
   1.250 +	return createIndex(pidx, 0, (void*)parent);
   1.251 +}
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/goatview/src/scenemodel.h	Wed May 14 05:34:38 2014 +0300
     2.3 @@ -0,0 +1,39 @@
     2.4 +#ifndef SCENEMODEL_H_
     2.5 +#define SCENEMODEL_H_
     2.6 +
     2.7 +#include <map>
     2.8 +#include <QtCore/QAbstractItemModel>
     2.9 +#include "goat3d.h"
    2.10 +
    2.11 +struct SceneNodeData {
    2.12 +	bool visible;
    2.13 +};
    2.14 +
    2.15 +class SceneModel : public QAbstractItemModel {
    2.16 +private:
    2.17 +	Q_OBJECT
    2.18 +
    2.19 +	goat3d *scn;
    2.20 +	std::map<goat3d_node*, SceneNodeData> node_data;
    2.21 +
    2.22 +public:
    2.23 +	SceneModel();
    2.24 +	~SceneModel();
    2.25 +
    2.26 +	void set_scene(goat3d *scn);
    2.27 +	void clear_scene();
    2.28 +
    2.29 +	SceneNodeData *get_node_data(goat3d_node *node) const;
    2.30 +
    2.31 +	Qt::ItemFlags flags(const QModelIndex &index) const;
    2.32 +	QVariant data(const QModelIndex &index, int role) const;
    2.33 +	bool setData(const QModelIndex &index, const QVariant &value, int role);
    2.34 +	QVariant headerData(int section, Qt::Orientation orient, int role) const;
    2.35 +	int rowCount(const QModelIndex &parent) const;
    2.36 +	int columnCount(const QModelIndex &parent) const;
    2.37 +	bool hasChildren(const QModelIndex &parent) const;
    2.38 +	QModelIndex index(int row, int column, const QModelIndex &parent) const;
    2.39 +	QModelIndex parent(const QModelIndex &index) const;
    2.40 +};
    2.41 +
    2.42 +#endif	// SCENEMODEL_H_