File: environmentprofilelistmodel.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 (202 lines) | stat: -rw-r--r-- 5,277 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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
    SPDX-FileCopyrightText: 2007 Andreas Pakulat <apaku@gmx.de>
    SPDX-FileCopyrightText: 2017 Friedrich W. H. Kossebau <kossebau@kde.org>

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

#include "environmentprofilelistmodel.h"

#include <KLocalizedString>

using namespace KDevelop;

EnvironmentProfileListModel::EnvironmentProfileListModel(QObject* parent)
    : QAbstractItemModel(parent)
{
}

int EnvironmentProfileListModel::rowCount(const QModelIndex& parent) const
{
    if (parent.isValid()) {
        return 0;
    }

    return profileNames().count();
}

int EnvironmentProfileListModel::columnCount(const QModelIndex& parent) const
{
    if (parent.isValid()) {
        return 0;
    }

    return 1;
}

QVariant EnvironmentProfileListModel::data(const QModelIndex& index, int role) const
{
    if (!index.isValid() ||
        index.row() < 0 || index.row() >= rowCount() ||
        index.column() != 0) {
        return {};
    }

    if (role == Qt::DisplayRole) {
        auto profileName = profileNames().at(index.row());
        if (profileName == defaultProfileName()) {
            profileName = i18n("%1 (default profile)", profileName);
        }
        return profileName;
    }

    return {};
}

QModelIndex EnvironmentProfileListModel::index(int row, int column, const QModelIndex& parent) const
{
    if (parent.isValid()) {
        return {};
    }
    return createIndex(row, column);
}

QModelIndex EnvironmentProfileListModel::parent(const QModelIndex& index) const
{
    Q_UNUSED(index);
    return {};
}

int EnvironmentProfileListModel::defaultProfileIndex() const
{
    return profileNames().indexOf(defaultProfileName());
}

QString EnvironmentProfileListModel::profileName(int profileIndex) const
{
    return profileNames().value(profileIndex);
}

int EnvironmentProfileListModel::profileIndex(const QString& profileName) const
{
    return profileNames().indexOf(profileName);
}

bool EnvironmentProfileListModel::hasProfile(const QString& profileName) const
{
    return profileNames().contains(profileName);
}

QMap<QString, QString>& EnvironmentProfileListModel::variables(const QString& profileName)
{
    return EnvironmentProfileList::variables(profileName);
}

int EnvironmentProfileListModel::addProfile(const QString& newProfileName)
{
    const auto profileNames = this->profileNames();
    if (newProfileName.isEmpty() ||
        profileNames.contains(newProfileName)) {
        return -1;
    }

    // estimate insert position by comparison as used by QMap for the keys
    int insertPos = 0;
    while (insertPos < profileNames.size() &&
           profileNames.at(insertPos) < newProfileName) {
        ++insertPos;
    }
    beginInsertRows(QModelIndex(), insertPos, insertPos);

    // trigger creation of new profile
    variables(newProfileName);

    endInsertRows();
    return insertPos;
}

void EnvironmentProfileListModel::removeProfile(int profileIndex)
{
    const auto profileNames = this->profileNames();
    if (profileIndex < 0 || profileIndex >= profileNames.size()) {
        return;
    }
    const auto profileName = profileNames.at(profileIndex);
    if (defaultProfileName() == profileName) {
        return;
    }

    emit profileAboutToBeRemoved(profileName);

    beginRemoveRows(QModelIndex(), profileIndex, profileIndex);

    EnvironmentProfileList::removeProfile(profileName);

    endRemoveRows();
}

int EnvironmentProfileListModel::cloneProfile(const QString& newProfileName, const QString& sourceProfileName)
{
    const auto profileNames = this->profileNames();
    if (newProfileName.isEmpty() ||
        profileNames.contains(newProfileName) ||
        !profileNames.contains(sourceProfileName)) {
        return -1;
    }

    // estimate insert position by comparison as used by QMap for the keys
    int insertPos = 0;
    while (insertPos < profileNames.size() &&
           profileNames.at(insertPos) < newProfileName) {
        ++insertPos;
    }

    beginInsertRows(QModelIndex(), insertPos, insertPos);

    variables(newProfileName) = variables(sourceProfileName);

    endInsertRows();

    return insertPos;
}

void EnvironmentProfileListModel::setDefaultProfile(int profileIndex)
{
    const auto profileNames = this->profileNames();
    const auto oldDefaultProfileName = defaultProfileName();
    const int oldDefaultProfileIndex = profileNames.indexOf(oldDefaultProfileName);

    if (profileIndex < 0 || profileIndex >= profileNames.size() ||
        oldDefaultProfileIndex == profileIndex) {
        return;
    }

    const auto oldIndex = index(oldDefaultProfileIndex, 0);

    const auto profileName = profileNames.at(profileIndex);
    EnvironmentProfileList::setDefaultProfile(profileName);

    const int newDefaultProfileIndex = profileNames.indexOf(profileName);
    const auto newIndex = index(newDefaultProfileIndex, 0);

    emit dataChanged(oldIndex, oldIndex);
    emit dataChanged(newIndex, newIndex);
    emit defaultProfileChanged(newDefaultProfileIndex);
}

void EnvironmentProfileListModel::loadFromConfig(KConfig* config)
{
    beginResetModel();

    loadSettings(config);

    endResetModel();
}


void EnvironmentProfileListModel::saveToConfig(KConfig* config)
{
    saveSettings(config);
}

#include "moc_environmentprofilelistmodel.cpp"