Differential D4672 Diff 22422 plugins/projectservicemanager/config/projectservicehookuplistmodel.cpp
Changeset View
Changeset View
Standalone View
Standalone View
plugins/projectservicemanager/config/projectservicehookuplistmodel.cpp
- This file was added.
1 | /* This file is part of KDevelop | ||||
---|---|---|---|---|---|
2 | Copyright 2017 Friedrich W. H. Kossebau <kossebau@kde.org> | ||||
3 | | ||||
4 | This library is free software; you can redistribute it and/or | ||||
5 | modify it under the terms of the GNU Library General Public | ||||
6 | License as published by the Free Software Foundation; either | ||||
7 | version 2 of the License, or (at your option) any later version. | ||||
8 | | ||||
9 | This library is distributed in the hope that it will be useful, | ||||
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||||
12 | Library General Public License for more details. | ||||
13 | | ||||
14 | You should have received a copy of the GNU Library General Public License | ||||
15 | along with this library; see the file COPYING.LIB. If not, write to | ||||
16 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | ||||
17 | Boston, MA 02110-1301, USA. | ||||
18 | */ | ||||
19 | | ||||
20 | #include "projectservicehookuplistmodel.h" | ||||
21 | | ||||
22 | #include <interfaces/iplugin.h> | ||||
23 | #include <interfaces/iprojectservicehookup.h> | ||||
24 | #include <interfaces/iprojectservicehookupconfiguration.h> | ||||
25 | | ||||
26 | #include <KLocalizedString> | ||||
27 | | ||||
28 | #include <QIcon> | ||||
29 | #include <QDebug> | ||||
30 | | ||||
31 | using namespace ProjectServiceHookupManager; | ||||
32 | using namespace KDevelop; | ||||
33 | | ||||
34 | ProjectServiceHookupListModel::ProjectServiceHookupListModel(QObject* parent) | ||||
35 | : QAbstractItemModel(parent) | ||||
36 | { | ||||
37 | } | ||||
38 | | ||||
39 | ProjectServiceHookupListModel::~ProjectServiceHookupListModel() | ||||
40 | { | ||||
41 | qDeleteAll(m_configurations); | ||||
42 | } | ||||
43 | | ||||
44 | QModelIndex ProjectServiceHookupListModel::parent(const QModelIndex& child) const | ||||
45 | { | ||||
46 | Q_UNUSED(child); | ||||
47 | return {}; | ||||
48 | } | ||||
49 | | ||||
50 | QModelIndex ProjectServiceHookupListModel::index(int row, int column, const QModelIndex& parent) const | ||||
51 | { | ||||
52 | if (!hasIndex(row, column, parent)) { | ||||
53 | return {}; | ||||
54 | } | ||||
55 | | ||||
56 | return createIndex(row, column); | ||||
57 | } | ||||
58 | | ||||
59 | | ||||
60 | int ProjectServiceHookupListModel::columnCount(const QModelIndex& parent) const | ||||
61 | { | ||||
62 | Q_UNUSED(parent); | ||||
63 | return 2; | ||||
64 | } | ||||
65 | | ||||
66 | int ProjectServiceHookupListModel::rowCount(const QModelIndex& parent) const | ||||
67 | { | ||||
68 | if (!parent.isValid()) { | ||||
69 | return m_configurations.size(); | ||||
70 | } | ||||
71 | | ||||
72 | return 0; | ||||
73 | } | ||||
74 | | ||||
75 | | ||||
76 | QVariant ProjectServiceHookupListModel::headerData(int section, Qt::Orientation orientation, int role) const | ||||
77 | { | ||||
78 | Q_UNUSED(orientation); | ||||
79 | | ||||
80 | if (section < TypeColumn || section > NameColumn) { | ||||
81 | return {}; | ||||
82 | } | ||||
83 | | ||||
84 | switch (role) { | ||||
85 | case Qt::DisplayRole: | ||||
86 | if (section == TypeColumn) { | ||||
87 | return i18n("Service Type"); | ||||
88 | } | ||||
89 | return i18n("Hook-up Name"); | ||||
90 | break; | ||||
91 | default: | ||||
92 | break; | ||||
93 | } | ||||
94 | | ||||
95 | return {}; | ||||
96 | } | ||||
97 | | ||||
98 | | ||||
99 | QVariant ProjectServiceHookupListModel::data(const QModelIndex& index, int role) const | ||||
100 | { | ||||
101 | if (!index.isValid() || | ||||
102 | (index.column() < TypeColumn || index.column() > NameColumn) || | ||||
103 | (index.row() < 0 || index.row() >= m_configurations.size())) { | ||||
104 | return {}; | ||||
105 | } | ||||
106 | | ||||
107 | switch (role) { | ||||
108 | case Qt::DisplayRole: { | ||||
109 | const IProjectServiceHookupConfiguration* configuration = m_configurations.at(index.row()); | ||||
110 | if (index.column() == 0) { | ||||
111 | return m_hookups[configuration->projectServiceId()]->projectServiceDisplayName(); | ||||
112 | } | ||||
113 | if (index.column() == NameColumn) { | ||||
114 | return configuration->displayName(); | ||||
115 | } | ||||
116 | break; | ||||
117 | } | ||||
118 | case Qt::DecorationRole: { | ||||
119 | const IProjectServiceHookupConfiguration* configuration = m_configurations.at(index.row()); | ||||
120 | if (index.column() == 0) { | ||||
121 | return m_hookups[configuration->projectServiceId()]->projectServiceIcon(); | ||||
122 | } | ||||
123 | break; | ||||
124 | } | ||||
125 | case Qt::EditRole: { | ||||
126 | const IProjectServiceHookupConfiguration* configuration = m_configurations.at(index.row()); | ||||
127 | if (index.column() == NameColumn) { | ||||
128 | return configuration->displayName(); | ||||
129 | } | ||||
130 | break; | ||||
131 | } | ||||
132 | default: | ||||
133 | break; | ||||
134 | } | ||||
135 | | ||||
136 | return {}; | ||||
137 | } | ||||
138 | | ||||
139 | Qt::ItemFlags ProjectServiceHookupListModel::flags(const QModelIndex& index) const | ||||
140 | { | ||||
141 | if (index.isValid()) { | ||||
142 | if (index.column() == NameColumn) { | ||||
143 | return Qt::ItemFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable); | ||||
144 | } | ||||
145 | return Qt::ItemFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable); | ||||
146 | } | ||||
147 | return Qt::NoItemFlags; | ||||
148 | } | ||||
149 | | ||||
150 | bool ProjectServiceHookupListModel::setData(const QModelIndex& index, const QVariant& value, int role) | ||||
151 | { | ||||
152 | if (!index.isValid() || | ||||
153 | index.column() != NameColumn || | ||||
154 | (index.row() < 0 || index.row() >= m_configurations.size()) || | ||||
155 | role != Qt::EditRole) { | ||||
156 | return false; | ||||
157 | } | ||||
158 | | ||||
159 | const auto configuration = m_configurations.at(index.row()); | ||||
160 | const auto newDisplayName = value.toString(); | ||||
161 | if (configuration->displayName() != newDisplayName) { | ||||
162 | configuration->setDisplayName(newDisplayName); | ||||
163 | emit dataChanged(index, index); | ||||
164 | return true; | ||||
165 | } | ||||
166 | | ||||
167 | return false; | ||||
168 | } | ||||
169 | | ||||
170 | int ProjectServiceHookupListModel::addConfiguration(IProjectServiceHookupConfiguration* configuration) | ||||
171 | { | ||||
172 | Q_ASSERT(configuration); | ||||
173 | if (!configuration) { | ||||
174 | return -1; | ||||
175 | } | ||||
176 | | ||||
177 | // ensure id is unique | ||||
178 | QSet<QString> ids; | ||||
179 | for (const auto c : m_configurations) { | ||||
180 | ids.insert(c->id()); | ||||
181 | } | ||||
182 | const QString initialId = configuration->id(); | ||||
183 | if (ids.contains(initialId)) { | ||||
184 | QString newId; | ||||
185 | int i = 1; | ||||
186 | do { | ||||
187 | newId = initialId + QString::number(i++); | ||||
188 | } while (ids.contains(newId)); | ||||
189 | configuration->setId(newId); | ||||
190 | } | ||||
191 | | ||||
192 | beginInsertRows(QModelIndex(), m_configurations.size(), m_configurations.size()); | ||||
193 | // TODO: ensure sorting for consistent display | ||||
194 | m_configurations.append(configuration); | ||||
195 | endInsertRows(); | ||||
196 | emit configurationAdded(configuration); | ||||
197 | | ||||
198 | return m_configurations.size() - 1; | ||||
199 | } | ||||
200 | | ||||
201 | void ProjectServiceHookupListModel::removeConfiguration(const QModelIndex& index) | ||||
202 | { | ||||
203 | const int row = index.row(); | ||||
204 | Q_ASSERT(row >= 0 && row < m_configurations.size()); | ||||
205 | if (row < 0 || row >= m_configurations.size()) { | ||||
206 | return; | ||||
207 | } | ||||
208 | | ||||
209 | beginRemoveRows(QModelIndex(), row, row); | ||||
210 | auto configuration = m_configurations.at(row); | ||||
211 | emit configurationToBeRemoved(configuration); | ||||
212 | m_configurations.removeAt(row); | ||||
213 | delete configuration; | ||||
214 | endRemoveRows(); | ||||
215 | emit configurationRemoved(configuration); | ||||
216 | } | ||||
217 | | ||||
218 | | ||||
219 | void ProjectServiceHookupListModel::setConfigurations(const QList<IProjectServiceHookupConfiguration*>& configurations) | ||||
220 | { | ||||
221 | beginResetModel(); | ||||
222 | qDeleteAll(m_configurations); | ||||
223 | // TODO: ensure sorting for consistent display | ||||
224 | m_configurations = configurations; | ||||
225 | endResetModel(); | ||||
226 | } | ||||
227 | | ||||
228 | void ProjectServiceHookupListModel::setHookups(const QList<IPlugin*>& plugins) | ||||
229 | { | ||||
230 | beginResetModel(); | ||||
231 | m_hookups.clear(); | ||||
232 | for (auto plugin : plugins) { | ||||
233 | IProjectServiceHookup* serviceHookup = plugin->extension<IProjectServiceHookup>(); | ||||
234 | m_hookups.insert(serviceHookup->projectServiceId(), serviceHookup); | ||||
235 | } | ||||
236 | endResetModel(); | ||||
237 | } | ||||
238 | | ||||
239 | QList<IProjectServiceHookupConfiguration*> ProjectServiceHookupListModel::configurations() const | ||||
240 | { | ||||
241 | return m_configurations; | ||||
242 | } | ||||
243 | IProjectServiceHookupConfiguration* ProjectServiceHookupListModel::configuration(const QModelIndex& index) const | ||||
244 | { | ||||
245 | const int row = index.row(); | ||||
246 | Q_ASSERT(row >= 0 && row < m_configurations.size()); | ||||
247 | return m_configurations.at(row); | ||||
248 | } | ||||
249 | | ||||
250 | QHash<QString, IProjectServiceHookup*> ProjectServiceHookupListModel::hookups() const | ||||
251 | { | ||||
252 | return m_hookups; | ||||
253 | } | ||||
254 | | ||||
255 | IProjectServiceHookup* ProjectServiceHookupListModel::hookup(const QString& projectServiceId) const | ||||
256 | { | ||||
257 | Q_ASSERT(m_hookups.contains(projectServiceId)); | ||||
258 | return m_hookups.value(projectServiceId); | ||||
259 | } |