# HG changeset patch # User John Tsiombikas # Date 1400034878 -10800 # Node ID 6d04caf510abc779adb39704a2e4090a154e837a # Parent 91e3aa1a60c39774c8db46a66b4e8ba0dfa61776 forgot to add the new source files diff -r 91e3aa1a60c3 -r 6d04caf510ab goatview/src/scenemodel.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/goatview/src/scenemodel.cc Wed May 14 05:34:38 2014 +0300 @@ -0,0 +1,248 @@ +#include +#include "scenemodel.h" + +SceneModel::SceneModel() +{ + scn = 0; +} + +SceneModel::~SceneModel() +{ + clear_scene(); +} + +void SceneModel::set_scene(goat3d *scn) +{ + clear_scene(); + this->scn = scn; + + if(!scn) return; + + // create the SceneNodeData for each node + int num_nodes = goat3d_get_node_count(scn); + for(int i=0; isecond; +} + +Qt::ItemFlags SceneModel::flags(const QModelIndex &index) const +{ + if(!index.isValid()) { + return 0; + } + + Qt::ItemFlags res = Qt::ItemIsEnabled | Qt::ItemIsSelectable; + if(index.column() == 1) { + res |= Qt::ItemIsUserCheckable | Qt::ItemIsEditable; + } + return res; +} + +QVariant SceneModel::data(const QModelIndex &index, int role) const +{ + if(!index.isValid()) { + return QVariant(); + } + + goat3d_node *node = (goat3d_node*)index.internalPointer(); + SceneNodeData *data = get_node_data(node); + + switch(index.column()) { + case 0: + if(role == Qt::DisplayRole) { + return QString(goat3d_get_node_name(node)); + } + break; + + case 1: + if(role == Qt::CheckStateRole && data) { + return data->visible ? Qt::Checked : Qt::Unchecked; + } + break; + + default: + break; + } + + return QVariant(); +} + +bool SceneModel::setData(const QModelIndex &index, const QVariant &value, int role) +{ + if(!index.isValid()) { + return false; + } + + goat3d_node *node = (goat3d_node*)index.internalPointer(); + SceneNodeData *data = get_node_data(node); + assert(data); + + switch(index.column()) { + case 1: + if(role == Qt::CheckStateRole) { + data->visible = value.toBool(); + emit dataChanged(index, index); + return true; + } + break; + + default: + break; + } + return false; +} + +QVariant SceneModel::headerData(int section, Qt::Orientation orient, int role) const +{ + if(orient == Qt::Horizontal && role == Qt::DisplayRole) { + switch(section) { + case 0: + return QString("name"); + case 1: + return QString("vis"); + default: + return QString("???"); + } + } + return QVariant(); +} + +int SceneModel::rowCount(const QModelIndex &parent) const +{ + if(!scn) return 0; + + if(!parent.isValid()) { + // return the number of root nodes + int num_nodes = goat3d_get_node_count(scn); + int num_root_nodes = 0; + for(int i=0; i 0; +} + +QModelIndex SceneModel::index(int row, int column, const QModelIndex &parent) const +{ + if(!scn) { + return QModelIndex(); + } + + goat3d_node *node = 0; + + if(!parent.isValid()) { + int num_nodes = goat3d_get_node_count(scn); + int idx = 0; + for(int i=0; i +#include +#include "goat3d.h" + +struct SceneNodeData { + bool visible; +}; + +class SceneModel : public QAbstractItemModel { +private: + Q_OBJECT + + goat3d *scn; + std::map node_data; + +public: + SceneModel(); + ~SceneModel(); + + void set_scene(goat3d *scn); + void clear_scene(); + + SceneNodeData *get_node_data(goat3d_node *node) const; + + Qt::ItemFlags flags(const QModelIndex &index) const; + QVariant data(const QModelIndex &index, int role) const; + bool setData(const QModelIndex &index, const QVariant &value, int role); + QVariant headerData(int section, Qt::Orientation orient, int role) const; + int rowCount(const QModelIndex &parent) const; + int columnCount(const QModelIndex &parent) const; + bool hasChildren(const QModelIndex &parent) const; + QModelIndex index(int row, int column, const QModelIndex &parent) const; + QModelIndex parent(const QModelIndex &index) const; +}; + +#endif // SCENEMODEL_H_