| 1 | // This file is part of Dust Racing 2D. |
| 2 | // Copyright (C) 2017 Jussi Lind <[email protected]> |
| 3 | // |
| 4 | // Dust Racing 2D is free software: you can redistribute it and/or modify |
| 5 | // it under the terms of the GNU General Public License as published by |
| 6 | // the Free Software Foundation, either version 3 of the License, or |
| 7 | // (at your option) any later version. |
| 8 | // Dust Racing 2D is distributed in the hope that it will be useful, |
| 9 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | // GNU General Public License for more details. |
| 12 | // |
| 13 | // You should have received a copy of the GNU General Public License |
| 14 | // along with Dust Racing 2D. If not, see <http://www.gnu.org/licenses/>. |
| 15 | |
| 16 | #include "application.hpp" |
| 17 | #include "argengine.hpp" |
| 18 | #include "mainwindow.hpp" |
| 19 | |
| 20 | #include "../common/config.hpp" |
| 21 | #include "../common/userexception.hpp" |
| 22 | |
| 23 | #include <QLocale> |
| 24 | #include <QSettings> |
| 25 | |
| 26 | #include <iostream> |
| 27 | |
| 28 | static void initTranslations(QTranslator & appTranslator, QGuiApplication & app, QString lang = "") |
| 29 | { |
| 30 | if (lang == "") |
| 31 | { |
| 32 | lang = QLocale::system().name(); |
| 33 | } |
| 34 | |
| 35 | if (appTranslator.load(QString(DATA_PATH) + "/translations/dustrac-editor_" + lang)) |
| 36 | { |
| 37 | app.installTranslator(&appTranslator); |
| 38 | std::cout << "Loaded translations for " << lang.toStdString() << std::endl; |
| 39 | } |
| 40 | else |
| 41 | { |
| 42 | std::cerr << "Failed to load translations for " << lang.toStdString() << std::endl; |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | void Application::parseArgs(int argc, char ** argv) |
| 47 | { |
| 48 | QString lang = ""; |
| 49 | |
| 50 | juzzlin::Argengine ae(argc, argv); |
| 51 | |
| 52 | ae.addOption( |
| 53 | { "--lang" }, [&](std::string value) { |
| 54 | lang = value.c_str(); |
| 55 | }, |
| 56 | false, "Force language: fi, fr, it, cs, nl, tr."); |
| 57 | |
| 58 | ae.setHelpText(std::string("Dust Racing 2D version ") + VERSION + "\n" + // |
| 59 | Config::General::COPYRIGHT + "\n\nUsage: " + std::string(argv[0]) + " [OPTIONS] [TRACK_FILE]"); |
| 60 | |
| 61 | ae.setPositionalArgumentCallback([=](juzzlin::Argengine::ArgumentVector args) { |
| 62 | m_trackFile = args.at(0).c_str(); |
| 63 | }); |
| 64 | |
| 65 | ae.parse(); |
| 66 | |
| 67 | initTranslations(m_appTranslator, m_app, lang); |
| 68 | } |
| 69 | |
| 70 | Application::Application(int & argc, char ** argv) |
| 71 | : m_app(argc, argv) |
| 72 | { |
| 73 | parseArgs(argc, argv); |
| 74 | |
| 75 | m_mainWindow = new MainWindow(m_trackFile); |
| 76 | m_mainWindow->show(); |
| 77 | } |
| 78 | |
| 79 | int Application::run() |
| 80 | { |
| 81 | return m_app.exec(); |
| 82 | } |
| 83 | |
| 84 | Application::~Application() |
| 85 | { |
| 86 | delete m_mainWindow; |
| 87 | } |