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 "globals.h" |
6 | |
7 | #include <QtCore/QFile> |
8 | #include <QtCore/QLibraryInfo> |
9 | #include <QtCore/QLocale> |
10 | #include <QtCore/QTranslator> |
11 | |
12 | #include <QtWidgets/QApplication> |
13 | #include <QtGui/QPixmap> |
14 | |
15 | #ifdef Q_OS_MAC |
16 | #include <QtCore/QUrl> |
17 | #include <QtGui/QFileOpenEvent> |
18 | #endif // Q_OS_MAC |
19 | |
20 | QT_USE_NAMESPACE |
21 | |
22 | #ifdef Q_OS_MAC |
23 | class ApplicationEventFilter : public QObject |
24 | { |
25 | Q_OBJECT |
26 | |
27 | public: |
28 | ApplicationEventFilter() |
29 | : m_mainWindow(0) |
30 | { |
31 | } |
32 | |
33 | void setMainWindow(MainWindow *mw) |
34 | { |
35 | m_mainWindow = mw; |
36 | if (!m_filesToOpen.isEmpty() && m_mainWindow) { |
37 | m_mainWindow->openFiles(m_filesToOpen); |
38 | m_filesToOpen.clear(); |
39 | } |
40 | } |
41 | |
42 | protected: |
43 | bool eventFilter(QObject *object, QEvent *event) override |
44 | { |
45 | if (object == qApp && event->type() == QEvent::FileOpen) { |
46 | QFileOpenEvent *e = static_cast<QFileOpenEvent*>(event); |
47 | QString file = e->url().toLocalFile(); |
48 | if (!m_mainWindow) |
49 | m_filesToOpen << file; |
50 | else |
51 | m_mainWindow->openFiles(QStringList() << file); |
52 | return true; |
53 | } |
54 | return QObject::eventFilter(object, event); |
55 | } |
56 | |
57 | private: |
58 | MainWindow *m_mainWindow; |
59 | QStringList m_filesToOpen; |
60 | }; |
61 | #endif // Q_OS_MAC |
62 | |
63 | int main(int argc, char **argv) |
64 | { |
65 | QApplication app(argc, argv); |
66 | QApplication::setOverrideCursor(Qt::WaitCursor); |
67 | |
68 | #ifdef Q_OS_MAC |
69 | ApplicationEventFilter eventFilter; |
70 | app.installEventFilter(&eventFilter); |
71 | #endif // Q_OS_MAC |
72 | |
73 | QStringList files; |
74 | QString resourceDir = QLibraryInfo::path(p: QLibraryInfo::TranslationsPath); |
75 | QStringList args = app.arguments(); |
76 | |
77 | for (int i = 1; i < args.size(); ++i) { |
78 | QString argument = args.at(i); |
79 | if (argument == QLatin1String("-resourcedir" )) { |
80 | if (i + 1 < args.size()) { |
81 | resourceDir = QFile::decodeName(localFileName: args.at(i: ++i).toLocal8Bit()); |
82 | } else { |
83 | // issue a warning |
84 | } |
85 | } else if (!files.contains(str: argument)) { |
86 | files.append(t: argument); |
87 | } |
88 | } |
89 | |
90 | QTranslator translator; |
91 | QTranslator qtTranslator; |
92 | if (translator.load(locale: QLocale(), filename: QLatin1String("linguist" ), prefix: QLatin1String("_" ), directory: resourceDir)) { |
93 | app.installTranslator(messageFile: &translator); |
94 | if (qtTranslator.load(locale: QLocale(), filename: QLatin1String("qt" ), prefix: QLatin1String("_" ), directory: resourceDir)) |
95 | app.installTranslator(messageFile: &qtTranslator); |
96 | else |
97 | app.removeTranslator(messageFile: &translator); |
98 | } |
99 | |
100 | app.setOrganizationName(QLatin1String("QtProject" )); |
101 | app.setApplicationName(QLatin1String("Linguist" )); |
102 | |
103 | MainWindow mw; |
104 | #ifdef Q_OS_MAC |
105 | eventFilter.setMainWindow(&mw); |
106 | #endif // Q_OS_MAC |
107 | mw.show(); |
108 | QApplication::restoreOverrideCursor(); |
109 | |
110 | mw.openFiles(names: files, readWrite: true); |
111 | |
112 | return app.exec(); |
113 | } |
114 | |
115 | #ifdef Q_OS_MAC |
116 | #include "main.moc" |
117 | #endif // Q_OS_MAC |
118 | |