File indexing completed on 2024-04-28 04:41:48

0001 /***************************************************************************
0002  *   Copyright (C) 2018 by Emmanuel Lepage Vallee                          *
0003  *   Author : Emmanuel Lepage Vallee <emmanuel.lepage@kde.org>             *
0004  *                                                                         *
0005  *   This program is free software; you can redistribute it and/or modify  *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 3 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  *                                                                         *
0010  *   This program is distributed in the hope that it will be useful,       *
0011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0013  *   GNU General Public License for more details.                          *
0014  *                                                                         *
0015  *   You should have received a copy of the GNU General Public License     *
0016  *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
0017  **************************************************************************/
0018 #ifndef KQUICKITEMVIEWS_MODELADAPTER_H
0019 #define KQUICKITEMVIEWS_MODELADAPTER_H
0020 
0021 #include <QtCore/QObject>
0022 
0023 // KQuickItemViews
0024 class SelectionAdapter;
0025 class ContextAdapterFactory;
0026 class Viewport;
0027 class ViewBase;
0028 class AbstractItemAdapter;
0029 class ModelAdapterPrivate;
0030 
0031 // Qt
0032 class QQmlComponent;
0033 class QAbstractItemModel;
0034 
0035 /**
0036  * Wrapper object to assign a model to a view.
0037  *
0038  * It supports both QSharedPointer based models and "raw" QAbstractItemModel.
0039  *
0040  * This class also allow multiple properties to be attached to the model, such
0041  * as a GUI delegate.
0042  */
0043 class Q_DECL_EXPORT ModelAdapter : public QObject
0044 {
0045     Q_OBJECT
0046 public:
0047     Q_PROPERTY(QVariant model READ model WRITE setModel NOTIFY modelChanged)
0048     Q_PROPERTY(QQmlComponent* delegate READ delegate WRITE setDelegate NOTIFY delegateChanged)
0049     Q_PROPERTY(bool empty READ isEmpty NOTIFY contentChanged)
0050 
0051     /// The view can be collapsed
0052     Q_PROPERTY(bool collapsable READ isCollapsable WRITE setCollapsable)
0053     /// Everything is collapsed or has no children
0054     Q_PROPERTY(bool collapsed READ isCollapsed NOTIFY collapsedChanged)
0055     /// Expand all elements by default
0056     Q_PROPERTY(bool autoExpand READ isAutoExpand WRITE setAutoExpand)
0057     /// The maximum depth of a tree (for performance)
0058     Q_PROPERTY(int maxDepth READ maxDepth WRITE setMaxDepth)
0059     /// Recycle existing QQuickItem delegates for new QModelIndex (for performance)
0060     Q_PROPERTY(RecyclingMode recyclingMode READ recyclingMode WRITE setRecyclingMode)
0061     /// The number of elements to be preloaded outside of the visible area (for latency)
0062     Q_PROPERTY(int cacheBuffer READ cacheBuffer WRITE setCacheBuffer)
0063     /// The number of delegates to be kept in a recycling pool (for performance)
0064     Q_PROPERTY(int poolSize READ poolSize WRITE setPoolSize)
0065 
0066     enum RecyclingMode {
0067         NoRecycling    , /*!< Destroy and create new QQuickItems all the time         */
0068         RecyclePerDepth, /*!< Keep different pools buffer for each levels of children */
0069         AlwaysRecycle  , /*!< Assume all depth level use the same delegate            */
0070     };
0071     Q_ENUM(RecyclingMode)
0072 
0073     explicit ModelAdapter(ViewBase *parent = nullptr);
0074     virtual ~ModelAdapter();
0075 
0076     QVariant model() const;
0077     void setModel(const QVariant& var);
0078 
0079     virtual void setDelegate(QQmlComponent* delegate);
0080     QQmlComponent* delegate() const;
0081 
0082     bool isCollapsable() const;
0083     void setCollapsable(bool value);
0084 
0085     bool isAutoExpand() const;
0086     void setAutoExpand(bool value);
0087 
0088     int maxDepth() const;
0089     void setMaxDepth(int depth);
0090 
0091     int cacheBuffer() const;
0092     void setCacheBuffer(int value);
0093 
0094     int poolSize() const;
0095     void setPoolSize(int value);
0096 
0097     RecyclingMode recyclingMode() const;
0098     void setRecyclingMode(RecyclingMode mode);
0099 
0100     bool isEmpty() const;
0101 
0102     bool isCollapsed() const;
0103 
0104     SelectionAdapter* selectionAdapter() const;
0105     ContextAdapterFactory* contextAdapterFactory() const;
0106 
0107     QVector<Viewport*> viewports() const;
0108 
0109     QAbstractItemModel *rawModel() const;
0110 
0111     ViewBase *view() const;
0112 
0113 protected:
0114     // Rather then scope-creeping this class, all selection related elements
0115     // are implemented independently.
0116     void setSelectionAdapter(SelectionAdapter* v);
0117 
0118 Q_SIGNALS:
0119     void modelAboutToChange(QAbstractItemModel* m, QAbstractItemModel* old);
0120     void modelChanged(QAbstractItemModel* m, QAbstractItemModel* old);
0121     void delegateChanged(QQmlComponent* delegate);
0122     void contentChanged();
0123     void collapsedChanged();
0124 
0125 private:
0126     ModelAdapterPrivate *d_ptr;
0127 };
0128 
0129 #endif