File indexing completed on 2024-11-10 05:11:10

0001 /*
0002  * Copyright 2018 by Marco Martin <mart@kde.org>
0003  *
0004  * Licensed under the Apache License, Version 2.0 (the "License");
0005  * you may not use this file except in compliance with the License.
0006  * You may obtain a copy of the License at
0007  *
0008  *    http://www.apache.org/licenses/LICENSE-2.0
0009  *
0010  * Unless required by applicable law or agreed to in writing, software
0011  * distributed under the License is distributed on an "AS IS" BASIS,
0012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0013  * See the License for the specific language governing permissions and
0014  * limitations under the License.
0015  *
0016  */
0017 
0018 #include "sessiondatamodel.h"
0019 
0020 #include <QDebug>
0021 
0022 SessionDataModel::SessionDataModel(QObject *parent)
0023     : QAbstractListModel(parent)
0024 {
0025 }
0026 
0027 SessionDataModel::~SessionDataModel()
0028 {
0029     //TODO: delete everything
0030 }
0031 
0032 void SessionDataModel::insertData(int position, const QList<QVariantMap> &dataList)
0033 {
0034     if (position < 0 || position > m_data.count()) {
0035         return;
0036     }
0037     if (dataList.isEmpty()) {
0038         return;
0039     }
0040 
0041     // First insert: prepare role names
0042     // NOTE: the role names MUST stay fixed for the entire lifetime of the model object, if new roles are needed, a new model object must be created
0043     if (m_roles.isEmpty()) {
0044         int role = Qt::UserRole + 1;
0045         for (const auto &key : dataList.first().keys()) {
0046             m_roles[role] = key.toUtf8();
0047             ++role;
0048         }
0049     }
0050 
0051     beginInsertRows(QModelIndex(), position, position + dataList.count() - 1);
0052     int i = 0;
0053     for (const auto &item : dataList) {
0054         m_data.insert(position + i, item);
0055         ++i;
0056     }
0057     endInsertRows();
0058 }
0059 
0060 void SessionDataModel::updateData(int position, const QList<QVariantMap> &dataList)
0061 {
0062     if (dataList.isEmpty()) {
0063         return;
0064     }
0065     //too much rows to update, we don't have enough
0066     if (m_data.count() - position < dataList.count()) {
0067         return;
0068     }
0069 
0070     QSet<int> roles;
0071 
0072     int i = 0;
0073     for (auto it = m_data.begin() + position; it < m_data.begin() + position + dataList.count(); ++it) {
0074         const QVariantMap newValues = dataList[i];
0075         for (auto newIt = newValues.begin(); newIt != newValues.end(); ++newIt) {
0076             (*it)[newIt.key()] = newIt.value();
0077             roles.insert(m_roles.key(newIt.key().toUtf8()));
0078         }
0079         ++i;
0080     }
0081     emit dataChanged(index(position, 0), index(position + dataList.length() - 1, 0), roles.values().toVector());
0082 }
0083 
0084 void SessionDataModel::clear()
0085 {
0086     beginResetModel();
0087     m_data.clear();
0088     endResetModel();
0089 }
0090 
0091 bool SessionDataModel::moveRows(const QModelIndex &sourceParent, int sourceRow, int count, const QModelIndex &destinationParent, int destinationChild)
0092 {
0093     if (sourceParent.isValid() || destinationParent.isValid()) {
0094         return false;
0095     }
0096 
0097     if (count <= 0 || sourceRow == destinationChild || sourceRow < 0 || sourceRow >= m_data.count() ||
0098         destinationChild < 0 || destinationChild > m_data.count() || count - destinationChild > m_data.count() - sourceRow) {
0099         return false;
0100     }
0101     const int sourceLast = sourceRow + count - 1;
0102 
0103     //beginMoveRows wants indexes before the source rows are removed from the old order
0104     if (!beginMoveRows(sourceParent, sourceRow, sourceLast, destinationParent, destinationChild)) {
0105         return false;
0106     }
0107 
0108     if (sourceRow < destinationChild) {
0109         for (int i = count - 1; i >= 0; --i) {
0110             m_data.move(sourceRow + i, destinationChild - count + i);
0111         }
0112     } else {
0113         for (int i = 0; i < count; ++i) {
0114             m_data.move(sourceRow + i, destinationChild + i);
0115         }
0116     }
0117 
0118     endMoveRows();
0119     return true;
0120 }
0121 
0122 bool SessionDataModel::removeRows(int row, int count, const QModelIndex &parent)
0123 {
0124     if (row < 0 || count <= 0 || row + count > m_data.count() || parent.isValid()) {
0125         return false;
0126     }
0127 
0128     beginRemoveRows(parent, row, row + count - 1);
0129 
0130     m_data.erase(m_data.begin() + row, m_data.begin() + row + count);
0131     endRemoveRows();
0132     return true;
0133 }
0134 
0135 
0136 int SessionDataModel::rowCount(const QModelIndex &parent) const
0137 {
0138     if (parent.isValid()) {
0139         return 0;
0140     }
0141     return m_data.count();
0142 }
0143 
0144 QVariant SessionDataModel::data(const QModelIndex &index, int role) const
0145 {
0146     if (!index.isValid()) {
0147         return QVariant();
0148     }
0149     const int row = index.row();
0150 
0151     if (row < 0 || row >= m_data.count() || !m_roles.contains(role)) {
0152         return QVariant();
0153     }
0154 
0155     return m_data[row][QString::fromUtf8(m_roles[role])];
0156 }
0157 
0158 QHash<int, QByteArray> SessionDataModel::roleNames() const
0159 {
0160     return m_roles;
0161 }
0162