File indexing completed on 2024-12-08 10:15:38
0001 /* This file is part of the KDE project 0002 Copyright (C) 2008-2015 Jarosław Staniek <staniek@kde.org> 0003 0004 This library is free software; you can redistribute it and/or 0005 modify it under the terms of the GNU Library General Public 0006 License as published by the Free Software Foundation; either 0007 version 2 of the License, or (at your option) any later version. 0008 0009 This library is distributed in the hope that it will be useful, 0010 but WITHOUT ANY WARRANTY; without even the implied warranty of 0011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0012 Library General Public License for more details. 0013 0014 You should have received a copy of the GNU Library General Public License 0015 along with this library; see the file COPYING.LIB. If not, write to 0016 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 0017 * Boston, MA 02110-1301, USA. 0018 */ 0019 0020 #ifndef KPROPERTYWIDGETS_FACTORY_H 0021 #define KPROPERTYWIDGETS_FACTORY_H 0022 0023 #include "KProperty.h" 0024 #include "KPropertyFactory.h" 0025 #include "KPropertyUtils.h" 0026 0027 #include <QObject> 0028 #include <QVariant> 0029 #include <QHash> 0030 #include <QLabel> 0031 #include <QPainter> 0032 #include <QStyleOptionViewItem> 0033 0034 /*! @brief Options for altering the editor widget creation process 0035 0036 @see KPropertyEditorCreatorInterface::createEditor(). 0037 @since 3.1 0038 */ 0039 class KPROPERTYWIDGETS_EXPORT KPropertyEditorCreatorOptions { 0040 public: 0041 KPropertyEditorCreatorOptions(); 0042 0043 KPropertyEditorCreatorOptions(const KPropertyEditorCreatorOptions &other); 0044 0045 ~KPropertyEditorCreatorOptions(); 0046 0047 /*! In order to have better look of the widget within the property editor view, 0048 we are usually hiding borders from the widget (see FactoryManager::createEditor()) 0049 and adding 1 pixel 'gray border' on the top. Default value of bordersVisible is @c false. */ 0050 bool bordersVisible() const; 0051 0052 void setBordersVisible(bool visible); 0053 0054 //! Assigns @a other to this KPropertyEditorCreatorOptions 0055 KPropertyEditorCreatorOptions& operator=(const KPropertyEditorCreatorOptions &other); 0056 0057 //! @return true if these options have exactly the same values options as @a other 0058 bool operator==(const KPropertyEditorCreatorOptions &other) const; 0059 0060 //! @return true if these options differs in at least one value from @a other 0061 bool operator!=(const KPropertyEditorCreatorOptions &other) const { return !operator==(other); } 0062 0063 private: 0064 class Private; 0065 Private * const d; 0066 }; 0067 0068 //! An interface for editor widget creators. 0069 /*! Options can be set in the options attribute in order to customize 0070 widget creation process. Do this in the EditorCreatorInterface constructor. 0071 */ 0072 class KPROPERTYWIDGETS_EXPORT KPropertyEditorCreatorInterface 0073 { 0074 public: 0075 KPropertyEditorCreatorInterface(); 0076 0077 virtual ~KPropertyEditorCreatorInterface(); 0078 0079 virtual QWidget * createEditor(int type, QWidget *parent, 0080 const QStyleOptionViewItem &option, const QModelIndex &index) const; 0081 0082 //! Options for editor creating 0083 //! @since 3.1 0084 const KPropertyEditorCreatorOptions *options() const; 0085 0086 //! @overload 0087 KPropertyEditorCreatorOptions *options(); 0088 0089 private: 0090 Q_DISABLE_COPY(KPropertyEditorCreatorInterface) 0091 class Private; 0092 Private * const d; 0093 }; 0094 0095 class KPROPERTYWIDGETS_EXPORT KPropertyValuePainterInterface 0096 { 0097 public: 0098 KPropertyValuePainterInterface(); 0099 virtual ~KPropertyValuePainterInterface(); 0100 virtual void paint( QPainter * painter, 0101 const QStyleOptionViewItem & option, const QModelIndex & index ) const = 0; 0102 0103 //! A helper that draws text obtained from index (EditRole data) on painter @a painter. 0104 //! iface->valueToString() is used to convert value to string. 0105 //! @since 3.1 0106 static void paint(const KPropertyValueDisplayInterface *iface, QPainter *painter, 0107 const QStyleOptionViewItem &option, const QModelIndex &index); 0108 }; 0109 0110 //! Label widget that can be used for displaying text-based read-only items 0111 //! Used in KPropertyLabelCreator. 0112 class KPROPERTYWIDGETS_EXPORT KPropertyLabel : public QLabel 0113 { 0114 Q_OBJECT 0115 Q_PROPERTY(QVariant value READ value WRITE setValue USER true) 0116 public: 0117 KPropertyLabel(QWidget *parent, const KProperty *property, 0118 const KPropertyValueDisplayInterface *iface); 0119 0120 ~KPropertyLabel() override; 0121 0122 QVariant value() const; 0123 0124 Q_SIGNALS: 0125 void commitData( QWidget * editor ); 0126 0127 public Q_SLOTS: 0128 void setValue(const QVariant& value); 0129 0130 protected: 0131 void paintEvent( QPaintEvent * event ) override; 0132 0133 private: 0134 Q_DISABLE_COPY(KPropertyLabel) 0135 class Private; 0136 Private * const d; 0137 }; 0138 0139 //! Creator returning editor 0140 template<class Widget> 0141 class KPROPERTYWIDGETS_EXPORT KPropertyEditorCreator : public KPropertyEditorCreatorInterface, 0142 public KPropertyValueDisplayInterface, 0143 public KPropertyValuePainterInterface 0144 { 0145 public: 0146 KPropertyEditorCreator() : KPropertyEditorCreatorInterface() {} 0147 0148 ~KPropertyEditorCreator() override {} 0149 0150 QWidget *createEditor(int type, QWidget *parent, const QStyleOptionViewItem &option, 0151 const QModelIndex &index) const override 0152 { 0153 Q_UNUSED(type); 0154 Q_UNUSED(option); 0155 KProperty *prop = KPropertyUtils::propertyForIndex(index); 0156 return new Widget(parent, prop, this); 0157 } 0158 0159 void paint(QPainter *painter, const QStyleOptionViewItem &option, 0160 const QModelIndex &index) const override 0161 { 0162 KPropertyValuePainterInterface::paint(this, painter, option, index); 0163 } 0164 }; 0165 0166 typedef KPropertyEditorCreator<KPropertyLabel> KPropertyLabelCreator; 0167 0168 0169 class KPROPERTYWIDGETS_EXPORT KPropertyWidgetsFactory : public KPropertyFactory 0170 { 0171 public: 0172 KPropertyWidgetsFactory(); 0173 ~KPropertyWidgetsFactory() override; 0174 0175 QHash<int, KPropertyEditorCreatorInterface*> editorCreators() const; 0176 QHash<int, KPropertyValuePainterInterface*> valuePainters() const; 0177 0178 //! Adds editor creator @a creator for type @a type. 0179 //! The creator becomes owned by the factory. 0180 void addEditor(int type, KPropertyEditorCreatorInterface *creator); 0181 0182 void addPainter(int type, KPropertyValuePainterInterface *painter); 0183 0184 static void paintTopGridLine(QWidget *widget); 0185 static void setTopAndBottomBordersUsingStyleSheet(QWidget *widget, 0186 const QString& extraStyleSheet = QString()); 0187 0188 protected: 0189 void addEditorInternal(int type, KPropertyEditorCreatorInterface *editor, bool own = true); 0190 0191 //! Adds value painter @a painter for type @a type. 0192 //! The painter becomes owned by the factory. 0193 void addPainterInternal(int type, KPropertyValuePainterInterface *painter, bool own = true); 0194 0195 Q_DISABLE_COPY(KPropertyWidgetsFactory) 0196 class Private; 0197 Private * const d; 0198 }; 0199 0200 #endif