cool-retro-term
126 строк · 4.7 Кб
1#include <QtQml/QQmlApplicationEngine>
2#include <QtGui/QGuiApplication>
3
4#include <QQmlContext>
5#include <QStringList>
6
7#include <QtWidgets/QApplication>
8#include <QIcon>
9#include <QQuickStyle>
10
11#include <QDebug>
12#include <stdlib.h>
13
14#include <QFontDatabase>
15#include <QLoggingCategory>
16
17#include <fileio.h>
18#include <monospacefontmanager.h>
19
20QString getNamedArgument(QStringList args, QString name, QString defaultName)
21{
22int index = args.indexOf(name);
23return (index != -1) ? args[index + 1] : QString(defaultName);
24}
25
26QString getNamedArgument(QStringList args, QString name)
27{
28return getNamedArgument(args, name, "");
29}
30
31int main(int argc, char *argv[])
32{
33// Some environmental variable are necessary on certain platforms.
34
35// This disables QT appmenu under Ubuntu, which is not working with QML apps.
36setenv("QT_QPA_PLATFORMTHEME", "", 1);
37
38// Disable Connections slot warnings
39QLoggingCategory::setFilterRules("qt.qml.connections.warning=false");
40
41#if defined (Q_OS_LINUX)
42setenv("QSG_RENDER_LOOP", "threaded", 0);
43#endif
44
45#if defined(Q_OS_MAC)
46// This allows UTF-8 characters usage in OSX.
47setenv("LC_CTYPE", "UTF-8", 1);
48#endif
49
50if (argc>1 && (!strcmp(argv[1],"-h") || !strcmp(argv[1],"--help"))) {
51QTextStream cout(stdout, QIODevice::WriteOnly);
52cout << "Usage: " << argv[0] << " [--default-settings] [--workdir <dir>] [--program <prog>] [-p|--profile <prof>] [--fullscreen] [-h|--help]" << endl;
53cout << " --default-settings Run cool-retro-term with the default settings" << endl;
54cout << " --workdir <dir> Change working directory to 'dir'" << endl;
55cout << " -e <cmd> Command to execute. This option will catch all following arguments, so use it as the last option." << endl;
56cout << " -T <title> Set window title to 'title'." << endl;
57cout << " --fullscreen Run cool-retro-term in fullscreen." << endl;
58cout << " -p|--profile <prof> Run cool-retro-term with the given profile." << endl;
59cout << " -h|--help Print this help." << endl;
60cout << " --verbose Print additional information such as profiles and settings." << endl;
61return 0;
62}
63
64QString appVersion("1.2.0");
65
66if (argc>1 && (!strcmp(argv[1],"-v") || !strcmp(argv[1],"--version"))) {
67QTextStream cout(stdout, QIODevice::WriteOnly);
68cout << "cool-retro-term " << appVersion << endl;
69return 0;
70}
71
72QApplication app(argc, argv);
73app.setAttribute(Qt::AA_MacDontSwapCtrlAndMeta, true);
74
75QQmlApplicationEngine engine;
76FileIO fileIO;
77MonospaceFontManager monospaceFontManager;
78
79#if !defined(Q_OS_MAC)
80app.setWindowIcon(QIcon::fromTheme("cool-retro-term", QIcon(":../icons/32x32/cool-retro-term.png")));
81#else
82app.setWindowIcon(QIcon(":../icons/32x32/cool-retro-term.png"));
83#endif
84
85app.setOrganizationName("cool-retro-term");
86app.setOrganizationDomain("cool-retro-term");
87
88// Manage command line arguments from the cpp side
89QStringList args = app.arguments();
90
91// Manage default command
92QStringList cmdList;
93if (args.contains("-e")) {
94cmdList << args.mid(args.indexOf("-e") + 1);
95}
96QVariant command(cmdList.empty() ? QVariant() : cmdList[0]);
97QVariant commandArgs(cmdList.size() <= 1 ? QVariant() : QVariant(cmdList.mid(1)));
98engine.rootContext()->setContextProperty("appVersion", appVersion);
99engine.rootContext()->setContextProperty("defaultCmd", command);
100engine.rootContext()->setContextProperty("defaultCmdArgs", commandArgs);
101
102engine.rootContext()->setContextProperty("workdir", getNamedArgument(args, "--workdir", "$HOME"));
103engine.rootContext()->setContextProperty("fileIO", &fileIO);
104engine.rootContext()->setContextProperty("monospaceSystemFonts", monospaceFontManager.retrieveMonospaceFonts());
105
106engine.rootContext()->setContextProperty("devicePixelRatio", app.devicePixelRatio());
107
108// Manage import paths for Linux and OSX.
109QStringList importPathList = engine.importPathList();
110importPathList.prepend(QCoreApplication::applicationDirPath() + "/qmltermwidget");
111importPathList.prepend(QCoreApplication::applicationDirPath() + "/../PlugIns");
112importPathList.prepend(QCoreApplication::applicationDirPath() + "/../../../qmltermwidget");
113engine.setImportPathList(importPathList);
114
115engine.load(QUrl(QStringLiteral ("qrc:/main.qml")));
116
117if (engine.rootObjects().isEmpty()) {
118qDebug() << "Cannot load QML interface";
119return EXIT_FAILURE;
120}
121
122// Quit the application when the engine closes.
123QObject::connect((QObject*) &engine, SIGNAL(quit()), (QObject*) &app, SLOT(quit()));
124
125return app.exec();
126}
127