File indexing completed on 2024-05-19 16:34:54
0001 /* 0002 KWin - the KDE window manager 0003 This file is part of the KDE project. 0004 0005 SPDX-FileCopyrightText: 2009 Martin Gräßlin <mgraesslin@kde.org> 0006 0007 SPDX-License-Identifier: GPL-2.0-or-later 0008 */ 0009 // own 0010 #include "desktopmodel.h" 0011 // tabbox 0012 #include "clientmodel.h" 0013 #include "tabboxconfig.h" 0014 #include "tabboxhandler.h" 0015 0016 #include <cmath> 0017 0018 namespace KWin 0019 { 0020 namespace TabBox 0021 { 0022 0023 DesktopModel::DesktopModel(QObject *parent) 0024 : QAbstractItemModel(parent) 0025 { 0026 } 0027 0028 DesktopModel::~DesktopModel() 0029 { 0030 } 0031 0032 QVariant DesktopModel::data(const QModelIndex &index, int role) const 0033 { 0034 if (!index.isValid() || index.column() != 0) { 0035 return QVariant(); 0036 } 0037 0038 if (index.parent().isValid()) { 0039 // parent is valid -> access to Client 0040 ClientModel *model = m_clientModels[m_desktopList[index.internalId() - 1]]; 0041 return model->data(model->index(index.row(), 0), role); 0042 } 0043 0044 const int desktopIndex = index.row(); 0045 if (desktopIndex >= m_desktopList.count()) { 0046 return QVariant(); 0047 } 0048 switch (role) { 0049 case Qt::DisplayRole: 0050 case DesktopNameRole: 0051 return tabBox->desktopName(m_desktopList[desktopIndex]); 0052 case DesktopRole: 0053 return m_desktopList[desktopIndex]; 0054 case ClientModelRole: 0055 return QVariant::fromValue<void *>(m_clientModels[m_desktopList[desktopIndex]]); 0056 default: 0057 return QVariant(); 0058 } 0059 } 0060 0061 QString DesktopModel::longestCaption() const 0062 { 0063 QString caption; 0064 for (int desktop : m_desktopList) { 0065 QString desktopName = tabBox->desktopName(desktop); 0066 if (desktopName.size() > caption.size()) { 0067 caption = desktopName; 0068 } 0069 } 0070 return caption; 0071 } 0072 0073 int DesktopModel::columnCount(const QModelIndex &parent) const 0074 { 0075 return 1; 0076 } 0077 0078 int DesktopModel::rowCount(const QModelIndex &parent) const 0079 { 0080 if (parent.isValid()) { 0081 if (parent.internalId() != 0 || parent.row() >= m_desktopList.count()) { 0082 return 0; 0083 } 0084 const int desktop = m_desktopList.at(parent.row()); 0085 const ClientModel *model = m_clientModels.value(desktop); 0086 return model->rowCount(); 0087 } 0088 return m_desktopList.count(); 0089 } 0090 0091 QModelIndex DesktopModel::parent(const QModelIndex &child) const 0092 { 0093 if (!child.isValid() || child.internalId() == 0) { 0094 return QModelIndex(); 0095 } 0096 const int row = child.internalId() - 1; 0097 if (row >= m_desktopList.count()) { 0098 return QModelIndex(); 0099 } 0100 return createIndex(row, 0); 0101 } 0102 0103 QModelIndex DesktopModel::index(int row, int column, const QModelIndex &parent) const 0104 { 0105 if (column != 0) { 0106 return QModelIndex(); 0107 } 0108 if (row < 0) { 0109 return QModelIndex(); 0110 } 0111 if (parent.isValid()) { 0112 if (parent.row() < 0 || parent.row() >= m_desktopList.count() || parent.internalId() != 0) { 0113 return QModelIndex(); 0114 } 0115 const int desktop = m_desktopList.at(parent.row()); 0116 const ClientModel *model = m_clientModels.value(desktop); 0117 if (row >= model->rowCount()) { 0118 return QModelIndex(); 0119 } 0120 return createIndex(row, column, parent.row() + 1); 0121 } 0122 if (row > m_desktopList.count() || m_desktopList.isEmpty()) { 0123 return QModelIndex(); 0124 } 0125 return createIndex(row, column); 0126 } 0127 0128 QHash<int, QByteArray> DesktopModel::roleNames() const 0129 { 0130 return { 0131 {Qt::DisplayRole, QByteArrayLiteral("display")}, 0132 {DesktopNameRole, QByteArrayLiteral("caption")}, 0133 {DesktopRole, QByteArrayLiteral("desktop")}, 0134 {ClientModelRole, QByteArrayLiteral("client")}, 0135 }; 0136 } 0137 0138 QModelIndex DesktopModel::desktopIndex(int desktop) const 0139 { 0140 if (desktop > m_desktopList.count()) { 0141 return QModelIndex(); 0142 } 0143 return createIndex(m_desktopList.indexOf(desktop), 0); 0144 } 0145 0146 void DesktopModel::createDesktopList() 0147 { 0148 beginResetModel(); 0149 m_desktopList.clear(); 0150 qDeleteAll(m_clientModels); 0151 m_clientModels.clear(); 0152 0153 switch (tabBox->config().desktopSwitchingMode()) { 0154 case TabBoxConfig::MostRecentlyUsedDesktopSwitching: { 0155 int desktop = tabBox->currentDesktop(); 0156 do { 0157 m_desktopList.append(desktop); 0158 ClientModel *clientModel = new ClientModel(this); 0159 clientModel->createClientList(desktop); 0160 m_clientModels.insert(desktop, clientModel); 0161 desktop = tabBox->nextDesktopFocusChain(desktop); 0162 } while (desktop != tabBox->currentDesktop()); 0163 break; 0164 } 0165 case TabBoxConfig::StaticDesktopSwitching: { 0166 for (int i = 1; i <= tabBox->numberOfDesktops(); i++) { 0167 m_desktopList.append(i); 0168 ClientModel *clientModel = new ClientModel(this); 0169 clientModel->createClientList(i); 0170 m_clientModels.insert(i, clientModel); 0171 } 0172 break; 0173 } 0174 } 0175 endResetModel(); 0176 } 0177 0178 } // namespace Tabbox 0179 } // namespace KWin