File indexing completed on 2024-12-15 03:45:00

0001 /*
0002     SPDX-FileCopyrightText: 2016 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: MIT
0005 */
0006 
0007 #include "aggregationelementmodel.h"
0008 
0009 #include <core/product.h>
0010 
0011 using namespace KUserFeedback::Console;
0012 
0013 AggregationElementModel::AggregationElementModel(QObject* parent) :
0014     QAbstractListModel(parent)
0015 {
0016 }
0017 
0018 AggregationElementModel::~AggregationElementModel() = default;
0019 
0020 void AggregationElementModel::setProduct(const Product& product)
0021 {
0022     beginResetModel();
0023     m_elements.clear();
0024     for (const auto &entry : product.schema()) {
0025         for (const auto &elem : entry.elements()) {
0026             AggregationElement e;
0027             e.setSchemaEntry(entry);
0028             e.setSchemaEntryElement(elem);
0029             e.setType(AggregationElement::Value);
0030             m_elements.push_back(e);
0031         }
0032         if (entry.dataType() != SchemaEntry::Scalar) {
0033             AggregationElement e;
0034             e.setSchemaEntry(entry);
0035             e.setType(AggregationElement::Size);
0036             m_elements.push_back(e);
0037         }
0038     }
0039     endResetModel();
0040 }
0041 
0042 int AggregationElementModel::rowCount(const QModelIndex& parent) const
0043 {
0044     if (parent.isValid())
0045         return 0;
0046     return m_elements.size();
0047 }
0048 
0049 QVariant AggregationElementModel::data(const QModelIndex& index, int role) const
0050 {
0051     if (!index.isValid() || m_elements.isEmpty())
0052         return {};
0053 
0054     const auto e = m_elements.at(index.row());
0055     switch (role) {
0056         case Qt::DisplayRole: return e.displayString();
0057         case Qt::EditRole: return QVariant::fromValue(e);
0058     }
0059 
0060     return {};
0061 }
0062 
0063 QModelIndexList AggregationElementModel::match(const QModelIndex& start, int role, const QVariant& value, int hits, Qt::MatchFlags flags) const
0064 {
0065     if (role == Qt::EditRole && value.userType() == qMetaTypeId<AggregationElement>() && hits == 1 && start.row() == 0) {
0066         const auto cmp = value.value<AggregationElement>();
0067         for (int i = 0; i < m_elements.size(); ++i) {
0068             if (m_elements.at(i) == cmp)
0069                 return { index(i, 0) };
0070         }
0071     }
0072     return QAbstractListModel::match(start, role, value, hits, flags);
0073 }
0074 
0075 #include "moc_aggregationelementmodel.cpp"