File indexing completed on 2024-10-06 08:01:52

0001 /*
0002     SPDX-FileCopyrightText: 2020 Michail Vourlakos <mvourlakos@gmail.com>
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 
0006 #include "tasksmodel.h"
0007 
0008 // Qt
0009 #include <QDebug>
0010 
0011 // Plasma
0012 #include <Plasma/Applet>
0013 #include <PlasmaQuick/AppletQuickItem>
0014 
0015 namespace Latte {
0016 namespace ViewPart {
0017 
0018 TasksModel::TasksModel(QObject *parent)
0019     : QAbstractListModel(parent)
0020 {
0021 }
0022 
0023 int TasksModel::count() const
0024 {
0025     return m_tasks.count();
0026 }
0027 
0028 int TasksModel::rowCount(const QModelIndex &parent) const
0029 {
0030     return m_tasks.count();
0031 }
0032 
0033 QVariant TasksModel::data(const QModelIndex &index, int role) const
0034 {
0035     bool rowIsValid = (index.row()>=0 && index.row()<m_tasks.count());
0036     if (!rowIsValid) {
0037         return QVariant();
0038     }
0039 
0040     if (role == Qt::UserRole) {
0041         return QVariant::fromValue(m_tasks[index.row()]);
0042     }
0043 
0044     return QVariant();
0045 }
0046 
0047 
0048 QHash<int, QByteArray> TasksModel::roleNames() const{
0049     QHash<int, QByteArray> roles;
0050     roles[Qt::UserRole] = "tasks";
0051     return roles;
0052 }
0053 
0054 void TasksModel::addTask(PlasmaQuick::AppletQuickItem *plasmoid)
0055 {
0056     if (plasmoid && m_tasks.contains(plasmoid)) {
0057         return;
0058     }
0059 
0060     beginInsertRows(QModelIndex(), rowCount(), rowCount());
0061     m_tasks << plasmoid;
0062     endInsertRows();
0063 
0064     connect(plasmoid, &QObject::destroyed, this, [&, plasmoid](){
0065         removeTask(plasmoid);
0066     });
0067 
0068     connect(plasmoid->applet(), &Plasma::Applet::destroyedChanged, this, [&, plasmoid](const bool &destroyed){
0069         if (destroyed) {
0070             moveIntoWaitingTasks(plasmoid);
0071         } else {
0072             restoreFromWaitingTasks(plasmoid);
0073         }
0074     });
0075 
0076     emit countChanged();
0077 }
0078 
0079 void TasksModel::moveIntoWaitingTasks(PlasmaQuick::AppletQuickItem *plasmoid)
0080 {
0081     if (plasmoid && !m_tasks.contains(plasmoid)) {
0082         return;
0083     }
0084 
0085     int tind = m_tasks.indexOf(plasmoid);
0086 
0087     if (tind >= 0) {
0088         beginRemoveRows(QModelIndex(), tind, tind);
0089         m_tasksWaiting << m_tasks.takeAt(tind);
0090         endRemoveRows();
0091         emit countChanged();
0092     }
0093 }
0094 
0095 void TasksModel::restoreFromWaitingTasks(PlasmaQuick::AppletQuickItem *plasmoid)
0096 {
0097     if (plasmoid && !m_tasksWaiting.contains(plasmoid)) {
0098         return;
0099     }
0100 
0101     int tind = m_tasksWaiting.indexOf(plasmoid);
0102 
0103     if (tind >= 0) {
0104         beginInsertRows(QModelIndex(), rowCount(), rowCount());
0105         m_tasks << m_tasksWaiting.takeAt(tind);
0106         endInsertRows();
0107         emit countChanged();
0108     }
0109 }
0110 
0111 void TasksModel::removeTask(PlasmaQuick::AppletQuickItem *plasmoid)
0112 {
0113     if (!plasmoid || (plasmoid && !m_tasks.contains(plasmoid) && !m_tasksWaiting.contains(plasmoid))) {
0114         return;
0115     }
0116 
0117     if (m_tasks.contains(plasmoid)) {
0118         int iex = m_tasks.indexOf(plasmoid);
0119 
0120         beginRemoveRows(QModelIndex(), iex, iex);
0121         m_tasks.removeAll(plasmoid);
0122         endRemoveRows();
0123 
0124         emit countChanged();
0125     } else if (m_tasksWaiting.contains(plasmoid)) {
0126         m_tasksWaiting.removeAll(plasmoid);
0127     }
0128 }
0129 
0130 }
0131 }