File indexing completed on 2024-12-15 03:45:00
0001 /* 0002 SPDX-FileCopyrightText: 2017 Volker Krause <vkrause@kde.org> 0003 0004 SPDX-License-Identifier: MIT 0005 */ 0006 0007 #include "aggregationelementeditmodel.h" 0008 0009 using namespace KUserFeedback::Console; 0010 0011 AggregationElementEditModel::AggregationElementEditModel(QObject* parent) 0012 : QAbstractListModel(parent) 0013 { 0014 } 0015 0016 AggregationElementEditModel::~AggregationElementEditModel() = default; 0017 0018 Aggregation AggregationElementEditModel::aggregation() const 0019 { 0020 return m_aggr; 0021 } 0022 0023 void AggregationElementEditModel::setAggregation(const Aggregation& aggregation) 0024 { 0025 beginResetModel(); 0026 m_aggr = aggregation; 0027 endResetModel(); 0028 } 0029 0030 int AggregationElementEditModel::rowCount(const QModelIndex& parent) const 0031 { 0032 if (parent.isValid()) 0033 return 0; 0034 return m_aggr.elements().size(); 0035 } 0036 0037 QVariant AggregationElementEditModel::data(const QModelIndex& index, int role) const 0038 { 0039 if (!index.isValid()) 0040 return {}; 0041 0042 switch (role) { 0043 case Qt::DisplayRole: 0044 { 0045 const auto elem = m_aggr.elements().at(index.row()); 0046 if (elem.isValid()) 0047 return elem.displayString(); 0048 return tr("<new element>"); 0049 } 0050 case Qt::EditRole: 0051 return QVariant::fromValue(m_aggr.elements().at(index.row())); 0052 } 0053 0054 return {}; 0055 } 0056 0057 QVariant AggregationElementEditModel::headerData(int section, Qt::Orientation orientation, int role) const 0058 { 0059 if (orientation == Qt::Horizontal && role == Qt::DisplayRole && section == 0) 0060 return tr("Element"); 0061 return QAbstractListModel::headerData(section, orientation, role); 0062 } 0063 0064 Qt::ItemFlags AggregationElementEditModel::flags(const QModelIndex& index) const 0065 { 0066 const auto baseFlags = QAbstractListModel::flags(index); 0067 return baseFlags | Qt::ItemIsEditable; 0068 } 0069 0070 bool AggregationElementEditModel::setData(const QModelIndex& index, const QVariant& value, int role) 0071 { 0072 if (!index.isValid() || role != Qt::EditRole || value.isNull()) 0073 return false; 0074 0075 auto elems = m_aggr.elements(); 0076 elems[index.row()] = value.value<AggregationElement>(); 0077 m_aggr.setElements(elems); 0078 Q_EMIT dataChanged(index, index); 0079 return true; 0080 } 0081 0082 #include "moc_aggregationelementeditmodel.cpp"