Added reload, added _ at the end of private member variables
authorDmitriy Morozov <morozov@cs.duke.edu>
Fri, 30 Jun 2006 12:00:05 -0400
changeset 8 2d94ef3aab03
parent 7 8871c5317b90
child 9 b16a476ef46d
Added reload, added _ at the end of private member variables
VEFViewer.cpp
VEFViewer.h
--- a/VEFViewer.cpp	Thu Jun 29 09:29:01 2006 -0400
+++ b/VEFViewer.cpp	Fri Jun 30 12:00:05 2006 -0400
@@ -167,46 +167,19 @@
 		QGLViewer::keyPressEvent(e);
 }
 
-void VEFViewer::readVertexFile(QString s)
+void VEFViewer::readVertexFile(const QString& s)
 {
-	FILE* file;
-	if ((file = fopen(s.toLocal8Bit().data(), "r")) == NULL)
-	{
-		qDebug() << "Could not open file " << s << " for reading";
-		return;
-	}
-
-	QFileInfo fi(s);
-	modelList->addItem(new QVerticesLWI(fi.baseName(), file, this));
-	fclose(file);
+	modelList->addItem(new QVerticesLWI(s, this));
 }
 
-void VEFViewer::readEdgeFile(QString s)
+void VEFViewer::readEdgeFile(const QString& s)
 {
-	FILE* file;
-	if ((file = fopen(s.toLocal8Bit().data(), "r")) == NULL)
-	{
-		qDebug() << "Could not open file " << s << " for reading";
-		return;
-	}
-
-	QFileInfo fi(s);
-	modelList->addItem(new QEdgesLWI(fi.baseName(), file, this));
-	fclose(file);
+	modelList->addItem(new QEdgesLWI(s, this));
 }
 
-void VEFViewer::readSTLFile(QString s)
+void VEFViewer::readSTLFile(const QString& s)
 {
-	FILE* file;
-	if ((file = fopen(s.toLocal8Bit().data(), "r")) == NULL)
-	{
-		qDebug() << "Could not open file " << s << " for reading";
-		return;
-	}
-
-	QFileInfo fi(s);
-	modelList->addItem(new QFacesLWI(fi.baseName(), file, this));
-	fclose(file);
+	modelList->addItem(new QFacesLWI(s, this));
 }
 
 
@@ -240,9 +213,9 @@
 	glColor3f(.5, .65, .65);
 
 	if (!isSphere())
-		glCallList(display_list);
+		glCallList(display_list_);
 	else
-		glCallList(sphere_display_list);
+		glCallList(sphere_display_list_);
 }
 void QVerticesLWI::drawWithNames(int offset) const
 {}
@@ -251,32 +224,47 @@
 void QVerticesLWI::highlight(int selected)
 {}
 
