Author: eudoxos
Date: 2009-08-05 12:09:52 +0200 (Wed, 05 Aug 2009)
New Revision: 1922

Modified:
   trunk/core/FrontEnd.hpp
   trunk/core/yade.cpp
   trunk/gui/py/PythonUI.hpp
   trunk/gui/qt3/QtGUI-python.cpp
   trunk/gui/qt3/QtGUI.cpp
   trunk/gui/qt3/QtGUI.hpp
   trunk/gui/qt3/qt.py
Log:
1. Be more tolerant to specifying invalid GUI at the command line
2. Throw exception about unopenable $DISPLAY rather than letting QApplication 
abort()



Modified: trunk/core/FrontEnd.hpp
===================================================================
--- trunk/core/FrontEnd.hpp     2009-08-05 09:02:32 UTC (rev 1921)
+++ trunk/core/FrontEnd.hpp     2009-08-05 10:09:52 UTC (rev 1922)
@@ -20,10 +20,11 @@
                virtual ~FrontEnd ();
 
                virtual int run(int , char * []) { return -1;};
+               // called before actually invoking it
+               virtual bool available(){return false;}
 
        REGISTER_CLASS_AND_BASE(FrontEnd,Factorable);
 };
-
 REGISTER_FACTORABLE(FrontEnd);
 
 

Modified: trunk/core/yade.cpp
===================================================================
--- trunk/core/yade.cpp 2009-08-05 09:02:32 UTC (rev 1921)
+++ trunk/core/yade.cpp 2009-08-05 10:09:52 UTC (rev 1922)
@@ -278,15 +278,23 @@
                }
        }
 
