File indexing completed on 2024-05-19 05:42:13
0001 // ct_lvtmdl_basetablemodel.cpp -*-C++-*- 0002 0003 /* 0004 // Copyright 2023 Codethink Ltd <codethink@codethink.co.uk> 0005 // SPDX-License-Identifier: Apache-2.0 0006 // 0007 // Licensed under the Apache License, Version 2.0 (the "License"); 0008 // you may not use this file except in compliance with the License. 0009 // You may obtain a copy of the License at 0010 // 0011 // http://www.apache.org/licenses/LICENSE-2.0 0012 // 0013 // Unless required by applicable law or agreed to in writing, software 0014 // distributed under the License is distributed on an "AS IS" BASIS, 0015 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 0016 // See the License for the specific language governing permissions and 0017 // limitations under the License. 0018 */ 0019 0020 #include <ct_lvtmdl_basetablemodel.h> 0021 0022 #include <cassert> 0023 #include <climits> 0024 0025 namespace { 0026 0027 [[nodiscard]] inline int sizeToInt(std::size_t i) 0028 // it is annoying that Qt uses int to measure sizes 0029 { 0030 assert(i <= (std::size_t) INT_MAX); 0031 return static_cast<int>(i); 0032 } 0033 0034 } // namespace 0035 0036 namespace Codethink::lvtmdl { 0037 0038 struct BaseTableModel::Private { 0039 std::string fullyQualifiedName; 0040 lvtshr::DiagramType type = lvtshr::DiagramType::NoneType; 0041 int columnCount = 0; 0042 0043 // outer vector: rows, inner vector: cols. 0044 std::vector<std::vector<QString>> elements; 0045 std::vector<QString> headerData; 0046 }; 0047 0048 BaseTableModel::BaseTableModel(int columnCount, QObject *parent): 0049 QAbstractTableModel(parent), d(std::make_unique<BaseTableModel::Private>()) 0050 { 0051 d->columnCount = columnCount; 0052 } 0053 0054 BaseTableModel::~BaseTableModel() = default; 0055 0056 int BaseTableModel::columnCount(const QModelIndex& unused) const 0057 { 0058 (void) unused; 0059 return d->columnCount; 0060 } 0061 0062 QVariant BaseTableModel::headerData(int section, Qt::Orientation orientation, int role) const 0063 { 0064 if (role != Qt::DisplayRole || (int) orientation != Qt::Horizontal) { 0065 return {}; 0066 } 0067 0068 if (section >= columnCount()) { 0069 return {}; 0070 } 0071 0072 return d->headerData[section]; 0073 } 0074 0075 QVariant BaseTableModel::data(const QModelIndex& index, int role) const 0076 { 0077 if (role != Qt::DisplayRole) { 0078 return {}; 0079 } 0080 0081 if (!index.isValid()) { 0082 return {}; 0083 } 0084 0085 return d->elements[index.row()][index.column()]; 0086 } 0087 0088 std::string BaseTableModel::fullyQualifiedName() const 0089 { 0090 return d->fullyQualifiedName; 0091 } 0092 0093 lvtshr::DiagramType BaseTableModel::type() const 0094 { 0095 return d->type; 0096 } 0097 0098 int BaseTableModel::rowCount(const QModelIndex& parent) const 0099 { 0100 (void) parent; 0101 0102 return sizeToInt(d->elements.size()); 0103 } 0104 0105 void BaseTableModel::clear() 0106 { 0107 beginResetModel(); 0108 d->elements = {}; 0109 endResetModel(); 0110 } 0111 0112 void BaseTableModel::addRow(const std::vector<QString>& row) 0113 { 0114 beginInsertRows(QModelIndex(), sizeToInt(d->elements.size()), sizeToInt(d->elements.size())); 0115 d->elements.push_back(row); 0116 endInsertRows(); 0117 } 0118 0119 void BaseTableModel::addRow(const std::string& row) 0120 { 0121 addRow({QString::fromStdString(row)}); 0122 } 0123 0124 void BaseTableModel::setHeader(const std::vector<QString>& header) 0125 { 0126 d->headerData = header; 0127 Q_EMIT headerDataChanged(Qt::Orientation::Horizontal, 0, columnCount(QModelIndex()) - 1); 0128 } 0129 0130 } // end namespace Codethink::lvtmdl