File indexing completed on 2024-04-28 15:13:59

0001 /***************************************************************************
0002     File                 : DatasetModel.cpp
0003     Project              : LabPlot
0004     Description          : Wrapper class for the collections of datasets
0005     --------------------------------------------------------------------
0006     Copyright            : (C) 2019 Kovacs Ferencz (kferike98@gmail.com)
0007     Copyright            : (C) 2019 by Alexander Semke (alexander.semke@web.de)
0008 
0009 ***************************************************************************/
0010 /***************************************************************************
0011  *                                                                         *
0012  *  This program is free software; you can redistribute it and/or modify   *
0013  *  it under the terms of the GNU General Public License as published by   *
0014  *  the Free Software Foundation; either version 2 of the License, or      *
0015  *  (at your option) any later version.                                    *
0016  *                                                                         *
0017  *  This program is distributed in the hope that it will be useful,        *
0018  *  but WITHOUT ANY WARRANTY; without even the implied warranty of         *
0019  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
0020  *  GNU General Public License for more details.                           *
0021  *                                                                         *
0022  *   You should have received a copy of the GNU General Public License     *
0023  *   along with this program; if not, write to the Free Software           *
0024  *   Foundation, Inc., 51 Franklin Street, Fifth Floor,                    *
0025  *   Boston, MA  02110-1301  USA                                           *
0026  *                                                                         *
0027  ***************************************************************************/
0028 
0029 #include <QVector>
0030 #include "DatasetModel.h"
0031 
0032 /*!
0033 \class DatasetModel
0034 \brief Wrapper class for datasets, and also for their categories and subcategories
0035 
0036 \ingroup kdefrontend
0037 */
0038 DatasetModel::DatasetModel(const QMap<QString, QMap<QString, QMap<QString, QVector<QString>>>>& datasetsMap) {
0039     initCollections(datasetsMap);
0040     initCategories(datasetsMap);
0041     initSubcategories(datasetsMap);
0042     initDatasets(datasetsMap);
0043 }
0044 
0045 DatasetModel::~DatasetModel() = default;
0046 
0047 /**
0048  * @brief Initializes the list of collections.
0049  */
0050 void DatasetModel::initCollections(const QMap<QString, QMap<QString, QMap<QString, QVector<QString> > > > & datasetMap) {
0051     m_collectionList = datasetMap.keys();
0052 }
0053 
0054 /**
0055  * @brief Initializes the list of categories.
0056  */
0057 void DatasetModel::initCategories(const QMap<QString, QMap<QString, QMap<QString, QVector<QString> > > > & datasetMap) {
0058     for(auto i = datasetMap.begin(); i != datasetMap.end(); ++i) {
0059         m_categories[i.key()] = i.value().keys();
0060 
0061         for(auto it = i.value().constBegin(); it != i.value().constEnd(); ++it) {
0062             const QString& category = it.key();
0063             if(!m_allCategories.contains(category))
0064                 m_allCategories.append(category);
0065         }
0066     }
0067 }
0068 
0069 /**
0070  * @brief Initializes the list of subcategories.
0071  */
0072 void DatasetModel::initSubcategories(const QMap<QString, QMap<QString, QMap<QString, QVector<QString> > > > & datasetMap) {
0073     for(auto collection = datasetMap.begin(); collection != datasetMap.end(); ++collection) {
0074         const QMap<QString, QMap<QString, QVector<QString> > > collection_ = collection.value();
0075 
0076         for(auto category = collection_.begin(); category != collection_.end(); ++category) {
0077             m_subcategories[collection.key()][category.key()] = category.value().keys();
0078 
0079             for(auto it = category.value().constBegin(); it != category.value().constEnd(); ++it) {
0080                 const QString& subcategory = it.key();
0081                 if(!m_allSubcategories[category.key()].contains(subcategory))
0082                     m_allSubcategories[category.key()].append(subcategory);
0083             }
0084         }
0085     }
0086 }
0087 
0088 /**
0089  * @brief Initializes the list of datasets.
0090  */
0091 void DatasetModel::initDatasets(const QMap<QString, QMap<QString, QMap<QString, QVector<QString> > > >& datasetMap) {
0092     for(auto collection = datasetMap.begin(); collection != datasetMap.end(); ++collection) {
0093         const QMap<QString, QMap<QString, QVector<QString> > > collection_ = collection.value();
0094 
0095         for(auto category = collection_.begin(); category != collection_.end(); ++category) {
0096             const QMap<QString, QVector<QString> >category_ = category.value();
0097 
0098             for(auto subcategory = category_.begin(); subcategory != category_.end(); ++subcategory) {
0099                 m_datasets[collection.key()][category.key()][subcategory.key()] = subcategory.value().toList();
0100                 m_allDatasets[category.key()][subcategory.key()].append(subcategory.value().toList());
0101                 m_datasetList.append(subcategory.value().toList());
0102             }
0103         }
0104     }
0105 }
0106 
0107 /**
0108  * @brief Returns the list of categories.
0109  */
0110 QStringList DatasetModel::allCategories() {
0111     return QVariant(m_allCategories).toStringList();
0112 }
0113 
0114 /**
0115  * @brief Returns the list of subcategories of a given category.
0116  * @param category the category the subcategories of which will be returned
0117  */
0118 QStringList DatasetModel::allSubcategories(const QString& category) {
0119     return QVariant(m_allSubcategories[category]).toStringList();
0120 }
0121 
0122 /**
0123  * @brief Returns the list of datasets of a given category and subcategory.
0124  */
0125 QVariant DatasetModel::allDatasets(const QString& category, const QString& subcategory) {
0126     return QVariant(m_allDatasets[category][subcategory]);
0127 }
0128 
0129 /**
0130  * @brief Returns the list of every dataset.
0131  */
0132 QVariant DatasetModel::allDatasetsList() {
0133     return QVariant(m_datasetList);
0134 }
0135 
0136 /**
0137  * @brief Returns the list of categories for a given collection
0138  */
0139 QStringList DatasetModel::categories(const QString& collection) {
0140     if(!collection.isEmpty())
0141         return m_categories[collection];
0142     else
0143         return allCategories();
0144 }
0145 
0146 /**
0147  * @brief  Returns the list of subcategories of a given collection and category.
0148  */
0149 QStringList DatasetModel::subcategories(const QString& collection, const QString& category) {
0150     if(!collection.isEmpty())
0151         return m_subcategories[collection][category];
0152     else
0153         return allSubcategories(category);
0154 }
0155 
0156 /**
0157  * @brief Returns the list of datasets of a given collection, category and subcategory.
0158  */
0159 QStringList DatasetModel::datasets(const QString& collection, const QString& category, const QString& subcategory) {
0160     if(!collection.isEmpty())
0161         return m_datasets[collection][category][subcategory];
0162     else
0163         return allDatasets(category, subcategory).toStringList();
0164 }
0165 
0166 /**
0167  * @brief Returns the number of datasets belonging to the given collection
0168  */
0169 int DatasetModel::datasetCount(const QString& collection) {
0170     int count = 0;
0171     for(const QString& category: categories(collection)) {
0172         for(const QString& subcategory: subcategories(collection, category))    {
0173             count += datasets(collection, category, subcategory).size();
0174         }
0175     }
0176     return count;
0177 }
0178 
0179 /**
0180  * @brief Returns the number of datasets belonging to the given collection and category
0181  */
0182 int DatasetModel::datasetCount(const QString& collection, const QString& category) {
0183     int count = 0;
0184     for (const QString& subcategory: subcategories(collection, category))
0185         count += datasets(collection, category, subcategory).size();
0186 
0187     return count;
0188 }
0189 
0190 /**
0191  * @brief Returns the number of datasets belonging to the given collection, category and subcategory
0192  */
0193 int DatasetModel::datasetCount(const QString& collection, const QString& category, const QString& subcategory) {
0194     return datasets(collection, category, subcategory).size();
0195 }
0196 
0197 /**
0198  * @brief Returns the list of every collection.
0199  */
0200 QStringList DatasetModel::collections() {
0201     return m_collectionList;
0202 }