File indexing completed on 2024-06-16 05:24:55

0001 /*
0002     This file is part of the Okteta Kasten module, made within the KDE community.
0003 
0004     SPDX-FileCopyrightText: 2008-2009 Friedrich W. H. Kossebau <kossebau@kde.org>
0005 
0006     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0007 */
0008 
0009 #include "podtablemodel.hpp"
0010 
0011 // lib
0012 #include "poddecodertool.hpp"
0013 // KF
0014 #include <KLocalizedString>
0015 #include <KColorScheme>
0016 // Qt
0017 #include <QApplication>
0018 
0019 namespace Kasten {
0020 
0021 PODTableModel::PODTableModel(PODDecoderTool* tool, QObject* parent)
0022     : QAbstractTableModel(parent)
0023     , mTool(tool)
0024     , mEmptyNote(QLatin1Char('-'))
0025 {
0026     connect(mTool, &PODDecoderTool::dataChanged, this, &PODTableModel::onDataChanged);
0027 }
0028 
0029 PODTableModel::~PODTableModel() = default;
0030 
0031 void PODTableModel::onDataChanged()
0032 {
0033     Q_EMIT dataChanged(index(0, ValueId), index(mTool->podCount() - 1, ValueId));
0034 }
0035 
0036 int PODTableModel::rowCount(const QModelIndex& parent) const
0037 {
0038     return (!parent.isValid()) ? mTool->podCount() : 0;
0039 }
0040 
0041 int PODTableModel::columnCount(const QModelIndex& parent) const
0042 {
0043     return (!parent.isValid()) ? NoOfColumnIds : 0;
0044 }
0045 
0046 QVariant PODTableModel::data(const QModelIndex& index, int role) const
0047 {
0048     QVariant result;
0049     switch (role)
0050     {
0051     case Qt::DisplayRole:
0052     {
0053         const int podId = index.row();
0054         const int column = index.column();
0055         switch (column)
0056         {
0057         case NameId:
0058         {
0059             result = mTool->nameOfPOD(podId);
0060             break;
0061         }
0062         case ValueId:
0063         {
0064             QVariant value = mTool->value(podId);
0065             if (value.isNull()) {
0066                 value = mEmptyNote;
0067             }
0068             result = value;
0069             break;
0070         }
0071         default:
0072             ;
0073         }
0074         break;
0075     }
0076     case Qt::EditRole:
0077     {
0078         const int column = index.column();
0079         if (column == ValueId) {
0080             const int podId = index.row();
0081             result = mTool->value(podId);
0082         }
0083         break;
0084     }
0085     case Qt::TextAlignmentRole:
0086     {
0087         const int column = index.column();
0088         result = static_cast<int>(Qt::AlignVCenter | ((column == NameId) ? Qt::AlignRight : Qt::AlignLeft));
0089         break;
0090     }
0091     case Qt::ForegroundRole:
0092     {
0093         const int column = index.column();
0094         if (column == ValueId) {
0095             const int podId = index.row();
0096             const QVariant value = mTool->value(podId);
0097 
0098             if (value.isNull()) {
0099                 const QPalette& palette = QApplication::palette();
0100                 const KColorScheme colorScheme(palette.currentColorGroup(), KColorScheme::View);
0101                 result = colorScheme.foreground(KColorScheme::InactiveText);
0102             }
0103         }
0104     }
0105     default:
0106         break;
0107     }
0108 
0109     return result;
0110 }
0111 
0112 Qt::ItemFlags PODTableModel::flags(const QModelIndex& index) const
0113 {
0114     Qt::ItemFlags result = QAbstractTableModel::flags(index);
0115     const int column = index.column();
0116     if (column == ValueId) {
0117         const int podId = index.row();
0118         const QVariant value = mTool->value(podId);
0119 
0120         // TODO: this check does not match types with dynamic byte length, e.g. utf-8!
0121         if (!value.isNull()) {
0122             result |= Qt::ItemIsEditable;
0123         }
0124     }
0125 
0126     return result;
0127 }
0128 
0129 QModelIndex PODTableModel::buddy(const QModelIndex& index) const
0130 {
0131     QModelIndex result;
0132 
0133     const int column = index.column();
0134     if (column == NameId) {
0135         const int row = index.row();
0136         result = createIndex(row, ValueId);
0137     } else {
0138         result = index;
0139     }
0140 
0141     return result;
0142 }
0143 
0144 QVariant PODTableModel::headerData(int section, Qt::Orientation orientation, int role) const
0145 {
0146     QVariant result;
0147 
0148     if (role == Qt::DisplayRole) {
0149         const QString titel =
0150             section == NameId ?   i18nc("@title:column name of the datatype",                "Type") :
0151             section == ValueId ?  i18nc("@title:column value of the bytes for the datatype", "Value") :
0152                                   QString();
0153         result = titel;
0154     } else if (role == Qt::ToolTipRole) {
0155         const QString titel =
0156             section == NameId ?
0157                 i18nc("@info:tooltip for column Type",    "The type of data") :
0158             section == ValueId ?
0159                 i18nc("@info:tooltip for column Value",   "The value of the bytes for the datatype") :
0160                 QString();
0161         result = titel;
0162     } else {
0163         result = QAbstractTableModel::headerData(section, orientation, role);
0164     }
0165 
0166     return result;
0167 }
0168 
0169 bool PODTableModel::setData(const QModelIndex& index, const QVariant& value, int role)
0170 {
0171     bool result;
0172 
0173     if (index.isValid() && role == Qt::EditRole) {
0174         const int podId = index.row();
0175 
0176         mTool->setData(value, podId);
0177 
0178         result = true;
0179     } else {
0180         result = false;
0181     }
0182 
0183     return result;
0184 }
0185 
0186 }
0187 
0188 #include "moc_podtablemodel.cpp"