File: sessionlistmodel.cpp

package info (click to toggle)
kdevelop 4%3A24.12.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 71,888 kB
  • sloc: cpp: 290,869; python: 3,626; javascript: 3,518; sh: 1,316; ansic: 703; xml: 401; php: 95; lisp: 66; makefile: 31; sed: 12
file content (76 lines) | stat: -rw-r--r-- 2,043 bytes parent folder | download | duplicates (2)
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
/*
    SPDX-FileCopyrightText: 2024 Friedrich W. H. Kossebau <kossebau@kde.org>

    SPDX-License-Identifier: LGPL-2.0-or-later
*/

#include "sessionlistmodel.h"

// KDevPlatform
#include <shell/core.h>
#include <shell/sessioncontroller.h>
// Std
#include <algorithm>

SessionListModel::SessionListModel(QObject* parent)
    : QAbstractListModel(parent)
    , m_sessions(KDevelop::SessionController::availableSessionInfos())
{
    // TODO: SessionController misses a signal for new sessions, Sessions main menu would also want that
    connect(KDevelop::Core::self()->sessionController(), &KDevelop::SessionController::sessionDeleted, this,
            &SessionListModel::onSessionDeleted);
}

int SessionListModel::size() const
{
    return m_sessions.size();
}

QHash<int, QByteArray> SessionListModel::roleNames() const
{
    auto roleNames = QAbstractListModel::roleNames();
    roleNames.insert(SessionIdRole, QByteArrayLiteral("sessionId"));
    return roleNames;
}

int SessionListModel::rowCount(const QModelIndex& parent) const
{
    return parent.isValid() ? 0 : m_sessions.size();
}

QVariant SessionListModel::data(const QModelIndex& index, int role) const
{
    if (index.row() < 0 || index.row() >= m_sessions.size()) {
        return QVariant();
    }

    const auto& sessionInfo = m_sessions[index.row()];

    switch (role) {
    case Qt::DisplayRole:
        return sessionInfo.description;
    case SessionIdRole:
        return sessionInfo.uuid.toString();
    }

    return QVariant();
}

void SessionListModel::onSessionDeleted(const QString& id)
{
    auto it = std::find_if(m_sessions.cbegin(), m_sessions.cend(), [id](const KDevelop::SessionInfo& info) {
        return (info.uuid.toString() == id);
    });
    if (it == m_sessions.cend()) {
        return;
    }

    const auto i = static_cast<int>(std::distance(m_sessions.cbegin(), it));
    beginRemoveRows(QModelIndex(), i, i);
    m_sessions.removeAt(i);
    endRemoveRows();

    Q_EMIT sizeChanged(m_sessions.size());
}

#include "moc_sessionlistmodel.cpp"