Files
fedora-tools/plasma-task-group-shortcuts/taskselection.cpp
T

94 lines
3.1 KiB
C++

// SPDX-License-Identifier: MIT
#include "taskselection.h"
#include <QAbstractItemModel>
#include <QHash>
#include <QSet>
#include <QUrl>
#include <abstracttasksmodel.h>
#include <tasktools.h>
using TaskManager::AbstractTasksModel;
QModelIndex taskToActivate(const QAbstractItemModel &model, int row, int cyclePosition)
{
const QModelIndex task = model.index(row, 0);
if (!task.isValid() || model.rowCount(task) == 0) {
return task;
}
const int childCount = model.rowCount(task);
const int childRow = ((cyclePosition % childCount) + childCount) % childCount;
return model.index(childRow, 0, task);
}
QVector<int> logicalTaskRows(const QAbstractItemModel &model, bool separateLaunchers)
{
QVector<int> rows;
rows.reserve(model.rowCount());
if (separateLaunchers) {
for (int row = 0; row < model.rowCount(); ++row) {
rows.append(row);
}
return rows;
}
QHash<int, int> launcherTargets;
QSet<int> mergedWindows;
for (int launcherRow = 0; launcherRow < model.rowCount(); ++launcherRow) {
const QModelIndex launcher = model.index(launcherRow, 0);
if (!launcher.data(AbstractTasksModel::IsLauncher).toBool()) {
continue;
}
for (int windowRow = 0; windowRow < model.rowCount(); ++windowRow) {
const QModelIndex window = model.index(windowRow, 0);
if (!window.data(AbstractTasksModel::IsWindow).toBool()) {
continue;
}
const QString launcherAppId = launcher.data(AbstractTasksModel::AppId).toString();
const QString windowAppId = window.data(AbstractTasksModel::AppId).toString();
const QUrl launcherUrl = launcher.data(AbstractTasksModel::LauncherUrlWithoutIcon).toUrl();
const QUrl windowUrl = window.data(AbstractTasksModel::LauncherUrlWithoutIcon).toUrl();
if ((!launcherAppId.isEmpty() && launcherAppId == windowAppId)
|| (launcherUrl.isValid() && windowUrl.isValid()
&& TaskManager::launcherUrlsMatch(launcherUrl, windowUrl, TaskManager::IgnoreQueryItems))) {
launcherTargets.insert(launcherRow, windowRow);
mergedWindows.insert(windowRow);
break;
}
}
}
for (int row = 0; row < model.rowCount(); ++row) {
if (launcherTargets.contains(row)) {
rows.append(launcherTargets.value(row));
} else if (!mergedWindows.contains(row)) {
rows.append(row);
}
}
return rows;
}
int initialCyclePosition(const QAbstractItemModel &model, int row, bool startWithFirst, bool backwards)
{
const QModelIndex group = model.index(row, 0);
const int childCount = model.rowCount(group);
if (childCount == 0) {
return 0;
}
if (!startWithFirst) {
for (int child = 0; child < childCount; ++child) {
if (model.index(child, 0, group).data(AbstractTasksModel::IsActive).toBool()) {
return child + (backwards ? -1 : 1);
}
}
}
return backwards ? childCount - 1 : 0;
}