File indexing completed on 2024-11-10 04:57:19
0001 /* 0002 SPDX-FileCopyrightText: 2021 Vlad Zahorodnii <vlad.zahorodnii@kde.org> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #include "virtualdesktopmodel.h" 0008 #include "virtualdesktops.h" 0009 0010 namespace KWin 0011 { 0012 0013 VirtualDesktopModel::VirtualDesktopModel(QObject *parent) 0014 : QAbstractListModel(parent) 0015 { 0016 VirtualDesktopManager *manager = VirtualDesktopManager::self(); 0017 0018 connect(manager, &VirtualDesktopManager::desktopAdded, 0019 this, &VirtualDesktopModel::handleVirtualDesktopAdded); 0020 connect(manager, &VirtualDesktopManager::desktopRemoved, 0021 this, &VirtualDesktopModel::handleVirtualDesktopRemoved); 0022 0023 m_virtualDesktops = manager->desktops(); 0024 } 0025 0026 VirtualDesktop *VirtualDesktopModel::create(uint position, const QString &name) 0027 { 0028 return VirtualDesktopManager::self()->createVirtualDesktop(position, name); 0029 } 0030 0031 void VirtualDesktopModel::remove(uint position) 0032 { 0033 if (position < m_virtualDesktops.count()) { 0034 VirtualDesktopManager::self()->removeVirtualDesktop(m_virtualDesktops[position]); 0035 } 0036 } 0037 0038 void VirtualDesktopModel::handleVirtualDesktopAdded(VirtualDesktop *desktop) 0039 { 0040 const int position = desktop->x11DesktopNumber() - 1; 0041 beginInsertRows(QModelIndex(), position, position); 0042 m_virtualDesktops.insert(position, desktop); 0043 endInsertRows(); 0044 } 0045 0046 void VirtualDesktopModel::handleVirtualDesktopRemoved(VirtualDesktop *desktop) 0047 { 0048 const int index = m_virtualDesktops.indexOf(desktop); 0049 Q_ASSERT(index != -1); 0050 0051 beginRemoveRows(QModelIndex(), index, index); 0052 m_virtualDesktops.removeAt(index); 0053 endRemoveRows(); 0054 } 0055 0056 QHash<int, QByteArray> VirtualDesktopModel::roleNames() const 0057 { 0058 QHash<int, QByteArray> roleNames = QAbstractListModel::roleNames(); 0059 roleNames.insert(DesktopRole, QByteArrayLiteral("desktop")); 0060 return roleNames; 0061 } 0062 0063 VirtualDesktop *VirtualDesktopModel::desktopFromIndex(const QModelIndex &index) const 0064 { 0065 if (!index.isValid() || index.row() < 0 || index.row() >= m_virtualDesktops.count()) { 0066 return nullptr; 0067 } 0068 return m_virtualDesktops[index.row()]; 0069 } 0070 0071 QVariant VirtualDesktopModel::data(const QModelIndex &index, int role) const 0072 { 0073 VirtualDesktop *desktop = desktopFromIndex(index); 0074 if (!desktop) { 0075 return QVariant(); 0076 } 0077 switch (role) { 0078 case Qt::DisplayRole: 0079 case DesktopRole: 0080 return QVariant::fromValue(desktop); 0081 default: 0082 return QVariant(); 0083 } 0084 } 0085 0086 int VirtualDesktopModel::rowCount(const QModelIndex &parent) const 0087 { 0088 return parent.isValid() ? 0 : m_virtualDesktops.count(); 0089 } 0090 0091 } // namespace KWin 0092 0093 #include "moc_virtualdesktopmodel.cpp"