Due to being a compositor, kwin has to conform to some certain interfaces. It means a lot of virtual functions and function tables to integrate with C APIs. Naturally, we not always want to use every argument in such functions. Since we get -Wunused-parameter from -Wall, we have to plumb those unused arguments in order to suppress compiler warnings at the moment. However, I don't think that extra work is worth it. We cannot change or alter prototypes in any way to fix the warning the desired way. Q_UNUSED and similar macros are not good indicators of whether an argument is used too, we tend to overlook putting or removing those macros. I've also noticed that Q_UNUSED are not used to guide us with the removal no longer needed parameters. Therefore, I think it's worth adding -Wno-unused-parameter compiler option to stop the compiler producing warnings about unused parameters. It changes nothing except that we don't need to put Q_UNUSED anymore, which can be really cumbersome sometimes. Note that it doesn't affect unused variables, you'll still get a -Wunused-variable compiler warning if a variable is unused.
281 lines
7.4 KiB
C++
281 lines
7.4 KiB
C++
/*
|
|
KWin - the KDE window manager
|
|
This file is part of the KDE project.
|
|
|
|
SPDX-FileCopyrightText: 2009 Martin Gräßlin <mgraesslin@kde.org>
|
|
|
|
SPDX-License-Identifier: GPL-2.0-or-later
|
|
*/
|
|
|
|
// own
|
|
#include "clientmodel.h"
|
|
// tabbox
|
|
#include "tabboxconfig.h"
|
|
// Qt
|
|
#include <QIcon>
|
|
#include <QUuid>
|
|
// TODO: remove with Qt 5, only for HTML escaping the caption
|
|
#include <QTextDocument>
|
|
// other
|
|
#include <cmath>
|
|
|
|
namespace KWin
|
|
{
|
|
namespace TabBox
|
|
{
|
|
|
|
ClientModel::ClientModel(QObject *parent)
|
|
: QAbstractItemModel(parent)
|
|
{
|
|
}
|
|
|
|
ClientModel::~ClientModel()
|
|
{
|
|
}
|
|
|
|
QVariant ClientModel::data(const QModelIndex &index, int role) const
|
|
{
|
|
if (!index.isValid()) {
|
|
return QVariant();
|
|
}
|
|
|
|
if (m_clientList.isEmpty()) {
|
|
return QVariant();
|
|
}
|
|
|
|
int clientIndex = index.row();
|
|
if (clientIndex >= m_clientList.count()) {
|
|
return QVariant();
|
|
}
|
|
QSharedPointer<TabBoxClient> client = m_clientList[clientIndex].toStrongRef();
|
|
if (!client) {
|
|
return QVariant();
|
|
}
|
|
switch (role) {
|
|
case Qt::DisplayRole:
|
|
case CaptionRole: {
|
|
QString caption = client->caption();
|
|
if (Qt::mightBeRichText(caption)) {
|
|
caption = caption.toHtmlEscaped();
|
|
}
|
|
return caption;
|
|
}
|
|
case ClientRole:
|
|
return QVariant::fromValue<void *>(client.data());
|
|
case DesktopNameRole: {
|
|
return tabBox->desktopName(client.data());
|
|
}
|
|
case WIdRole:
|
|
return client->internalId();
|
|
case MinimizedRole:
|
|
return client->isMinimized();
|
|
case CloseableRole:
|
|
// clients that claim to be first are not closeable
|
|
return client->isCloseable() && !client->isFirstInTabBox();
|
|
case IconRole:
|
|
return client->icon();
|
|
default:
|
|
return QVariant();
|
|
}
|
|
}
|
|
|
|
QString ClientModel::longestCaption() const
|
|
{
|
|
QString caption;
|
|
for (const QWeakPointer<TabBoxClient> &clientPointer : qAsConst(m_clientList)) {
|
|
QSharedPointer<TabBoxClient> client = clientPointer.toStrongRef();
|
|
if (!client) {
|
|
continue;
|
|
}
|
|
if (client->caption().size() > caption.size()) {
|
|
caption = client->caption();
|
|
}
|
|
}
|
|
return caption;
|
|
}
|
|
|
|
int ClientModel::columnCount(const QModelIndex &parent) const
|
|
{
|
|
return 1;
|
|
}
|
|
|
|
int ClientModel::rowCount(const QModelIndex &parent) const
|
|
{
|
|
if (parent.isValid()) {
|
|
return 0;
|
|
}
|
|
return m_clientList.count();
|
|
}
|
|
|
|
QModelIndex ClientModel::parent(const QModelIndex &child) const
|
|
{
|
|
return QModelIndex();
|
|
}
|
|
|
|
QModelIndex ClientModel::index(int row, int column, const QModelIndex &parent) const
|
|
{
|
|
if (row < 0 || column != 0 || parent.isValid()) {
|
|
return QModelIndex();
|
|
}
|
|
int index = row * columnCount();
|
|
if (index >= m_clientList.count() && !m_clientList.isEmpty()) {
|
|
return QModelIndex();
|
|
}
|
|
return createIndex(row, 0);
|
|
}
|
|
|
|
QHash<int, QByteArray> ClientModel::roleNames() const
|
|
{
|
|
return {
|
|
{CaptionRole, QByteArrayLiteral("caption")},
|
|
{DesktopNameRole, QByteArrayLiteral("desktopName")},
|
|
{MinimizedRole, QByteArrayLiteral("minimized")},
|
|
{WIdRole, QByteArrayLiteral("windowId")},
|
|
{CloseableRole, QByteArrayLiteral("closeable")},
|
|
{IconRole, QByteArrayLiteral("icon")},
|
|
};
|
|
}
|
|
|
|
QModelIndex ClientModel::index(QWeakPointer<TabBoxClient> client) const
|
|
{
|
|
if (!m_clientList.contains(client)) {
|
|
return QModelIndex();
|
|
}
|
|
int index = m_clientList.indexOf(client);
|
|
int row = index / columnCount();
|
|
int column = index % columnCount();
|
|
return createIndex(row, column);
|
|
}
|
|
|
|
void ClientModel::createClientList(bool partialReset)
|
|
{
|
|
createClientList(tabBox->currentDesktop(), partialReset);
|
|
}
|
|
|
|
void ClientModel::createFocusChainClientList(int desktop,
|
|
const QSharedPointer<TabBoxClient> &start, TabBoxClientList &stickyClients)
|
|
{
|
|
auto c = start;
|
|
if (!tabBox->isInFocusChain(c.data())) {
|
|
QSharedPointer<TabBoxClient> firstClient = tabBox->firstClientFocusChain().toStrongRef();
|
|
if (firstClient) {
|
|
c = firstClient;
|
|
}
|
|
}
|
|
auto stop = c;
|
|
do {
|
|
QSharedPointer<TabBoxClient> add = tabBox->clientToAddToList(c.data(), desktop);
|
|
if (!add.isNull()) {
|
|
m_clientList += add;
|
|
if (add.data()->isFirstInTabBox()) {
|
|
stickyClients << add;
|
|
}
|
|
}
|
|
c = tabBox->nextClientFocusChain(c.data());
|
|
} while (c && c != stop);
|
|
}
|
|
|
|
void ClientModel::createStackingOrderClientList(int desktop,
|
|
const QSharedPointer<TabBoxClient> &start, TabBoxClientList &stickyClients)
|
|
{
|
|
// TODO: needs improvement
|
|
const TabBoxClientList stacking = tabBox->stackingOrder();
|
|
auto c = stacking.first().toStrongRef();
|
|
auto stop = c;
|
|
int index = 0;
|
|
while (c) {
|
|
QSharedPointer<TabBoxClient> add = tabBox->clientToAddToList(c.data(), desktop);
|
|
if (!add.isNull()) {
|
|
if (start == add.data()) {
|
|
m_clientList.removeAll(add);
|
|
m_clientList.prepend(add);
|
|
} else {
|
|
m_clientList += add;
|
|
}
|
|
if (add.data()->isFirstInTabBox()) {
|
|
stickyClients << add;
|
|
}
|
|
}
|
|
if (index >= stacking.size() - 1) {
|
|
c = nullptr;
|
|
} else {
|
|
c = stacking[++index];
|
|
}
|
|
|
|
if (c == stop) {
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
void ClientModel::createClientList(int desktop, bool partialReset)
|
|
{
|
|
auto start = tabBox->activeClient().toStrongRef();
|
|
// TODO: new clients are not added at correct position
|
|
if (partialReset && !m_clientList.isEmpty()) {
|
|
QSharedPointer<TabBoxClient> firstClient = m_clientList.constFirst();
|
|
if (firstClient) {
|
|
start = firstClient;
|
|
}
|
|
}
|
|
|
|
beginResetModel();
|
|
m_clientList.clear();
|
|
TabBoxClientList stickyClients;
|
|
|
|
switch (tabBox->config().clientSwitchingMode()) {
|
|
case TabBoxConfig::FocusChainSwitching: {
|
|
createFocusChainClientList(desktop, start, stickyClients);
|
|
break;
|
|
}
|
|
case TabBoxConfig::StackingOrderSwitching: {
|
|
createStackingOrderClientList(desktop, start, stickyClients);
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (tabBox->config().orderMinimizedMode() == TabBoxConfig::GroupByMinimized) {
|
|
// Put all non-minimized included clients first.
|
|
std::stable_partition(m_clientList.begin(), m_clientList.end(), [](const auto &client) {
|
|
return !client.toStrongRef()->isMinimized();
|
|
});
|
|
}
|
|
|
|
for (const QWeakPointer<TabBoxClient> &c : qAsConst(stickyClients)) {
|
|
m_clientList.removeAll(c);
|
|
m_clientList.prepend(c);
|
|
}
|
|
if (tabBox->config().clientApplicationsMode() != TabBoxConfig::AllWindowsCurrentApplication
|
|
&& (tabBox->config().showDesktopMode() == TabBoxConfig::ShowDesktopClient || m_clientList.isEmpty())) {
|
|
QWeakPointer<TabBoxClient> desktopClient = tabBox->desktopClient();
|
|
if (!desktopClient.isNull()) {
|
|
m_clientList.append(desktopClient);
|
|
}
|
|
}
|
|
endResetModel();
|
|
}
|
|
|
|
void ClientModel::close(int i)
|
|
{
|
|
QModelIndex ind = index(i, 0);
|
|
if (!ind.isValid()) {
|
|
return;
|
|
}
|
|
QSharedPointer<TabBoxClient> client = m_clientList.at(i).toStrongRef();
|
|
if (client) {
|
|
client->close();
|
|
}
|
|
}
|
|
|
|
void ClientModel::activate(int i)
|
|
{
|
|
QModelIndex ind = index(i, 0);
|
|
if (!ind.isValid()) {
|
|
return;
|
|
}
|
|
tabBox->setCurrentIndex(ind);
|
|
tabBox->activateAndClose();
|
|
}
|
|
|
|
} // namespace Tabbox
|
|
} // namespace KWin
|