File: tasksmodel.cpp

package info (click to toggle)
latte-dock 0.10.9-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid
  • size: 11,480 kB
  • sloc: cpp: 43,957; xml: 833; javascript: 734; sh: 43; makefile: 3
file content (131 lines) | stat: -rw-r--r-- 2,956 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
    SPDX-FileCopyrightText: 2020 Michail Vourlakos <mvourlakos@gmail.com>
    SPDX-License-Identifier: GPL-2.0-or-later
*/

#include "tasksmodel.h"

// Qt
#include <QDebug>

// Plasma
#include <Plasma/Applet>
#include <PlasmaQuick/AppletQuickItem>

namespace Latte {
namespace ViewPart {

TasksModel::TasksModel(QObject *parent)
    : QAbstractListModel(parent)
{
}

int TasksModel::count() const
{
    return m_tasks.count();
}

int TasksModel::rowCount(const QModelIndex &parent) const
{
    return m_tasks.count();
}

QVariant TasksModel::data(const QModelIndex &index, int role) const
{
    bool rowIsValid = (index.row()>=0 && index.row()<m_tasks.count());
    if (!rowIsValid) {
        return QVariant();
    }

    if (role == Qt::UserRole) {
        return QVariant::fromValue(m_tasks[index.row()]);
    }

    return QVariant();
}


QHash<int, QByteArray> TasksModel::roleNames() const{
    QHash<int, QByteArray> roles;
    roles[Qt::UserRole] = "tasks";
    return roles;
}

void TasksModel::addTask(PlasmaQuick::AppletQuickItem *plasmoid)
{
    if (plasmoid && m_tasks.contains(plasmoid)) {
        return;
    }

    beginInsertRows(QModelIndex(), rowCount(), rowCount());
    m_tasks << plasmoid;
    endInsertRows();

    connect(plasmoid, &QObject::destroyed, this, [&, plasmoid](){
        removeTask(plasmoid);
    });

    connect(plasmoid->applet(), &Plasma::Applet::destroyedChanged, this, [&, plasmoid](const bool &destroyed){
        if (destroyed) {
            moveIntoWaitingTasks(plasmoid);
        } else {
            restoreFromWaitingTasks(plasmoid);
        }
    });

    emit countChanged();
}

void TasksModel::moveIntoWaitingTasks(PlasmaQuick::AppletQuickItem *plasmoid)
{
    if (plasmoid && !m_tasks.contains(plasmoid)) {
        return;
    }

    int tind = m_tasks.indexOf(plasmoid);

    if (tind >= 0) {
        beginRemoveRows(QModelIndex(), tind, tind);
        m_tasksWaiting << m_tasks.takeAt(tind);
        endRemoveRows();
        emit countChanged();
    }
}

void TasksModel::restoreFromWaitingTasks(PlasmaQuick::AppletQuickItem *plasmoid)
{
    if (plasmoid && !m_tasksWaiting.contains(plasmoid)) {
        return;
    }

    int tind = m_tasksWaiting.indexOf(plasmoid);

    if (tind >= 0) {
        beginInsertRows(QModelIndex(), rowCount(), rowCount());
        m_tasks << m_tasksWaiting.takeAt(tind);
        endInsertRows();
        emit countChanged();
    }
}

void TasksModel::removeTask(PlasmaQuick::AppletQuickItem *plasmoid)
{
    if (!plasmoid || (plasmoid && !m_tasks.contains(plasmoid) && !m_tasksWaiting.contains(plasmoid))) {
        return;
    }

    if (m_tasks.contains(plasmoid)) {
        int iex = m_tasks.indexOf(plasmoid);

        beginRemoveRows(QModelIndex(), iex, iex);
        m_tasks.removeAll(plasmoid);
        endRemoveRows();

        emit countChanged();
    } else if (m_tasksWaiting.contains(plasmoid)) {
        m_tasksWaiting.removeAll(plasmoid);
    }
}

}
}