Mercurial > retraceit
diff 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 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main.cpp Mon Mar 23 12:41:52 2015 +0100 @@ -0,0 +1,172 @@ +/* Copyright (C) 2014 by Intevation GmbH + * + * This file is Free Software under the GNU GPL (v>=2) + * and comes with ABSOLUTELY NO WARRANTY! + * See LICENSE.txt for details. + */ + +/** @file Main entry point for the application. + * + * This file is the wrapper around the qt application. + * Does command line parsing and preparation of the QApplication. + */ +#include "constants.h" + +#include "mainwindow.h" + +#include "strhelp.h" +#include "util.h" + +#include <QApplication> +#include <QCommandLineParser> +#include <QtPlugin> +#include <QDebug> +#include <QTranslator> +#include <QSettings> +#include <QFontDatabase> +#include <QSplashScreen> +#include <QTimer> + +#ifdef Q_OS_WIN + + #include <windows.h> + #include <shlobj.h> + + Q_IMPORT_PLUGIN(QWindowsIntegrationPlugin) +#else +/* this is only necessary if we build statically for GNU/Linux */ +// Q_IMPORT_PLUGIN(QXcbIntegrationPlugin) +#endif + +#ifdef IS_TAG_BUILD +bool g_debug = false; +#else +bool g_debug = true; +#endif + +QtMessageHandler g_default_msg_handler = NULL; + +void filterDebugOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg) +{ + if (!g_debug && type == QtDebugMsg) { + return; + } + + if (g_default_msg_handler) { + (*g_default_msg_handler)(type, context, msg); + } +} + +int realMain(int argc, char **argv); + +#if defined(WIN32) && defined(UNICODE) + +/** @brief Unicode entry point. + * + * Converts arguments to UTF-8 and executes the real + * entry point realMain. + */ +int wmain(int argc, wchar_t **argv, wchar_t **envp) +{ + char **utf8args = NULL; + + utf8args = (char**) xmalloc0 ((argc + 1) * sizeof(char*)); + + for (int i = 0; i < argc; i++) { + utf8args[i] = wchar_to_utf8(argv[i], wcslen(argv[i])); + if (utf8args[i] == NULL) { + printf ("Fatal: could not convert arguments to UTF-8.\n"); + exit(-1); + } + } + int ret = realMain(argc, utf8args); + strv_free(utf8args); + + return ret; +} +#else +int main(int argc, char **argv) +{ + return realMain(argc, argv); +} +#endif + +/** @brief The real entry point to the application. + * + * @param [in] argc the count of the arguments. + * @param [in] argv On GNU/Linux this function expects argv to be in the + * native system encoding. On Windows the arguments + * shall be UTF-8 + * + * @returns 0 on success an error code otherwise. */ +int realMain(int argc, char **argv) +{ + /* QApplication setup */ + QApplication app (argc, argv); + QApplication::setQuitOnLastWindowClosed(true); + QApplication::setOrganizationName(QString::fromLatin1(ORGANIZATION)); + QApplication::setApplicationName(QString::fromLatin1(APPNAME)); + QApplication::setApplicationVersion(QString::fromLatin1(VERSION)); + QSettings::setDefaultFormat(QSettings::IniFormat); + + /* Setup translations */ + QTranslator translator; + if (QLocale::system().name() == "C") { + /* Useful for testing / development as the primary target is german */ + translator.load(":/l10n/main_de_DE"); + } else { + translator.load(":/l10n/main_" + QLocale::system().name()); + } + app.installTranslator(&translator); + + /* Install static fonts */ + + /* The putenv here works around a bug in qt (#29192). Qt thinks it is a fatal + * error if the font directory does not exist. Will be fixed in Qt 5.4 */ + char * inst_dir = get_install_dir(); + if (inst_dir) { + qputenv("QT_QPA_FONTDIR", inst_dir); + xfree(inst_dir); + } + int fontId = QFontDatabase::addApplicationFont(":/fonts/DejaVuSans.ttf"); + if (fontId != -1) + { + QFont font("DejaVuSans"); + font.setPointSize(9); + app.setFont(font); + } + + /* Parse the command line */ + QCommandLineParser parser; + parser.setApplicationDescription(QObject::tr(DESCRIPTION) + "\n" + + QObject::tr(COPYRIGHT)); + parser.addHelpOption(); + + parser.addPositionalArgument("folder", QCoreApplication::translate("main", "The folder containing the data to replay.")); + QCommandLineOption debugOpt(QStringList() << "debug", + QCoreApplication::translate("main", "Print debug output.")); + parser.addOption(debugOpt); + + parser.process(app); + +#ifdef IS_TAG_BUILD + g_debug = parser.isSet(debugOpt); +#else + g_debug = true; +#endif + g_default_msg_handler = qInstallMessageHandler(filterDebugOutput); + + + /* Initialize the main window */ + MainWindow mainWin; + + const QStringList args = parser.positionalArguments(); + if (args.isEmpty() || args.size() > 1) { + parser.showHelp(1); + } + mainWin.setFolder(args.first()); + + mainWin.show(); + + return app.exec(); /* Go event loop */ +}