File indexing completed on 2025-02-16 04:59:01

0001 /*
0002     Copyright (c) 2009 Stephen Kelly <steveire@gmail.com>
0003 
0004     This library is free software; you can redistribute it and/or modify it
0005     under the terms of the GNU Library General Public License as published by
0006     the Free Software Foundation; either version 2 of the License, or (at your
0007     option) any later version.
0008 
0009     This library is distributed in the hope that it will be useful, but WITHOUT
0010     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0011     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
0012     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 the
0016     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
0017     02110-1301, USA.
0018 */
0019 
0020 
0021 #ifndef KDESCENDANTSPROXYMODEL_P_H
0022 #define KDESCENDANTSPROXYMODEL_P_H
0023 
0024 #include <QAbstractProxyModel>
0025 
0026 class KDescendantsProxyModelPrivate;
0027 
0028 #define KDEUI_EXPORT
0029 
0030 /**
0031 @brief Proxy Model for restructuring a Tree into a list.
0032 
0033 A KDescendantsProxyModel may be used to alter how the items in the tree are presented.
0034 
0035 Given a model which is represented as a tree:
0036 
0037 \image html entitytreemodel.png "A plain EntityTreeModel in a view"
0038 
0039 The KDescendantsProxyModel restructures the sourceModel to represent it as a flat list.
0040 
0041 @code
0042 // ... Create an entityTreeModel
0043 KDescendantsProxyModel *descProxy = new KDescendantsProxyModel(this);
0044 descProxy->setSourceModel(entityTree);
0045 view->setModel(descProxy);
0046 @endcode
0047 
0048 \image html descendantentitiesproxymodel.png "A KDescendantsProxyModel."
0049 
0050 KDescendantEntitiesProxyModel can also display the ancestors of the index in the source model as part of its display.
0051 
0052 @code
0053 // ... Create an entityTreeModel
0054 KDescendantsProxyModel *descProxy = new KDescendantsProxyModel(this);
0055 descProxy->setSourceModel(entityTree);
0056 
0057 // #### This is new
0058 descProxy->setDisplayAncestorData(true);
0059 descProxy->setDisplayAncestorSeparator(QString(" / "));
0060 
0061 view->setModel(descProxy);
0062 
0063 @endcode
0064 
0065 \image html descendantentitiesproxymodel-withansecnames.png "A KDescendantsProxyModel with ancestor names."
0066 
0067 @since 4.6
0068 @author Stephen Kelly <steveire@gmail.com>
0069 */
0070 class KDEUI_EXPORT KDescendantsProxyModel : public QAbstractProxyModel
0071 {
0072   Q_OBJECT
0073 
0074   public:
0075 
0076     /**
0077      * Creates a new descendant entities proxy model.
0078      *
0079      * @param parent The parent object.
0080      */
0081     explicit KDescendantsProxyModel( QObject *parent = 0 );
0082 
0083     /**
0084      * Destroys the descendant entities proxy model.
0085      */
0086     virtual ~KDescendantsProxyModel();
0087 
0088     /**
0089      * Sets the source @p model of the proxy.
0090      */
0091     virtual void setSourceModel( QAbstractItemModel *model );
0092 
0093     /**
0094      * @deprecated
0095      *
0096      * This method does nothing.
0097      */
0098     void setRootIndex( const QModelIndex &index);
0099 
0100     /**
0101      * Set whether to show ancestor data in the model. If @p display is true, then
0102      * a source model which is displayed as
0103      *
0104      * @code
0105      *  -> "Item 0-0" (this is row-depth)
0106      *  -> -> "Item 0-1"
0107      *  -> -> "Item 1-1"
0108      *  -> -> -> "Item 0-2"
0109      *  -> -> -> "Item 1-2"
0110      *  -> "Item 1-0"
0111      * @endcode
0112      *
0113      * will be displayed as
0114      *
0115      * @code
0116      *  -> *Item 0-0"
0117      *  -> "Item 0-0 / Item 0-1"
0118      *  -> "Item 0-0 / Item 1-1"
0119      *  -> "Item 0-0 / Item 1-1 / Item 0-2"
0120      *  -> "Item 0-0 / Item 1-1 / Item 1-2"
0121      *  -> "Item 1-0"
0122      * @endcode
0123      *
0124      * If @p display is false, the proxy will show
0125      *
0126      * @code
0127      *  -> *Item 0-0"
0128      *  -> "Item 0-1"
0129      *  -> "Item 1-1"
0130      *  -> "Item 0-2"
0131      *  -> "Item 1-2"
0132      *  -> "Item 1-0"
0133      * @endcode
0134      *
0135      * Default is false.
0136      */
0137     void setDisplayAncestorData( bool display );
0138 
0139     /**
0140      * Whether ancestor data will be displayed.
0141      */
0142     bool displayAncestorData() const;
0143 
0144     /**
0145      * Sets the ancestor @p separator used between data of ancestors.
0146      */
0147     void setAncestorSeparator( const QString &separator );
0148 
0149     /**
0150      * Separator used between data of ancestors.
0151      */
0152     QString ancestorSeparator() const;
0153 
0154     /**
0155      * Returns the number of descendant entries for the given model @p index.
0156      */
0157     int descendantCount( const QModelIndex &index ) const;
0158 
0159     QModelIndex mapFromSource ( const QModelIndex & sourceIndex ) const;
0160     QModelIndex mapToSource ( const QModelIndex & proxyIndex ) const;
0161 
0162     virtual Qt::ItemFlags flags( const QModelIndex &index ) const;
0163     QVariant data( const QModelIndex & index, int role = Qt::DisplayRole ) const;
0164     virtual int rowCount( const QModelIndex & parent = QModelIndex() ) const;
0165     virtual QVariant headerData(int section, Qt::Orientation orientation, int role) const;
0166 
0167     virtual QMimeData* mimeData( const QModelIndexList & indexes ) const;
0168     virtual QStringList mimeTypes() const;
0169 
0170     virtual bool hasChildren ( const QModelIndex & parent = QModelIndex() ) const;
0171     virtual QModelIndex index(int, int, const QModelIndex &parent = QModelIndex() ) const;
0172     virtual QModelIndex parent(const QModelIndex&) const;
0173     virtual int columnCount(const QModelIndex& index = QModelIndex()) const;
0174 
0175     virtual Qt::DropActions supportedDropActions() const;
0176 
0177     /**
0178     Reimplemented to match all descendants.
0179     */
0180     virtual QModelIndexList match(const QModelIndex& start, int role, const QVariant& value,
0181         int hits = 1, Qt::MatchFlags flags = Qt::MatchFlags( Qt::MatchStartsWith | Qt::MatchWrap ) ) const;
0182 
0183     virtual QHash<int,QByteArray> roleNames() const;
0184 
0185     void proxySetRoleNames(const QHash<int,QByteArray> &roleNames);
0186 
0187 
0188 private:
0189   Q_DECLARE_PRIVATE( KDescendantsProxyModel )
0190   //@cond PRIVATE
0191   KDescendantsProxyModelPrivate *d_ptr;
0192 
0193   Q_PRIVATE_SLOT(d_func(), void sourceRowsAboutToBeInserted(const QModelIndex &, int, int))
0194   Q_PRIVATE_SLOT(d_func(), void sourceRowsInserted(const QModelIndex &, int, int))
0195   Q_PRIVATE_SLOT(d_func(), void sourceRowsAboutToBeRemoved(const QModelIndex &, int, int))
0196   Q_PRIVATE_SLOT(d_func(), void sourceRowsRemoved(const QModelIndex &, int, int))
0197   Q_PRIVATE_SLOT(d_func(), void sourceRowsAboutToBeMoved(const QModelIndex &, int, int, const QModelIndex &, int))
0198   Q_PRIVATE_SLOT(d_func(), void sourceRowsMoved(const QModelIndex &, int, int, const QModelIndex &, int))
0199   Q_PRIVATE_SLOT(d_func(), void sourceModelAboutToBeReset())
0200   Q_PRIVATE_SLOT(d_func(), void sourceModelReset())
0201   Q_PRIVATE_SLOT(d_func(), void sourceLayoutAboutToBeChanged())
0202   Q_PRIVATE_SLOT(d_func(), void sourceLayoutChanged())
0203   Q_PRIVATE_SLOT(d_func(), void sourceDataChanged(const QModelIndex &, const QModelIndex &))
0204   Q_PRIVATE_SLOT(d_func(), void sourceModelDestroyed())
0205 
0206   Q_PRIVATE_SLOT(d_func(), void processPendingParents())
0207 
0208 
0209   // Make these private, they shouldn't be called by applications
0210 //   virtual bool insertRows(int , int, const QModelIndex & = QModelIndex());
0211 //   virtual bool insertColumns(int, int, const QModelIndex & = QModelIndex());
0212 //   virtual bool removeRows(int, int, const QModelIndex & = QModelIndex());
0213 //   virtual bool removeColumns(int, int, const QModelIndex & = QModelIndex());
0214 
0215 
0216   //@endcond
0217 };
0218 
0219 #endif