BALL
1.4.1
|
00001 // -*- Mode: C++; tab-width: 2; -*- 00002 // vi: set ts=2: 00003 // 00004 00005 // order of includes is important: first qapplication, than BALL includes 00006 #include <QtGui/QApplication> 00007 #include <BALL/CONFIG/config.h> 00008 00009 #ifdef BALL_HAS_GLEW 00010 # include <GL/glew.h> 00011 #endif 00012 00013 #include <QtCore/QLocale> 00014 #include <QtCore/QTranslator> 00015 00016 #include <QtGui/QMessageBox> 00017 #include <QtGui/QSplashScreen> 00018 #include <QtOpenGL/qgl.h> 00019 00020 #include "mainframe.h" 00021 #include <BALL/SYSTEM/path.h> 00022 #include <BALL/SYSTEM/directory.h> 00023 #include <BALL/FORMAT/INIFile.h> 00024 #include <BALL/SYSTEM/fileSystem.h> 00025 #include <BALL/COMMON/logStream.h> 00026 00027 #include <iostream> 00028 00029 #ifdef Q_WS_X11 00030 #include <X11/Xlib.h> 00031 #endif 00032 00033 void logMessages(QtMsgType type, const char *msg) 00034 { 00035 BALL::String s(msg); 00036 if (s.hasPrefix("QTextBrowser")) return; 00037 00038 switch ( type ) { 00039 case QtDebugMsg: 00040 BALL::Log.info() << msg << std::endl; 00041 break; 00042 case QtWarningMsg: 00043 BALL::Log.warn() << msg << std::endl; 00044 break; 00045 case QtFatalMsg: 00046 fprintf( stderr, "Fatal: %s\n", msg ); 00047 abort(); // deliberately core dump 00048 case QtCriticalMsg: 00049 fprintf( stderr, "Critical: %s\n", msg ); 00050 abort(); // deliberately core dump 00051 } 00052 } 00053 00054 00055 // uncomment this to use debugging to std::cout! 00056 //#undef BALL_OS_WINDOWS 00057 00058 #ifndef BALL_OS_WINDOWS 00059 int main(int argc, char **argv) 00060 { 00061 #else 00062 int WINAPI WinMain(HINSTANCE, HINSTANCE, PSTR cmd_line, int) 00063 { 00064 int argc = __argc; 00065 char** argv = __argv; 00066 #endif 00067 00068 #ifdef Q_WS_X11 00069 XInitThreads(); 00070 #endif 00071 00072 qInstallMsgHandler(logMessages); 00073 00074 putenv("BALL_RETURN_VALUE="); 00075 QApplication application(argc, argv); 00076 00077 QPixmap splash_pm(":BALLView-1.4-Splashscreen.png"); 00078 QSplashScreen* splash = new QSplashScreen(splash_pm); 00079 splash->show(); 00080 00081 // =============== testing for opengl support ====================================== 00082 if (!QGLFormat::hasOpenGL()) 00083 { 00084 QMessageBox::critical(0, "Error while starting BALLView", 00085 "Your computer has no OpenGL support, please install the correct drivers. Aborting for now...", 00086 QMessageBox::Ok, Qt::NoButton, Qt::NoButton); 00087 return -1; 00088 } 00089 00090 BALL::String home_dir = BALL::Directory::getUserHomeDir(); 00091 00092 // =============== load translations ===================== 00093 BALL::INIFile f(home_dir + BALL::FileSystem::PATH_SEPARATOR + ".BALLView"); 00094 f.read(); 00095 00096 if (f.hasEntry("GENERAL", "language")) 00097 { 00098 QString str = f.getValue("GENERAL", "language").c_str(); 00099 00100 if (!(str == "Default")) 00101 { 00102 QString loc = "BALLView." + str; 00103 00104 BALL::Path p; 00105 QStringList dpaths = QString(p.getDataPath().c_str()).split("\n"); 00106 00107 QTranslator* translator = new QTranslator(&application); 00108 foreach(QString str, dpaths) 00109 { 00110 translator->load(loc, str + "BALLView/translations"); 00111 if (!translator->isEmpty()) 00112 { 00113 QCoreApplication::installTranslator(translator); 00114 break; 00115 } 00116 } 00117 } 00118 } 00119 00120 // =============== testing if we can write in current directory ===================== 00121 if (home_dir == "") 00122 { 00123 try 00124 { 00125 BALL::String temp_file_name; 00126 BALL::File::createTemporaryFilename(temp_file_name); 00127 BALL::File out(temp_file_name, std::ios::out); 00128 out << "test" << std::endl; 00129 out.remove(); 00130 } 00131 catch(...) 00132 { 00133 QMessageBox::warning(0, "Error while starting BALLView", 00134 QString("You dont have write access to the current working directory\n") + 00135 "and BALLView can not find your home directory. This can cause\n" + 00136 "unexpected behaviour. Please start BALLView from your homedir with\n" + 00137 "absolute path (e.g. C:\\Programs\\BALLView\\BALLView).\n"); 00138 } 00139 } 00140 00141 // =============== initialize Mainframe ============================================ 00142 // Create the mainframe. 00143 BALL::Mainframe mainframe(0, "Mainframe"); 00144 00145 // can we use the users homedir as working dir? 00146 if (home_dir != "") 00147 { 00148 mainframe.setWorkingDir(home_dir); 00149 } 00150 00151 // Register the mainfram (required for Python support). 00152 mainframe.setIdentifier("Mainframe"); 00153 mainframe.registerThis(); 00154 00155 // Show the main window. 00156 mainframe.show(); 00157 00158 // =============== parsing command line arguments ================================== 00159 // If there are additional command line arguments, interpret them as files to open or logging flag. 00160 for (BALL::Index i = 1; i < argc; ++i) 00161 { 00162 BALL::String argument(argv[i]); 00163 if (argument == "-l") 00164 { 00165 mainframe.enableLoggingToFile(); 00166 continue; 00167 } 00168 00169 mainframe.openFile(argument); 00170 } 00171 00172 // enable ending of program from python script 00173 if (mainframe.isAboutToQuit()) 00174 { 00175 mainframe.aboutToExit(); 00176 return 0; 00177 } 00178 00179 // Remove the splashscreen 00180 splash->finish(&mainframe); 00181 delete splash; 00182 00183 // Hand over control to the application. 00184 int value = application.exec(); 00185 char* return_value = getenv("BALL_RETURN_VALUE"); 00186 if (return_value != 0) 00187 { 00188 try 00189 { 00190 value = BALL::String(return_value).toInt(); 00191 } 00192 catch(...) 00193 { 00194 } 00195 } 00196 00197 return value; 00198 }