Warning, file /office/calligra/libs/widgets/KoGenericRegistryModel.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /* This file is part of the KDE project
0002  *
0003  *  Copyright (c) 2007 Cyrille Berger <cberger@cberger.net>
0004  *
0005  * This library is free software; you can redistribute it and/or
0006  * modify it under the terms of the GNU Lesser General Public
0007  * License as published by the Free Software Foundation; either
0008  * version 2.1 of the License, or (at your option) any later version.
0009  *
0010  * This library 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 GNU
0013  * Library General Public License for more details.
0014  *
0015  * You should have received a copy of the GNU Lesser General Public License
0016  * along with this library; see the file COPYING.LIB.  If not, write to
0017  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0018  * Boston, MA 02110-1301, USA.
0019  */
0020 
0021 #ifndef _KO_GENERIC_REGISTRY_MODEL_H_
0022 #define _KO_GENERIC_REGISTRY_MODEL_H_
0023 
0024 #include <QAbstractListModel>
0025 #include "KoGenericRegistry.h"
0026 
0027 /**
0028  * This is a model that you can use to display the content of a registry.
0029  *
0030  * @param T is the type of the data in the registry
0031  */
0032 template<typename T>
0033 class KoGenericRegistryModel : public QAbstractListModel
0034 {
0035 
0036 public:
0037 
0038     KoGenericRegistryModel(KoGenericRegistry<T>* registry);
0039 
0040     ~KoGenericRegistryModel() override = default;
0041 
0042 public:
0043 
0044     /**
0045      * @return the number of elements in the registry
0046      */
0047     int rowCount(const QModelIndex &parent = QModelIndex()) const override;
0048 
0049     /**
0050      * When role == Qt::DisplayRole, this function will return the name of the
0051      * element.
0052      */
0053     QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
0054 
0055     /**
0056      * @return the element at the given index
0057      */
0058     T get(const QModelIndex &index) const;
0059 
0060 private:
0061 
0062     KoGenericRegistry<T>* m_registry;
0063 };
0064 
0065 // -- Implementation --
0066 template<typename T>
0067 KoGenericRegistryModel<T>::KoGenericRegistryModel(KoGenericRegistry<T>* registry) : m_registry(registry)
0068 {
0069 }
0070 
0071 template<typename T>
0072 int KoGenericRegistryModel<T>::rowCount(const QModelIndex &/*parent*/) const
0073 {
0074     return m_registry->keys().size();
0075 }
0076 
0077 template<typename T>
0078 QVariant KoGenericRegistryModel<T>::data(const QModelIndex &index, int role) const
0079 {
0080     if (!index.isValid()) {
0081         return QVariant();
0082     }
0083     if (role == Qt::DisplayRole || role == Qt::EditRole) {
0084         return QVariant(get(index)->name());
0085     }
0086     return QVariant();
0087 }
0088 
0089 template<typename T>
0090 T KoGenericRegistryModel<T>::get(const QModelIndex &index) const
0091 {
0092     return m_registry->get(m_registry->keys()[index.row()]);
0093 }
0094 
0095 #endif