File indexing completed on 2024-06-16 07:35:00

0001 /* This file is part of the KDE project
0002    Copyright (C) 2008-2018 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 KPROPERTY_EDITORDATAMODEL_H
0021 #define KPROPERTY_EDITORDATAMODEL_H
0022 
0023 #include "kpropertywidgets_export.h"
0024 #include "KPropertySet.h"
0025 
0026 #include <QAbstractItemModel>
0027 #include <QModelIndex>
0028 
0029 class KProperty;
0030 class KPropertyEditorView;
0031 
0032 /*! @short A data model that integrates a KPropertySet object with the Qt's model/view API.
0033  @see KPropertyEditorView
0034  @internal
0035 */
0036 class KPropertyEditorDataModel : public QAbstractItemModel
0037 {
0038     Q_OBJECT
0039 public:
0040     //! Creates a new model. @a view and view->propertySet() are required.
0041     explicit KPropertyEditorDataModel(KPropertyEditorView *view,
0042                                       KPropertySetIterator::Order order = KPropertySetIterator::Order::Insertion);
0043     ~KPropertyEditorDataModel() override;
0044 
0045     enum Role {
0046         PropertyModifiedRole = Qt::UserRole + 0,
0047         PropertyGroupRole = Qt::UserRole + 1
0048     };
0049     QVariant data(const QModelIndex &index, int role) const override;
0050     QVariant headerData(int section, Qt::Orientation orientation,
0051                         int role = Qt::DisplayRole) const override;
0052 
0053     QModelIndex index(int row, int column,
0054                       const QModelIndex &parent = QModelIndex()) const override;
0055     QModelIndex parent(const QModelIndex &index) const override;
0056 
0057     int rowCount(const QModelIndex &parent = QModelIndex()) const override;
0058     int columnCount(const QModelIndex &parent = QModelIndex()) const override;
0059 
0060     Qt::ItemFlags flags(const QModelIndex &index) const override;
0061     bool setData(const QModelIndex &index, const QVariant &value,
0062                  int role = Qt::EditRole) override;
0063     bool setHeaderData(int section, Qt::Orientation orientation,
0064                        const QVariant &value, int role = Qt::EditRole) override;
0065 
0066     QModelIndex buddy(const QModelIndex &index) const override;
0067 
0068     //! @return property set object for this model. It is never @c nullptr.
0069     KPropertySet* propertySet() const;
0070 
0071     //! @return property object for model index @a index
0072     //! or @c nullptr for invalid index or index without a property assigned.
0073     KProperty *propertyForIndex(const QModelIndex& index) const;
0074 
0075     //! @return model index for property named @a propertyName
0076     //! or invalid index if such property could not be found.
0077     QModelIndex indexForPropertyName(const QByteArray& propertyName) const;
0078 
0079     //! @return a sibling for model index @a index and column @a column
0080     QModelIndex indexForColumn(const QModelIndex& index, int column) const;
0081 
0082     //! @return order for properties.
0083     KPropertySetIterator::Order order() const;
0084 
0085     //! Reimplemented for optimization.
0086     bool hasChildren(const QModelIndex &parent = QModelIndex()) const override;
0087 
0088 public Q_SLOTS:
0089     //! Sets order for properties.
0090     void setOrder(KPropertySetIterator::Order order);
0091 
0092     /*! Updates visibility of property groups.
0093      @see KPropertyEditorView::setGroupsVisible(bool)
0094      @since 3.1 */
0095     void updateGroupsVisibility();
0096 
0097 private:
0098     //! Collects persistent indices for the model. They are dependent on groupping and sorting.
0099     void collectIndices() const;
0100 
0101     Q_DISABLE_COPY(KPropertyEditorDataModel)
0102     class Private;
0103     Private * const d;
0104 
0105     friend class KPropertyEditorView;
0106 };
0107 
0108 #endif