File indexing completed on 2024-12-01 13:00:43

0001 /* This file is part of the KDE project
0002    Copyright (C) 2004 Cedric Pasteur <cedric.pasteur@free.fr>
0003    Copyright (C) 2004 Alexander Dymo <cloudtemple@mskat.net>
0004    Copyright (C) 2004-2017 Jarosław Staniek <staniek@kde.org>
0005 
0006    This library is free software; you can redistribute it and/or
0007    modify it under the terms of the GNU Library General Public
0008    License as published by the Free Software Foundation; either
0009    version 2 of the License, or (at your option) any later version.
0010 
0011    This library is distributed in the hope that it will be useful,
0012    but WITHOUT ANY WARRANTY; without even the implied warranty of
0013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0014    Library General Public License for more details.
0015 
0016    You should have received a copy of the GNU Library General Public License
0017    along with this library; see the file COPYING.LIB.  If not, write to
0018    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0019  * Boston, MA 02110-1301, USA.
0020 */
0021 
0022 #ifndef KPROPERTY_SET_P_H
0023 #define KPROPERTY_SET_P_H
0024 
0025 #include "KPropertySet.h"
0026 #include "kproperty_debug.h"
0027 
0028 class KPROPERTYCORE_EXPORT KPropertySetPrivate
0029 {
0030 public:
0031     explicit KPropertySetPrivate(KPropertySet *set, bool isOwnProperty);
0032 
0033     ~KPropertySetPrivate();
0034 
0035     //! Asccessor within the KProperty*
0036     inline static KPropertySetPrivate* d(KPropertySet *set) { return set->d; }
0037     inline static const KPropertySetPrivate* d(const KPropertySet *set) { return set->d; }
0038 
0039     inline int visiblePropertiesCount() const { return m_visiblePropertiesCount; }
0040 
0041     inline KProperty* property(const QByteArray &name) const {
0042         return m_hash.value(name.toLower());
0043     }
0044 
0045     inline KProperty& propertyOrNull(const QByteArray &name) const
0046     {
0047         KProperty *p = property(name);
0048         if (p)
0049             return *p;
0050         m_nonConstNull.setName(nullptr); //to ensure returned property is null
0051         kprWarning() << "PROPERTY" << name << "NOT FOUND";
0052         return m_nonConstNull;
0053     }
0054 
0055     void addProperty(KProperty *property, const QByteArray &group/*, bool updateSortingKey*/);
0056 
0057     void removeProperty(KProperty *property);
0058 
0059     void clear();
0060 
0061     inline int count() const { return m_list.count(); }
0062 
0063     inline bool isEmpty() const { return m_list.isEmpty(); }
0064 
0065     /*! @return @c true if there are groups explicitly defined.
0066      In this case groups are displayed by the property editor.
0067      If there is only one "common" group, it means that all properties belong to this group,
0068      and no groups are displayed.
0069      @since 3.1 */
0070     bool hasGroups() const;
0071 
0072     inline QByteArray groupForProperty(const KProperty *property) const {
0073         return m_groupForProperties.value(const_cast<KProperty*>(property));
0074     }
0075 
0076     inline void setGroupCaption(const QByteArray &group, const QString &caption)
0077     {
0078         m_groupCaptions.insert(group.toLower(), caption);
0079     }
0080 
0081     inline void addPropertyToGroup(KProperty *property, const QByteArray &groupLower) {
0082         m_groupForProperties.insert(property, groupLower);
0083     }
0084 
0085     inline void removePropertyFromGroup(KProperty *property) {
0086         m_groupForProperties.remove(property);
0087     }
0088 
0089     //! Copy all attributes except complex ones
0090     void copyAttributesFrom(const KPropertySetPrivate &other);
0091 
0092     //! Copy all properties from the other set
0093     void copyPropertiesFrom(
0094         const QList<KProperty*>::ConstIterator& constBegin,
0095         const QList<KProperty*>::ConstIterator& constEnd, const KPropertySet & set);
0096 
0097     QList<QByteArray> groupNames() const
0098     {
0099         return m_groupNames;
0100     }
0101 
0102     /*! Add property to a group.*/
0103     void addToGroup(const QByteArray &group, KProperty *property);
0104 
0105     /*! Remove property from a group.*/
0106     void removeFromGroup(KProperty *property);
0107 
0108     /*! Used to declare that \a property wants to be informed
0109      that the set has been cleared (all properties are deleted) */
0110     void informAboutClearing(bool* cleared);
0111 
0112     /*! Helper for Private class. */
0113     void addRelatedProperty(KProperty *p1, KProperty *p2) const;
0114 
0115     inline QList<KProperty*>::ConstIterator listConstIterator() const {
0116         return m_list.constBegin();
0117     }
0118 
0119     inline QList<KProperty*>::ConstIterator listConstEnd() const {
0120         return m_list.constEnd();
0121     }
0122 
0123     /*! @return index of property @a property within its parent or group. */
0124     int indexOfProperty(const KProperty *property) const;
0125 
0126     /*! @return index of property @a property within its group. */
0127     int indexOfPropertyInGroup(const KProperty *property) const;
0128 
0129     QString groupCaption(const QByteArray &group) const;
0130 
0131     inline void setGroupIconName(const QByteArray &group, const QString& iconName)
0132     {
0133         m_groupIconNames.insert(group.toLower(), iconName);
0134     }
0135 
0136     inline QString groupIconName(const QByteArray &group) const
0137     {
0138         return m_groupIconNames.value(group);
0139     }
0140 
0141     inline QByteArray previousSelection() const
0142     {
0143         return m_prevSelection;
0144     }
0145 
0146     inline void setPreviousSelection(const QByteArray &prevSelection)
0147     {
0148         m_prevSelection = prevSelection;
0149     }
0150 
0151     inline QList<QByteArray> *propertyNamesForGroup(const QByteArray &group)
0152     {
0153         return m_propertiesOfGroup.value(group);
0154     }
0155 
0156     inline QByteArray groupName(int index) const
0157     {
0158         return m_groupNames.value(index);
0159     }
0160 
0161     inline int indexOfGroup(const QByteArray &group) const
0162     {
0163         return m_groupNames.indexOf(group);
0164     }
0165 
0166     bool readOnly = false;
0167 
0168 private:
0169     KPropertySet *q;
0170 
0171     //groups of properties:
0172     // list of group name: (list of property names)
0173     QMap<QByteArray, QList<QByteArray>* > m_propertiesOfGroup;
0174     QList<QByteArray> m_groupNames;
0175     QHash<QByteArray, QString> m_groupCaptions;
0176     QHash<QByteArray, QString> m_groupIconNames;
0177     // map of property: group
0178 
0179     bool m_ownProperty;
0180     QByteArray m_prevSelection;
0181 
0182     mutable KProperty m_nonConstNull;
0183 
0184     //! A list of properties, preserving their order, owner of KProperty objects
0185     QList<KProperty*> m_list;
0186     //! A hash of properties in form name -> property
0187     QHash<QByteArray, KProperty*> m_hash;
0188     QHash<KProperty*, QByteArray> m_groupForProperties;
0189     int m_visiblePropertiesCount = 0; //!< Cache for optimization,
0190                                        //!< used by @ref bool KPropertySet::hasVisibleProperties()
0191     //! Used in KPropertySetPrivate::informAboutClearing(bool&) to declare that the property wants
0192     //! to be informed that the set has been cleared (all properties are deleted)
0193     bool* m_informAboutClearing = nullptr;
0194 };
0195 
0196 #endif