1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the examples of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:BSD$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** BSD License Usage
18** Alternatively, you may use this file under the terms of the BSD license
19** as follows:
20**
21** "Redistribution and use in source and binary forms, with or without
22** modification, are permitted provided that the following conditions are
23** met:
24** * Redistributions of source code must retain the above copyright
25** notice, this list of conditions and the following disclaimer.
26** * Redistributions in binary form must reproduce the above copyright
27** notice, this list of conditions and the following disclaimer in
28** the documentation and/or other materials provided with the
29** distribution.
30** * Neither the name of The Qt Company Ltd nor the names of its
31** contributors may be used to endorse or promote products derived
32** from this software without specific prior written permission.
33**
34**
35** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46**
47** $QT_END_LICENSE$
48**
49****************************************************************************/
50
51
52#include "plugindialog.h"
53#include "interfaces.h"
54
55#include <QDir>
56#include <QGridLayout>
57#include <QHeaderView>
58#include <QLabel>
59#include <QPluginLoader>
60#include <QPushButton>
61#include <QStringList>
62#include <QTreeWidget>
63#include <QTreeWidgetItem>
64
65PluginDialog::PluginDialog(const QString &path, const QStringList &fileNames,
66 QWidget *parent) :
67 QDialog(parent),
68 label(new QLabel),
69 treeWidget(new QTreeWidget),
70 okButton(new QPushButton(tr("OK")))
71{
72 treeWidget->setAlternatingRowColors(false);
73 treeWidget->setSelectionMode(QAbstractItemView::NoSelection);
74 treeWidget->setColumnCount(1);
75 treeWidget->header()->hide();
76
77 okButton->setDefault(true);
78
79 connect(okButton, &QAbstractButton::clicked, this, &QWidget::close);
80
81 QGridLayout *mainLayout = new QGridLayout;
82 mainLayout->setColumnStretch(0, 1);
83 mainLayout->setColumnStretch(2, 1);
84 mainLayout->addWidget(label, 0, 0, 1, 3);
85 mainLayout->addWidget(treeWidget, 1, 0, 1, 3);
86 mainLayout->addWidget(okButton, 2, 1);
87 setLayout(mainLayout);
88
89 interfaceIcon.addPixmap(style()->standardPixmap(QStyle::SP_DirOpenIcon),
90 QIcon::Normal, QIcon::On);
91 interfaceIcon.addPixmap(style()->standardPixmap(QStyle::SP_DirClosedIcon),
92 QIcon::Normal, QIcon::Off);
93 featureIcon.addPixmap(style()->standardPixmap(QStyle::SP_FileIcon));
94
95 setWindowTitle(tr("Plugin Information"));
96 findPlugins(path, fileNames);
97}
98
99//! [0]
100void PluginDialog::findPlugins(const QString &path,
101 const QStringList &fileNames)
102{
103 label->setText(tr("Plug & Paint found the following plugins\n"
104 "(looked in %1):")
105 .arg(QDir::toNativeSeparators(path)));
106
107 const QDir dir(path);
108
109 const auto staticInstances = QPluginLoader::staticInstances();
110 for (QObject *plugin : staticInstances)
111 populateTreeWidget(plugin, tr("%1 (Static Plugin)")
112 .arg(plugin->metaObject()->className()));
113
114 for (const QString &fileName : fileNames) {
115 QPluginLoader loader(dir.absoluteFilePath(fileName));
116 QObject *plugin = loader.instance();
117 if (plugin)
118 populateTreeWidget(plugin, fileName);
119 }
120}
121//! [0]
122
123//! [1]
124void PluginDialog::populateTreeWidget(QObject *plugin, const QString &text)
125{
126 auto pluginItem = new QTreeWidgetItem(treeWidget);
127 pluginItem->setText(0, text);
128 pluginItem->setExpanded(true);
129
130 QFont boldFont = pluginItem->font(0);
131 boldFont.setBold(true);
132 pluginItem->setFont(0, boldFont);
133
134 if (plugin) {
135 auto iBrush = qobject_cast<BrushInterface *>(plugin);
136 if (iBrush)
137 addItems(pluginItem, "BrushInterface", iBrush->brushes());
138
139 auto iShape = qobject_cast<ShapeInterface *>(plugin);
140 if (iShape)
141 addItems(pluginItem, "ShapeInterface", iShape->shapes());
142
143 auto iFilter = qobject_cast<FilterInterface *>(plugin);
144 if (iFilter)
145 addItems(pluginItem, "FilterInterface", iFilter->filters());
146 }
147}
148//! [1]
149
150void PluginDialog::addItems(QTreeWidgetItem *pluginItem,
151 const char *interfaceName,
152 const QStringList &features)
153{
154 auto interfaceItem = new QTreeWidgetItem(pluginItem);
155 interfaceItem->setText(0, interfaceName);
156 interfaceItem->setIcon(0, interfaceIcon);
157
158 for (QString feature : features) {
159 if (feature.endsWith("..."))
160 feature.chop(3);
161 auto featureItem = new QTreeWidgetItem(interfaceItem);
162 featureItem->setText(0, feature);
163 featureItem->setIcon(0, featureIcon);
164 }
165}
166