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