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() ...@@ -1301,10 +1301,6 @@ void PluginManagerPrivate::resolveDependencies()
spec->d->resolveDependencies(pluginSpecs); spec->d->resolveDependencies(pluginSpecs);
} }
// Reset disabledIndirectly flag
foreach (PluginSpec *spec, loadQueue())
spec->d->disabledIndirectly = false;
foreach (PluginSpec *spec, loadQueue()) { foreach (PluginSpec *spec, loadQueue()) {
spec->d->disableIndirectlyIfDependencyDisabled(); spec->d->disableIndirectlyIfDependencyDisabled();
} }
...@@ -1329,11 +1325,6 @@ PluginSpec *PluginManagerPrivate::pluginForOption(const QString &option, bool *r ...@@ -1329,11 +1325,6 @@ PluginSpec *PluginManagerPrivate::pluginForOption(const QString &option, bool *r
return 0; return 0;
} }
void PluginManagerPrivate::disablePluginIndirectly(PluginSpec *spec)
{
spec->d->disabledIndirectly = true;
}
PluginSpec *PluginManagerPrivate::pluginByName(const QString &name) const PluginSpec *PluginManagerPrivate::pluginByName(const QString &name) const
{ {
foreach (PluginSpec *spec, pluginSpecs) foreach (PluginSpec *spec, pluginSpecs)
......
...@@ -80,7 +80,6 @@ public: ...@@ -80,7 +80,6 @@ public:
void setGlobalSettings(QSettings *settings); void setGlobalSettings(QSettings *settings);
void readSettings(); void readSettings();
void writeSettings(); void writeSettings();
void disablePluginIndirectly(PluginSpec *spec);
class TestSpec { class TestSpec {
public: public:
......
...@@ -542,11 +542,6 @@ void PluginSpec::setDisabledByDefault(bool value) ...@@ -542,11 +542,6 @@ void PluginSpec::setDisabledByDefault(bool value)
d->disabledByDefault = value; d->disabledByDefault = value;
} }
void PluginSpec::setDisabledIndirectly(bool value)
{
d->disabledIndirectly = value;
}
void PluginSpec::setForceEnabled(bool value) void PluginSpec::setForceEnabled(bool value)
{ {
d->forceEnabled = value; d->forceEnabled = value;
......
...@@ -113,7 +113,6 @@ public: ...@@ -113,7 +113,6 @@ public:
void setEnabled(bool value); void setEnabled(bool value);
void setDisabledByDefault(bool value); void setDisabledByDefault(bool value);
void setDisabledIndirectly(bool value);
void setForceEnabled(bool value); void setForceEnabled(bool value);
void setForceDisabled(bool value); void setForceDisabled(bool value);
......
...@@ -32,14 +32,17 @@ ...@@ -32,14 +32,17 @@
#include "pluginmanager.h" #include "pluginmanager.h"
#include "pluginspec.h" #include "pluginspec.h"
#include "plugincollection.h" #include "plugincollection.h"
#include <utils/algorithm.h>
#include <utils/itemviews.h> #include <utils/itemviews.h>
#include <utils/treemodel.h>
#include <QDebug> #include <QDebug>
#include <QDir> #include <QDir>
#include <QGridLayout> #include <QGridLayout>
#include <QHeaderView> #include <QHeaderView>
#include <QPalette> #include <QSet>
#include <QTreeWidgetItem> #include <QItemSelectionModel>
/*! /*!
\class ExtensionSystem::PluginView \class ExtensionSystem::PluginView
...@@ -66,317 +69,321 @@ ...@@ -66,317 +69,321 @@
for example by a double-click. for example by a double-click.
*/ */
using namespace ExtensionSystem;
Q_DECLARE_METATYPE(ExtensionSystem::PluginSpec*) Q_DECLARE_METATYPE(ExtensionSystem::PluginSpec*)
Q_DECLARE_METATYPE(ExtensionSystem::PluginCollection*) Q_DECLARE_METATYPE(ExtensionSystem::PluginCollection*)
/*! using namespace Utils;
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);
m_okIcon = QIcon(QLatin1String(":/extensionsystem/images/ok.png")); namespace ExtensionSystem {
m_errorIcon = QIcon(QLatin1String(":/extensionsystem/images/error.png"));
m_notLoadedIcon = QIcon(QLatin1String(":/extensionsystem/images/notloaded.png"));
connect(PluginManager::instance(), SIGNAL(pluginsChanged()), this, SLOT(updateList())); enum Columns { NameColumn, LoadedColumn, VersionColumn, VendorColumn, };
connect(m_categoryWidget, SIGNAL(currentItemChanged(QTreeWidgetItem*,QTreeWidgetItem*)),
this, SLOT(selectPlugin(QTreeWidgetItem*)));
connect(m_categoryWidget, SIGNAL(itemActivated(QTreeWidgetItem*,int)),
this, SLOT(activatePlugin(QTreeWidgetItem*)));
updateList(); enum IconIndex { OkIcon, ErrorIcon, NotLoadedIcon };
}
/*! static const QIcon &icon(int num)
\internal
*/
PluginView::~PluginView()
{ {
static QIcon icons[] = {
QIcon(QLatin1String(":/extensionsystem/images/ok.png")),
QIcon(QLatin1String(":/extensionsystem/images/error.png")),
QIcon(QLatin1String(":/extensionsystem/images/notloaded.png")),
};
return icons[num];
} }
/*! class PluginItem : public TreeItem
Returns the current selection in the list of plugins.
*/
PluginSpec *PluginView::currentPlugin() const
{ {
if (!m_categoryWidget->currentItem()) public:
return 0; PluginItem(PluginSpec *spec, PluginView *view)
if (!m_categoryWidget->currentItem()->data(0, Qt::UserRole).isNull()) : m_spec(spec), m_view(view)
return m_categoryWidget->currentItem()->data(0, Qt::UserRole).value<PluginSpec *>(); {}
return 0;
} 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() case LoadedColumn:
{ if (!m_spec->isAvailableForHostPlatform()) {
connect(m_categoryWidget, SIGNAL(itemChanged(QTreeWidgetItem*,int)), if (role == Qt::CheckStateRole)
this, SLOT(updatePluginSettings(QTreeWidgetItem*,int))); 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; case VersionColumn:
foreach (PluginCollection *collection, PluginManager::pluginCollections()) { if (role == Qt::DisplayRole)
if (collection->name().isEmpty() || collection->plugins().isEmpty()) { return QString::fromLatin1("%1 (%2)").arg(m_spec->version(), m_spec->compatVersion());
defaultCollection = collection; break;
continue;
case VendorColumn:
if (role == Qt::DisplayRole)
return m_spec->vendor();
break;
} }
// State, name, load, version, vendor.
QTreeWidgetItem *collectionItem = new QTreeWidgetItem(QStringList() return QVariant();
<< 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));
} }
QList<PluginSpec *> plugins = defaultCollection ? defaultCollection->plugins() : QList<PluginSpec *>(); bool setData(int column, const QVariant &data, int role)
if (!plugins.isEmpty()) { {
// add all non-categorized plugins into utilities. could also be added as root items if (column == LoadedColumn && role == Qt::CheckStateRole) {
// but that makes the tree ugly. m_spec->setEnabled(data.toBool());
QTreeWidgetItem *defaultCollectionItem = new QTreeWidgetItem(QStringList() update();
<< QString(tr("Utilities")) parent()->update();
<< QString() emit m_view->pluginSettingsChanged(m_spec);
<< QString() return true;
<< QString() }
<< QString()); return false;
}
m_items.append(defaultCollectionItem);
Qt::CheckState groupState = Qt::Unchecked; bool isEnabled() const
int state = parsePluginSpecs(defaultCollectionItem, groupState, plugins); {
if (m_spec->isRequired() || !m_spec->isAvailableForHostPlatform())
defaultCollectionItem->setIcon(0, iconForState(state)); return false;
defaultCollectionItem->setData(C_LOAD, Qt::CheckStateRole, QVariant(groupState)); foreach (PluginSpec *spec, m_view->m_pluginDependencies.value(m_spec))
defaultCollectionItem->setToolTip(C_LOAD, tr("Load on Startup")); if (!spec->isEnabledInSettings())
defaultCollectionItem->setData(0, Qt::UserRole, qVariantFromValue(defaultCollection)); 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 (column == LoadedColumn) {
if (!m_items.isEmpty()) { if (m_spec->isAvailableForHostPlatform() && !m_spec->isRequired())
m_categoryWidget->addTopLevelItems(m_items); ret |= Qt::ItemIsEditable | Qt ::ItemIsUserCheckable;
m_categoryWidget->expandAll();
} }
m_categoryWidget->sortItems(0, Qt::AscendingOrder); return ret;
if (m_categoryWidget->topLevelItemCount()) }
m_categoryWidget->setCurrentItem(m_categoryWidget->topLevelItem(0));
} 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; public:
int checkedCount = 0; 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) { int columnCount() const { return 4; }
PluginSpec *spec = plugins[i];
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()) if (spec->hasError())
ret |= ParsedWithErrors; return icon(ErrorIcon);
if (!spec->isEnabledInSettings())
QTreeWidgetItem *pluginItem = new QTreeWidgetItem(QStringList() return icon(NotLoadedIcon);
<< spec->name() }
<< QString() // load on startup return icon(OkIcon);
<< 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;
} }
if (!spec->isAvailableForHostPlatform()) { if (column == LoadedColumn) {
pluginItem->setData(C_LOAD, Qt::CheckStateRole, Qt::Unchecked); if (role == Qt::ToolTipRole)
pluginItem->setFlags(Qt::ItemIsSelectable); return PluginView::tr("Load on Startup");
pluginItem->setToolTip(C_LOAD, tr("Plugin is not available on this platform.")); if (role == Qt::CheckStateRole) {
} else if (spec->isRequired()){ int checkedCount = 0;
pluginItem->setData(C_LOAD, Qt::CheckStateRole, Qt::Checked); foreach (PluginSpec *spec, m_plugins) {
pluginItem->setFlags(Qt::ItemIsSelectable); if (spec->isEnabledInSettings())
pluginItem->setToolTip(C_LOAD, tr("Plugin is required.")); ++checkedCount;
} else {
pluginItem->setData(C_LOAD, Qt::CheckStateRole, state);
pluginItem->setToolTip(C_LOAD, tr("Load on startup"));
} }
m_specToItem.insert(spec, pluginItem); if (checkedCount == 0)
return Qt::Unchecked;
if (checkedCount == m_plugins.length())
return Qt::Checked;
return Qt::PartiallyChecked;
}
}
if (parentItem) return QVariant();
parentItem->addChild(pluginItem);
else
m_items.append(pluginItem);
} }
if (checkedCount == 0) { bool setData(int column, const QVariant &data, int role)
groupState = Qt::Unchecked; {
ret |= ParsedNone; if (column == LoadedColumn && role == Qt::CheckStateRole) {
} else if (checkedCount == plugins.length()) { foreach (TreeItem *item, children())
groupState = Qt::Checked; static_cast<PluginItem *>(item)->setData(column, data, role);
ret |= ParsedAll; update();
} else { return true;
groupState = Qt::PartiallyChecked;
ret = ret | ParsedPartial;
} }
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; 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) m_categoryView = new TreeView(this);
return m_errorIcon; 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) QGridLayout *gridLayout = new QGridLayout(this);
return m_notLoadedIcon; 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) connect(PluginManager::instance(), &PluginManager::pluginsChanged,
{ this, &PluginView::updatePlugins);
if (!current)
emit currentPluginChanged(0); connect(m_categoryView, &QAbstractItemView::activated,
else if (current->data(0, Qt::UserRole).canConvert<PluginSpec*>()) [this](const QModelIndex &idx) { pluginActivated(pluginForIndex(idx)); });
emit currentPluginChanged(current->data(0, Qt::UserRole).value<PluginSpec *>());
else connect(m_categoryView->selectionModel(), &QItemSelectionModel::currentChanged,
emit currentPluginChanged(0); [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 pluginForIndex(m_categoryView->currentIndex());
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())