← Back to team overview

yade-dev team mailing list archive

[Branch ~yade-dev/yade/trunk] Rev 2019: 1. Catch io exceptions in the gui

 

------------------------------------------------------------
revno: 2019
committer: Václav Šmilauer <eudoxos@xxxxxxxx>
branch nick: trunk
timestamp: Fri 2010-02-12 02:28:18 +0100
message:
  1. Catch io exceptions in the gui 
  2. Detect io error and throw in IOFormatManager
  3. Set config dir at startup, enabling use of logging.conf etc
  4. Fix display issues we had. When not running script/xml from command line, yade doesn't display the controller (press F12 to have it). Let me know if this is OK or not.
  5. Fix OpenGL initialization code.
modified:
  core/Omega.cpp
  core/main/main.py.in
  core/main/pyboot.cpp
  gui/qt3/QtGUI.cpp
  gui/qt3/SimulationController.cpp
  gui/qt3/YadeQtMainWindow.cpp
  lib/serialization/IOFormatManager.tpp
  pkg/common/RenderingEngine/OpenGLRenderingEngine.cpp
  pkg/common/RenderingEngine/OpenGLRenderingEngine.hpp


--
lp:yade
https://code.launchpad.net/~yade-dev/yade/trunk

Your team Yade developers is subscribed to branch lp:yade.
To unsubscribe from this branch go to https://code.launchpad.net/~yade-dev/yade/trunk/+edit-subscription.
=== modified file 'core/Omega.cpp'
--- core/Omega.cpp	2010-02-09 20:22:04 +0000
+++ core/Omega.cpp	2010-02-12 01:28:18 +0000
@@ -316,10 +316,12 @@
 		FormatChecker::format=FormatChecker::XML;
 		IOFormatManager::saveToFile("XMLFormatManager",name,"scene",scene);
 	}
-	else if(algorithm::ends_with(name,".yade")){
-		FormatChecker::format=FormatChecker::BIN;
-		IOFormatManager::saveToFile("BINFormatManager",name,"scene",scene);
-	}
+	#if 0
+		else if(algorithm::ends_with(name,".yade")){
+			FormatChecker::format=FormatChecker::BIN;
+			IOFormatManager::saveToFile("BINFormatManager",name,"scene",scene);
+		}
+	#endif
 	else if(algorithm::starts_with(name,":memory:")){
 		if(memSavedSimulations.count(simulationFileName)>0) LOG_INFO("Overwriting in-memory saved simulation "<<name);
 		ostringstream oss;
@@ -327,7 +329,7 @@
 		memSavedSimulations[name]=oss.str();
 	}
 	else {
-		throw runtime_error("Filename extension not recognized in `"+name+"'");
+		throw runtime_error("File format not recognized: `"+name+"' (admissible filename patterns: :memory:* *.xml *.xml.bz2)");
 	}
 }
 

=== modified file 'core/main/main.py.in'
--- core/main/main.py.in	2010-02-02 12:26:33 +0000
+++ core/main/main.py.in	2010-02-12 01:28:18 +0000
@@ -25,6 +25,7 @@
 	('' if 'log4cxx' in features else " (Since this build doesn't use log4cxx, this option will only have effect if repeated twice (-vv), equivalent to setting YADE_DEBUG environment variable)"),action='count',dest='verbosity')
 if debug:
 	par.add_option('--no-gdb',help='Do not show backtrace when yade crashes.',dest='noGdb',action='store_true',)
+par.add_option('-n',help="Run without graphical interface (equivalent to unsetting the DISPLAY environment variable)",dest='nogui',action='store_true')
 
 opts,args=par.parse_args()
 # c++ boot code checks for YADE_DEBUG at some places; debug verbosity is equivalent
@@ -77,11 +78,12 @@
 
 # open GUI if possible
 qtEnabled=False
-try:
-	import yade.qt
-	qtEnabled=True
-	yade.qt.Controller()
-except ImportError: pass
+if not opts.nogui:
+	try:
+		import yade.qt
+		qtEnabled=True
+	except ImportError: pass
+
 
 # prepare nice namespace for users
 from yade import *
