1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the Qt Linguist of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
21 | ** included in the packaging of this file. Please review the following |
22 | ** information to ensure the GNU General Public License requirements will |
23 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
24 | ** |
25 | ** $QT_END_LICENSE$ |
26 | ** |
27 | ****************************************************************************/ |
28 | |
29 | #include "mainwindow.h" |
30 | #include "globals.h" |
31 | |
32 | #include <QtCore/QFile> |
33 | #include <QtCore/QLibraryInfo> |
34 | #include <QtCore/QLocale> |
35 | #include <QtCore/QTranslator> |
36 | |
37 | #include <QtWidgets/QApplication> |
38 | #include <QtGui/QPixmap> |
39 | |
40 | #ifdef Q_OS_MAC |
41 | #include <QtCore/QUrl> |
42 | #include <QtGui/QFileOpenEvent> |
43 | #endif // Q_OS_MAC |
44 | |
45 | QT_USE_NAMESPACE |
46 | |
47 | #ifdef Q_OS_MAC |
48 | class ApplicationEventFilter : public QObject |
49 | { |
50 | Q_OBJECT |
51 | |
52 | public: |
53 | ApplicationEventFilter() |
54 | : m_mainWindow(0) |
55 | { |
56 | } |
57 | |
58 | void setMainWindow(MainWindow *mw) |
59 | { |
60 | m_mainWindow = mw; |
61 | if (!m_filesToOpen.isEmpty() && m_mainWindow) { |
62 | m_mainWindow->openFiles(m_filesToOpen); |
63 | m_filesToOpen.clear(); |
64 | } |
65 | } |
66 | |
67 | protected: |
68 | bool eventFilter(QObject *object, QEvent *event) |
69 | { |
70 | if (object == qApp && event->type() == QEvent::FileOpen) { |
71 | QFileOpenEvent *e = static_cast<QFileOpenEvent*>(event); |
72 | QString file = e->url().toLocalFile(); |
73 | if (!m_mainWindow) |
74 | m_filesToOpen << file; |
75 | else |
76 | m_mainWindow->openFiles(QStringList() << file); |
77 | return true; |
78 | } |
79 | return QObject::eventFilter(object, event); |
80 | } |
81 | |
82 | private: |
83 | MainWindow *m_mainWindow; |
84 | QStringList m_filesToOpen; |
85 | }; |
86 | #endif // Q_OS_MAC |
87 | |
88 | int main(int argc, char **argv) |
89 | { |
90 | Q_INIT_RESOURCE(linguist); |
91 | |
92 | QCoreApplication::setAttribute(attribute: Qt::AA_EnableHighDpiScaling); |
93 | QCoreApplication::setAttribute(attribute: Qt::AA_UseHighDpiPixmaps); |
94 | |
95 | QApplication app(argc, argv); |
96 | QApplication::setOverrideCursor(Qt::WaitCursor); |
97 | |
98 | #ifdef Q_OS_MAC |
99 | ApplicationEventFilter eventFilter; |
100 | app.installEventFilter(&eventFilter); |
101 | #endif // Q_OS_MAC |
102 | |
103 | QStringList files; |
104 | QString resourceDir = QLibraryInfo::location(QLibraryInfo::TranslationsPath); |
105 | QStringList args = app.arguments(); |
106 | |
107 | for (int i = 1; i < args.count(); ++i) { |
108 | QString argument = args.at(i); |
109 | if (argument == QLatin1String("-resourcedir" )) { |
110 | if (i + 1 < args.count()) { |
111 | resourceDir = QFile::decodeName(localFileName: args.at(i: ++i).toLocal8Bit()); |
112 | } else { |
113 | // issue a warning |
114 | } |
115 | } else if (!files.contains(str: argument)) { |
116 | files.append(t: argument); |
117 | } |
118 | } |
119 | |
120 | QTranslator translator; |
121 | QTranslator qtTranslator; |
122 | QString sysLocale = QLocale::system().name(); |
123 | if (translator.load(filename: QLatin1String("linguist_" ) + sysLocale, directory: resourceDir)) { |
124 | app.installTranslator(messageFile: &translator); |
125 | if (qtTranslator.load(filename: QLatin1String("qt_" ) + sysLocale, directory: resourceDir)) |
126 | app.installTranslator(messageFile: &qtTranslator); |
127 | else |
128 | app.removeTranslator(messageFile: &translator); |
129 | } |
130 | |
131 | app.setOrganizationName(QLatin1String("QtProject" )); |
132 | app.setApplicationName(QLatin1String("Linguist" )); |
133 | |
134 | MainWindow mw; |
135 | #ifdef Q_OS_MAC |
136 | eventFilter.setMainWindow(&mw); |
137 | #endif // Q_OS_MAC |
138 | mw.show(); |
139 | QApplication::restoreOverrideCursor(); |
140 | |
141 | mw.openFiles(names: files, readWrite: true); |
142 | |
143 | return app.exec(); |
144 | } |
145 | |
146 | #ifdef Q_OS_MAC |
147 | #include "main.moc" |
148 | #endif // Q_OS_MAC |
149 | |