Changeset View
Changeset View
Standalone View
Standalone View
plugins/projectservicemanager/config/projectconfigpage.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 "projectconfigpage.h" | ||||
21 | #include "ui_projectconfigpage.h" | ||||
22 | | ||||
23 | #include "projectconfig.h" | ||||
24 | | ||||
25 | #include <interfaces/iplugincontroller.h> | ||||
26 | #include <interfaces/iproject.h> | ||||
27 | #include <interfaces/iplugin.h> | ||||
28 | #include <interfaces/icore.h> | ||||
29 | #include <interfaces/iprojectservicehookup.h> | ||||
30 | #include <interfaces/iprojectservicehookupconfiguration.h> | ||||
31 | #include <interfaces/projectservicehookupconfigpage.h> | ||||
32 | | ||||
33 | #include <QAction> | ||||
34 | #include <QMenu> | ||||
35 | | ||||
36 | using namespace ProjectServiceHookupManager; | ||||
37 | using namespace KDevelop; | ||||
38 | | ||||
39 | ProjectConfigPage::ProjectConfigPage(KDevelop::IPlugin* plugin, KDevelop::IProject* project, QWidget* parent) | ||||
40 | : ConfigPage(plugin, new ProjectConfig, parent) | ||||
41 | , m_ui(new Ui::ProjectConfigPage) | ||||
42 | , m_configurationPage(nullptr) | ||||
43 | , m_project(project) | ||||
44 | , m_editedConfiguration(nullptr) | ||||
45 | { | ||||
46 | configSkeleton()->setSharedConfig(m_project->projectConfiguration()); | ||||
47 | configSkeleton()->load(); | ||||
48 | | ||||
49 | m_ui->setupUi(this); | ||||
50 | | ||||
51 | m_ui->removeServiceHookupButton->setEnabled(false); | ||||
52 | connect(m_ui->removeServiceHookupButton, &QPushButton::clicked, | ||||
53 | this, &ProjectConfigPage::deleteHookupConfiguration); | ||||
54 | | ||||
55 | QList<IProjectServiceHookupConfiguration*> configurations; | ||||
56 | QMenu* serviceHookupsMenu = new QMenu(this); | ||||
57 | | ||||
58 | const QList<IPlugin*> plugins = ICore::self()->pluginController()->allPluginsForExtension("org.kdevelop.IProjectServiceHookup"); | ||||
59 | | ||||
60 | for (IPlugin* plugin : plugins) { | ||||
61 | IProjectServiceHookup* serviceHookup = plugin->extension<IProjectServiceHookup>(); | ||||
62 | Q_ASSERT(serviceHookup); | ||||
63 | | ||||
64 | configurations += serviceHookup->loadProjectServiceHookupConfigurations(m_project); | ||||
65 | | ||||
66 | QAction* action = new QAction(serviceHookup->projectServiceIcon(), | ||||
67 | serviceHookup->projectServiceDisplayName(), serviceHookupsMenu); | ||||
68 | action->setData(qVariantFromValue<IPlugin*>(plugin)); | ||||
69 | connect(action, &QAction::triggered, this, &ProjectConfigPage::addHookupConfiguration); | ||||
70 | serviceHookupsMenu->addAction(action); | ||||
71 | } | ||||
72 | | ||||
73 | m_serviceHookupListModel.setHookups(plugins); | ||||
74 | m_serviceHookupListModel.setConfigurations(configurations); | ||||
75 | connect(&m_serviceHookupListModel, &ProjectServiceHookupListModel::configurationAdded, | ||||
76 | this, &ProjectConfigPage::changed); | ||||
77 | connect(&m_serviceHookupListModel, &ProjectServiceHookupListModel::dataChanged, | ||||
78 | this, &ProjectConfigPage::changed); | ||||
79 | connect(&m_serviceHookupListModel, &ProjectServiceHookupListModel::modelAboutToBeReset, | ||||
80 | this, &ProjectConfigPage::onConfigurationsAboutToBeReset); | ||||
81 | connect(&m_serviceHookupListModel, &ProjectServiceHookupListModel::configurationToBeRemoved, | ||||
82 | this, &ProjectConfigPage::onConfigurationToBeRemoved); | ||||
83 | | ||||
84 | m_ui->addServiceHookupButton->setMenu(serviceHookupsMenu); | ||||
85 | m_ui->addServiceHookupButton->setEnabled(!serviceHookupsMenu->isEmpty()); | ||||
86 | m_ui->configurationsView->setModel(&m_serviceHookupListModel); | ||||
87 | connect(m_ui->configurationsView->selectionModel(), &QItemSelectionModel::selectionChanged, | ||||
88 | this, &ProjectConfigPage::onSelectionChanged); | ||||
89 | } | ||||
90 | | ||||
91 | ProjectConfigPage::~ProjectConfigPage() = default; | ||||
92 | | ||||
93 | QIcon ProjectConfigPage::icon() const | ||||
94 | { | ||||
95 | return QIcon::fromTheme(QStringLiteral("services")); | ||||
96 | } | ||||
97 | | ||||
98 | QString ProjectConfigPage::name() const | ||||
99 | { | ||||
100 | return i18n("Service Hook-ups"); | ||||
101 | } | ||||
102 | | ||||
103 | void ProjectConfigPage::apply() | ||||
104 | { | ||||
105 | ConfigPage::apply(); | ||||
106 | | ||||
107 | // | ||||
108 | QHash<QString, QList<IProjectServiceHookupConfiguration*> > configurationsByService; | ||||
109 | const auto configurations = m_serviceHookupListModel.configurations(); | ||||
110 | | ||||
111 | for (auto configuration : configurations) { | ||||
112 | configurationsByService[configuration->projectServiceId()].append(configuration); | ||||
113 | } | ||||
114 | | ||||
115 | // store for any servicehookup, including those without configuration, | ||||
116 | // so they clean up any old stored configs | ||||
117 | const auto serviceHookups = m_serviceHookupListModel.hookups(); | ||||
118 | | ||||
119 | for (auto serviceHookup : serviceHookups) { | ||||
120 | const auto configurations = configurationsByService.value(serviceHookup->projectServiceId()); | ||||
121 | serviceHookup->saveProjectServiceHookupConfigurations(configurations, m_project); | ||||
122 | } | ||||
123 | } | ||||
124 | | ||||
125 | void ProjectConfigPage::defaults() | ||||
126 | { | ||||
127 | ConfigPage::defaults(); | ||||
128 | | ||||
129 | // nothing really maps here to the meaning, removing all hookups might not really be expected | ||||
130 | } | ||||
131 | | ||||
132 | void ProjectConfigPage::reset() | ||||
133 | { | ||||
134 | ConfigPage::reset(); | ||||
135 | | ||||
136 | QList<IProjectServiceHookupConfiguration*> configurations; | ||||
137 | | ||||
138 | const auto serviceHookups = m_serviceHookupListModel.hookups(); | ||||
139 | | ||||
140 | for (auto serviceHookup : serviceHookups) { | ||||
141 | configurations += serviceHookup->loadProjectServiceHookupConfigurations(m_project); | ||||
142 | } | ||||
143 | | ||||
144 | m_serviceHookupListModel.setConfigurations(configurations); | ||||
145 | } | ||||
146 | | ||||
147 | void ProjectConfigPage::addHookupConfiguration() | ||||
148 | { | ||||
149 | QAction* action = qobject_cast<QAction*>(sender()); | ||||
150 | Q_ASSERT(action); | ||||
151 | | ||||
152 | IPlugin* plugin = action->data().value<IPlugin*>(); | ||||
153 | Q_ASSERT(plugin); | ||||
154 | IProjectServiceHookup* serviceHookup = plugin->extension<IProjectServiceHookup>(); | ||||
155 | Q_ASSERT(serviceHookup); | ||||
156 | | ||||
157 | IProjectServiceHookupConfiguration* hookupConfiguration = serviceHookup->createProjectServiceHookupConfiguration(m_project); | ||||
158 | | ||||
159 | const int row = m_serviceHookupListModel.addConfiguration(hookupConfiguration); | ||||
160 | | ||||
161 | // select in list and start name editing | ||||
162 | const QModelIndex index = m_serviceHookupListModel.index(row, ProjectServiceHookupListModel::NameColumn, QModelIndex()); | ||||
163 | m_ui->configurationsView->selectionModel()->select(index, QItemSelectionModel::Rows|QItemSelectionModel::ClearAndSelect); | ||||
164 | m_ui->configurationsView->edit(index); | ||||
165 | } | ||||
166 | | ||||
167 | void ProjectConfigPage::deleteHookupConfiguration() | ||||
168 | { | ||||
169 | const auto selectedRows = m_ui->configurationsView->selectionModel()->selectedRows(); | ||||
170 | | ||||
171 | if (selectedRows.isEmpty()) { | ||||
172 | // should not happen | ||||
173 | return; | ||||
174 | } | ||||
175 | | ||||
176 | m_serviceHookupListModel.removeConfiguration(selectedRows.first()); | ||||
177 | m_ui->configurationsView->setFocus(Qt::OtherFocusReason); | ||||
178 | } | ||||
179 | | ||||
180 | void ProjectConfigPage::onConfigurationsAboutToBeReset() | ||||
181 | { | ||||
182 | if (m_configurationPage) { | ||||
183 | m_ui->configPageContainer->removeWidget(m_configurationPage); | ||||
184 | delete m_configurationPage; | ||||
185 | m_configurationPage = nullptr; | ||||
186 | } | ||||
187 | m_editedConfiguration = nullptr; | ||||
188 | } | ||||
189 | | ||||
190 | void ProjectConfigPage::onConfigurationToBeRemoved(IProjectServiceHookupConfiguration* configuration) | ||||
191 | { | ||||
192 | if (m_editedConfiguration != configuration) { | ||||
193 | return; | ||||
194 | } | ||||
195 | | ||||
196 | if (m_configurationPage) { | ||||
197 | m_ui->configPageContainer->removeWidget(m_configurationPage); | ||||
198 | delete m_configurationPage; | ||||
199 | m_configurationPage = nullptr; | ||||
200 | } | ||||
201 | m_editedConfiguration = nullptr; | ||||
202 | } | ||||
203 | | ||||
204 | void ProjectConfigPage::onSelectionChanged(const QItemSelection& selected, const QItemSelection& deselected) | ||||
205 | { | ||||
206 | Q_UNUSED(deselected); | ||||
207 | | ||||
208 | const auto selectedIndexes = selected.indexes(); | ||||
209 | const bool hasSelected = !selectedIndexes.isEmpty(); | ||||
210 | | ||||
211 | m_ui->removeServiceHookupButton->setEnabled(hasSelected); | ||||
212 | | ||||
213 | if (m_configurationPage) { | ||||
214 | m_ui->configPageContainer->removeWidget(m_configurationPage); | ||||
215 | delete m_configurationPage; | ||||
216 | m_configurationPage = nullptr; | ||||
217 | } | ||||
218 | | ||||
219 | if (hasSelected) { | ||||
220 | m_editedConfiguration = m_serviceHookupListModel.configuration(selectedIndexes.first()); | ||||
221 | const auto serviceHookup = m_serviceHookupListModel.hookup(m_editedConfiguration->projectServiceId()); | ||||
222 | m_configurationPage = serviceHookup->createProjectServiceHookupConfigPage(m_ui->configPageContainer); | ||||
223 | if (m_configurationPage) { | ||||
224 | m_configurationPage->loadFromConfiguration(m_editedConfiguration); | ||||
225 | connect(m_configurationPage, &ProjectServiceHookupConfigPage::changed, | ||||
226 | this, &ProjectConfigPage::onConfigPageContentChanged); | ||||
227 | const int widgetIndex = m_ui->configPageContainer->addWidget(m_configurationPage); | ||||
228 | m_ui->configPageContainer->setCurrentIndex(widgetIndex); | ||||
229 | } | ||||
230 | } | ||||
231 | } | ||||
232 | | ||||
233 | void ProjectConfigPage::onConfigPageContentChanged() | ||||
234 | { | ||||
235 | if (!m_configurationPage || !m_editedConfiguration) { | ||||
236 | return; | ||||
237 | } | ||||
238 | // TODO: improve by squashing updates with some delay timer | ||||
239 | m_configurationPage->saveToConfiguration(m_editedConfiguration); | ||||
240 | emit changed(); | ||||
241 | } |