-QVerticesLWI::QVerticesLWI(QString name, FILE* file, VEFViewer* v): 
-	QDisplayListLWI(name), isSphere_(false)
+QVerticesLWI::QVerticesLWI(const QString& fname, VEFViewer* v): 
+	isSphere_(false), viewer_(v)
 {
+	filename_ = fname;
+	QFileInfo fi(filename_);
+	setText(fi.baseName());
+	init();
+}
+
+void QVerticesLWI::init()
+{
+	FILE* file;
+	if ((file = fopen(filename_.toLocal8Bit().data(), "r")) == NULL)
+	{
+		qDebug() << "Could not open file " << filename_ << " for reading";
+		return;
+	}
+
 	// Read in vertices
 	Point p;
 	int result = fscanf(file, "%f %f %f", &p.x, &p.y, &p.z);
 	while(result != EOF)
 	{
-		vertices.push_back(p);
-		v->updateMinMax(p.x,p.y,p.z);
+		vertices_.push_back(p);
+		viewer_->updateMinMax(p.x,p.y,p.z);
 		result = fscanf(file, "%f %f %f", &p.x, &p.y, &p.z);
 	}
 
 	// Create display list of points
-	display_list = glGenLists(1);
-	glNewList(display_list, GL_COMPILE);
+	display_list_ = glGenLists(1);
+	glNewList(display_list_, GL_COMPILE);
 	glBegin(GL_POINTS);
-		for (VertexContainer::const_iterator cur = vertices.begin(); cur != vertices.end(); ++cur)
+		for (VertexContainer::const_iterator cur = vertices_.begin(); cur != vertices_.end(); ++cur)
 			glVertex3f(cur->x,cur->y,cur->z);
 	glEnd();
 	glEndList();
 
 	// Create display list of spheres
-	sphere_display_list = glGenLists(1);
-	glNewList(sphere_display_list, GL_COMPILE);
-	for (VertexContainer::const_iterator cur = vertices.begin(); cur != vertices.end(); ++cur)
+	sphere_display_list_ = glGenLists(1);
+	glNewList(sphere_display_list_, GL_COMPILE);
+	for (VertexContainer::const_iterator cur = vertices_.begin(); cur != vertices_.end(); ++cur)
 	{
 		XYZ c;
 		c.x = cur->x; c.y = cur->y; c.z = cur->z;
@@ -285,6 +273,14 @@
 	glEnd();
 	glEndList();
 
+	fclose(file);
+}
+
+void QVerticesLWI::destroy()
+{
+	vertices_.clear();
+	glDeleteLists(sphere_display_list_, 1);
+	QDisplayListLWI::destroy();
 }
 
 
@@ -294,11 +290,11 @@
 	if (!isVisible()) return;
 
 	// Draw selected
-	if (highlighted >= 0)
+	if (highlighted_ >= 0)
 	{
 	   	glColor3f(.9, .4, 0);
 		glBegin(GL_TRIANGLES);
-			const Triangle& t = triangles[highlighted];
+			const Triangle& t = triangles_[highlighted_];
 			glNormal3f(t.nx, t.ny, t.nz);
 			glVertex3f(t.x0, t.y0, t.z0);
 			glVertex3f(t.x1, t.y1, t.z1);
@@ -310,12 +306,12 @@
 	if (!isWireframe())	
     {
 	    glColor3f(0., .4, .6);
-		glCallList(display_list);
+		glCallList(display_list_);
     }
 	else
     {
 	    glColor3f(1., 0, 0);
-		glCallList(wireframe_display_list);
+		glCallList(wireframe_display_list_);
     }
 }
 
@@ -324,9 +320,9 @@
 	if (!isVisible()) return;
 	if (isWireframe()) return;
 		
-	for (int i = 0; i < triangles.size(); i++)
+	for (int i = 0; i < triangles_.size(); i++)
 	{
-		const Triangle& t = triangles[i];
+		const Triangle& t = triangles_[i];
 		glPushName(offset + i);
 		glBegin(GL_TRIANGLES);
 		glVertex3f(t.x0, t.y0, t.z0);
@@ -338,22 +334,37 @@
 }
 
 int QFacesLWI::numElements() const
-{ return triangles.size(); }
+{ return triangles_.size(); }
 
 void QFacesLWI::highlight(int selected)
 {
-	highlighted = selected;
-	if (highlighted < 0) return;
+	highlighted_ = selected;
+	if (highlighted_ < 0) return;
 
-	const Triangle& t = triangles[highlighted];
+	const Triangle& t = triangles_[highlighted_];
 	std::cout << "Highlighted: (" << t.x0 << " " << t.y0 << " " << t.z0 << std::endl;
 	std::cout << "              " << t.x1 << " " << t.y1 << " " << t.z1 << std::endl;
 	std::cout << "              " << t.x2 << " " << t.y2 << " " << t.z2 << ")" << std::endl;
 }
 
-QFacesLWI::QFacesLWI(QString name, FILE* file, VEFViewer* v):
-	QDisplayListLWI(name), highlighted(-1), isWireframe_(false)
+QFacesLWI::QFacesLWI(const QString& fname, VEFViewer* v):
+	highlighted_(-1), isWireframe_(false), viewer_(v)
 {
+	filename_ = fname;
+	QFileInfo fi(filename_);
+	setText(fi.baseName());
+	init();
+}
+
+void QFacesLWI::init()
+{
+	FILE* file;
+	if ((file = fopen(filename_.toLocal8Bit().data(), "r")) == NULL)
+	{
+		qDebug() << "Could not open file " << filename_ << " for reading";
+		return;
+	}
+
 	float nx,ny,nz;			// not used
 	float x0,y0,z0;
 	float x1,y1,z1;
@@ -366,34 +377,41 @@
 		qglviewer::Vec n = computeNormal(qglviewer::Vec(x0,y0,z0), 
 										 qglviewer::Vec(x1,y1,z1), 
 										 qglviewer::Vec(x2,y2,z2)); 
-		triangles.push_back(Triangle(n.x,n.y,n.z, x0,y0,z0, x1,y1,z1, x2,y2,z2));
+		triangles_.push_back(Triangle(n.x,n.y,n.z, x0,y0,z0, x1,y1,z1, x2,y2,z2));
 
-		v->updateMinMax(x0,y0,z0);
-		v->updateMinMax(x1,y1,z1);
-		v->updateMinMax(x2,y2,z2);
+		viewer_->updateMinMax(x0,y0,z0);
+		viewer_->updateMinMax(x1,y1,z1);
+		viewer_->updateMinMax(x2,y2,z2);
 
 		result = readFacet(file, nx,ny,nz, x0,y0,z0, x1,y1,z1, x2,y2,z2);
 	}
+	fclose(file);
 	
-	std::cout << "Triangles read: " << triangles.size() << std::endl;
+	std::cout << "Triangles read: " << triangles_.size() << std::endl;
 	
 	// Create triangles display list
-	display_list = glGenLists(1);
-	glNewList(display_list, GL_COMPILE);
+	display_list_ = glGenLists(1);
+	glNewList(display_list_, GL_COMPILE);
 	drawTriangles(GL_TRIANGLES);
 	glEndList();
 
 	// Create wireframe display list
-	wireframe_display_list = glGenLists(1);
-	glNewList(wireframe_display_list, GL_COMPILE);
+	wireframe_display_list_ = glGenLists(1);
+	glNewList(wireframe_display_list_, GL_COMPILE);
 	drawTriangles(GL_LINE_LOOP);
 	glEndList();
+}
 
