BALL  1.4.2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
main.C
Go to the documentation of this file.
1 // -*- Mode: C++; tab-width: 2; -*-
2 // vi: set ts=2:
3 //
4 
5 // order of includes is important: first qapplication, than BALL includes
6 #include <QtGui/QApplication>
7 #include <BALL/CONFIG/config.h>
8 
9 #ifdef BALL_HAS_GLEW
10 # include <GL/glew.h>
11 #endif
12 
13 #include <QtCore/QLocale>
14 #include <QtCore/QTranslator>
15 
16 #include <QtGui/QMessageBox>
17 #include <QtGui/QSplashScreen>
18 #include <QtOpenGL/qgl.h>
19 
20 #include "mainframe.h"
21 #include <BALL/SYSTEM/path.h>
22 #include <BALL/SYSTEM/directory.h>
23 #include <BALL/FORMAT/INIFile.h>
24 #include <BALL/SYSTEM/fileSystem.h>
25 #include <BALL/COMMON/logStream.h>
26 
27 #include <iostream>
28 
29 #ifdef Q_WS_X11
30 #include <X11/Xlib.h>
31 #endif
32 
33 void logMessages(QtMsgType type, const char *msg)
34 {
35  BALL::String s(msg);
36  if (s.hasPrefix("QTextBrowser")) return;
37 
38  switch ( type ) {
39  case QtDebugMsg:
40  BALL::Log.info() << msg << std::endl;
41  break;
42  case QtWarningMsg:
43  BALL::Log.warn() << msg << std::endl;
44  break;
45  case QtFatalMsg:
46  fprintf( stderr, "Fatal: %s\n", msg );
47  abort(); // deliberately core dump
48  case QtCriticalMsg:
49  fprintf( stderr, "Critical: %s\n", msg );
50  abort(); // deliberately core dump
51  }
52 }
53 
54 
55 // uncomment this to use debugging to std::cout!
56 //#undef BALL_OS_WINDOWS
57 
58 #ifndef BALL_OS_WINDOWS
59 int main(int argc, char **argv)
60 {
61 #else
62 int WINAPI WinMain(HINSTANCE, HINSTANCE, PSTR cmd_line, int)
63 {
64  int argc = __argc;
65  char** argv = __argv;
66 #endif
67 
68 #ifdef Q_WS_X11
69  XInitThreads();
70 #endif
71 
72  qInstallMsgHandler(logMessages);
73 
74  QApplication application(argc, argv);
75 
76  QPixmap splash_pm(":BALLView-1.4-Splashscreen.png");
77  QSplashScreen* splash = new QSplashScreen(splash_pm);
78  splash->show();
79 
80  // =============== testing for opengl support ======================================
81  if (!QGLFormat::hasOpenGL())
82  {
83  QMessageBox::critical(0, "Error while starting BALLView",
84  "Your computer has no OpenGL support, please install the correct drivers. Aborting for now...",
85  QMessageBox::Ok, Qt::NoButton, Qt::NoButton);
86  return -1;
87  }
88 
90 
91  // =============== load translations =====================
92  BALL::INIFile f(home_dir + BALL::FileSystem::PATH_SEPARATOR + ".BALLView");
93  f.read();
94 
95  if (f.hasEntry("GENERAL", "language"))
96  {
97  QString str = f.getValue("GENERAL", "language").c_str();
98 
99  if (!(str == "Default"))
100  {
101  QString loc = "BALLView." + str;
102 
103  BALL::Path p;
104  QStringList dpaths = QString(p.getDataPath().c_str()).split("\n");
105 
106  QTranslator* translator = new QTranslator(&application);
107  foreach(QString str, dpaths)
108  {
109  translator->load(loc, str + "BALLView/translations");
110  if (!translator->isEmpty())
111  {
112  QCoreApplication::installTranslator(translator);
113  break;
114  }
115  }
116  }
117  }
118 
119  // =============== testing if we can write in current directory =====================
120  if (home_dir == "")
121  {
122  try
123  {
124  BALL::String temp_file_name;
125  BALL::File::createTemporaryFilename(temp_file_name);
126  BALL::File out(temp_file_name, std::ios::out);
127  out << "test" << std::endl;
128  out.remove();
129  }
130  catch(...)
131  {
132  QMessageBox::warning(0, "Error while starting BALLView",
133  QString("You dont have write access to the current working directory\n") +
134  "and BALLView can not find your home directory. This can cause\n" +
135  "unexpected behaviour. Please start BALLView from your homedir with\n" +
136  "absolute path (e.g. C:\\Programs\\BALLView\\BALLView).\n");
137  }
138  }
139 
140  // =============== initialize Mainframe ============================================
141  // Create the mainframe.
142  BALL::Mainframe mainframe(0, "Mainframe");
143 
144  // can we use the users homedir as working dir?
145  if (home_dir != "")
146  {
147  mainframe.setWorkingDir(home_dir);
148  }
149 
150  // Register the mainfram (required for Python support).
151  mainframe.setIdentifier("Mainframe");
152  mainframe.registerThis();
153 
154  // Show the main window.
155  mainframe.show();
156 
157  // =============== parsing command line arguments ==================================
158  // If there are additional command line arguments, interpret them as files to open or logging flag.
159  for (BALL::Index i = 1; i < argc; ++i)
160  {
161  BALL::String argument(argv[i]);
162  if (argument == "-l")
163  {
164  mainframe.enableLoggingToFile();
165  continue;
166  }
167 
168  mainframe.openFile(argument);
169  }
170 
171  // enable ending of program from python script
172  if (mainframe.isAboutToQuit())
173  {
174  mainframe.aboutToExit();
175  return 0;
176  }
177 
178  // Remove the splashscreen
179  splash->finish(&mainframe);
180  delete splash;
181 
182  // Hand over control to the application.
183  return application.exec();
184 }