@@ -89,6 +91,7 @@
 
 # something to run?
 if len(args)>0:
+	if qtEnabled: yade.qt.Controller()
 	if args[0].endswith('.xml') or args[0].endswith('.xml.bz2'):
 		if len(args)>1: raise RuntimeError('Extra arguments to XML simulation to run: '+' '.join(args[1:]))
 		sys.stderr.write("Running simulation "+args[0]+'\n')

=== modified file 'core/main/pyboot.cpp'
--- core/main/pyboot.cpp	2009-12-20 22:03:17 +0000
+++ core/main/pyboot.cpp	2010-02-12 01:28:18 +0000
@@ -9,6 +9,8 @@
 #include<stdexcept>
 
 #include<boost/python.hpp>
+#include<boost/filesystem/convenience.hpp>
+
 
 #ifdef YADE_LOG4CXX
 	#include<log4cxx/consoleappender.h>
@@ -55,6 +57,7 @@
 	Omega& O(Omega::instance());
 	O.init();
 	O.origArgv=NULL; O.origArgc=0; // not needed, anyway
+	O.yadeConfigPath=string(getenv("HOME"))+"/.yade" SUFFIX;
 	O.initTemps();
 	#ifdef YADE_DEBUG
 		ofstream gdbBatch;
@@ -63,6 +66,14 @@
 		signal(SIGABRT,crashHandler);
 		signal(SIGSEGV,crashHandler);
 	#endif
+	#ifdef YADE_LOG4CXX
+		// read logging configuration from file and watch it (creates a separate thread)
+		if(filesystem::exists(O.yadeConfigPath+"/logging.conf")){
+			std::string logConf=O.yadeConfigPath+"/logging.conf";
+			log4cxx::PropertyConfigurator::configure(logConf);
+			LOG_INFO("Loaded "<<logConf);
+		}
+	#endif
 	vector<string> ppp; for(int i=0; i<python::len(pp); i++) ppp.push_back(python::extract<string>(pp[i]));
 	Omega::instance().loadPlugins(ppp);
 }

=== modified file 'gui/qt3/QtGUI.cpp'
--- gui/qt3/QtGUI.cpp	2009-11-29 11:03:25 +0000
+++ gui/qt3/QtGUI.cpp	2010-02-12 01:28:18 +0000
@@ -73,7 +73,7 @@
 
 bool QtGUI::checkDisplay(bool quiet){
 	bool ret=(XOpenDisplay(NULL)!=0);
-	if(!ret) LOG_ERROR("Unable to open display `"<<getenv("DISPLAY")<<"'.");
+	if(!ret && !quiet) LOG_ERROR("Unable to open display `"<<getenv("DISPLAY")<<"'.");
 	return ret;
 }
 
@@ -83,7 +83,7 @@
 			LOG_ERROR("$DISPLAY environment var not set, not starting qt3 gui.");
 			return false;
 		};
-		LOG_INFO("Creating QApplication");
+		LOG_DEBUG("Creating QApplication");
 		XInitThreads();
 		int argc=0;
 	   app=new QApplication(argc,(char**)NULL);

