BALL  1.4.79
 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 
28 
29 #include <iostream>
30 
31 #ifdef Q_WS_X11
32 #include <X11/Xlib.h>
33 #endif
34 
35 void logMessages(QtMsgType type, const char *msg)
36 {
37  BALL::String s(msg);
38  if (s.hasPrefix("QTextBrowser")) return;
39 
40  switch ( type ) {
41  case QtDebugMsg:
42  BALL::Log.info() << msg << std::endl;
43  break;
44  case QtWarningMsg:
45  BALL::Log.warn() << msg << std::endl;
46  break;
47  case QtFatalMsg:
48  fprintf( stderr, "Fatal: %s\n", msg );
49  abort(); // deliberately core dump
50  case QtCriticalMsg:
51  fprintf( stderr, "Critical: %s\n", msg );
52  abort(); // deliberately core dump
53  }
54 }
55 
56 
57 // uncomment this to use debugging to std::cout!
58 //#undef BALL_OS_WINDOWS
59 
60 #ifndef BALL_OS_WINDOWS
61 int main(int argc, char **argv)
62 {
63 #else
64 int WINAPI WinMain(HINSTANCE, HINSTANCE, PSTR cmd_line, int)
65 {
66  int argc = __argc;
67  char** argv = __argv;
68 #endif
69 
70 #ifdef Q_WS_X11
71  XInitThreads();
72 #endif
73 
74  qInstallMsgHandler(logMessages);
75 
76  // *sigh* this is required as long as Qt does not correctly paint on OpenGL 2 contexts
77  QGL::setPreferredPaintEngine(QPaintEngine::OpenGL);
78 
79  QApplication application(argc, argv);
80 
81  QStringList arguments = application.arguments();
82  QStringList::const_iterator arg_it;
83 
84  bool kiosk_mode = false;
85  for (arg_it = arguments.constBegin(); arg_it != arguments.constEnd(); ++arg_it)
86  {
87  if (arg_it->toLocal8Bit() == "-kiosk")
88  {
89  kiosk_mode = true;
90  }
91  }
92 
93  if (kiosk_mode)
94  {
96  }
97 
98  QPixmap splash_pm(":BALLView-1.4-Splashscreen.png");
99  QSplashScreen* splash = new QSplashScreen(splash_pm);
100  splash->show();
101 
102  // =============== testing for opengl support ======================================
103  if (!QGLFormat::hasOpenGL())
104  {
105  QMessageBox::critical(0, "Error while starting BALLView",
106  "Your computer has no OpenGL support, please install the correct drivers. Aborting for now...",
107  QMessageBox::Ok, Qt::NoButton, Qt::NoButton);
108  return -1;
109  }
110 
112 
113  // =============== load translations =====================
114  BALL::INIFile f(home_dir + BALL::FileSystem::PATH_SEPARATOR + ".BALLView");
115  f.read();
116 
117  if (f.hasEntry("GENERAL", "language"))
118  {
119  QString str = f.getValue("GENERAL", "language").c_str();
120 
121  if (!(str == "Default"))
122  {
123  QString loc = "BALLView." + str;
124 
125  BALL::Path p;
126  QStringList dpaths = QString(p.getDataPath().c_str()).split("\n");
127 
128  QTranslator* translator = new QTranslator(&application);
129  foreach(QString str, dpaths)
130  {
131  translator->load(loc, str + "BALLView/translations");
132  if (!translator->isEmpty())
133  {
134  QCoreApplication::installTranslator(translator);
135  break;
136  }
137  }
138  }
139  }
140 
141  // =============== testing if we can write in current directory =====================
142  if (home_dir == "")
143  {
144  try
145  {
146  BALL::String temp_file_name;
147  BALL::File::createTemporaryFilename(temp_file_name);
148  BALL::File out(temp_file_name, std::ios::out);
149  out << "test" << std::endl;
150  out.remove();
151  }
152  catch(...)
153  {
154  QMessageBox::warning(0, "Error while starting BALLView",
155  QString("You dont have write access to the current working directory\n") +
156  "and BALLView can not find your home directory. This can cause\n" +
157  "unexpected behaviour. Please start BALLView from your homedir with\n" +
158  "absolute path (e.g. C:\\Programs\\BALLView\\BALLView).\n");
159  }
160  }
161 
162  // =============== initialize Mainframe ============================================
163  // Create the mainframe.
164  BALL::Mainframe mainframe(0, "Mainframe");
165 
166  // can we use the users homedir as working dir?
167  if (home_dir != "")
168  {
169  mainframe.setWorkingDir(home_dir);
170  }
171 
172  // Register the mainfram (required for Python support).
173  mainframe.setIdentifier("Mainframe");
174  mainframe.registerThis();
175 
176  // Show the main window.
177  mainframe.show();
178 
179  // =============== parsing command line arguments ==================================
180  // If there are additional command line arguments, interpret them as files to open or logging flag.
181  for (BALL::Index i = 1; i < argc; ++i)
182  {
183  BALL::String argument(argv[i]);
184  if (argument == "-l")
185  {
186  mainframe.enableLoggingToFile();
187  continue;
188  }
189  else if (argument == "-kiosk")
190  {
191  // the kiosk mode has already been handled
192  continue;
193  }
194 
195  mainframe.openFile(argument);
196  }
197 
198  // enable ending of program from python script
199  if (mainframe.isAboutToQuit())
200  {
201  mainframe.aboutToExit();
202  return 0;
203  }
204 
205  // Remove the splashscreen
206  splash->finish(&mainframe);
207  delete splash;
208 
209  // Hand over control to the application.
210  return application.exec();
211 }
void setWorkingDir(const String &dir)
Set the working directory for the next file dialog and file operation to the given directory...
int main(int argc, char **argv)
Definition: main.C:61
static String getUserHomeDir()
Get the home directory of the current user.
BALL_EXPORT LogStream Log
String getDataPath()
bool hasEntry(const String &section, const String &key) const
LogStream & info(int n=0)
void setMode(OperationMode new_mode)
static const char PATH_SEPARATOR
Definition: fileSystem.h:52
const char * c_str() const BALL_NOEXCEPT
static UIOperationMode & instance()
LogStream & warn(int n=0)
virtual void aboutToExit()
bool hasPrefix(const String &s) const
True, if the string starts with s
void logMessages(QtMsgType type, const char *msg)
Definition: main.C:35
virtual void registerThis()
void setIdentifier(const String &identifier)
static bool remove(String name)
static bool createTemporaryFilename(String &temporary, const String &suffix=".TMP")
String getValue(const String &section, const String &key) const
virtual void openFile(const String &file)