+       // handle this a little more inteligently, use FrontEnd::available to 
chec kif the GUI will really run (QtGUi without DISPLAY and similar)
        if(gui.size()==0) gui=Omega::instance().preferences->defaultGUILibName;
        #ifdef YADE_OPENGL
                if(!explicitUI && gui=="PythonUI" && !getenv("TERM")){ 
LOG_WARN("No $TERM, using QtGUI instead of PythonUI"); gui="QtGUI"; }
        #else
                if(gui=="QtGUI"){LOG_WARN("openGL-less build, using PythonUI 
instead of QtGUI"); gui="PythonUI";}
        #endif
+       if(string(getenv("DISPLAY")).empty()) unsetenv("DISPLAY"); // empty 
$DISPLAY is no display
        if(gui=="QtGUI" && !getenv("DISPLAY")){ LOG_WARN("No $DISPLAY, using 
PythonUI instead of QtUI"); gui="PythonUI"; }
-               
-       shared_ptr<FrontEnd> frontEnd = 
dynamic_pointer_cast<FrontEnd>(ClassFactory::instance().createShared(gui));
+       shared_ptr<FrontEnd> frontEnd;
+       try{
+               
frontEnd=dynamic_pointer_cast<FrontEnd>(ClassFactory::instance().createShared(gui));
+               if(!frontEnd){ LOG_FATAL("Selected class `"<<gui<<"' is not an 
UI."); exit(1);}
+       } catch (FactoryError& e){
+               LOG_FATAL("Unable to create UI `"<<gui<<"', got error: 
"<<e.what());
+               exit(1);
+       }
        
        // for(int i=0;i<argc; i++)cerr<<"Argument "<<i<<": "<<argv[i]<<endl;
        int ok = frontEnd->run(argc,argv);

Modified: trunk/gui/py/PythonUI.hpp
===================================================================
--- trunk/gui/py/PythonUI.hpp   2009-08-05 09:02:32 UTC (rev 1921)
+++ trunk/gui/py/PythonUI.hpp   2009-08-05 10:09:52 UTC (rev 1922)
@@ -22,7 +22,8 @@
                static bool stopAfter;
                static bool nonInteractive;
                static vector<string> scriptArgs;
-       
+
+       virtual bool available(){return true;}
        REGISTER_CLASS_NAME(PythonUI);
        REGISTER_BASE_CLASS_NAME(FrontEnd);
        DECLARE_LOGGER;

Modified: trunk/gui/qt3/QtGUI-python.cpp
===================================================================
--- trunk/gui/qt3/QtGUI-python.cpp      2009-08-05 09:02:32 UTC (rev 1921)
+++ trunk/gui/qt3/QtGUI-python.cpp      2009-08-05 10:09:52 UTC (rev 1922)
@@ -20,7 +20,10 @@
 void qtGuiActivate(){
        if(qtGuiIsActive()) return;
        QtGUI* gui=new QtGUI();
-       gui->runNaked();
+       if(!gui->checkDisplay(/* quiet */ false) || !gui->runNaked()){
+               PyErr_SetString(PyExc_ImportError,"Qt3 GUI could not be 
activated.");
+               python::throw_error_already_set();
+       }
 }
 
 YadeQtMainWindow* ensuredMainWindow(){if(!qtGuiIsActive()){qtGuiActivate(); 
while(!YadeQtMainWindow::self) usleep(50000); } /* throw runtime_error("No 
instance of YadeQtMainWindow");*/ return YadeQtMainWindow::self; }

Modified: trunk/gui/qt3/QtGUI.cpp
===================================================================
--- trunk/gui/qt3/QtGUI.cpp     2009-08-05 09:02:32 UTC (rev 1921)
+++ trunk/gui/qt3/QtGUI.cpp     2009-08-05 10:09:52 UTC (rev 1922)
@@ -81,12 +81,17 @@
        return res;
 }
 
+bool QtGUI::checkDisplay(bool quiet){
+       bool ret=(XOpenDisplay(NULL)!=0);
+       if(!ret) LOG_ERROR("Unable to open display `"<<getenv("DISPLAY")<<"'.");
+       return ret;
+}
 
-void QtGUI::runNaked(){
+bool QtGUI::runNaked(){
        if(!app){ // no app existing yet
                if(getenv("DISPLAY")==0){
                        LOG_ERROR("$DISPLAY environment var not set, not 
starting qt3 gui.");
-                       return;
+                       return false;
                };
                LOG_INFO("Creating QApplication");
                XInitThreads();
@@ -99,4 +104,5 @@
                } else { LOG_ERROR("Main window was there, but not 
QtGUI::app??"); }
                boost::thread appThread(boost::bind(&QApplication::exec,app));
        }
+       return true;
 }

Modified: trunk/gui/qt3/QtGUI.hpp
===================================================================
--- trunk/gui/qt3/QtGUI.hpp     2009-08-05 09:02:32 UTC (rev 1921)
+++ trunk/gui/qt3/QtGUI.hpp     2009-08-05 10:09:52 UTC (rev 1922)
@@ -29,7 +29,11 @@
 
        public :
                QtGUI ();
-               void runNaked();
+               // run the QtGUI without showing the main window (called from 
python when importing yade.qt; yade.qt.Controller() will then show just the 
Controller and so on)
+               bool runNaked();
+               // try to open the X display, return false if fails; optionally 
display error to the log.
+               bool checkDisplay(bool quiet=true);
+               virtual bool available(){return checkDisplay();}
                virtual ~QtGUI ();
                virtual int run(int argc, char *argv[]);
                void help();

Modified: trunk/gui/qt3/qt.py
===================================================================
--- trunk/gui/qt3/qt.py 2009-08-05 09:02:32 UTC (rev 1921)
+++ trunk/gui/qt3/qt.py 2009-08-05 10:09:52 UTC (rev 1922)
@@ -2,8 +2,8 @@
 from _qt import *
 
 if not isActive():
+       # will raise ImportError if the GUI could not be activated for some 
reason
        activate()
-#      # raise ImportError("The Qt gui is not being used (run with -N QtGUI).")
 
 
 


_______________________________________________
Mailing list: https://launchpad.net/~yade-dev
Post to     : [email protected]
Unsubscribe : https://launchpad.net/~yade-dev
More help   : https://help.launchpad.net/ListHelp
_______________________________________________
yade-dev mailing list
[email protected]
https://lists.berlios.de/mailman/listinfo/yade-dev

Reply via email to