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 | |
65 | PluginDialog::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(s: "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(sender: okButton, signal: &QAbstractButton::clicked, receiver: this, slot: &QWidget::close); |
80 | |
81 | QGridLayout *mainLayout = new QGridLayout; |
82 | mainLayout->setColumnStretch(column: 0, stretch: 1); |
83 | mainLayout->setColumnStretch(column: 2, stretch: 1); |
84 | mainLayout->addWidget(label, row: 0, column: 0, rowSpan: 1, columnSpan: 3); |
85 | mainLayout->addWidget(treeWidget, row: 1, column: 0, rowSpan: 1, columnSpan: 3); |
86 | mainLayout->addWidget(okButton, row: 2, column: 1); |
87 | setLayout(mainLayout); |
88 | |
89 | interfaceIcon.addPixmap(pixmap: style()->standardPixmap(standardPixmap: QStyle::SP_DirOpenIcon), |
90 | mode: QIcon::Normal, state: QIcon::On); |
91 | interfaceIcon.addPixmap(pixmap: style()->standardPixmap(standardPixmap: QStyle::SP_DirClosedIcon), |
92 | mode: QIcon::Normal, state: QIcon::Off); |
93 | featureIcon.addPixmap(pixmap: style()->standardPixmap(standardPixmap: QStyle::SP_FileIcon)); |
94 | |
95 | setWindowTitle(tr(s: "Plugin Information" )); |
96 | findPlugins(path, fileNames); |
97 | } |
98 | |
99 | //! [0] |
100 | void PluginDialog::findPlugins(const QString &path, |
101 | const QStringList &fileNames) |
102 | { |
103 | label->setText(tr(s: "Plug & Paint found the following plugins\n" |
104 | "(looked in %1):" ) |
105 | .arg(a: QDir::toNativeSeparators(pathName: path))); |
106 | |
107 | const QDir dir(path); |
108 | |
109 | const auto staticInstances = QPluginLoader::staticInstances(); |
110 | for (QObject *plugin : staticInstances) |
111 | populateTreeWidget(plugin, text: tr(s: "%1 (Static Plugin)" ) |
112 | .arg(a: 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, text: fileName); |
119 | } |
120 | } |
121 | //! [0] |
122 | |
123 | //! [1] |
124 | void PluginDialog::populateTreeWidget(QObject *plugin, const QString &text) |
125 | { |
126 | auto pluginItem = new QTreeWidgetItem(treeWidget); |
127 | pluginItem->setText(column: 0, atext: text); |
128 | pluginItem->setExpanded(true); |
129 | |
130 | QFont boldFont = pluginItem->font(column: 0); |
131 | boldFont.setBold(true); |
132 | pluginItem->setFont(column: 0, afont: boldFont); |
133 | |
134 | if (plugin) { |
135 | auto iBrush = qobject_cast<BrushInterface *>(object: plugin); |
136 | if (iBrush) |
137 | addItems(pluginItem, interfaceName: "BrushInterface" , features: iBrush->brushes()); |
138 | |
139 | auto iShape = qobject_cast<ShapeInterface *>(object: plugin); |
140 | if (iShape) |
141 | addItems(pluginItem, interfaceName: "ShapeInterface" , features: iShape->shapes()); |
142 | |
143 | auto iFilter = qobject_cast<FilterInterface *>(object: plugin); |
144 | if (iFilter) |
145 | addItems(pluginItem, interfaceName: "FilterInterface" , features: iFilter->filters()); |
146 | } |
147 | } |
148 | //! [1] |
149 | |
150 | void PluginDialog::addItems(QTreeWidgetItem *pluginItem, |
151 | const char *interfaceName, |
152 | const QStringList &features) |
153 | { |
154 | auto interfaceItem = new QTreeWidgetItem(pluginItem); |
155 | interfaceItem->setText(column: 0, atext: interfaceName); |
156 | interfaceItem->setIcon(column: 0, aicon: interfaceIcon); |
157 | |
158 | for (QString feature : features) { |
159 | if (feature.endsWith(s: "..." )) |
160 | feature.chop(n: 3); |
161 | auto featureItem = new QTreeWidgetItem(interfaceItem); |
162 | featureItem->setText(column: 0, atext: feature); |
163 | featureItem->setIcon(column: 0, aicon: featureIcon); |
164 | } |
165 | } |
166 | |