+void QFacesLWI::destroy()
+{
+	triangles_.clear();
+	glDeleteLists(wireframe_display_list_, 1);
+	QDisplayListLWI::destroy();
 }
 
 void QFacesLWI::drawTriangles(GLenum mode) const
 {
-	for (TrianglesContainer::const_iterator cur = triangles.begin(); cur != triangles.end(); ++cur)
+	for (TrianglesContainer::const_iterator cur = triangles_.begin(); cur != triangles_.end(); ++cur)
 	{
 		glBegin(mode);
 		glNormal3f(cur->nx,cur->ny,cur->nz);
@@ -436,7 +454,7 @@
 	if (!isVisible()) return;
 
 	glColor3f(.6, .2, .6);
-	glCallList(display_list);
+	glCallList(display_list_);
 }
 void QEdgesLWI::drawWithNames(int offset) const
 {}
@@ -445,11 +463,26 @@
 void QEdgesLWI::highlight(int selected)
 {}
 
-QEdgesLWI::QEdgesLWI(QString name, FILE* file, VEFViewer* v):
-	QDisplayListLWI(name)
+QEdgesLWI::QEdgesLWI(const QString& fname, VEFViewer* v):
+	viewer_(v)
 {
-	display_list = glGenLists(1);
-	glNewList(display_list, GL_COMPILE);
+	filename_ = fname;
+	QFileInfo fi(filename_);
+	setText(fi.baseName());
+	init();
+}
+
+void QEdgesLWI::init()
+{
+	FILE* file;
+	if ((file = fopen(filename_.toLocal8Bit().data(), "r")) == NULL)
+	{
+		qDebug() << "Could not open file " << filename_ << " for reading";
+		return;
+	}
+
+	display_list_ = glGenLists(1);
+	glNewList(display_list_, GL_COMPILE);
 	glBegin(GL_LINES);
 	
 		float nx,ny,nz;
@@ -462,14 +495,16 @@
 			glVertex3f(x0,y0,z0);
 			glVertex3f(x1,y1,z1);
 
-			v->updateMinMax(x0,y0,z0);
-			v->updateMinMax(x1,y1,z1);
+			viewer_->updateMinMax(x0,y0,z0);
+			viewer_->updateMinMax(x1,y1,z1);
 
 			result = readEdge(file, x0,y0,z0, x1,y1,z1);
 		}
 
 	glEnd();
 	glEndList();
+
+	fclose(file);
 }
 
 int QEdgesLWI::readEdge  (FILE* file,
--- a/VEFViewer.h	Thu Jun 29 09:29:01 2006 -0400
+++ b/VEFViewer.h	Fri Jun 30 12:00:05 2006 -0400
@@ -27,9 +27,9 @@
 		virtual void postSelection(const QPoint& point);
 
 	private:
-		void readVertexFile(QString s);
-		void readEdgeFile(QString s);
-		void readSTLFile(QString s);
+		void readVertexFile(const QString& s);
+		void readEdgeFile(const QString& s);
+		void readSTLFile(const QString& s);
 		void updateWeightedCenter(float x, float y, float z);
 		
 		QListWidget* modelList;
@@ -47,11 +47,11 @@
 	Q_OBJECT
 
 	public:
-		QDisplayListLWI(QString name):
-			QListWidgetItem(name), visible(false)
+		QDisplayListLWI():
+			visible_(false)
 		{ toggleVisible(); }
-		GLuint getDisplayList() const	{ return display_list; }
-		~QDisplayListLWI()				{ glDeleteLists(display_list, 1); }
+		GLuint getDisplayList() const	{ return display_list_; }
+		~QDisplayListLWI()				{ destroy(); }
 
 		virtual void draw() const =0;
 		virtual void drawWithNames(int offset) const =0;
@@ -59,24 +59,29 @@
 		virtual void highlight(int selected) =0;
 		virtual void setupMenu(QMenu& m) const
 		{
+			QAction* reload = m.addAction("Reload");
+			connect(reload, SIGNAL(triggered()), this, SLOT(reload()));
 			QAction* visible = m.addAction("Visible");
 			connect(visible, SIGNAL(triggered()), this, SLOT(toggleVisible()));
 			visible->setCheckable(true);
 			visible->setChecked(isVisible());
 		}
 		
-		bool isVisible() const			{ return visible; }
+		bool isVisible() const			{ return visible_; }
 
 	public slots:
-		void setVisible(bool vis = true)	{ visible = !vis; adjustFont(); }
-		void toggleVisible()				{ visible = !visible; adjustFont(); }
+		void setVisible(bool vis = true)	{ visible_ = !vis; adjustFont(); }
+		void toggleVisible()				{ visible_ = !visible_; adjustFont(); }
+		virtual void reload()				{ destroy(); init(); }
+		virtual void init() =0;
+		virtual void destroy()				{ glDeleteLists(display_list_, 1); }
 
 	protected:
-		GLuint display_list;
-		bool visible;
+		GLuint display_list_;
+		bool visible_;
 
 	private:
-		void adjustFont()				{ QFont f; if (visible)	f.setBold(true); setFont(f); }
+		void adjustFont()				{ QFont f; if (visible_) f.setBold(true); setFont(f); }
 				
 };
 
