File indexing completed on 2024-05-12 16:40:13

0001 /* This file is part of the KDE project
0002    Copyright (C) 2009 Adam Pigg <adam@piggz.co.uk>
0003    Copyright (C) 2009-2016 Jarosław Staniek <staniek@kde.org>
0004 
0005    This library is free software; you can redistribute it and/or
0006    modify it under the terms of the GNU Library General Public
0007    License version 2 as published by the Free Software Foundation.
0008 
0009    This library is distributed in the hope that it will be useful,
0010    but WITHOUT ANY WARRANTY; without even the implied warranty of
0011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012    Library General Public License for more details.
0013 
0014    You should have received a copy of the GNU Library General Public License
0015    along with this library; see the file COPYING.LIB.  If not, write to
0016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017    Boston, MA 02110-1301, USA.
0018 */
0019 
0020 #include "AlterSchemaTableModel.h"
0021 
0022 #include <KDbTableSchema>
0023 
0024 #include <QDebug>
0025 
0026 const int RECORDS_FOR_PREVIEW = 3;
0027 
0028 AlterSchemaTableModel::AlterSchemaTableModel(QObject* parent)
0029     : QAbstractTableModel(parent)
0030     , m_schema(nullptr)
0031     , m_data(nullptr)
0032     , m_recordCount(RECORDS_FOR_PREVIEW)
0033 {
0034 }
0035 
0036 AlterSchemaTableModel::~AlterSchemaTableModel()
0037 {
0038     delete m_data;
0039 }
0040 
0041 QVariant AlterSchemaTableModel::data(const QModelIndex& index, int role) const
0042 {
0043     if (!index.isValid())
0044         return QVariant();
0045 
0046     if (index.column() >= (int)m_schema->fieldCount())
0047         return QVariant();
0048 
0049     if (role == Qt::DisplayRole) {
0050         if (m_data->length() > index.row()) {
0051             const KDbRecordData* r(m_data->value(index.row()));
0052             return r->value(index.column());
0053         }
0054     }
0055     return QVariant();
0056 }
0057 
0058 QVariant AlterSchemaTableModel::headerData(int section, Qt::Orientation orientation, int role) const
0059 {
0060     if (role != Qt::DisplayRole)
0061         return QVariant();
0062 
0063     if (orientation == Qt::Horizontal) {
0064         if (m_schema) {
0065             KDbField *fld = m_schema->field(section);
0066             if (fld)
0067                 return m_schema->field(section)->captionOrName();
0068         }
0069         return QString("Column %1").arg(section);
0070     }
0071     return QString("Record %1").arg(section + 1);
0072 }
0073 
0074 int AlterSchemaTableModel::columnCount ( const QModelIndex& parent ) const
0075 {
0076     Q_UNUSED(parent);
0077     if (m_schema) {
0078         return m_schema->fieldCount();
0079     }
0080     return 0;
0081 }
0082 
0083 int AlterSchemaTableModel::rowCount(const QModelIndex& parent) const
0084 {
0085     Q_UNUSED(parent);
0086     return m_recordCount;
0087 }
0088 
0089 void AlterSchemaTableModel::setSchema(KDbTableSchema *schema)
0090 {
0091     m_schema = schema;
0092     if (!m_schema) {
0093         return;
0094     }
0095     beginInsertColumns(QModelIndex(), 0, m_schema->fieldCount() - 1);
0096     endInsertColumns();
0097 
0098     emit layoutChanged();
0099 }
0100 
0101 void AlterSchemaTableModel::setData(QList<KDbRecordData*> *data)
0102 {
0103     m_data = data;
0104 }
0105 
0106 void AlterSchemaTableModel::setRowCount(int i)
0107 {
0108     if (i != m_recordCount) {
0109         m_recordCount = i;
0110         emit layoutChanged();
0111     }
0112 }