File indexing completed on 2024-11-10 04:57:19

0001 /*
0002     KWin - the KDE window manager
0003     This file is part of the KDE project.
0004 
0005     SPDX-FileCopyrightText: 2022 Marco Martin <mart@kde.org>
0006 
0007     SPDX-License-Identifier: GPL-2.0-or-later
0008 */
0009 
0010 #include "tilemodel.h"
0011 #include "tiles/tilemanager.h"
0012 #include "workspace.h"
0013 
0014 #include <KConfigGroup>
0015 #include <KLocalizedString>
0016 #include <KSharedConfig>
0017 
0018 #include <QJsonArray>
0019 #include <QJsonDocument>
0020 #include <QJsonObject>
0021 #include <QTimer>
0022 
0023 namespace KWin
0024 {
0025 
0026 TileModel::TileModel(TileManager *parent)
0027     : QAbstractItemModel(parent)
0028     , m_tileManager(parent)
0029 {
0030 }
0031 
0032 TileModel::~TileModel()
0033 {
0034 }
0035 
0036 QHash<int, QByteArray> TileModel::roleNames() const
0037 {
0038     return {
0039         {TileRole, QByteArrayLiteral("tile")},
0040     };
0041 }
0042 
0043 QVariant TileModel::data(const QModelIndex &index, int role) const
0044 {
0045     if (!index.isValid()) {
0046         return QVariant();
0047     }
0048 
0049     if (role == TileRole) {
0050         return QVariant::fromValue(static_cast<CustomTile *>(index.internalPointer()));
0051     }
0052 
0053     return QVariant();
0054 }
0055 
0056 Qt::ItemFlags TileModel::flags(const QModelIndex &index) const
0057 {
0058     if (!index.isValid()) {
0059         return Qt::NoItemFlags;
0060     }
0061 
0062     return QAbstractItemModel::flags(index);
0063 }
0064 
0065 QModelIndex TileModel::index(int row, int column, const QModelIndex &parent) const
0066 {
0067     if (column > 0 || !hasIndex(row, column, parent)) {
0068         return QModelIndex();
0069     }
0070 
0071     CustomTile *parentItem;
0072 
0073     if (!parent.isValid()) {
0074         parentItem = m_tileManager->rootTile();
0075     } else {
0076         parentItem = static_cast<CustomTile *>(parent.internalPointer());
0077     }
0078 
0079     CustomTile *childItem = static_cast<CustomTile *>(parentItem->childTile(row));
0080     if (childItem) {
0081         return createIndex(row, column, childItem);
0082     }
0083     return QModelIndex();
0084 }
0085 
0086 QModelIndex TileModel::parent(const QModelIndex &index) const
0087 {
0088     if (!index.isValid()) {
0089         return QModelIndex();
0090     }
0091 
0092     CustomTile *childItem = static_cast<CustomTile *>(index.internalPointer());
0093     CustomTile *parentItem = static_cast<CustomTile *>(childItem->parentTile());
0094 
0095     if (!parentItem || parentItem == m_tileManager->rootTile()) {
0096         return QModelIndex();
0097     }
0098 
0099     return createIndex(parentItem->row(), 0, parentItem);
0100 }
0101 
0102 int TileModel::rowCount(const QModelIndex &parent) const
0103 {
0104     Tile *parentItem;
0105     if (parent.column() > 0) {
0106         return 0;
0107     }
0108 
0109     if (!parent.isValid()) {
0110         parentItem = m_tileManager->rootTile();
0111     } else {
0112         parentItem = static_cast<CustomTile *>(parent.internalPointer());
0113     }
0114 
0115     return parentItem->childCount();
0116 }
0117 
0118 int TileModel::columnCount(const QModelIndex &parent) const
0119 {
0120     return 1;
0121 }
0122 
0123 void TileModel::beginInsertTile(CustomTile *tile, int position)
0124 {
0125     Q_ASSERT(position >= 0);
0126     CustomTile *parentTile = static_cast<CustomTile *>(tile->parentTile());
0127     Q_ASSERT(parentTile);
0128 
0129     auto index = parentTile == m_tileManager->rootTile() ? QModelIndex() : createIndex(parentTile->row(), 0, parentTile);
0130     const int pos = std::clamp(position, 0, parentTile->childCount());
0131 
0132     beginInsertRows(index, pos, pos);
0133 }
0134 
0135 void TileModel::endInsertTile()
0136 {
0137     endInsertRows();
0138 }
0139 
0140 void TileModel::beginRemoveTile(CustomTile *tile)
0141 {
0142     const auto parentTile = static_cast<CustomTile *>(tile->parentTile());
0143     if (!parentTile) {
0144         qCWarning(KWIN_CORE) << "Can't remove the root tile";
0145         return;
0146     }
0147 
0148     QModelIndex parentIndex = parentTile == m_tileManager->rootTile() ? QModelIndex() : createIndex(parentTile->row(), 0, parentTile);
0149     beginRemoveRows(parentIndex, tile->row(), tile->row());
0150 }
0151 
0152 void TileModel::endRemoveTile()
0153 {
0154     endRemoveRows();
0155 }
0156 
0157 } // namespace KWin
0158 
0159 #include "moc_tilemodel.cpp"