@@ -86,7 +91,7 @@
 	Q_OBJECT
 		
 	public:
-		QVerticesLWI(QString name, FILE* file, VEFViewer* v);
+		QVerticesLWI(const QString& fname, VEFViewer* v);
 		virtual void draw() const;
 		virtual void drawWithNames(int offset) const;
 		virtual int numElements() const;
@@ -107,11 +112,17 @@
 		void toggleSphere()		{ isSphere_ = !isSphere_; }
 
 	private:
+		void init();
+		void destroy();
+
 		bool isSphere_;
-		GLuint sphere_display_list;
+		GLuint sphere_display_list_;
 
 		typedef 	std::vector<Point>		VertexContainer;
-		VertexContainer vertices;
+		VertexContainer vertices_;
+
+		QString filename_;
+		VEFViewer* viewer_;
 };
 
 class Triangle;
@@ -120,7 +131,7 @@
 	Q_OBJECT
 		
 	public:
-		QFacesLWI(QString name, FILE* file, VEFViewer* v);
+		QFacesLWI(const QString& fname, VEFViewer* v);
 		virtual void draw() const;
 		virtual void drawWithNames(int offset) const;
 		virtual int numElements() const;
@@ -140,6 +151,9 @@
 		void toggleWireframe()		{ isWireframe_ = !isWireframe_; }
 		
 	private:
+		void init();
+		void destroy();
+
 		int readFacet(FILE* file,
 					  float& nx, float& ny, float& nz,
                       float& x0, float& y0, float& z0,
@@ -149,26 +163,34 @@
 		void drawTriangles(GLenum mode) const;
 
 		typedef 	std::vector<Triangle>		TrianglesContainer;
-		TrianglesContainer triangles;
+		TrianglesContainer triangles_;
 
 		bool isWireframe_;
-		GLuint wireframe_display_list;
-		int highlighted;
+		GLuint wireframe_display_list_;
+		int highlighted_;
+
+		QString filename_;
+		VEFViewer* viewer_;
 };
 
 class QEdgesLWI: public QDisplayListLWI
 {
 	public:
-		QEdgesLWI(QString name, FILE* file, VEFViewer* v);
+		QEdgesLWI(const QString& fname, VEFViewer* v);
 		virtual void draw() const;
 		virtual void drawWithNames(int offset) const;
 		virtual int numElements() const;
 		virtual void highlight(int selected);
 
 	private:
+		virtual void init();
+
 		int readEdge (FILE* file,
                       float& x0, float& y0, float& z0,
                       float& x1, float& y1, float& z1);
+
+		QString filename_;
+		VEFViewer* viewer_;
 };
 
 class Triangle