forked from openkylin/ukui-panel
179 lines
6.5 KiB
C++
179 lines
6.5 KiB
C++
/*
|
|
*
|
|
* * Copyright (C) 2023, KylinSoft Co., Ltd.
|
|
* *
|
|
* * This program is free software: you can redistribute it and/or modify
|
|
* * it under the terms of the GNU General Public License as published by
|
|
* * the Free Software Foundation, either version 3 of the License, or
|
|
* * (at your option) any later version.
|
|
* *
|
|
* * This program is distributed in the hope that it will be useful,
|
|
* * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* * GNU General Public License for more details.
|
|
* *
|
|
* * You should have received a copy of the GNU General Public License
|
|
* * along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
* *
|
|
* * Authors: iaom <zhangpengfei@kylinos.cn>
|
|
*
|
|
*/
|
|
|
|
#include "task-manager-model.h"
|
|
#include <QAbstractItemModel>
|
|
#include <QDebug>
|
|
#include <QVariant>
|
|
#include "ukui-task-manager.h"
|
|
|
|
namespace TaskManager {
|
|
TaskManagerModel::TaskManagerModel(UkuiTaskManager *parent) : QAbstractListModel(parent), m_sourceModel(parent)
|
|
{
|
|
connect(m_sourceModel, &QAbstractItemModel::rowsInserted, this, &TaskManagerModel::onRowInserted);
|
|
connect(m_sourceModel, &QAbstractItemModel::rowsAboutToBeRemoved, this, &TaskManagerModel::onRowRemoved);
|
|
connect(m_sourceModel, &QAbstractItemModel::dataChanged, this,
|
|
[&](const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles = QVector<int>()){
|
|
Q_EMIT dataChanged(index(m_data.indexOf(topLeft), 0, {}), index(m_data.indexOf(bottomRight), 0, {}), roles);
|
|
});
|
|
for(int row = 0; row < m_sourceModel->rowCount({}); ++row) {
|
|
m_data.append(m_sourceModel->index(row, 0, {}));
|
|
}
|
|
}
|
|
|
|
QModelIndex TaskManagerModel::index(int row, int column, const QModelIndex &parent) const
|
|
{
|
|
if (row < 0 || column != 0) {
|
|
return {};
|
|
}
|
|
return createIndex(row, column, nullptr);
|
|
}
|
|
|
|
int TaskManagerModel::rowCount(const QModelIndex &parent) const
|
|
{
|
|
return parent.isValid() ? 0 : m_data.count();;
|
|
}
|
|
|
|
QHash<int, QByteArray> TaskManagerModel::roleNames() const
|
|
{
|
|
return m_sourceModel->roleNames();
|
|
}
|
|
|
|
QVariant TaskManagerModel::data(const QModelIndex &index, int role) const
|
|
{
|
|
if (!index.isValid() || index.row() >= m_data.count()) {
|
|
return {};
|
|
}
|
|
if(role == UkuiTaskManager::Actions) {
|
|
Actions actions = m_data.at(index.row()).data(UkuiTaskManager::Actions).value<Actions>();
|
|
for(auto action : actions) {
|
|
if(action->type() == Action::Type::AddQuickLauncher) {
|
|
action->setParam(quickLauncherCountBeforeRow(index.row()));
|
|
break;
|
|
}
|
|
}
|
|
return QVariant::fromValue(actions);
|
|
}
|
|
return m_data.at(index.row()).data(role);
|
|
}
|
|
|
|
void TaskManagerModel::onRowInserted(const QModelIndex &parent, int first, int last)
|
|
{
|
|
for(int row = first; row <= last; ++row) {
|
|
QModelIndex index = m_sourceModel->index(row, 0, {});
|
|
int quickLauncherIndex = index.data(UkuiTaskManager::QuickLauncherIndex).toInt();
|
|
|
|
if(quickLauncherIndex < 0) {
|
|
beginInsertRows({}, m_data.size(), m_data.size());
|
|
m_data.append(index);
|
|
endInsertRows();
|
|
} else {
|
|
QString desktopFile = index.data(UkuiTaskManager::Id).toString();
|
|
if(m_quickLauncherCache.contains(desktopFile)) {
|
|
int order = m_quickLauncherCache.take(desktopFile);
|
|
beginInsertRows({}, order, order);
|
|
m_data.insert(order, index);
|
|
endInsertRows();
|
|
} else {
|
|
beginInsertRows({}, m_data.size(), m_data.size());
|
|
m_data.append(index);
|
|
endInsertRows();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void TaskManagerModel::onRowRemoved(const QModelIndex &parent, int first, int last)
|
|
{
|
|
for(int row = first; row <= last; ++row) {
|
|
QModelIndex sourceIndex = m_sourceModel->index(row, 0, {});
|
|
int order = m_data.indexOf(sourceIndex);
|
|
beginRemoveRows({}, order, order);
|
|
m_data.remove(order);
|
|
endRemoveRows();
|
|
}
|
|
}
|
|
|
|
void TaskManagerModel::setOrder(const QModelIndex &oldIndex, const QModelIndex &newIndex)
|
|
{
|
|
QModelIndex sourceIndex = m_data.at(oldIndex.row());
|
|
int oldOrder = oldIndex.row();
|
|
int order = newIndex.row();
|
|
|
|
if(order == oldOrder) {
|
|
return;
|
|
}
|
|
int oldQuickLauncher = sourceIndex.data(UkuiTaskManager::QuickLauncherIndex).toInt();
|
|
int launcherCount = 0;
|
|
if(order > oldOrder) {
|
|
for(int row = oldOrder + 1; row <= order; ++row) {
|
|
if(m_data.at(row).data(UkuiTaskManager::QuickLauncherIndex).toInt() >= 0) {
|
|
launcherCount++;
|
|
}
|
|
}
|
|
if(oldQuickLauncher >= 0 && launcherCount > 0) {
|
|
m_sourceModel->AddQuickLauncher(sourceIndex.data(UkuiTaskManager::Id).toString(),
|
|
oldQuickLauncher + launcherCount);
|
|
}
|
|
beginMoveRows(oldIndex.parent(), oldOrder, oldOrder, oldIndex.parent(), order + 1);
|
|
m_data.insert(order, m_data.takeAt(oldOrder));
|
|
endMoveRows();
|
|
} else {
|
|
for(int row = oldOrder - 1; row >= order; --row) {
|
|
if(m_data.at(row).data(UkuiTaskManager::QuickLauncherIndex).toInt() >= 0) {
|
|
launcherCount++;
|
|
}
|
|
}
|
|
if(oldQuickLauncher >= 0 && launcherCount > 0) {
|
|
m_sourceModel->AddQuickLauncher(sourceIndex.data(UkuiTaskManager::Id).toString(),
|
|
oldQuickLauncher - launcherCount);
|
|
}
|
|
beginMoveRows(oldIndex.parent(), oldOrder, oldOrder, oldIndex.parent(), order);
|
|
m_data.insert(order, m_data.takeAt(oldOrder));
|
|
endMoveRows();
|
|
}
|
|
}
|
|
|
|
void TaskManagerModel::addQuickLauncher(const QString &desktopFile, const QModelIndex &newIndex)
|
|
{
|
|
if(!newIndex.isValid()) {
|
|
m_sourceModel->AddQuickLauncher(desktopFile, quickLauncherCountBeforeRow(rowCount({})));
|
|
}
|
|
m_quickLauncherCache.insert(desktopFile, newIndex.row());
|
|
m_sourceModel->AddQuickLauncher(desktopFile, quickLauncherCountBeforeRow(newIndex.row()));
|
|
}
|
|
|
|
void TaskManagerModel::removeQuickLauncher(const QString &desktopFile)
|
|
{
|
|
m_sourceModel->AddQuickLauncher(desktopFile, -1);
|
|
}
|
|
|
|
int TaskManagerModel::quickLauncherCountBeforeRow(int row) const
|
|
{
|
|
int launcherCount = 0;
|
|
for(int i = 0; i < row; ++i) {
|
|
if(m_data.at(i).data(UkuiTaskManager::QuickLauncherIndex).toInt() >= 0) {
|
|
launcherCount++;
|
|
}
|
|
}
|
|
return launcherCount;
|
|
}
|
|
} // UkuiTaskManager
|