1 | // Copyright (C) 2016 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 QITEMEDITORFACTORY_H |
5 | #define QITEMEDITORFACTORY_H |
6 | |
7 | #include <QtWidgets/qtwidgetsglobal.h> |
8 | #include <QtCore/qmetaobject.h> |
9 | #include <QtCore/qbytearray.h> |
10 | #include <QtCore/qhash.h> |
11 | #include <QtCore/qvariant.h> |
12 | |
13 | QT_REQUIRE_CONFIG(itemviews); |
14 | |
15 | QT_BEGIN_NAMESPACE |
16 | |
17 | class QWidget; |
18 | |
19 | class Q_WIDGETS_EXPORT QItemEditorCreatorBase |
20 | { |
21 | public: |
22 | virtual ~QItemEditorCreatorBase(); |
23 | |
24 | virtual QWidget *createWidget(QWidget *parent) const = 0; |
25 | virtual QByteArray valuePropertyName() const = 0; |
26 | }; |
27 | |
28 | template <class T> |
29 | class QItemEditorCreator : public QItemEditorCreatorBase |
30 | { |
31 | public: |
32 | inline explicit QItemEditorCreator(const QByteArray &valuePropertyName); |
33 | inline QWidget *createWidget(QWidget *parent) const override { return new T(parent); } |
34 | inline QByteArray valuePropertyName() const override { return propertyName; } |
35 | |
36 | private: |
37 | QByteArray propertyName; |
38 | }; |
39 | |
40 | template <class T> |
41 | class QStandardItemEditorCreator: public QItemEditorCreatorBase |
42 | { |
43 | public: |
44 | inline QStandardItemEditorCreator() |
45 | : propertyName(T::staticMetaObject.userProperty().name()) |
46 | {} |
47 | inline QWidget *createWidget(QWidget *parent) const override { return new T(parent); } |
48 | inline QByteArray valuePropertyName() const override { return propertyName; } |
49 | |
50 | private: |
51 | QByteArray propertyName; |
52 | }; |
53 | |
54 | |
55 | template <class T> |
56 | Q_INLINE_TEMPLATE QItemEditorCreator<T>::QItemEditorCreator(const QByteArray &avaluePropertyName) |
57 | : propertyName(avaluePropertyName) {} |
58 | |
59 | class Q_WIDGETS_EXPORT QItemEditorFactory |
60 | { |
61 | public: |
62 | inline QItemEditorFactory() {} |
63 | virtual ~QItemEditorFactory(); |
64 | |
65 | virtual QWidget *createEditor(int userType, QWidget *parent) const; |
66 | virtual QByteArray valuePropertyName(int userType) const; |
67 | |
68 | void registerEditor(int userType, QItemEditorCreatorBase *creator); |
69 | |
70 | static const QItemEditorFactory *defaultFactory(); |
71 | static void setDefaultFactory(QItemEditorFactory *factory); |
72 | |
73 | private: |
74 | QHash<int, QItemEditorCreatorBase *> creatorMap; |
75 | }; |
76 | |
77 | QT_END_NAMESPACE |
78 | |
79 | #endif // QITEMEDITORFACTORY_H |
80 |