goat3d

diff goatview/src/goatview.cc @ 85:91e3aa1a60c3

more powerful treeview
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 14 May 2014 05:34:18 +0300
parents 022b13ed975b
children 7941e89798e5
line diff
     1.1 --- a/goatview/src/goatview.cc	Mon May 12 07:37:30 2014 +0300
     1.2 +++ b/goatview/src/goatview.cc	Wed May 14 05:34:18 2014 +0300
     1.3 @@ -1,17 +1,16 @@
     1.4  #include <stdio.h>
     1.5 +#include <map>
     1.6  #include "opengl.h"
     1.7  #include <QtOpenGL/QtOpenGL>
     1.8  #include <vmath/vmath.h>
     1.9  #include "goatview.h"
    1.10  #include "goat3d.h"
    1.11  
    1.12 -static void update_tree(QTreeWidget *tree);
    1.13 -static void add_tree(QTreeWidget *tree, goat3d_node *node, QTreeWidgetItem *parent);
    1.14 -
    1.15  static void draw_node(goat3d_node *node);
    1.16  static void draw_mesh(goat3d_mesh *mesh);
    1.17  
    1.18  goat3d *scene;
    1.19 +static SceneModel *sdata;
    1.20  
    1.21  static long anim_time;
    1.22  static float cam_theta, cam_phi, cam_dist = 8;
    1.23 @@ -23,6 +22,7 @@
    1.24  GoatView::GoatView()
    1.25  {
    1.26  	glview = 0;
    1.27 +	scene_model = 0;
    1.28  
    1.29  	QSettings settings;
    1.30  	resize(settings.value("main/size", QSize(1024, 768)).toSize());
    1.31 @@ -41,6 +41,8 @@
    1.32  
    1.33  GoatView::~GoatView()
    1.34  {
    1.35 +	delete scene_model;
    1.36 +	sdata = 0;
    1.37  }
    1.38  
    1.39  void GoatView::closeEvent(QCloseEvent *ev)
    1.40 @@ -69,7 +71,11 @@
    1.41  		printf("bounds size: %f, cam_dist: %f\n", bsize, cam_dist);
    1.42  	}
    1.43  
    1.44 -	update_tree(scntree);
    1.45 +	scene_model->set_scene(scene);
    1.46 +	treeview->expandAll();
    1.47 +	treeview->resizeColumnToContents(0);
    1.48 +
    1.49 +	sdata = scene_model;	// set the global sdata ptr
    1.50  	return true;
    1.51  }
    1.52  
    1.53 @@ -130,20 +136,24 @@
    1.54  	addDockWidget(Qt::LeftDockWidgetArea, dock);
    1.55  
    1.56  	// make the tree view widget
    1.57 -	scntree = new QTreeWidget;
    1.58 +	treeview = new QTreeView;
    1.59 +	/*
    1.60  	scntree->setColumnCount(1);
    1.61  	QStringList hdrstr;
    1.62  	hdrstr << "Node";// << "Type";
    1.63  	scntree->setHeaderItem(new QTreeWidgetItem((QTreeWidget*)0, hdrstr));
    1.64 -	scntree->setAlternatingRowColors(true);
    1.65 -	dock_vbox->addWidget(scntree);
    1.66 +	*/
    1.67 +	treeview->setAlternatingRowColors(true);
    1.68 +	dock_vbox->addWidget(treeview);
    1.69  
    1.70 -	update_tree(scntree);
    1.71 +	scene_model = new SceneModel;
    1.72 +	connect(scene_model, &SceneModel::dataChanged, [&](){ glview->updateGL(); });
    1.73 +	treeview->setModel(scene_model);
    1.74  
    1.75  	// misc
    1.76  	QPushButton *bn_quit = new QPushButton("quit");
    1.77  	dock_vbox->addWidget(bn_quit);
    1.78 -	connect(bn_quit, &QPushButton::clicked, [&](){qApp->quit();});
    1.79 +	connect(bn_quit, &QPushButton::clicked, [&](){ qApp->quit(); });
    1.80  
    1.81  	// ---- bottom dock ----
    1.82  	dock_cont = new QWidget;
    1.83 @@ -189,43 +199,6 @@
    1.84  	statusBar()->showMessage("opening animation...");
    1.85  }
    1.86  
    1.87 -static void update_tree(QTreeWidget *tree)
    1.88 -{
    1.89 -	tree->clear();
    1.90 -
    1.91 -	if(!scene) return;
    1.92 -
    1.93 -	int num_nodes = goat3d_get_node_count(scene);
    1.94 -	for(int i=0; i<num_nodes; i++) {
    1.95 -		goat3d_node *node = goat3d_get_node(scene, i);
    1.96 -		if(goat3d_get_node_parent(node)) {
    1.97 -			continue;
    1.98 -		}
    1.99 -
   1.100 -		// only add the root nodes, the rest will be added recursively by them
   1.101 -		add_tree(tree, node, 0);
   1.102 -	}
   1.103 -	tree->expandAll();
   1.104 -}
   1.105 -
   1.106 -static void add_tree(QTreeWidget *tree, goat3d_node *node, QTreeWidgetItem *parent)
   1.107 -{
   1.108 -	//char icon_name[64];
   1.109 -	//sprintf(icon_name, ":/icons/icons/icon_%s.png", node->get_type());
   1.110 -
   1.111 -	QStringList row;
   1.112 -	row << goat3d_get_node_name(node) << "M";
   1.113 -	QTreeWidgetItem *item = new QTreeWidgetItem(parent, row);
   1.114 -	//item->setIcon(0, QIcon(icon_name));
   1.115 -	tree->addTopLevelItem(item);
   1.116 -
   1.117 -	int num_children = goat3d_get_node_child_count(node);
   1.118 -	for(int i=0; i<num_children; i++) {
   1.119 -		add_tree(tree, goat3d_get_node_child(node, i), item);
   1.120 -	}
   1.121 -}
   1.122 -
   1.123 -
   1.124  
   1.125  // ---- OpenGL viewport ----
   1.126  GoatViewport::GoatViewport(QWidget *main_win)
   1.127 @@ -329,16 +302,21 @@
   1.128  
   1.129  static void draw_node(goat3d_node *node)
   1.130  {
   1.131 +	SceneNodeData *data = sdata ? sdata->get_node_data(node) : 0;
   1.132 +	if(!data) return;
   1.133 +
   1.134  	float xform[16];
   1.135  	goat3d_get_node_matrix(node, xform, anim_time);
   1.136  
   1.137  	glPushMatrix();
   1.138  	glMultTransposeMatrixf(xform);
   1.139  
   1.140 -	if(goat3d_get_node_type(node) == GOAT3D_NODE_MESH) {
   1.141 -		goat3d_mesh *mesh = (goat3d_mesh*)goat3d_get_node_object(node);
   1.142 +	if(data->visible) {
   1.143 +		if(goat3d_get_node_type(node) == GOAT3D_NODE_MESH) {
   1.144 +			goat3d_mesh *mesh = (goat3d_mesh*)goat3d_get_node_object(node);
   1.145  
   1.146 -		draw_mesh(mesh);
   1.147 +			draw_mesh(mesh);
   1.148 +		}
   1.149  	}
   1.150  
   1.151  	int num_child = goat3d_get_node_child_count(node);