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 "aggregationeditormodel.h" 0008 0009 #include <core/aggregation.h> 0010 #include <core/util.h> 0011 0012 using namespace KUserFeedback::Console; 0013 0014 AggregationEditorModel::AggregationEditorModel(QObject* parent) : 0015 QAbstractTableModel(parent) 0016 { 0017 } 0018 0019 AggregationEditorModel::~AggregationEditorModel() = default; 0020 0021 Product AggregationEditorModel::product() const 0022 { 0023 return m_product; 0024 } 0025 0026 void AggregationEditorModel::setProduct(const Product& product) 0027 { 0028 beginResetModel(); 0029 m_product = product; 0030 endResetModel(); 0031 } 0032 0033 int AggregationEditorModel::columnCount(const QModelIndex& parent) const 0034 { 0035 Q_UNUSED(parent); 0036 return 3; 0037 } 0038 0039 int AggregationEditorModel::rowCount(const QModelIndex& parent) const 0040 { 0041 if (parent.isValid()) 0042 return 0; 0043 return m_product.aggregations().size(); 0044 } 0045 0046 QVariant AggregationEditorModel::data(const QModelIndex& index, int role) const 0047 { 0048 if (!index.isValid() || !m_product.isValid()) 0049 return {}; 0050 0051 if (role == Qt::DisplayRole) { 0052 const auto aggr = m_product.aggregations().at(index.row()); 0053 switch (index.column()) { 0054 case 0: 0055 return aggr.name(); 0056 case 1: 0057 return Util::enumToString(aggr.type()); 0058 case 2: 0059 { 0060 if (aggr.elements().isEmpty()) 0061 return tr("<none>"); 0062 QStringList l; 0063 l.reserve(aggr.elements().size()); 0064 foreach (const auto &elem, aggr.elements()) 0065 l.push_back(elem.displayString()); 0066 return l.join(QStringLiteral(", ")); 0067 } 0068 } 0069 } else if (role == Qt::EditRole) { 0070 const auto aggr = m_product.aggregations().at(index.row()); 0071 switch (index.column()) { 0072 case 0: 0073 return aggr.name(); 0074 case 1: 0075 return QVariant::fromValue(aggr.type()); 0076 case 2: 0077 if (aggr.elements().isEmpty()) 0078 return QVariant::fromValue(AggregationElement()); 0079 return QVariant::fromValue(aggr.elements().at(0)); 0080 } 0081 } 0082 0083 return {}; 0084 } 0085 0086 QVariant AggregationEditorModel::headerData(int section, Qt::Orientation orientation, int role) const 0087 { 0088 if (orientation == Qt::Horizontal && role == Qt::DisplayRole) { 0089 switch (section) { 0090 case 0: return tr("Name"); 0091 case 1: return tr("Type"); 0092 case 2: return tr("Element"); 0093 } 0094 } 0095 0096 return QAbstractTableModel::headerData(section, orientation, role); 0097 } 0098 0099 Qt::ItemFlags AggregationEditorModel::flags(const QModelIndex& index) const 0100 { 0101 const auto baseFlags = QAbstractTableModel::flags(index); 0102 if (!index.isValid()) 0103 return baseFlags; 0104 0105 const auto aggr = m_product.aggregations().at(index.row()); 0106 if (aggr.type() == Aggregation::Category && index.column() == 2) 0107 return baseFlags; 0108 0109 return baseFlags | Qt::ItemIsEditable; 0110 } 0111 0112 bool AggregationEditorModel::setData(const QModelIndex& index, const QVariant& value, int role) 0113 { 0114 if (!index.isValid() || role != Qt::EditRole) 0115 return false; 0116 0117 auto aggrs = m_product.aggregations(); 0118 auto &aggr = aggrs[index.row()]; 0119 switch (index.column()) { 0120 case 0: 0121 aggr.setName(value.toString()); 0122 break; 0123 case 1: 0124 aggr.setType(value.value<Aggregation::Type>()); 0125 break; 0126 case 2: 0127 aggr.setElements({ value.value<AggregationElement>() }); 0128 break; 0129 } 0130 m_product.setAggregations(aggrs); 0131 Q_EMIT dataChanged(index, index); 0132 return true; 0133 } 0134 0135 #include "moc_aggregationeditormodel.cpp"