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 "aggregateddatamodel.h" 0008 #include <model/timeaggregationmodel.h> 0009 0010 #include <QDebug> 0011 0012 using namespace KUserFeedback::Console; 0013 0014 AggregatedDataModel::AggregatedDataModel(QObject *parent) : 0015 QAbstractTableModel(parent) 0016 { 0017 } 0018 0019 AggregatedDataModel::~AggregatedDataModel() = default; 0020 0021 void AggregatedDataModel::addSourceModel(QAbstractItemModel* model, const QString &prefix) 0022 { 0023 Q_ASSERT(model); 0024 m_models.push_back(model); 0025 m_prefixes.push_back(prefix); 0026 connect(model, &QAbstractItemModel::modelReset, this, &AggregatedDataModel::recreateColumnMapping); 0027 recreateColumnMapping(); 0028 } 0029 0030 void AggregatedDataModel::clear() 0031 { 0032 beginResetModel(); 0033 m_columnMapping.clear(); 0034 m_columnOffset.clear(); 0035 foreach (auto model, m_models) 0036 disconnect(model, &QAbstractItemModel::modelReset, this, &AggregatedDataModel::recreateColumnMapping); 0037 m_models.clear(); 0038 m_prefixes.clear(); 0039 endResetModel(); 0040 } 0041 0042 int AggregatedDataModel::columnCount(const QModelIndex& parent) const 0043 { 0044 Q_UNUSED(parent); 0045 return m_columnMapping.size(); 0046 } 0047 0048 int AggregatedDataModel::rowCount(const QModelIndex& parent) const 0049 { 0050 if (parent.isValid() || m_models.isEmpty()) 0051 return 0; 0052 return m_models.first()->rowCount(); 0053 } 0054 0055 QVariant AggregatedDataModel::data(const QModelIndex& index, int role) const 0056 { 0057 if (!index.isValid() || m_models.isEmpty()) 0058 return {}; 0059 0060 if (index.column() == 0 && role == Qt::DisplayRole) { 0061 return m_models.at(0)->index(index.row(), 0).data(TimeAggregationModel::TimeDisplayRole); 0062 } 0063 0064 const auto model = m_models.at(m_columnMapping.at(index.column())); 0065 const auto srcIdx = model->index(index.row(), m_columnOffset.at(index.column())); 0066 if (role == Qt::DisplayRole) 0067 return srcIdx.data(TimeAggregationModel::DataDisplayRole); 0068 return srcIdx.data(role); 0069 } 0070 0071 QVariant AggregatedDataModel::headerData(int section, Qt::Orientation orientation, int role) const 0072 { 0073 if (orientation == Qt::Horizontal && !m_models.isEmpty()) { 0074 const auto v = m_models.at(m_columnMapping.at(section))->headerData(m_columnOffset.at(section), orientation, role); 0075 if (role != Qt::DisplayRole || m_prefixes.at(m_columnMapping.at(section)).isEmpty()) 0076 return v; 0077 return QString(m_prefixes.at(m_columnMapping.at(section)) + QStringLiteral(": ") + v.toString()); 0078 } 0079 return QAbstractTableModel::headerData(section, orientation, role); 0080 } 0081 0082 void AggregatedDataModel::recreateColumnMapping() 0083 { 0084 beginResetModel(); 0085 m_columnMapping.clear(); 0086 m_columnOffset.clear(); 0087 0088 for (int i = 0; i < m_models.size(); ++i) { 0089 for (int j = (i == 0 ? 0 : 1); j < m_models.at(i)->columnCount(); ++j) { 0090 m_columnMapping.push_back(i); 0091 m_columnOffset.push_back(j); 0092 } 0093 } 0094 endResetModel(); 0095 } 0096 0097 #include "moc_aggregateddatamodel.cpp"