Commit bab7794a authored by hjk's avatar hjk
Browse files

Use TreeModel in plugin dialog



Change-Id: Ifc18e9779b9841a2f08c48dcd8dad36627d6e537
Reviewed-by: default avatarEike Ziller <eike.ziller@theqtcompany.com>
parent 273c3fa0
......@@ -1301,10 +1301,6 @@ void PluginManagerPrivate::resolveDependencies()
spec->d->resolveDependencies(pluginSpecs);
}
// Reset disabledIndirectly flag
foreach (PluginSpec *spec, loadQueue())
spec->d->disabledIndirectly = false;
foreach (PluginSpec *spec, loadQueue()) {
spec->d->disableIndirectlyIfDependencyDisabled();
}
......@@ -1329,11 +1325,6 @@ PluginSpec *PluginManagerPrivate::pluginForOption(const QString &option, bool *r
return 0;
}
void PluginManagerPrivate::disablePluginIndirectly(PluginSpec *spec)
{
spec->d->disabledIndirectly = true;
}
PluginSpec *PluginManagerPrivate::pluginByName(const QString &name) const
{
foreach (PluginSpec *spec, pluginSpecs)
......
......@@ -80,7 +80,6 @@ public:
void setGlobalSettings(QSettings *settings);
void readSettings();
void writeSettings();
void disablePluginIndirectly(PluginSpec *spec);
class TestSpec {
public:
......
......@@ -542,11 +542,6 @@ void PluginSpec::setDisabledByDefault(bool value)
d->disabledByDefault = value;
}
void PluginSpec::setDisabledIndirectly(bool value)
{
d->disabledIndirectly = value;
}
void PluginSpec::setForceEnabled(bool value)
{
d->forceEnabled = value;
......
......@@ -113,7 +113,6 @@ public:
void setEnabled(bool value);
void setDisabledByDefault(bool value);
void setDisabledIndirectly(bool value);
void setForceEnabled(bool value);
void setForceDisabled(bool value);
......
......@@ -32,14 +32,17 @@
#include "pluginmanager.h"
#include "pluginspec.h"
#include "plugincollection.h"
#include <utils/algorithm.h>
#include <utils/itemviews.h>
#include <utils/treemodel.h>
#include <QDebug>
#include <QDir>
#include <QGridLayout>
#include <QHeaderView>
#include <QPalette>
#include <QTreeWidgetItem>
#include <QSet>
#include <QItemSelectionModel>
/*!
\class ExtensionSystem::PluginView
......@@ -66,317 +69,321 @@
for example by a double-click.
*/
using namespace ExtensionSystem;
Q_DECLARE_METATYPE(ExtensionSystem::PluginSpec*)
Q_DECLARE_METATYPE(ExtensionSystem::PluginCollection*)
/*!
Constructs a PluginView that gets the list of plugins from the
given plugin \a manager with a given \a parent widget.
*/
PluginView::PluginView(QWidget *parent)
: QWidget(parent),
m_allowCheckStateUpdate(true),
C_LOAD(1)
{
m_categoryWidget = new Utils::TreeWidget(this);
m_categoryWidget->setAlternatingRowColors(true);
m_categoryWidget->setIndentation(20);
m_categoryWidget->setUniformRowHeights(true);
m_categoryWidget->setSortingEnabled(true);
m_categoryWidget->setColumnCount(4);
m_categoryWidget->setColumnWidth(C_LOAD, 40);
m_categoryWidget->header()->setDefaultSectionSize(120);
m_categoryWidget->header()->setMinimumSectionSize(35);
m_categoryWidget->setActivationMode(Utils::DoubleClickActivation);
QTreeWidgetItem *headerItem = m_categoryWidget->headerItem();
headerItem->setText(0, tr("Name"));
headerItem->setText(1, tr("Load"));
headerItem->setText(2, tr("Version"));
headerItem->setText(3, tr("Vendor"));
QGridLayout *gridLayout = new QGridLayout(this);
gridLayout->setContentsMargins(2, 2, 2, 2);
gridLayout->addWidget(m_categoryWidget, 1, 0, 1, 1);
QHeaderView *header = m_categoryWidget->header();
header->setSectionResizeMode(0, QHeaderView::ResizeToContents);
header->setSectionResizeMode(2, QHeaderView::ResizeToContents);
using namespace Utils;
m_okIcon = QIcon(QLatin1String(":/extensionsystem/images/ok.png"));
m_errorIcon = QIcon(QLatin1String(":/extensionsystem/images/error.png"));
m_notLoadedIcon = QIcon(QLatin1String(":/extensionsystem/images/notloaded.png"));
namespace ExtensionSystem {
connect(PluginManager::instance(), SIGNAL(pluginsChanged()), this, SLOT(updateList()));
connect(m_categoryWidget, SIGNAL(currentItemChanged(QTreeWidgetItem*,QTreeWidgetItem*)),
this, SLOT(selectPlugin(QTreeWidgetItem*)));
connect(m_categoryWidget, SIGNAL(itemActivated(QTreeWidgetItem*,int)),
this, SLOT(activatePlugin(QTreeWidgetItem*)));
enum Columns { NameColumn, LoadedColumn, VersionColumn, VendorColumn, };
updateList();
}
enum IconIndex { OkIcon, ErrorIcon, NotLoadedIcon };
/*!
\internal
*/
PluginView::~PluginView()
static const QIcon &icon(int num)
{
static QIcon icons[] = {
QIcon(QLatin1String(":/extensionsystem/images/ok.png")),
QIcon(QLatin1String(":/extensionsystem/images/error.png")),
QIcon(QLatin1String(":/extensionsystem/images/notloaded.png")),
};
return icons[num];
}
/*!
Returns the current selection in the list of plugins.
*/
PluginSpec *PluginView::currentPlugin() const
class PluginItem : public TreeItem
{
if (!m_categoryWidget->currentItem())
return 0;
if (!m_categoryWidget->currentItem()->data(0, Qt::UserRole).isNull())
return m_categoryWidget->currentItem()->data(0, Qt::UserRole).value<PluginSpec *>();
return 0;
}
public:
PluginItem(PluginSpec *spec, PluginView *view)
: m_spec(spec), m_view(view)
{}
int columnCount() const { return 4; }
QVariant data(int column, int role) const
{
switch (column) {
case NameColumn:
if (role == Qt::DisplayRole)
return m_spec->name();
if (role == Qt::ToolTipRole)
return QDir::toNativeSeparators(m_spec->filePath());
if (role == Qt::DecorationRole) {
bool ok = !m_spec->hasError();
QIcon i = icon(ok ? OkIcon : ErrorIcon);
if (ok && m_spec->state() != PluginSpec::Running)
i = icon(NotLoadedIcon);
return i;
}
break;
void PluginView::updateList()
{
connect(m_categoryWidget, SIGNAL(itemChanged(QTreeWidgetItem*,int)),
this, SLOT(updatePluginSettings(QTreeWidgetItem*,int)));
case LoadedColumn:
if (!m_spec->isAvailableForHostPlatform()) {
if (role == Qt::CheckStateRole)
return Qt::Unchecked;
if (role == Qt::ToolTipRole)
return PluginView::tr("Plugin is not available on this platform.");
} else if (m_spec->isRequired()) {
if (role == Qt::CheckStateRole)
return Qt::Checked;
if (role == Qt::ToolTipRole)
return PluginView::tr("Plugin is required.");
} else {
if (role == Qt::CheckStateRole)
return m_spec->isEnabledInSettings() ? Qt::Checked : Qt::Unchecked;
if (role == Qt::ToolTipRole)
return PluginView::tr("Load on startup");
}
break;
PluginCollection *defaultCollection = 0;
foreach (PluginCollection *collection, PluginManager::pluginCollections()) {
if (collection->name().isEmpty() || collection->plugins().isEmpty()) {
defaultCollection = collection;
continue;
case VersionColumn:
if (role == Qt::DisplayRole)
return QString::fromLatin1("%1 (%2)").arg(m_spec->version(), m_spec->compatVersion());
break;
case VendorColumn:
if (role == Qt::DisplayRole)
return m_spec->vendor();
break;
}
// State, name, load, version, vendor.
QTreeWidgetItem *collectionItem = new QTreeWidgetItem(QStringList()
<< collection->name()
<< QString() // state
<< QString() // load
<< QString() // version
<< QString()); // vendor
m_items.append(collectionItem);
Qt::CheckState groupState = Qt::Unchecked;
int state = parsePluginSpecs(collectionItem, groupState, collection->plugins());
collectionItem->setIcon(0, iconForState(state));
collectionItem->setData(C_LOAD, Qt::CheckStateRole, QVariant(groupState));
collectionItem->setToolTip(C_LOAD, tr("Load on Startup"));
collectionItem->setData(0, Qt::UserRole, qVariantFromValue(collection));
return QVariant();
}
QList<PluginSpec *> plugins = defaultCollection ? defaultCollection->plugins() : QList<PluginSpec *>();
if (!plugins.isEmpty()) {
// add all non-categorized plugins into utilities. could also be added as root items
// but that makes the tree ugly.
QTreeWidgetItem *defaultCollectionItem = new QTreeWidgetItem(QStringList()
<< QString(tr("Utilities"))
<< QString()
<< QString()
<< QString()
<< QString());
m_items.append(defaultCollectionItem);
Qt::CheckState groupState = Qt::Unchecked;
int state = parsePluginSpecs(defaultCollectionItem, groupState, plugins);
defaultCollectionItem->setIcon(0, iconForState(state));
defaultCollectionItem->setData(C_LOAD, Qt::CheckStateRole, QVariant(groupState));
defaultCollectionItem->setToolTip(C_LOAD, tr("Load on Startup"));
defaultCollectionItem->setData(0, Qt::UserRole, qVariantFromValue(defaultCollection));
bool setData(int column, const QVariant &data, int role)
{
if (column == LoadedColumn && role == Qt::CheckStateRole) {
m_spec->setEnabled(data.toBool());
update();
parent()->update();
emit m_view->pluginSettingsChanged(m_spec);
return true;
}
return false;
}
bool isEnabled() const
{
if (m_spec->isRequired() || !m_spec->isAvailableForHostPlatform())
return false;
foreach (PluginSpec *spec, m_view->m_pluginDependencies.value(m_spec))
if (!spec->isEnabledInSettings())
return false;
return true;
}
updatePluginDependencies();
Qt::ItemFlags flags(int column) const
{
Qt::ItemFlags ret = Qt::ItemIsSelectable;
if (isEnabled())
ret |= Qt::ItemIsEnabled;
m_categoryWidget->clear();
if (!m_items.isEmpty()) {
m_categoryWidget->addTopLevelItems(m_items);
m_categoryWidget->expandAll();
if (column == LoadedColumn) {
if (m_spec->isAvailableForHostPlatform() && !m_spec->isRequired())
ret |= Qt::ItemIsEditable | Qt ::ItemIsUserCheckable;
}
m_categoryWidget->sortItems(0, Qt::AscendingOrder);
if (m_categoryWidget->topLevelItemCount())
m_categoryWidget->setCurrentItem(m_categoryWidget->topLevelItem(0));
}
return ret;
}
public:
PluginSpec *m_spec; // Not owned.
PluginView *m_view; // Not owned.
};
int PluginView::parsePluginSpecs(QTreeWidgetItem *parentItem, Qt::CheckState &groupState, QList<PluginSpec*> plugins)
class CollectionItem : public TreeItem
{
int ret = 0;
int checkedCount = 0;
public:
CollectionItem(const QString &name, QList<PluginSpec *> plugins, PluginView *view)
: m_name(name), m_plugins(plugins), m_view(view)
{
foreach (PluginSpec *spec, plugins)
appendChild(new PluginItem(spec, view));
}
for (int i = 0; i < plugins.length(); ++i) {
PluginSpec *spec = plugins[i];
int columnCount() const { return 4; }
QVariant data(int column, int role) const
{
if (column == NameColumn) {
if (role == Qt::DisplayRole)
return m_name;
if (role == Qt::DecorationRole) {
foreach (PluginSpec *spec, m_plugins) {
if (spec->hasError())
ret |= ParsedWithErrors;
QTreeWidgetItem *pluginItem = new QTreeWidgetItem(QStringList()
<< spec->name()
<< QString() // load on startup
<< QString::fromLatin1("%1 (%2)").arg(spec->version(), spec->compatVersion())
<< spec->vendor());
pluginItem->setToolTip(0, QDir::toNativeSeparators(spec->filePath()));
bool ok = !spec->hasError();
QIcon icon = ok ? m_okIcon : m_errorIcon;
if (ok && (spec->state() != PluginSpec::Running))
icon = m_notLoadedIcon;
pluginItem->setIcon(0, icon);
pluginItem->setData(0, Qt::UserRole, qVariantFromValue(spec));
Qt::CheckState state = Qt::Unchecked;
if (spec->isEnabledInSettings()) {
state = Qt::Checked;
++checkedCount;
return icon(ErrorIcon);
if (!spec->isEnabledInSettings())
return icon(NotLoadedIcon);
}
return icon(OkIcon);
}
}
if (!spec->isAvailableForHostPlatform()) {
pluginItem->setData(C_LOAD, Qt::CheckStateRole, Qt::Unchecked);
pluginItem->setFlags(Qt::ItemIsSelectable);
pluginItem->setToolTip(C_LOAD, tr("Plugin is not available on this platform."));
} else if (spec->isRequired()){
pluginItem->setData(C_LOAD, Qt::CheckStateRole, Qt::Checked);
pluginItem->setFlags(Qt::ItemIsSelectable);
pluginItem->setToolTip(C_LOAD, tr("Plugin is required."));
} else {
pluginItem->setData(C_LOAD, Qt::CheckStateRole, state);
pluginItem->setToolTip(C_LOAD, tr("Load on startup"));
if (column == LoadedColumn) {
if (role == Qt::ToolTipRole)
return PluginView::tr("Load on Startup");
if (role == Qt::CheckStateRole) {
int checkedCount = 0;
foreach (PluginSpec *spec, m_plugins) {
if (spec->isEnabledInSettings())
++checkedCount;
}
m_specToItem.insert(spec, pluginItem);
if (checkedCount == 0)
return Qt::Unchecked;
if (checkedCount == m_plugins.length())
return Qt::Checked;
return Qt::PartiallyChecked;
}
}
if (parentItem)
parentItem->addChild(pluginItem);
else
m_items.append(pluginItem);
return QVariant();
}
if (checkedCount == 0) {
groupState = Qt::Unchecked;
ret |= ParsedNone;
} else if (checkedCount == plugins.length()) {
groupState = Qt::Checked;
ret |= ParsedAll;
} else {
groupState = Qt::PartiallyChecked;
ret = ret | ParsedPartial;
bool setData(int column, const QVariant &data, int role)
{
if (column == LoadedColumn && role == Qt::CheckStateRole) {
foreach (TreeItem *item, children())
static_cast<PluginItem *>(item)->setData(column, data, role);
update();
return true;
}
return false;
}
Qt::ItemFlags flags(int column) const
{
Qt::ItemFlags ret = Qt::ItemIsSelectable | Qt::ItemIsEnabled;
if (column == LoadedColumn)
ret |= Qt::ItemIsEditable | Qt::ItemIsUserCheckable;
return ret;
}
}
QIcon PluginView::iconForState(int state)
public:
QString m_name;
QList<PluginSpec *> m_plugins;
PluginView *m_view; // Not owned.
};
/*!
Constructs a PluginView that gets the list of plugins from the
given plugin \a manager with a given \a parent widget.
*/
PluginView::PluginView(QWidget *parent)
: QWidget(parent)
{
if (state & ParsedWithErrors)
return m_errorIcon;
m_categoryView = new TreeView(this);
m_categoryView->setAlternatingRowColors(true);
m_categoryView->setIndentation(20);
m_categoryView->setUniformRowHeights(true);
m_categoryView->setSortingEnabled(true);
m_categoryView->setColumnWidth(LoadedColumn, 40);
m_categoryView->header()->setDefaultSectionSize(120);
m_categoryView->header()->setMinimumSectionSize(35);
m_categoryView->setActivationMode(Utils::DoubleClickActivation);
m_categoryView->setSelectionMode(QAbstractItemView::SingleSelection);
m_categoryView->setSelectionBehavior(QAbstractItemView::SelectRows);
m_model = new TreeModel(this);
m_model->setRootItem(new TreeItem(QStringList()
<< tr("Name") << tr("Load") << tr("Version") << tr("Vendor")));
m_categoryView->setModel(m_model);
if (state & ParsedNone || state & ParsedPartial)
return m_notLoadedIcon;
QGridLayout *gridLayout = new QGridLayout(this);
gridLayout->setContentsMargins(2, 2, 2, 2);
gridLayout->addWidget(m_categoryView, 1, 0, 1, 1);
return m_okIcon;
}
QHeaderView *header = m_categoryView->header();
header->setSectionResizeMode(0, QHeaderView::ResizeToContents);
header->setSectionResizeMode(2, QHeaderView::ResizeToContents);
void PluginView::selectPlugin(QTreeWidgetItem *current)
{
if (!current)
emit currentPluginChanged(0);
else if (current->data(0, Qt::UserRole).canConvert<PluginSpec*>())
emit currentPluginChanged(current->data(0, Qt::UserRole).value<PluginSpec *>());
else
emit currentPluginChanged(0);
connect(PluginManager::instance(), &PluginManager::pluginsChanged,
this, &PluginView::updatePlugins);
connect(m_categoryView, &QAbstractItemView::activated,
[this](const QModelIndex &idx) { pluginActivated(pluginForIndex(idx)); });
connect(m_categoryView->selectionModel(), &QItemSelectionModel::currentChanged,
[this](const QModelIndex &idx) { currentPluginChanged(pluginForIndex(idx)); });
updatePlugins();
}
void PluginView::activatePlugin(QTreeWidgetItem *item)
/*!
\internal
*/
PluginView::~PluginView()
{
if (item->data(0, Qt::UserRole).canConvert<PluginSpec*>()) {
emit pluginActivated(item->data(0, Qt::UserRole).value<PluginSpec *>());
} else
emit pluginActivated(0);
}
void PluginView::updatePluginSettings(QTreeWidgetItem *item, int column)
/*!
Returns the current selection in the list of plugins.
*/
PluginSpec *PluginView::currentPlugin() const
{
if (!m_allowCheckStateUpdate)
return;
m_allowCheckStateUpdate = false;
bool loadOnStartup = item->data(C_LOAD, Qt::CheckStateRole).toBool();
if (item->data(0, Qt::UserRole).canConvert<PluginSpec*>()) {
PluginSpec *spec = item->data(0, Qt::UserRole).value<PluginSpec *>();
if (column == C_LOAD) {
spec->setEnabled(loadOnStartup);
updatePluginDependencies();
if (item->parent()) {
PluginCollection *collection = item->parent()->data(0, Qt::UserRole).value<PluginCollection *>();
Qt::CheckState state = Qt::PartiallyChecked;
int loadCount = 0;
for (int i = 0; i < collection->plugins().length(); ++i) {
if (collection->plugins().at(i)->isEnabledInSettings())
++loadCount;
}
if (loadCount == collection->plugins().length())
state = Qt::Checked;
else if (loadCount == 0)
state = Qt::Unchecked;
item->parent()->setData(C_LOAD, Qt::CheckStateRole, state);
}
return pluginForIndex(m_categoryView->currentIndex());
}
emit pluginSettingsChanged(spec);
}
PluginSpec *PluginView::pluginForIndex(const QModelIndex &index) const
{
auto item = dynamic_cast<PluginItem *>(m_model->itemFromIndex(index));
return item ? item->m_spec: 0;
}
} else {
PluginCollection *collection = item->data(0, Qt::UserRole).value<PluginCollection *>();
for (int i = 0; i < collection->plugins().length(); ++i) {
PluginSpec *spec = collection->plugins().at(i);
QTreeWidgetItem *child = m_specToItem.value(spec);
if (spec->isAvailableForHostPlatform() && !spec->isRequired()) {
spec->setEnabled(loadOnStartup);
Qt::CheckState state = (loadOnStartup ? Qt::Checked : Qt::Unchecked);
child->setData(C_LOAD, Qt::CheckStateRole, state);
}
static void queryDependendPlugins(PluginSpec *spec, QSet<PluginSpec *> *dependencies)
{
QHashIterator<PluginDependency, PluginSpec *> it(spec->dependencySpecs());
while (it.hasNext()) {
it.next();
PluginSpec *dep = it.value();
if (!dependencies->contains(dep)) {
dependencies->insert(dep);
queryDependendPlugins(dep, dependencies);
}
updatePluginDependencies();
if (!collection->plugins().isEmpty())
emit pluginSettingsChanged(collection->plugins().first());
}
m_allowCheckStateUpdate = true;
}
void PluginView::updatePluginDependencies()
void PluginView::updatePlugins()
{
// Dependencies.