File indexing completed on 2024-04-21 04:41:45

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_CONTEXTADAPTERFACTORY_H
0019 #define KQUICKITEMVIEWS_CONTEXTADAPTERFACTORY_H
0020 
0021 // Qt
0022 #include <QtCore/QObject>
0023 class QAbstractItemModel;
0024 class QQmlContext;
0025 class QQuickItem;
0026 
0027 // LibStdC++
0028 #include <functional>
0029 
0030 // KQuickItemViews
0031 class ContextAdapterFactoryPrivate;
0032 class ContextExtension;
0033 class ContextAdapter;
0034 
0035 /**
0036  * This class manage the content of the AbstractItemAdapter::context().
0037  *
0038  * It dynamically compute the roles used by the delegate and optimize
0039  * the updates.
0040  *
0041  * This avoids calling setProperty on every single role and refresh them in
0042  * every single `emit dataChanged`.
0043  *
0044  * `applyRoles` can be re-implemented to either add more properties to the
0045  * context or to convert some roles into a format easier to consume from QML.
0046  */
0047 class Q_DECL_EXPORT ContextAdapterFactory
0048 {
0049     friend class ContextAdapter; // Public API
0050     friend class DynamicContext; // call finish()
0051 public:
0052     /**
0053      * This constructor has an optional template parameter to specify a
0054      * different class to be built by the factory. By default it builds a
0055      * plain ContextAdapter
0056      */
0057     template<typename T = ContextAdapter> explicit ContextAdapterFactory() :
0058         ContextAdapterFactory([](QQmlContext* c)->ContextAdapter*{return new T(c);}){}
0059 
0060     virtual ~ContextAdapterFactory();
0061 
0062     QAbstractItemModel *model() const;
0063     void setModel(QAbstractItemModel *m);
0064 
0065     /**
0066      * Add more properties to the context.
0067      *
0068      * This must be called from the view constructor. If called later, it will
0069      * Q_ASSERT.
0070      *
0071      * Implementations must subclass ContextExtension to use this.
0072      */
0073     void addContextExtension(ContextExtension* pg);
0074 
0075     QSet<QByteArray> usedRoles() const;
0076 
0077     /**
0078      * Create a context adapter.
0079      *
0080      * This can only be done after the last context extension is added.
0081      */
0082     ContextAdapter* createAdapter(QQmlContext *parentContext = nullptr) const;
0083 
0084     /**
0085      * Alternate version of createAdapter with the ability to create derivative
0086      * of the ContextAdapter.
0087      */
0088     template<typename T>
0089     T* createAdapter(QQmlContext *p = nullptr) const {
0090         return static_cast<T*>(createAdapter([](QQmlContext* c)->ContextAdapter*{return new T(c);}, p));
0091     }
0092 
0093 private:
0094     using FactoryFunctor = std::function<ContextAdapter*(QQmlContext*)>;
0095     ContextAdapterFactory(FactoryFunctor f);
0096     ContextAdapter* createAdapter(FactoryFunctor, QQmlContext*) const;
0097 
0098     ContextAdapterFactoryPrivate* d_ptr;
0099     Q_DECLARE_PRIVATE(ContextAdapterFactory);
0100 };
0101 
0102 #endif