-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathPluginSettingsPage.cpp
319 lines (260 loc) · 11.3 KB
/
PluginSettingsPage.cpp
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
//-----------------------------------------------------------------------------
// File: PluginSettingsPage.cpp
//-----------------------------------------------------------------------------
// Project: Kactus 2
// Author: Joni-Matti Maatta
// Date: 19.02.2013
//
// Description:
// Settings page for adjusting plugin settings.
//-----------------------------------------------------------------------------
#include "PluginSettingsPage.h"
#include <KactusAPI/include/IPlugin.h>
#include <KactusAPI/include/IGeneratorPlugin.h>
#include <KactusAPI/include/ISourceAnalyzerPlugin.h>
#include <KactusAPI/include/ImportPlugin.h>
#include <settings/PluginInfoWidget.h>
#include <settings/NewPluginsDialog.h>
#include <IPXACTmodels/utilities/XmlUtils.h>
#include <QApplication>
#include <QVBoxLayout>
#include <QLabel>
#include <QGroupBox>
namespace
{
//! Enumeration for model roles.
enum Roles
{
PLUGIN_POINTER_ROLE = Qt::UserRole, //<! Pointer to the plugin.
PLUGIN_STACK_INDEX_ROLE //<! Index of the plugin in stacks.
};
}
//-----------------------------------------------------------------------------
// Function: PluginSettingsPage::PluginSettingsPage()
//-----------------------------------------------------------------------------
PluginSettingsPage::PluginSettingsPage(QSettings& settings):
SettingsPage(settings),
settings_(settings),
pluginDirSelector_(QApplication::applicationDirPath(),
settings.value("Platform/PluginsPath", QStringList("Plugins")).toStringList(), this),
pluginsTree_(this),
infoStack_(this),
directoriesChanged_(false)
{
// Initialize the widgets and create the layout.
pluginDirSelector_.setPersistentDirectory("Plugins");
pluginDirSelector_.setPersistentDirectory("plugins");
pluginsTree_.setObjectName("PluginsTree");
QString stylesheet("QTreeView#PluginsTree::indicator:unchecked {image: url(:icons/common/graphics/traffic-light_gray.png);}"
"QTreeView#PluginsTree::indicator:checked {image: url(:icons/common/graphics/traffic-light_green.png);}");
pluginsTree_.setStyleSheet(stylesheet);
pluginsTree_.setHeaderHidden(true);
pluginsTree_.setColumnCount(1);
pluginsTree_.setSelectionBehavior(QAbstractItemView::SelectItems);
setupLayout();
refreshPluginsTree();
connect(&pluginDirSelector_, SIGNAL(contentChanged()),
this, SLOT(onDirectoriesChanged()), Qt::UniqueConnection);
connect(&pluginsTree_, SIGNAL(currentItemChanged(QTreeWidgetItem*, QTreeWidgetItem*)),
this, SLOT(onTreeItemChanged(QTreeWidgetItem*, QTreeWidgetItem*)));
}
//-----------------------------------------------------------------------------
// Function: PluginSettingsPage::validate()
//-----------------------------------------------------------------------------
bool PluginSettingsPage::validate()
{
for (int i = 0; i < pluginsTree_.topLevelItemCount(); ++i)
{
QTreeWidgetItem* categoryRoot = pluginsTree_.topLevelItem(i);
for (int j = 0; j < categoryRoot->childCount(); ++j)
{
QTreeWidgetItem* item = categoryRoot->child(j);
// Retrieve the plugin pointer from the data.
IPlugin* plugin = static_cast<IPlugin*>(item->data(0, PLUGIN_POINTER_ROLE).value<void*>());
Q_ASSERT(plugin != 0);
if (plugin->getSettingsModel() &&!plugin->getSettingsModel()->validateSettings())
{
return false;
}
}
}
return true;
}
//-----------------------------------------------------------------------------
// Function: PluginSettingsPage::apply()
//-----------------------------------------------------------------------------
void PluginSettingsPage::apply()
{
if (directoriesChanged_ || settings_.value("Platform/PluginsPath").toString().isEmpty())
{
// Save the new plugins directories and update plugin manager.
settings_.setValue("Platform/PluginsPath", pluginDirSelector_.getDirectories());
PluginManager::getInstance().setPluginPaths(pluginDirSelector_.getDirectories());
}
settings_.beginGroup("PluginSettings");
// Save settings and active state for each individual plugin.
for (int i = 0; i < pluginsTree_.topLevelItemCount(); ++i)
{
QTreeWidgetItem* categoryRoot = pluginsTree_.topLevelItem(i);
for (int j = 0; j < categoryRoot->childCount(); ++j)
{
QTreeWidgetItem* item = categoryRoot->child(j);
// Retrieve the plugin pointer from the data.
IPlugin* plugin = static_cast<IPlugin*>(item->data(0, PLUGIN_POINTER_ROLE).value<void*>());
Q_ASSERT(plugin != 0);
// Save the active state.
settings_.beginGroup(XmlUtils::removeWhiteSpace(plugin->getName()));
settings_.setValue("Active", item->checkState(0) == Qt::Checked);
if (plugin->getSettingsModel())
{
plugin->getSettingsModel()->saveSettings(settings_);
}
settings_.endGroup();
}
}
settings_.endGroup();
}
//-----------------------------------------------------------------------------
// Function: PluginSettingsPage::onTreeItemChanged()
//-----------------------------------------------------------------------------
void PluginSettingsPage::onTreeItemChanged(QTreeWidgetItem* current, QTreeWidgetItem* /*prev*/)
{
int index = 0;
if (current != nullptr)
{
index = current->data(0, PLUGIN_STACK_INDEX_ROLE).toInt();
}
Q_ASSERT(index < infoStack_.count());
infoStack_.setCurrentIndex(index);
}
//-----------------------------------------------------------------------------
// Function: PluginSettingsPage::createCategoryItem()
//-----------------------------------------------------------------------------
QTreeWidgetItem* PluginSettingsPage::createCategoryItem(QString const& text, QIcon const& icon)
{
QTreeWidgetItem* item = new QTreeWidgetItem();
item->setText(0, text);
item->setIcon(0, icon);
item->setData(0, PLUGIN_STACK_INDEX_ROLE, 0);
// Use bold font for category items.
QFont rootFont = item->font(0);
rootFont.setBold(true);
item->setFont(0, rootFont);
pluginsTree_.addTopLevelItem(item);
return item;
}
//-----------------------------------------------------------------------------
// Function: PluginSettingsPage::createItem()
//-----------------------------------------------------------------------------
QTreeWidgetItem* PluginSettingsPage::createPluginItem(IPlugin* plugin)
{
QTreeWidgetItem* item = new QTreeWidgetItem();
item->setText(0, plugin->getName() + " (" + plugin->getVersion() + ")");
if (settings_.value("Active", true).toBool())
{
item->setCheckState(0, Qt::Checked);
}
else
{
item->setCheckState(0, Qt::Unchecked);
}
item->setData(0, PLUGIN_POINTER_ROLE, QVariant::fromValue(static_cast<void*>(plugin)));
item->setData(0, PLUGIN_STACK_INDEX_ROLE, infoStack_.count());
infoStack_.addWidget(new PluginInfoWidget(plugin));
return item;
}
//-----------------------------------------------------------------------------
// Function: PluginSettingsPage::onDirectoriesChanged()
//-----------------------------------------------------------------------------
void PluginSettingsPage::onDirectoriesChanged()
{
directoriesChanged_ = true;
refreshPluginsTree(true);
}
//-----------------------------------------------------------------------------
// Function: PluginSettingsPage::refreshPluginsTree()
//-----------------------------------------------------------------------------
void PluginSettingsPage::refreshPluginsTree(bool displayChanges)
{
QApplication::setOverrideCursor(Qt::WaitCursor);
QList<IPlugin*> newPlugins;
QList<IPlugin*> oldPlugins = PluginManager::getInstance().getAllPlugins();
resetStacks();
pluginsTree_.clear();
// Create the category items.
QTreeWidgetItem* generatorsItem = createCategoryItem(tr("Generators"),
QIcon(":icons/common/graphics/configuration.png"));
QTreeWidgetItem* analyzersItem = createCategoryItem(tr("Source Analyzers"),
QIcon(":icons/common/graphics/plugin-source_analyzer.png"));
QTreeWidgetItem* importItems = createCategoryItem(tr("Import Plugins"),
QIcon(":icons/common/graphics/import.png"));
// Enumerate all plugins and add them to the correct root.
QList<IPlugin*> plugins = PluginManager::findPluginsInPaths(pluginDirSelector_.getDirectories());
settings_.beginGroup("PluginSettings");
for (IPlugin* plugin : plugins)
{
settings_.beginGroup(XmlUtils::removeWhiteSpace(plugin->getName()));
// Create the tree item.
QTreeWidgetItem* pluginItem = createPluginItem(plugin);
// Add item under the right parent.
if (dynamic_cast<IGeneratorPlugin*>(plugin) != 0)
{
pluginItem->setIcon(0, dynamic_cast<IGeneratorPlugin*>(plugin)->getIcon());
generatorsItem->addChild(pluginItem);
}
else if (dynamic_cast<ISourceAnalyzerPlugin*>(plugin) != 0)
{
analyzersItem->addChild(pluginItem);
}
else if(dynamic_cast<ImportPlugin*>(plugin) != 0)
{
importItems->addChild(pluginItem);
}
// Check if the plugin has not been visible before.
if (displayChanges && !oldPlugins.contains(plugin))
{
newPlugins.append(plugin);
}
settings_.endGroup();
}
settings_.endGroup();
pluginsTree_.expandAll();
QApplication::restoreOverrideCursor();
if (displayChanges && newPlugins.size() > 0)
{
NewPluginsDialog pluginDialog(this);
pluginDialog.addPlugins(newPlugins);
pluginDialog.exec();
}
}
//-----------------------------------------------------------------------------
// Function: PluginSettingsPage::resetStacks()
//-----------------------------------------------------------------------------
void PluginSettingsPage::resetStacks()
{
while (infoStack_.count() > 0)
{
infoStack_.removeWidget(infoStack_.widget(0));
}
infoStack_.addWidget(new PluginInfoWidget());
}
//-----------------------------------------------------------------------------
// Function: PluginSettingsPage::setupLayout()
//-----------------------------------------------------------------------------
void PluginSettingsPage::setupLayout()
{
pluginDirSelector_.setMinimumHeight(120);
pluginDirSelector_.setMinimumWidth(240);
auto infoLayout = new QVBoxLayout();
infoLayout->addWidget(&infoStack_);
auto infoGroup = new QGroupBox(tr("Plugin general information"), this);
infoGroup->setLayout(infoLayout);
infoGroup->setFlat(true);
auto layout = new QGridLayout(this);
layout->addWidget(new QLabel(tr("Plugin directories:"), this), 0, 0, 1, 1);
layout->addWidget(new QLabel(tr("Available plugins:"), this), 0, 1, 1, 1);
layout->addWidget(&pluginDirSelector_, 1, 0, 1, 1);
layout->addWidget(&pluginsTree_, 1, 1, 1, 1);
layout->addWidget(infoGroup, 2, 0, 1, 2);
layout->setRowStretch(1, 2);
}