comparison src/main.cpp @ 0:147b08bc7d64

Initial commit of a basic Application framework.
author Andre Heinecke <andre.heinecke@intevation.de>
date Mon, 23 Mar 2015 12:41:52 +0100
parents
children e3c8f61e45a9
comparison
equal deleted inserted replaced
-1:000000000000 0:147b08bc7d64
1 /* Copyright (C) 2014 by Intevation GmbH
2 *
3 * This file is Free Software under the GNU GPL (v>=2)
4 * and comes with ABSOLUTELY NO WARRANTY!
5 * See LICENSE.txt for details.
6 */
7
8 /** @file Main entry point for the application.
9 *
10 * This file is the wrapper around the qt application.
11 * Does command line parsing and preparation of the QApplication.
12 */
13 #include "constants.h"
14
15 #include "mainwindow.h"
16
17 #include "strhelp.h"
18 #include "util.h"
19
20 #include <QApplication>
21 #include <QCommandLineParser>
22 #include <QtPlugin>
23 #include <QDebug>
24 #include <QTranslator>
25 #include <QSettings>
26 #include <QFontDatabase>
27 #include <QSplashScreen>
28 #include <QTimer>
29
30 #ifdef Q_OS_WIN
31
32 #include <windows.h>
33 #include <shlobj.h>
34
35 Q_IMPORT_PLUGIN(QWindowsIntegrationPlugin)
36 #else
37 /* this is only necessary if we build statically for GNU/Linux */
38 // Q_IMPORT_PLUGIN(QXcbIntegrationPlugin)
39 #endif
40
41 #ifdef IS_TAG_BUILD
42 bool g_debug = false;
43 #else
44 bool g_debug = true;
45 #endif
46
47 QtMessageHandler g_default_msg_handler = NULL;
48
49 void filterDebugOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg)
50 {
51 if (!g_debug && type == QtDebugMsg) {
52 return;
53 }
54
55 if (g_default_msg_handler) {
56 (*g_default_msg_handler)(type, context, msg);
57 }
58 }
59
60 int realMain(int argc, char **argv);
61
62 #if defined(WIN32) && defined(UNICODE)
63
64 /** @brief Unicode entry point.
65 *
66 * Converts arguments to UTF-8 and executes the real
67 * entry point realMain.
68 */
69 int wmain(int argc, wchar_t **argv, wchar_t **envp)
70 {
71 char **utf8args = NULL;
72
73 utf8args = (char**) xmalloc0 ((argc + 1) * sizeof(char*));
74
75 for (int i = 0; i < argc; i++) {
76 utf8args[i] = wchar_to_utf8(argv[i], wcslen(argv[i]));
77 if (utf8args[i] == NULL) {
78 printf ("Fatal: could not convert arguments to UTF-8.\n");
79 exit(-1);
80 }
81 }
82 int ret = realMain(argc, utf8args);
83 strv_free(utf8args);
84
85 return ret;
86 }
87 #else
88 int main(int argc, char **argv)
89 {
90 return realMain(argc, argv);
91 }
92 #endif
93
94 /** @brief The real entry point to the application.
95 *
96 * @param [in] argc the count of the arguments.
97 * @param [in] argv On GNU/Linux this function expects argv to be in the
98 * native system encoding. On Windows the arguments
99 * shall be UTF-8
100 *
101 * @returns 0 on success an error code otherwise. */
102 int realMain(int argc, char **argv)
103 {
104 /* QApplication setup */
105 QApplication app (argc, argv);
106 QApplication::setQuitOnLastWindowClosed(true);
107 QApplication::setOrganizationName(QString::fromLatin1(ORGANIZATION));
108 QApplication::setApplicationName(QString::fromLatin1(APPNAME));
109 QApplication::setApplicationVersion(QString::fromLatin1(VERSION));
110 QSettings::setDefaultFormat(QSettings::IniFormat);
111
112 /* Setup translations */
113 QTranslator translator;
114 if (QLocale::system().name() == "C") {
115 /* Useful for testing / development as the primary target is german */
116 translator.load(":/l10n/main_de_DE");
117 } else {
118 translator.load(":/l10n/main_" + QLocale::system().name());
119 }
120 app.installTranslator(&translator);
121
122 /* Install static fonts */
123
124 /* The putenv here works around a bug in qt (#29192). Qt thinks it is a fatal
125 * error if the font directory does not exist. Will be fixed in Qt 5.4 */
126 char * inst_dir = get_install_dir();
127 if (inst_dir) {
128 qputenv("QT_QPA_FONTDIR", inst_dir);
129 xfree(inst_dir);
130 }
131 int fontId = QFontDatabase::addApplicationFont(":/fonts/DejaVuSans.ttf");
132 if (fontId != -1)
133 {
134 QFont font("DejaVuSans");
135 font.setPointSize(9);
136 app.setFont(font);
137 }
138
139 /* Parse the command line */
140 QCommandLineParser parser;
141 parser.setApplicationDescription(QObject::tr(DESCRIPTION) + "\n" +
142 QObject::tr(COPYRIGHT));
143 parser.addHelpOption();
144
145 parser.addPositionalArgument("folder", QCoreApplication::translate("main", "The folder containing the data to replay."));
146 QCommandLineOption debugOpt(QStringList() << "debug",
147 QCoreApplication::translate("main", "Print debug output."));
148 parser.addOption(debugOpt);
149
150 parser.process(app);
151
152 #ifdef IS_TAG_BUILD
153 g_debug = parser.isSet(debugOpt);
154 #else
155 g_debug = true;
156 #endif
157 g_default_msg_handler = qInstallMessageHandler(filterDebugOutput);
158
159
160 /* Initialize the main window */
161 MainWindow mainWin;
162
163 const QStringList args = parser.positionalArguments();
164 if (args.isEmpty() || args.size() > 1) {
165 parser.showHelp(1);
166 }
167 mainWin.setFolder(args.first());
168
169 mainWin.show();
170
171 return app.exec(); /* Go event loop */
172 }
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)