Warning, file /graphics/krita/interfaces/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 * SPDX-FileCopyrightText: 2007 Cyrille Berger <cberger@cberger.net> 0004 * 0005 * SPDX-License-Identifier: LGPL-2.1-or-later 0006 */ 0007 0008 #ifndef _KO_GENERIC_REGISTRY_MODEL_H_ 0009 #define _KO_GENERIC_REGISTRY_MODEL_H_ 0010 0011 #include <QAbstractListModel> 0012 #include "KoGenericRegistry.h" 0013 0014 /** 0015 * This is a model that you can use to display the content of a registry. 0016 * 0017 * @param T is the type of the data in the registry 0018 */ 0019 template<typename T> 0020 class KoGenericRegistryModel : public QAbstractListModel 0021 { 0022 0023 public: 0024 0025 KoGenericRegistryModel(KoGenericRegistry<T>* registry); 0026 0027 ~KoGenericRegistryModel() override; 0028 0029 public: 0030 0031 /** 0032 * @return the number of elements in the registry 0033 */ 0034 int rowCount(const QModelIndex &parent = QModelIndex()) const override; 0035 0036 /** 0037 * When role == Qt::DisplayRole, this function will return the name of the 0038 * element. 0039 */ 0040 QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; 0041 0042 /** 0043 * @return the element at the given index 0044 */ 0045 T get(const QModelIndex &index) const; 0046 0047 private: 0048 0049 KoGenericRegistry<T>* m_registry; 0050 }; 0051 0052 // -- Implementation -- 0053 template<typename T> 0054 KoGenericRegistryModel<T>::KoGenericRegistryModel(KoGenericRegistry<T>* registry) : m_registry(registry) 0055 { 0056 } 0057 0058 template<typename T> 0059 KoGenericRegistryModel<T>::~KoGenericRegistryModel() 0060 { 0061 } 0062 0063 template<typename T> 0064 int KoGenericRegistryModel<T>::rowCount(const QModelIndex &/*parent*/) const 0065 { 0066 return m_registry->keys().size(); 0067 } 0068 0069 template<typename T> 0070 QVariant KoGenericRegistryModel<T>::data(const QModelIndex &index, int role) const 0071 { 0072 if (!index.isValid()) { 0073 return QVariant(); 0074 } 0075 if (role == Qt::DisplayRole || role == Qt::EditRole) { 0076 return QVariant(get(index)->name()); 0077 } 0078 return QVariant(); 0079 } 0080 0081 template<typename T> 0082 T KoGenericRegistryModel<T>::get(const QModelIndex &index) const 0083 { 0084 return m_registry->get(m_registry->keys()[index.row()]); 0085 } 0086 0087 #endif