File indexing completed on 2024-04-28 04:41:47
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_CONTEXTADAPTER_H 0019 #define KQUICKITEMVIEWS_CONTEXTADAPTER_H 0020 0021 // Qt 0022 #include <QtCore/QModelIndex> 0023 class QQmlContext; 0024 0025 // KQuickItemViews 0026 class AbstractItemAdapter; 0027 class ContextAdapterFactory; 0028 class DynamicContext; 0029 class ContextExtension; 0030 0031 /** 0032 * Calling `context()` on an instance of this object will create a 0033 * QQmlContext reflecting the model roles (and other property groups). 0034 * 0035 * One instance needs exists per context. It is possible to replace the 0036 * QModelIndex later on as needs suit. The two main use cases are to 0037 * either track multiple QModelIndex at once and display them or use a 0038 * single instance along with some QQmlExpression to add "just-in-time" 0039 * properties to objects (like QItemDelegate or 0040 * QSortFilterProxyModel::acceptRow) 0041 * 0042 * These objects MUST BE CREATED AFTER the last call to addContextExtension 0043 * has been made and the model(index) has been set. 0044 */ 0045 class ContextAdapter 0046 { 0047 friend class AbstractItemAdapter; 0048 friend class ContextAdapterFactory; //factory 0049 public: 0050 virtual ~ContextAdapter(); 0051 0052 bool isCacheEnabled() const; 0053 void setCacheEnabled(bool v); 0054 0055 bool isActive() const; 0056 0057 virtual QModelIndex index() const; 0058 void setModelIndex(const QModelIndex& index); 0059 0060 virtual QQmlContext* context() const final; 0061 virtual AbstractItemAdapter* item() const; 0062 0063 /** 0064 * Clear the cache entry and send the notify signal on the property `id` 0065 * from the extension `e`. 0066 */ 0067 void dismissCache(ContextExtension* e, int id); 0068 0069 /** 0070 * Notify the adapter some QModelIndex roles changed. 0071 * 0072 * @return True when the updated roles affect the adapter, false otherwise. 0073 */ 0074 bool updateRoles(const QVector<int> &modified) const; 0075 0076 /** 0077 * Clear the cache of role values. 0078 * 0079 * To improve performance, QAbstractItemModel::data is only called once 0080 * unless dataChanged() is called. The value is then stored in the QML 0081 * context. Call this to manually invalidate the cache and for roles to be 0082 * reloaded. 0083 */ 0084 void flushCache(); 0085 0086 QObject *contextObject() const; 0087 0088 protected: 0089 explicit ContextAdapter(QQmlContext *parentContext = nullptr); 0090 0091 private: 0092 DynamicContext* d_ptr {nullptr}; 0093 }; 0094 0095 Q_DECLARE_METATYPE(ContextAdapter*) 0096 0097 #endif