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_CONTEXTEXTENSION_H
0019 #define KQUICKITEMVIEWS_CONTEXTEXTENSION_H
0020 
0021 // Qt
0022 #include <QtCore/QVariant>
0023 #include <QtCore/QByteArray>
0024 #include <QtCore/QModelIndex>
0025 
0026 class ContextExtensionPrivate;
0027 
0028 /**
0029  * Add more properties to the QML context.
0030  *
0031  * This allows to add a predefined set of extra properties to the QML
0032  * context. It does *not* support adding more properties at runtime.
0033  *
0034  * The memory ownership is not transferred, do not re-use the instance
0035  * across many views (it will crash).
0036  */
0037 class ContextExtension
0038 {
0039 public:
0040 
0041     explicit ContextExtension();
0042     virtual ~ContextExtension() {}
0043 
0044     /**
0045         * Return a list of properties.
0046         *
0047         * This **MUST NEVER CHANGE** and needs to always return the same
0048         * vector. Implementations should use a `static` QVector.
0049         *
0050         * The property index is what's passed to getProperty, setProperty
0051         * and is the argument of changeProperty.
0052         */
0053     virtual QVector<QByteArray>& propertyNames() const;
0054 
0055     /**
0056         * The default implementation returns propertyNames().size()
0057         *
0058         * Implementing this rather than using `propertyNames` makes sense
0059         * when the properties contained in an existing data structure or
0060         * associated with extra metadata.
0061         */
0062     virtual uint size() const;
0063 
0064     /**
0065         * The default implementation returns propertyNames()[id];
0066         *
0067         * Implementing this rather than using `propertyNames` makes sense
0068         * when the properties contained in an existing data structure or
0069         * associated with extra metadata.
0070         */
0071     virtual QByteArray getPropertyName(uint id) const;
0072 
0073     /**
0074         * Some variants, like QModelIndex ones, cannot be cached and will
0075         * most likely point to invalid memory.
0076         *
0077         * If the group has such properties, implement this function. Note
0078         * that the return value cannot change.
0079         *
0080         * The default implementation always returns true.
0081         */
0082     virtual bool supportCaching(uint id) const;
0083 
0084     /**
0085         * The id comes from propertyNames.
0086         *
0087         * It is recommended to use a switch statement or if/else_if for the
0088         * implementation and avoid QHash (or worst).
0089         */
0090     virtual QVariant getProperty(AbstractItemAdapter* item, uint id, const QModelIndex& index) const = 0;
0091 
0092     /**
0093         * Optionally make the property read/write.
0094         */
0095     virtual bool setProperty(AbstractItemAdapter* item, uint id, const QVariant& value, const QModelIndex& index) const;
0096 
0097     /**
0098         * Notify that content of this property has changed.
0099         */
0100     void changeProperty(AbstractItemAdapter* item, uint id);
0101 
0102     ContextExtensionPrivate *d_ptr;
0103 };
0104 
0105 #endif