qt6windows7/examples/widgets/tools/plugandpaint/app/plugindialog.cpp

119 lines
3.7 KiB
C++
Raw Normal View History

2023-10-30 06:33:08 +08:00
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
#include "plugindialog.h"
#include "interfaces.h"
#include <QDir>
#include <QGridLayout>
#include <QHeaderView>
#include <QLabel>
#include <QPluginLoader>
#include <QPushButton>
#include <QStringList>
#include <QTreeWidget>
#include <QTreeWidgetItem>
PluginDialog::PluginDialog(const QString &path, const QStringList &fileNames,
QWidget *parent) :
QDialog(parent),
label(new QLabel),
treeWidget(new QTreeWidget),
okButton(new QPushButton(tr("OK")))
{
treeWidget->setAlternatingRowColors(false);
treeWidget->setSelectionMode(QAbstractItemView::NoSelection);
treeWidget->setColumnCount(1);
treeWidget->header()->hide();
okButton->setDefault(true);
connect(okButton, &QAbstractButton::clicked, this, &QWidget::close);
QGridLayout *mainLayout = new QGridLayout;
mainLayout->setColumnStretch(0, 1);
mainLayout->setColumnStretch(2, 1);
mainLayout->addWidget(label, 0, 0, 1, 3);
mainLayout->addWidget(treeWidget, 1, 0, 1, 3);
mainLayout->addWidget(okButton, 2, 1);
setLayout(mainLayout);
interfaceIcon.addPixmap(style()->standardPixmap(QStyle::SP_DirOpenIcon),
QIcon::Normal, QIcon::On);
interfaceIcon.addPixmap(style()->standardPixmap(QStyle::SP_DirClosedIcon),
QIcon::Normal, QIcon::Off);
featureIcon.addPixmap(style()->standardPixmap(QStyle::SP_FileIcon));
setWindowTitle(tr("Plugin Information"));
findPlugins(path, fileNames);
}
//! [0]
void PluginDialog::findPlugins(const QString &path,
const QStringList &fileNames)
{
label->setText(tr("Plug & Paint found the following plugins\n"
"(looked in %1):")
.arg(QDir::toNativeSeparators(path)));
const QDir dir(path);
const auto staticInstances = QPluginLoader::staticInstances();
for (QObject *plugin : staticInstances)
populateTreeWidget(plugin, tr("%1 (Static Plugin)")
.arg(plugin->metaObject()->className()));
for (const QString &fileName : fileNames) {
QPluginLoader loader(dir.absoluteFilePath(fileName));
QObject *plugin = loader.instance();
if (plugin)
populateTreeWidget(plugin, fileName);
}
}
//! [0]
//! [1]
void PluginDialog::populateTreeWidget(QObject *plugin, const QString &text)
{
auto pluginItem = new QTreeWidgetItem(treeWidget);
pluginItem->setText(0, text);
pluginItem->setExpanded(true);
QFont boldFont = pluginItem->font(0);
boldFont.setBold(true);
pluginItem->setFont(0, boldFont);
if (plugin) {
auto iBrush = qobject_cast<BrushInterface *>(plugin);
if (iBrush)
addItems(pluginItem, "BrushInterface", iBrush->brushes());
auto iShape = qobject_cast<ShapeInterface *>(plugin);
if (iShape)
addItems(pluginItem, "ShapeInterface", iShape->shapes());
auto iFilter = qobject_cast<FilterInterface *>(plugin);
if (iFilter)
addItems(pluginItem, "FilterInterface", iFilter->filters());
}
}
//! [1]
void PluginDialog::addItems(QTreeWidgetItem *pluginItem,
const char *interfaceName,
const QStringList &features)
{
auto interfaceItem = new QTreeWidgetItem(pluginItem);
interfaceItem->setText(0, interfaceName);
interfaceItem->setIcon(0, interfaceIcon);
for (QString feature : features) {
if (feature.endsWith("..."))
feature.chop(3);
auto featureItem = new QTreeWidgetItem(interfaceItem);
featureItem->setText(0, feature);
featureItem->setIcon(0, featureIcon);
}
}