Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
main.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
3
4#include "mainwindow.h"
5#include <helpclient.h>
6
7#include <QtCore/QFile>
8#include <QtCore/QLibraryInfo>
9#include <QtCore/QLocale>
10#include <QtCore/QTranslator>
11#include <QtCore/qcommandlineparser.h>
12#include <QtCore/qcommandlineoption.h>
13
14#include <QtWidgets/QApplication>
15#include <QtGui/QPixmap>
16
17#ifdef Q_OS_MAC
18#include <QtCore/QUrl>
19#include <QtGui/QFileOpenEvent>
20#endif // Q_OS_MAC
21
22QT_USE_NAMESPACE
23
24using namespace Qt::Literals::StringLiterals;
25
26#ifdef Q_OS_MAC
27class ApplicationEventFilter : public QObject
28{
29 Q_OBJECT
30
31public:
32 ApplicationEventFilter()
33 : m_mainWindow(0)
34 {
35 }
36
37 void setMainWindow(MainWindow *mw)
38 {
39 m_mainWindow = mw;
40 if (!m_filesToOpen.isEmpty() && m_mainWindow) {
41 m_mainWindow->openFiles(m_filesToOpen);
42 m_filesToOpen.clear();
43 }
44 }
45
46protected:
47 bool eventFilter(QObject *object, QEvent *event) override
48 {
49 if (object == qApp && event->type() == QEvent::FileOpen) {
50 QFileOpenEvent *e = static_cast<QFileOpenEvent*>(event);
51 QString file = e->url().toLocalFile();
52 if (!m_mainWindow)
53 m_filesToOpen << file;
54 else
55 m_mainWindow->openFiles(QStringList() << file);
56 return true;
57 }
58 return QObject::eventFilter(object, event);
59 }
60
61private:
62 MainWindow *m_mainWindow;
63 QStringList m_filesToOpen;
64};
65#endif // Q_OS_MAC
66
67int main(int argc, char **argv)
68{
69 QApplication app(argc, argv);
70 QCoreApplication::setApplicationVersion(QLatin1StringView(qVersion()));
71 QCoreApplication::setApplicationName(u"Qt Linguist"_s);
72 QApplication::setOverrideCursor(Qt::WaitCursor);
73
74#ifdef Q_OS_MAC
75 ApplicationEventFilter eventFilter;
76 app.installEventFilter(&eventFilter);
77#endif // Q_OS_MAC
78
79
80 QCommandLineParser parser;
81 parser.setApplicationDescription(QCoreApplication::applicationName() + "\n\n"_L1 + MainWindow::description());
82 parser.addHelpOption();
83 parser.addVersionOption();
84 parser.setSingleDashWordOptionMode(QCommandLineParser::ParseAsLongOptions);
85
86 const QCommandLineOption resourceDirOption(u"resourcedir"_s,
87 u"Resource directory"_s,
88 u"directory"_s);
89 parser.addOption(resourceDirOption);
90
91 const QCommandLineOption webHelpOption(u"web-help"_s, u"Use the Web documentation"_s);
92 parser.addOption(webHelpOption);
93
94 parser.addPositionalArgument(u"files"_s, u"The .ts files to open."_s);
95
96 parser.process(app);
97
98 QString resourceDir = parser.isSet(resourceDirOption)
99 ? parser.value(resourceDirOption) : QLibraryInfo::path(QLibraryInfo::TranslationsPath);
100
101 QTranslator translator;
102 QTranslator qtTranslator;
103 if (translator.load(QLocale(), "linguist"_L1, "_"_L1, resourceDir)) {
104 app.installTranslator(&translator);
105 if (qtTranslator.load(QLocale(), "qt"_L1, "_"_L1, resourceDir))
106 app.installTranslator(&qtTranslator);
107 else
108 app.removeTranslator(&translator);
109 }
110
111 app.setOrganizationName("QtProject"_L1);
112 app.setApplicationName("Linguist"_L1);
113
114 MainWindow mw(parser.isSet(webHelpOption) ? HelpClientType::Web : HelpClientType::Assistant);
115#ifdef Q_OS_MAC
116 eventFilter.setMainWindow(&mw);
117#endif // Q_OS_MAC
118 app.installEventFilter(&mw);
119 mw.show();
120 QApplication::restoreOverrideCursor();
121
122 mw.openFiles(parser.positionalArguments());
123
124 return app.exec();
125}
126
127#ifdef Q_OS_MAC
128#include "main.moc"
129#endif // Q_OS_MAC
HelpClientType
Definition helpclient.h:16
int main(int argc, char *argv[])
[ctor_close]