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