KProperty

KPropertyWidgetsFactory.h
1/* This file is part of the KDE project
2 Copyright (C) 2008-2015 Jarosław Staniek <staniek@kde.org>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18*/
19
20#ifndef KPROPERTYWIDGETS_FACTORY_H
21#define KPROPERTYWIDGETS_FACTORY_H
22
23#include "KProperty.h"
24#include "KPropertyFactory.h"
25#include "KPropertyUtils.h"
26
27#include <QObject>
28#include <QVariant>
29#include <QHash>
30#include <QLabel>
31#include <QPainter>
32#include <QStyleOptionViewItem>
33
34/*! @brief Options for altering the editor widget creation process
35
36 @see KPropertyEditorCreatorInterface::createEditor().
37 @since 3.1
38*/
39class KPROPERTYWIDGETS_EXPORT KPropertyEditorCreatorOptions {
40public:
42
44
46
47 /*! In order to have better look of the widget within the property editor view,
48 we are usually hiding borders from the widget (see FactoryManager::createEditor())
49 and adding 1 pixel 'gray border' on the top. Default value of bordersVisible is @c false. */
50 bool bordersVisible() const;
51
52 void setBordersVisible(bool visible);
53
54 //! Assigns @a other to this KPropertyEditorCreatorOptions
56
57 //! @return true if these options have exactly the same values options as @a other
58 bool operator==(const KPropertyEditorCreatorOptions &other) const;
59
60 //! @return true if these options differs in at least one value from @a other
61 bool operator!=(const KPropertyEditorCreatorOptions &other) const { return !operator==(other); }
62
63private:
64 class Private;
65 Private * const d;
66};
67
68//! An interface for editor widget creators.
69/*! Options can be set in the options attribute in order to customize
70 widget creation process. Do this in the EditorCreatorInterface constructor.
71*/
72class KPROPERTYWIDGETS_EXPORT KPropertyEditorCreatorInterface
73{
74public:
76
78
79 virtual QWidget * createEditor(int type, QWidget *parent,
80 const QStyleOptionViewItem &option, const QModelIndex &index) const;
81
82 //! Options for editor creating
83 //! @since 3.1
84 const KPropertyEditorCreatorOptions *options() const;
85
86 //! @overload
88
89private:
91 class Private;
92 Private * const d;
93};
94
95class KPROPERTYWIDGETS_EXPORT KPropertyValuePainterInterface
96{
97public:
98 KPropertyValuePainterInterface();
99 virtual ~KPropertyValuePainterInterface();
100 virtual void paint( QPainter * painter,
101 const QStyleOptionViewItem & option, const QModelIndex & index ) const = 0;
102
103 //! A helper that draws text obtained from index (EditRole data) on painter @a painter.
104 //! iface->valueToString() is used to convert value to string.
105 //! @since 3.1
106 static void paint(const KPropertyValueDisplayInterface *iface, QPainter *painter,
107 const QStyleOptionViewItem &option, const QModelIndex &index);
108};
109
110//! Label widget that can be used for displaying text-based read-only items
111//! Used in KPropertyLabelCreator.
112class KPROPERTYWIDGETS_EXPORT KPropertyLabel : public QLabel
113{
114 Q_OBJECT
115 Q_PROPERTY(QVariant value READ value WRITE setValue USER true)
116public:
117 KPropertyLabel(QWidget *parent, const KProperty *property,
118 const KPropertyValueDisplayInterface *iface);
119
120 ~KPropertyLabel() override;
121
122 QVariant value() const;
123
124Q_SIGNALS:
125 void commitData( QWidget * editor );
126
127public Q_SLOTS:
128 void setValue(const QVariant& value);
129
130protected:
131 void paintEvent( QPaintEvent * event ) override;
132
133private:
134 Q_DISABLE_COPY(KPropertyLabel)
135 class Private;
136 Private * const d;
137};
138
139//! Creator returning editor
140template<class Widget>
141class KPROPERTYWIDGETS_EXPORT KPropertyEditorCreator : public KPropertyEditorCreatorInterface,
143 public KPropertyValuePainterInterface
144{
145public:
147
148 ~KPropertyEditorCreator() override {}
149
150 QWidget *createEditor(int type, QWidget *parent, const QStyleOptionViewItem &option,
151 const QModelIndex &index) const override
152 {
153 Q_UNUSED(type);
154 Q_UNUSED(option);
155 KProperty *prop = KPropertyUtils::propertyForIndex(index);
156 return new Widget(parent, prop, this);
157 }
158
159 void paint(QPainter *painter, const QStyleOptionViewItem &option,
160 const QModelIndex &index) const override
161 {
162 KPropertyValuePainterInterface::paint(this, painter, option, index);
163 }
164};
165
167
168
169class KPROPERTYWIDGETS_EXPORT KPropertyWidgetsFactory : public KPropertyFactory
170{
171public:
172 KPropertyWidgetsFactory();
173 ~KPropertyWidgetsFactory() override;
174
177
178 //! Adds editor creator @a creator for type @a type.
179 //! The creator becomes owned by the factory.
180 void addEditor(int type, KPropertyEditorCreatorInterface *creator);
181
182 void addPainter(int type, KPropertyValuePainterInterface *painter);
183
184 static void paintTopGridLine(QWidget *widget);
185 static void setTopAndBottomBordersUsingStyleSheet(QWidget *widget,
186 const QString& extraStyleSheet = QString());
187
188protected:
189 void addEditorInternal(int type, KPropertyEditorCreatorInterface *editor, bool own = true);
190
191 //! Adds value painter @a painter for type @a type.
192 //! The painter becomes owned by the factory.
193 void addPainterInternal(int type, KPropertyValuePainterInterface *painter, bool own = true);
194
195 Q_DISABLE_COPY(KPropertyWidgetsFactory)
196 class Private;
197 Private * const d;
198};
199
200#endif
An interface for editor widget creators.
Options for altering the editor widget creation process.
bool operator!=(const KPropertyEditorCreatorOptions &other) const
Creator returning editor.
Label widget that can be used for displaying text-based read-only items Used in KPropertyLabelCreator...
Provides a specialized conversion of value to string depending on type.
The base class representing a single property.
Definition KProperty.h:96
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 22 2024 12:12:37 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.