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
13QT_REQUIRE_CONFIG(itemviews);
14
15QT_BEGIN_NAMESPACE
16
17class QWidget;
18
19class Q_WIDGETS_EXPORT QItemEditorCreatorBase
20{
21public:
22 virtual ~QItemEditorCreatorBase();
23
24 virtual QWidget *createWidget(QWidget *parent) const = 0;
25 virtual QByteArray valuePropertyName() const = 0;
26};
27
28template <class T>
29class QItemEditorCreator : public QItemEditorCreatorBase
30{
31public:
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
36private:
37 QByteArray propertyName;
38};
39
40template <class T>
41class QStandardItemEditorCreator: public QItemEditorCreatorBase
42{
43public:
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
50private:
51 QByteArray propertyName;
52};
53
54
55template <class T>
56Q_INLINE_TEMPLATE QItemEditorCreator<T>::QItemEditorCreator(const QByteArray &avaluePropertyName)
57 : propertyName(avaluePropertyName) {}
58
59class Q_WIDGETS_EXPORT QItemEditorFactory
60{
61public:
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
73private:
74 QHash<int, QItemEditorCreatorBase *> creatorMap;
75};
76
77QT_END_NAMESPACE
78
79#endif // QITEMEDITORFACTORY_H
80

source code of qtbase/src/widgets/itemviews/qitemeditorfactory.h