MUtilities development repository
修订版 | b7d04c37691ee33d3241699e4b11b3c566d70eba (tree) |
---|---|
时间 | 2017-01-08 02:08:24 |
作者 | ![]() |
Commiter | LoRd_MuldeR |
Some refactoring and clean-up in create_qt() method.
@@ -40,7 +40,7 @@ namespace MUtils | ||
40 | 40 | MUTILS_API int startup(int &argc, char **argv, main_function_t *const entry_point, const char* const appName, const bool &debugConsole); |
41 | 41 | |
42 | 42 | //Initialize Qt |
43 | - MUTILS_API QApplication *create_qt(int &argc, char **argv, const QString &appName); | |
43 | + MUTILS_API QApplication *create_qt(int &argc, char **argv, const QString &appName, const QString &appAuthor = QLatin1String("LoRd_MuldeR"), const QString &appDomain = QLatin1String("muldersoft.com")); | |
44 | 44 | } |
45 | 45 | } |
46 | 46 |
@@ -39,11 +39,21 @@ | ||
39 | 39 | #include <QtPlugin> |
40 | 40 | #if QT_VERSION >= QT_VERSION_CHECK(5,0,0) |
41 | 41 | #include <QAbstractNativeEventFilter> |
42 | +#else | |
43 | +#define QAbstractNativeEventFilter QObject | |
44 | +#define Q_DECL_OVERRIDE | |
42 | 45 | #endif |
43 | 46 | |
44 | 47 | //CRT |
45 | 48 | #include <string.h> |
46 | 49 | |
50 | +//MSVC | |
51 | +#if defined(_MSC_VER) | |
52 | +#define FORCE_INLINE __forceinline | |
53 | +#else | |
54 | +#define FORCE_INLINE inline | |
55 | +#endif | |
56 | + | |
47 | 57 | /////////////////////////////////////////////////////////////////////////////// |
48 | 58 | // Qt Static Initialization |
49 | 59 | /////////////////////////////////////////////////////////////////////////////// |
@@ -126,12 +136,10 @@ static void qt_message_handler(QtMsgType type, const QMessageLogContext&, const | ||
126 | 136 | } |
127 | 137 | #endif |
128 | 138 | |
129 | -#if QT_VERSION < QT_VERSION_CHECK(5,0,0) | |
130 | -static bool qt_event_filter(void *message, long *result) | |
131 | -{ | |
132 | - return MUtils::OS::handle_os_message(message, result); | |
133 | -} | |
134 | -#else | |
139 | +/////////////////////////////////////////////////////////////////////////////// | |
140 | +// EVENT FILTER | |
141 | +/////////////////////////////////////////////////////////////////////////////// | |
142 | + | |
135 | 143 | namespace MUtils |
136 | 144 | { |
137 | 145 | namespace Startup |
@@ -143,27 +151,43 @@ namespace MUtils | ||
143 | 151 | public: |
144 | 152 | bool nativeEventFilter(const QByteArray&, void *message, long *result) Q_DECL_OVERRIDE |
145 | 153 | { |
146 | - return MUtils::OS::handle_os_message(message, result); | |
154 | + return filterEvent(message, result); | |
147 | 155 | }; |
156 | + | |
157 | + static FORCE_INLINE bool filterEvent(void *message, long *result) | |
158 | + { | |
159 | + return MUtils::OS::handle_os_message(message, result); | |
160 | + } | |
161 | + | |
162 | + static NativeEventFilter *instance(void) | |
163 | + { | |
164 | + while (m_instance.isNull()) | |
165 | + { | |
166 | + m_instance.reset(new NativeEventFilter()); | |
167 | + } | |
168 | + return m_instance.data(); | |
169 | + } | |
170 | + | |
171 | + private: | |
172 | + NativeEventFilter(void) {} | |
173 | + static QScopedPointer<MUtils::Startup::Internal::NativeEventFilter> m_instance; | |
148 | 174 | }; |
149 | 175 | } |
150 | 176 | } |
151 | 177 | } |
152 | -static QScopedPointer<MUtils::Startup::Internal::NativeEventFilter> qt_event_filter; | |
153 | -#endif | |
154 | 178 | |
155 | 179 | /////////////////////////////////////////////////////////////////////////////// |
156 | 180 | // STARTUP FUNCTION |
157 | 181 | /////////////////////////////////////////////////////////////////////////////// |
158 | 182 | |
159 | -static int startup_main(int &argc, char **argv, MUtils::Startup::main_function_t *const entry_point, const char* const appName, const bool &debugConsole) | |
183 | +static FORCE_INLINE int startup_main(int &argc, char **argv, MUtils::Startup::main_function_t *const entry_point, const char* const appName, const bool &debugConsole) | |
160 | 184 | { |
161 | 185 | qInstallMsgHandler(qt_message_handler); |
162 | 186 | MUtils::Terminal::setup(argc, argv, appName, MUTILS_DEBUG || debugConsole); |
163 | 187 | return entry_point(argc, argv); |
164 | 188 | } |
165 | 189 | |
166 | -static int startup_helper(int &argc, char **argv, MUtils::Startup::main_function_t *const entry_point, const char* const appName, const bool &debugConsole) | |
190 | +static FORCE_INLINE int startup_helper(int &argc, char **argv, MUtils::Startup::main_function_t *const entry_point, const char* const appName, const bool &debugConsole) | |
167 | 191 | { |
168 | 192 | int iResult = -1; |
169 | 193 | try |
@@ -220,7 +244,7 @@ int MUtils::Startup::startup(int &argc, char **argv, main_function_t *const entr | ||
220 | 244 | static QMutex g_init_lock; |
221 | 245 | static const char *const g_imageformats[] = {"bmp", "png", "jpg", "gif", "ico", "xpm", "svg", NULL}; |
222 | 246 | |
223 | -static QString getExecutableName(int &argc, char **argv) | |
247 | +static FORCE_INLINE QString getExecutableName(int &argc, char **argv) | |
224 | 248 | { |
225 | 249 | if(argc >= 1) |
226 | 250 | { |
@@ -241,22 +265,23 @@ static QString getExecutableName(int &argc, char **argv) | ||
241 | 265 | return QLatin1String("Program.exe"); |
242 | 266 | } |
243 | 267 | |
244 | -static void qt_registry_cleanup(void) | |
268 | +static FORCE_INLINE void qt_registry_cleanup(void) | |
245 | 269 | { |
246 | 270 | static const wchar_t *const QT_JUNK_KEY = L"Software\\Trolltech\\OrganizationDefaults"; |
247 | 271 | MUtils::Registry::reg_key_delete(MUtils::Registry::root_user, MUTILS_QSTR(QT_JUNK_KEY), true, true); |
248 | 272 | } |
249 | 273 | |
250 | -QApplication *MUtils::Startup::create_qt(int &argc, char **argv, const QString &appName) | |
274 | +QApplication *MUtils::Startup::create_qt(int &argc, char **argv, const QString &appName, const QString &appAuthor, const QString &appDomain) | |
251 | 275 | { |
252 | 276 | QMutexLocker lock(&g_init_lock); |
253 | 277 | const OS::ArgumentMap &arguments = MUtils::OS::arguments(); |
254 | 278 | |
255 | 279 | //Don't initialized again, if done already |
256 | - if(QApplication::instance() != NULL) | |
280 | + QScopedPointer<QApplication> application(dynamic_cast<QApplication*>(QApplication::instance())); | |
281 | + if(!application.isNull()) | |
257 | 282 | { |
258 | 283 | qWarning("Qt is already initialized!"); |
259 | - return NULL; | |
284 | + return application.take(); | |
260 | 285 | } |
261 | 286 | |
262 | 287 | //Extract executable name from argv[] array |
@@ -321,7 +346,7 @@ QApplication *MUtils::Startup::create_qt(int &argc, char **argv, const QString & | ||
321 | 346 | QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8")); |
322 | 347 | |
323 | 348 | //Create Qt application instance |
324 | - QApplication *application = new QApplication(argc, argv); | |
349 | + application.reset(new QApplication(argc, argv)); | |
325 | 350 | |
326 | 351 | //Register the Qt clean-up function |
327 | 352 | atexit(qt_registry_cleanup); |
@@ -332,13 +357,12 @@ QApplication *MUtils::Startup::create_qt(int &argc, char **argv, const QString & | ||
332 | 357 | |
333 | 358 | //Set application properties |
334 | 359 | application->setApplicationName(appName); |
335 | - application->setOrganizationName("LoRd_MuldeR"); | |
336 | - application->setOrganizationDomain("mulder.at.gg"); | |
360 | + application->setOrganizationDomain(appDomain); | |
361 | + application->setOrganizationName(appAuthor); | |
337 | 362 | #if QT_VERSION < QT_VERSION_CHECK(5,0,0) |
338 | - application->setEventFilter(qt_event_filter); | |
363 | + application->setEventFilter(&Internal::NativeEventFilter::filterEvent); | |
339 | 364 | #else |
340 | - qt_event_filter.reset(new Internal::NativeEventFilter); | |
341 | - application->installNativeEventFilter(qt_event_filter.data()); | |
365 | + application->installNativeEventFilter(Internal::NativeEventFilter::instance()); | |
342 | 366 | #endif |
343 | 367 | |
344 | 368 | //Check for supported image formats |
@@ -348,7 +372,6 @@ QApplication *MUtils::Startup::create_qt(int &argc, char **argv, const QString & | ||
348 | 372 | if(!supportedFormats.contains(g_imageformats[i])) |
349 | 373 | { |
350 | 374 | qFatal("Qt initialization error: QImageIOHandler for '%s' missing!", g_imageformats[i]); |
351 | - MUTILS_DELETE(application); | |
352 | 375 | return NULL; |
353 | 376 | } |
354 | 377 | } |
@@ -378,13 +401,12 @@ QApplication *MUtils::Startup::create_qt(int &argc, char **argv, const QString & | ||
378 | 401 | messageBox.addButton("Ignore", QMessageBox::NoRole); |
379 | 402 | if(messageBox.exec() == 0) |
380 | 403 | { |
381 | - MUTILS_DELETE(application); | |
382 | 404 | return NULL; |
383 | 405 | } |
384 | 406 | } |
385 | 407 | |
386 | - //Qt created successfully | |
387 | - return application; | |
408 | + //QApplication created successfully | |
409 | + return application.take(); | |
388 | 410 | } |
389 | 411 | |
390 | 412 | /////////////////////////////////////////////////////////////////////////////// |