=== modified file 'gui/qt3/SimulationController.cpp'
--- gui/qt3/SimulationController.cpp	2010-02-09 20:22:04 +0000
+++ gui/qt3/SimulationController.cpp	2010-02-12 01:28:18 +0000
@@ -238,26 +238,15 @@
 void SimulationController::pbSaveClicked()
 {
 	pbStopClicked();
-
 	string selectedFilter;
 	std::vector<string> filters;
-	filters.push_back("Yade Binary File (*.yade)");
-	filters.push_back("XML Yade File (*.xml *.xml.gz *.xml.bz2)");
-	string fileName = FileDialog::getSaveFileName(".", filters, "Specify file name to save", parentWorkspace, selectedFilter );
-
-	if(fileName.size()!=0  && (fileName != "/")&& (fileName != "."))
-	{
-
-		if(filesystem::extension(fileName)=="") // user forgot to specify extension - fix it.
-			fileName+=".xml";
-
-		cerr << "saving simulation: " << fileName << "\n";
+	// filters.push_back("Yade Binary File (*.yade)");
+	filters.push_back("XML Yade File (*.xml *.xml.bz2)"); // *.xml.gz 
+	string fileName=FileDialog::getSaveFileName(".", filters, "Specify file name to save", parentWorkspace, selectedFilter );
+	try{
 		Omega::instance().saveSimulation(fileName);
-
-	}
-	else
-	{
-		shared_ptr<MessageDialog> md = shared_ptr<MessageDialog>(new MessageDialog("Save aborted (check file extension).",NULL));
+	}catch(std::exception& e){
+		shared_ptr<MessageDialog> md = shared_ptr<MessageDialog>(new MessageDialog(string("Error: ")+e.what(),NULL));
 		md->exec(); 
 	}
 }

=== modified file 'gui/qt3/YadeQtMainWindow.cpp'
--- gui/qt3/YadeQtMainWindow.cpp	2009-12-18 17:50:07 +0000
+++ gui/qt3/YadeQtMainWindow.cpp	2010-02-12 01:28:18 +0000
@@ -53,25 +53,21 @@
 	preferences = shared_ptr<QtGUIPreferences>(new QtGUIPreferences);
 	filesystem::path yadeQtGUIPrefPath = filesystem::path( Omega::instance().yadeConfigPath + "/QtGUIPreferences.xml", filesystem::native);
 
-	if ( !filesystem::exists( yadeQtGUIPrefPath ) )
-	{
-		preferences->mainWindowPositionX	= 50;
-		preferences->mainWindowPositionY	= 50;
-		preferences->mainWindowSizeX		= 150; preferences->mainWindowSizeY		= 150;
-		IOFormatManager::saveToFile("XMLFormatManager",yadeQtGUIPrefPath.string(),"preferences",preferences);
-	}
-
-	try
-	{
-		LOG_INFO("Loading configuration file: "<<yadeQtGUIPrefPath.string()<<".")
-		IOFormatManager::loadFromFile("XMLFormatManager",yadeQtGUIPrefPath.string(),"preferences",preferences);
-	}
-	catch(SerializableError&)
-	{
-		preferences->mainWindowPositionX	= 50;
-		preferences->mainWindowPositionY	= 50;
-		preferences->mainWindowSizeX		= 150; preferences->mainWindowSizeY		= 150;
-		IOFormatManager::saveToFile("XMLFormatManager",yadeQtGUIPrefPath.string(),"preferences",preferences);
+	bool configLoaded=false;
+	try {
+		if (filesystem::exists(yadeQtGUIPrefPath)){
+			LOG_INFO("Loading configuration file: "<<yadeQtGUIPrefPath.string()<<".")
+			IOFormatManager::loadFromFile("XMLFormatManager",yadeQtGUIPrefPath.string(),"preferences",preferences);
+		}
+		configLoaded=true;
+	} catch (...){}
+	if(!configLoaded){
+		preferences->mainWindowPositionX	= 50;
+		preferences->mainWindowPositionY	= 50;
+		preferences->mainWindowSizeX		= 150; preferences->mainWindowSizeY		= 150;
+		try{
+			IOFormatManager::saveToFile("XMLFormatManager",yadeQtGUIPrefPath.string(),"preferences",preferences);
+		}catch(...){}
 	}
 
 	resize(preferences->mainWindowSizeX,preferences->mainWindowSizeY);

=== modified file 'lib/serialization/IOFormatManager.tpp'
--- lib/serialization/IOFormatManager.tpp	2009-01-04 14:32:53 +0000
+++ lib/serialization/IOFormatManager.tpp	2010-02-12 01:28:18 +0000
@@ -58,7 +58,9 @@
 void IOFormatManager::saveToFile(const string& libName, const string& fileName,const string& name, Type& t){
 	iostreams::filtering_ostream out;
 	if(boost::algorithm::ends_with(fileName,".bz2")) out.push(iostreams::bzip2_compressor());
-	out.push(iostreams::file_sink(fileName));
+	iostreams::file_sink sink(fileName);
+	if(!sink.is_open()) throw std::runtime_error("Unable to open file '"+fileName+"' for writing.");
+	out.push(sink);
 	if(!out.good()) throw SerializableError(IOManagerExceptions::FileNotGood);
 	saveToStream(libName,out,name,t);
 }

=== modified file 'pkg/common/RenderingEngine/OpenGLRenderingEngine.cpp'
--- pkg/common/RenderingEngine/OpenGLRenderingEngine.cpp	2010-02-11 08:25:11 +0000
+++ pkg/common/RenderingEngine/OpenGLRenderingEngine.cpp	2010-02-12 01:28:18 +0000
@@ -22,7 +22,7 @@
 YADE_REQUIRE_FEATURE(OPENGL)
 CREATE_LOGGER(OpenGLRenderingEngine);
 
-bool OpenGLRenderingEngine::glutInitDone=false;
+bool OpenGLRenderingEngine::initDone=false;
 const int OpenGLRenderingEngine::numClipPlanes;
 
 void OpenGLRenderingEngine::init(){
@@ -40,10 +40,16 @@
 
 	clipPlaneNormals.resize(numClipPlanes);
 
-	if (glutInitDone) return;
-	glutInit(&Omega::instance().origArgc,Omega::instance().origArgv);
-	/* transparent spheres (still not working): glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH | GLUT_MULTISAMPLE | GLUT_ALPHA); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE); */
-	glutInitDone=true;
+	initgl();
+
+	static bool glutInitDone=false;
+	if(!glutInitDone){
+		glutInit(&Omega::instance().origArgc,Omega::instance().origArgv);
+		/* transparent spheres (still not working): glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH | GLUT_MULTISAMPLE | GLUT_ALPHA); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE); */
+		glutInitDone=true;
+	}
+
+	initDone=true;
 }
 
 void OpenGLRenderingEngine::setBodiesRefSe3(){
@@ -130,7 +136,8 @@
 
 void OpenGLRenderingEngine::render(const shared_ptr<Scene>& _scene,body_id_t selection /* not sure. maybe a list of selections, or maybe bodies themselves should remember if they are selected? */) {
 
-	assert(glutInitDone);
+	if(!initDone) init();
+	assert(initDone);
 	current_selection = selection;
 
 	scene=_scene;

=== modified file 'pkg/common/RenderingEngine/OpenGLRenderingEngine.hpp'
--- pkg/common/RenderingEngine/OpenGLRenderingEngine.hpp	2010-02-11 08:25:11 +0000
+++ pkg/common/RenderingEngine/OpenGLRenderingEngine.hpp	2010-02-12 01:28:18 +0000
@@ -19,7 +19,7 @@
 		bool pointClipped(const Vector3r& p);
 		vector<Vector3r> clipPlaneNormals;
 		void setBodiesDispInfo();
-		static bool glutInitDone;
+		static bool initDone;
 		Vector3r viewDirection; // updated from GLViewer regularly
 		GLViewInfo viewInfo; // update from GLView regularly
 		Vector3r highlightEmission0;
@@ -105,7 +105,7 @@
 		((vector<int>,clipPlaneActive,vector<int>(numClipPlanes,0),"Activate/deactivate respective clipping planes"))
 		((size_t,selectBodyLimit,1000,"Limit number of bodies to allow picking body with mouse (performance reasons)"))
 		((bool,intrAllWire,false,"Draw wire for all interactions, blue for potential and green for real ones (mostly for debugging)")),
-		/*ctor*/ init(); ,
+		/*ctor*/ ,
 		/*py*/
 		.def("setRefSe3",&OpenGLRenderingEngine::setBodiesRefSe3,"Make current positions and orientation reference for scaleDisplacements and scaleRotations.");
 	);