Changeset View
Changeset View
Standalone View
Standalone View
shell/settings/environmentprofilemodel.cpp
- This file was added.
1 | /* This file is part of KDevelop | ||||
---|---|---|---|---|---|
2 | Copyright 2007 Andreas Pakulat <apaku@gmx.de> | ||||
3 | Copyright 2017 Friedrich W. H. Kossebau <kossebau@kde.org> | ||||
4 | | ||||
5 | This library is free software; you can redistribute it and/or | ||||
6 | modify it under the terms of the GNU Library General Public | ||||
7 | License as published by the Free Software Foundation; either | ||||
8 | version 2 of the License, or (at your option) any later version. | ||||
9 | | ||||
10 | This library is distributed in the hope that it will be useful, | ||||
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||||
13 | Library General Public License for more details. | ||||
14 | | ||||
15 | You should have received a copy of the GNU Library General Public License | ||||
16 | along with this library; see the file COPYING.LIB. If not, write to | ||||
17 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | ||||
18 | Boston, MA 02110-1301, USA. | ||||
19 | */ | ||||
20 | | ||||
21 | #include "environmentprofilemodel.h" | ||||
22 | | ||||
23 | #include "environmentprofilelistmodel.h" | ||||
24 | | ||||
25 | #include <KLocalizedString> | ||||
26 | | ||||
27 | #include <QVariant> | ||||
28 | #include <QModelIndex> | ||||
29 | | ||||
30 | using namespace KDevelop; | ||||
31 | | ||||
32 | EnvironmentProfileModel::EnvironmentProfileModel(EnvironmentProfileListModel* profileListModel, | ||||
33 | QObject* parent) | ||||
34 | : QAbstractTableModel(parent) | ||||
35 | , m_profileListModel(profileListModel) | ||||
36 | { | ||||
37 | connect(m_profileListModel, &EnvironmentProfileListModel::profileAboutToBeRemoved, | ||||
38 | this, &EnvironmentProfileModel::onProfileAboutToBeRemoved); | ||||
39 | } | ||||
40 | | ||||
41 | int EnvironmentProfileModel::rowCount(const QModelIndex& parent) const | ||||
42 | { | ||||
43 | if (parent.isValid()) { | ||||
44 | return 0; | ||||
45 | } | ||||
46 | | ||||
47 | return m_varsByIndex.count(); | ||||
48 | } | ||||
49 | | ||||
50 | int EnvironmentProfileModel::columnCount(const QModelIndex& parent) const | ||||
51 | { | ||||
52 | if (parent.isValid()) { | ||||
53 | return 0; | ||||
54 | } | ||||
55 | | ||||
56 | return 2; | ||||
57 | } | ||||
58 | | ||||
59 | Qt::ItemFlags EnvironmentProfileModel::flags(const QModelIndex& index) const | ||||
60 | { | ||||
61 | if (!index.isValid()) { | ||||
62 | return Qt::NoItemFlags; | ||||
63 | } | ||||
64 | | ||||
65 | return (Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsEnabled); | ||||
66 | } | ||||
67 | | ||||
68 | QVariant EnvironmentProfileModel::data(const QModelIndex& index, int role) const | ||||
69 | { | ||||
70 | if (!index.isValid() || | ||||
71 | index.row() < 0 || index.row() >= rowCount() || | ||||
72 | index.column() < 0 || index.column() >= columnCount(QModelIndex()) || | ||||
73 | m_currentProfileName.isEmpty()) { | ||||
74 | return {}; | ||||
75 | } | ||||
76 | | ||||
77 | const auto variable = m_varsByIndex[index.row()]; | ||||
78 | if (role == VariableRole) { | ||||
79 | return variable; | ||||
80 | } | ||||
81 | if (role == ValueRole) { | ||||
82 | const auto& variables = m_profileListModel->variables(m_currentProfileName); | ||||
83 | return variables.value(variable); | ||||
84 | } | ||||
85 | if (role == Qt::DisplayRole || role == Qt::EditRole) { | ||||
86 | if (index.column() == VariableColumn) { | ||||
87 | return variable; | ||||
88 | } | ||||
89 | const auto& variables = m_profileListModel->variables(m_currentProfileName); | ||||
90 | return variables.value(variable); | ||||
91 | } | ||||
92 | return {}; | ||||
93 | } | ||||
94 | | ||||
95 | QVariant EnvironmentProfileModel::headerData(int section, Qt::Orientation orientation, int role) const | ||||
96 | { | ||||
97 | if (section < 0 || section >= columnCount(QModelIndex()) || | ||||
98 | orientation != Qt::Horizontal || | ||||
99 | role != Qt::DisplayRole) { | ||||
100 | return {}; | ||||
101 | } | ||||
102 | | ||||
103 | if (section == VariableColumn) { | ||||
104 | return i18n("Variable"); | ||||
105 | } | ||||
106 | return i18n("Value"); | ||||
107 | } | ||||
108 | | ||||
109 | bool EnvironmentProfileModel::setData(const QModelIndex& index, const QVariant& data, int role) | ||||
110 | { | ||||
111 | if (!index.isValid() || | ||||
112 | index.row() < 0 || index.row() >= rowCount() || | ||||
113 | index.column() < 0 || index.column() >= columnCount(QModelIndex()) || | ||||
114 | m_currentProfileName.isEmpty()) { | ||||
115 | return false; | ||||
116 | } | ||||
117 | | ||||
118 | if (role == Qt::EditRole) { | ||||
119 | auto& variables = m_profileListModel->variables(m_currentProfileName); | ||||
120 | if (index.column() == VariableColumn) { | ||||
121 | const QString oldVariable = m_varsByIndex[index.row()]; | ||||
122 | const QString value = variables.take(oldVariable); | ||||
123 | const QString newVariable = data.toString(); | ||||
124 | variables.insert(newVariable, value); | ||||
125 | m_varsByIndex[index.row()] = newVariable; | ||||
126 | } else { | ||||
127 | variables.insert(m_varsByIndex[index.row()], data.toString()); | ||||
128 | } | ||||
129 | emit dataChanged(index, index); | ||||
130 | } | ||||
131 | return true; | ||||
132 | } | ||||
133 | | ||||
134 | void EnvironmentProfileModel::addVariable(const QString& variableName, const QString& value) | ||||
135 | { | ||||
136 | if (m_currentProfileName.isEmpty()) { | ||||
137 | return; | ||||
138 | } | ||||
139 | | ||||
140 | const int pos = m_varsByIndex.indexOf(variableName); | ||||
141 | if (pos != -1) { | ||||
142 | return; // No duplicates, first value | ||||
143 | } | ||||
144 | | ||||
145 | auto& variables = m_profileListModel->variables(m_currentProfileName); | ||||
146 | | ||||
147 | const int insertPos = rowCount(); | ||||
148 | beginInsertRows(QModelIndex(), insertPos, insertPos); | ||||
149 | m_varsByIndex << variableName; | ||||
150 | variables.insert(variableName, value); | ||||
151 | endInsertRows(); | ||||
152 | } | ||||
153 | | ||||
154 | void EnvironmentProfileModel::removeVariables(const QStringList& variableNames) | ||||
155 | { | ||||
156 | for (const auto& variableName : variableNames) { | ||||
157 | removeVariable(variableName); | ||||
158 | } | ||||
159 | } | ||||
160 | | ||||
161 | void EnvironmentProfileModel::removeVariable(const QString& variableName) | ||||
162 | { | ||||
163 | if (m_currentProfileName.isEmpty()) { | ||||
164 | return; | ||||
165 | } | ||||
166 | | ||||
167 | const int pos = m_varsByIndex.indexOf(variableName); | ||||
168 | if (pos == -1) { | ||||
169 | return; | ||||
170 | } | ||||
171 | | ||||
172 | auto& variables = m_profileListModel->variables(m_currentProfileName); | ||||
173 | | ||||
174 | beginRemoveRows(QModelIndex(), pos, pos); | ||||
175 | m_varsByIndex.removeAt(pos); | ||||
176 | variables.remove(variableName); | ||||
177 | endRemoveRows(); | ||||
178 | } | ||||
179 | | ||||
180 | void EnvironmentProfileModel::setCurrentProfile(const QString& profileName) | ||||
181 | { | ||||
182 | if (profileName == m_currentProfileName) { | ||||
183 | return; | ||||
184 | } | ||||
185 | | ||||
186 | beginResetModel(); | ||||
187 | m_currentProfileName = profileName; | ||||
188 | m_varsByIndex.clear(); | ||||
189 | | ||||
190 | if (!m_currentProfileName.isEmpty()) { | ||||
191 | const auto& variables = m_profileListModel->variables(m_currentProfileName); | ||||
192 | | ||||
193 | const auto endIt = variables.constEnd(); | ||||
194 | for (auto it = variables.constBegin(); it != endIt; ++it) { | ||||
195 | m_varsByIndex << it.key(); | ||||
196 | } | ||||
197 | } | ||||
198 | endResetModel(); | ||||
199 | } | ||||
200 | | ||||
201 | void EnvironmentProfileModel::setVariablesFromString(const QString& plainText) | ||||
202 | { | ||||
203 | if (m_currentProfileName.isEmpty()) { | ||||
204 | return; | ||||
205 | } | ||||
206 | | ||||
207 | beginResetModel(); | ||||
208 | | ||||
209 | auto& variables = m_profileListModel->variables(m_currentProfileName); | ||||
210 | variables.clear(); | ||||
211 | m_varsByIndex.clear(); | ||||
212 | | ||||
213 | const auto lines = plainText.split(QLatin1Char('\n'), QString::SkipEmptyParts); | ||||
214 | for (const auto& line : lines) { | ||||
215 | const int pos = line.indexOf(QLatin1Char('=')); | ||||
216 | // has a = and at least 1 char | ||||
217 | if (pos < 0) { | ||||
218 | continue; | ||||
219 | } | ||||
220 | const QString variableName = line.left(pos).trimmed(); | ||||
221 | if (variableName.isEmpty()) { | ||||
222 | continue; | ||||
223 | } | ||||
224 | const QString value = line.mid(pos+1).trimmed(); | ||||
225 | m_varsByIndex << variableName; | ||||
226 | variables.insert(variableName, value); | ||||
227 | } | ||||
228 | | ||||
229 | endResetModel(); | ||||
230 | } | ||||
231 | | ||||
232 | void EnvironmentProfileModel::onProfileAboutToBeRemoved(const QString& profileName) | ||||
233 | { | ||||
234 | if (m_currentProfileName == profileName) { | ||||
235 | setCurrentProfile(QString()); | ||||
236 | } | ||||
237 | } |