1// Copyright (C) 2017 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QWIDGETPLATFORM_P_H
5#define QWIDGETPLATFORM_P_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists purely as an
12// implementation detail. This header file may change from version to
13// version without notice, or even be removed.
14//
15// We mean it.
16//
17
18#include <QtCore/qdebug.h>
19#include <QtCore/qcoreapplication.h>
20#include <QtGui/qpa/qplatformtheme.h>
21#include <QtGui/qpa/qplatformdialoghelper.h>
22#include <QtGui/qpa/qplatformsystemtrayicon.h>
23#include <QtGui/qpa/qplatformmenu.h>
24
25#ifdef QT_WIDGETS_LIB
26#include <QtWidgets/qtwidgetsglobal.h>
27#if QT_CONFIG(colordialog)
28#include "qwidgetplatformcolordialog_p.h"
29#endif
30#if QT_CONFIG(filedialog)
31#include "qwidgetplatformfiledialog_p.h"
32#endif
33#if QT_CONFIG(fontdialog)
34#include "qwidgetplatformfontdialog_p.h"
35#endif
36#if QT_CONFIG(messagebox)
37#include "qwidgetplatformmessagedialog_p.h"
38#endif
39#if QT_CONFIG(menu)
40#include "qwidgetplatformmenu_p.h"
41#include "qwidgetplatformmenuitem_p.h"
42#endif
43#ifndef QT_NO_SYSTEMTRAYICON
44#include "qwidgetplatformsystemtrayicon_p.h"
45#endif
46#endif
47
48QT_BEGIN_NAMESPACE
49
50#ifndef QT_WIDGETS_LIB
51typedef QPlatformMenu QWidgetPlatformMenu;
52typedef QPlatformMenuItem QWidgetPlatformMenuItem;
53typedef QPlatformColorDialogHelper QWidgetPlatformColorDialog;
54typedef QPlatformFileDialogHelper QWidgetPlatformFileDialog;
55typedef QPlatformFontDialogHelper QWidgetPlatformFontDialog;
56typedef QPlatformMessageDialogHelper QWidgetPlatformMessageDialog;
57typedef QPlatformSystemTrayIcon QWidgetPlatformSystemTrayIcon;
58#endif
59
60namespace QWidgetPlatform
61{
62 static inline bool isAvailable(const char *type)
63 {
64 if (!qApp->inherits(classname: "QApplication")) {
65 qCritical(msg: "\nERROR: No native %s implementation available."
66 "\nQt Labs Platform requires Qt Widgets on this setup."
67 "\nAdd 'QT += widgets' to .pro and create QApplication in main().\n", type);
68 return false;
69 }
70 return true;
71 }
72
73 template<typename T>
74 static inline T *createWidget(const char *name, QObject *parent)
75 {
76 static bool available = isAvailable(type: name);
77#ifdef QT_WIDGETS_LIB
78 if (available)
79 return new T(parent);
80#else
81 Q_UNUSED(parent);
82 Q_UNUSED(available);
83#endif
84 return nullptr;
85 }
86
87 static inline QPlatformMenu *createMenu(QObject *parent = nullptr) {
88#if defined(QT_WIDGETS_LIB) && QT_CONFIG(menu)
89 return createWidget<QWidgetPlatformMenu>(name: "Menu", parent);
90#else
91 Q_UNUSED(parent);
92 return nullptr;
93#endif
94 }
95 static inline QPlatformMenuItem *createMenuItem(QObject *parent = nullptr) {
96#if defined(QT_WIDGETS_LIB) && QT_CONFIG(menu)
97 return createWidget<QWidgetPlatformMenuItem>(name: "MenuItem", parent);
98#else
99 Q_UNUSED(parent);
100 return nullptr;
101#endif
102 }
103 static inline QPlatformSystemTrayIcon *createSystemTrayIcon(QObject *parent = nullptr) {
104#ifndef QT_NO_SYSTEMTRAYICON
105 return createWidget<QWidgetPlatformSystemTrayIcon>(name: "SystemTrayIcon", parent);
106#else
107 Q_UNUSED(parent);
108 return nullptr;
109#endif
110 }
111 static inline QPlatformDialogHelper *createDialog(QPlatformTheme::DialogType type, QObject *parent = nullptr)
112 {
113#if !defined(QT_WIDGETS_LIB) || !(QT_CONFIG(colordialog) || QT_CONFIG(filedialog) || QT_CONFIG(fontdialog) || QT_CONFIG(messagebox))
114 Q_UNUSED(parent);
115#endif
116 switch (type) {
117#if defined(QT_WIDGETS_LIB) && QT_CONFIG(colordialog)
118 case QPlatformTheme::ColorDialog: return createWidget<QWidgetPlatformColorDialog>(name: "ColorDialog", parent);
119#endif
120#if defined(QT_WIDGETS_LIB) && QT_CONFIG(filedialog)
121 case QPlatformTheme::FileDialog: return createWidget<QWidgetPlatformFileDialog>(name: "FileDialog", parent);
122#endif
123#if defined(QT_WIDGETS_LIB) && QT_CONFIG(fontdialog)
124 case QPlatformTheme::FontDialog: return createWidget<QWidgetPlatformFontDialog>(name: "FontDialog", parent);
125#endif
126#if defined(QT_WIDGETS_LIB) && QT_CONFIG(messagebox)
127 case QPlatformTheme::MessageDialog: return createWidget<QWidgetPlatformMessageDialog>(name: "MessageDialog", parent);
128#endif
129 default: break;
130 }
131 return nullptr;
132 }
133}
134
135QT_END_NAMESPACE
136
137#endif // QWIDGETPLATFORM_P_H
138

source code of qtdeclarative/src/labs/platform/widgets/qwidgetplatform_p.h