| 1 | /**************************************************************************** | 
|---|
| 2 | ** | 
|---|
| 3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). | 
|---|
| 4 | ** Contact: Qt Software Information (qt-info@nokia.com) | 
|---|
| 5 | ** | 
|---|
| 6 | ** This file is part of the examples of the Qt Toolkit. | 
|---|
| 7 | ** | 
|---|
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ | 
|---|
| 9 | ** Commercial Usage | 
|---|
| 10 | ** Licensees holding valid Qt Commercial licenses may use this file in | 
|---|
| 11 | ** accordance with the Qt Commercial License Agreement provided with the | 
|---|
| 12 | ** Software or, alternatively, in accordance with the terms contained in | 
|---|
| 13 | ** a written agreement between you and Nokia. | 
|---|
| 14 | ** | 
|---|
| 15 | ** GNU Lesser General Public License Usage | 
|---|
| 16 | ** Alternatively, this file may be used under the terms of the GNU Lesser | 
|---|
| 17 | ** General Public License version 2.1 as published by the Free Software | 
|---|
| 18 | ** Foundation and appearing in the file LICENSE.LGPL included in the | 
|---|
| 19 | ** packaging of this file.  Please review the following information to | 
|---|
| 20 | ** ensure the GNU Lesser General Public License version 2.1 requirements | 
|---|
| 21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | 
|---|
| 22 | ** | 
|---|
| 23 | ** In addition, as a special exception, Nokia gives you certain | 
|---|
| 24 | ** additional rights. These rights are described in the Nokia Qt LGPL | 
|---|
| 25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this | 
|---|
| 26 | ** package. | 
|---|
| 27 | ** | 
|---|
| 28 | ** GNU General Public License Usage | 
|---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU | 
|---|
| 30 | ** General Public License version 3.0 as published by the Free Software | 
|---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the | 
|---|
| 32 | ** packaging of this file.  Please review the following information to | 
|---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be | 
|---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html. | 
|---|
| 35 | ** | 
|---|
| 36 | ** If you are unsure which license is appropriate for your use, please | 
|---|
| 37 | ** contact the sales department at qt-sales@nokia.com. | 
|---|
| 38 | ** $QT_END_LICENSE$ | 
|---|
| 39 | ** | 
|---|
| 40 | ****************************************************************************/ | 
|---|
| 41 |  | 
|---|
| 42 |  | 
|---|
| 43 | #include "interfaces.h" | 
|---|
| 44 | #include "plugindialog.h" | 
|---|
| 45 |  | 
|---|
| 46 | #include <QPluginLoader> | 
|---|
| 47 | #include <QStringList> | 
|---|
| 48 | #include <QDir> | 
|---|
| 49 |  | 
|---|
| 50 | #include <QLabel> | 
|---|
| 51 | #include <QGridLayout> | 
|---|
| 52 | #include <QPushButton> | 
|---|
| 53 | #include <QTreeWidget> | 
|---|
| 54 | #include <QTreeWidgetItem> | 
|---|
| 55 | #include <QHeaderView> | 
|---|
| 56 |  | 
|---|
| 57 | PluginDialog::PluginDialog(const QString &path, const QStringList &fileNames, | 
|---|
| 58 | QWidget *parent) : | 
|---|
| 59 | QDialog(parent), | 
|---|
| 60 | label(new QLabel), | 
|---|
| 61 | treeWidget(new QTreeWidget), | 
|---|
| 62 | okButton(new QPushButton(tr("OK"))) | 
|---|
| 63 | { | 
|---|
| 64 | treeWidget->setAlternatingRowColors(false); | 
|---|
| 65 | treeWidget->setSelectionMode(QAbstractItemView::NoSelection); | 
|---|
| 66 | treeWidget->setColumnCount(1); | 
|---|
| 67 | treeWidget->header()->hide(); | 
|---|
| 68 |  | 
|---|
| 69 | okButton->setDefault(true); | 
|---|
| 70 |  | 
|---|
| 71 | connect(okButton, SIGNAL(clicked()), this, SLOT(close())); | 
|---|
| 72 |  | 
|---|
| 73 | QGridLayout *mainLayout = new QGridLayout; | 
|---|
| 74 | mainLayout->setColumnStretch(0, 1); | 
|---|
| 75 | mainLayout->setColumnStretch(2, 1); | 
|---|
| 76 | mainLayout->addWidget(label, 0, 0, 1, 3); | 
|---|
| 77 | mainLayout->addWidget(treeWidget, 1, 0, 1, 3); | 
|---|
| 78 | mainLayout->addWidget(okButton, 2, 1); | 
|---|
| 79 | setLayout(mainLayout); | 
|---|
| 80 |  | 
|---|
| 81 | interfaceIcon.addPixmap(style()->standardPixmap(QStyle::SP_DirOpenIcon), | 
|---|
| 82 | QIcon::Normal, QIcon::On); | 
|---|
| 83 | interfaceIcon.addPixmap(style()->standardPixmap(QStyle::SP_DirClosedIcon), | 
|---|
| 84 | QIcon::Normal, QIcon::Off); | 
|---|
| 85 | featureIcon.addPixmap(style()->standardPixmap(QStyle::SP_FileIcon)); | 
|---|
| 86 |  | 
|---|
| 87 | setWindowTitle(tr("Plugin Information")); | 
|---|
| 88 | findPlugins(path, fileNames); | 
|---|
| 89 | } | 
|---|
| 90 |  | 
|---|
| 91 | //! [0] | 
|---|
| 92 | void PluginDialog::findPlugins(const QString &path, | 
|---|
| 93 | const QStringList &fileNames) | 
|---|
| 94 | { | 
|---|
| 95 | label->setText(tr("Plug & Paint found the following plugins\n" | 
|---|
| 96 | "(looked in %1):") | 
|---|
| 97 | .arg(QDir::toNativeSeparators(path))); | 
|---|
| 98 |  | 
|---|
| 99 | const QDir dir(path); | 
|---|
| 100 |  | 
|---|
| 101 | foreach (QObject *plugin, QPluginLoader::staticInstances()) | 
|---|
| 102 | populateTreeWidget(plugin, tr("%1 (Static Plugin)") | 
|---|
| 103 | .arg(plugin->metaObject()->className())); | 
|---|
| 104 |  | 
|---|
| 105 | foreach (QString fileName, fileNames) { | 
|---|
| 106 | QPluginLoader loader(dir.absoluteFilePath(fileName)); | 
|---|
| 107 | QObject *plugin = loader.instance(); | 
|---|
| 108 | if (plugin) | 
|---|
| 109 | populateTreeWidget(plugin, fileName); | 
|---|
| 110 | } | 
|---|
| 111 | } | 
|---|
| 112 | //! [0] | 
|---|
| 113 |  | 
|---|
| 114 | //! [1] | 
|---|
| 115 | void PluginDialog::populateTreeWidget(QObject *plugin, const QString &text) | 
|---|
| 116 | { | 
|---|
| 117 | QTreeWidgetItem *pluginItem = new QTreeWidgetItem(treeWidget); | 
|---|
| 118 | pluginItem->setText(0, text); | 
|---|
| 119 | treeWidget->setItemExpanded(pluginItem, true); | 
|---|
| 120 |  | 
|---|
| 121 | QFont boldFont = pluginItem->font(0); | 
|---|
| 122 | boldFont.setBold(true); | 
|---|
| 123 | pluginItem->setFont(0, boldFont); | 
|---|
| 124 |  | 
|---|
| 125 | if (plugin) { | 
|---|
| 126 | BrushInterface *iBrush = qobject_cast<BrushInterface *>(plugin); | 
|---|
| 127 | if (iBrush) | 
|---|
| 128 | addItems(pluginItem, "BrushInterface", iBrush->brushes()); | 
|---|
| 129 |  | 
|---|
| 130 | ShapeInterface *iShape = qobject_cast<ShapeInterface *>(plugin); | 
|---|
| 131 | if (iShape) | 
|---|
| 132 | addItems(pluginItem, "ShapeInterface", iShape->shapes()); | 
|---|
| 133 |  | 
|---|
| 134 | FilterInterface *iFilter = | 
|---|
| 135 | qobject_cast<FilterInterface *>(plugin); | 
|---|
| 136 | if (iFilter) | 
|---|
| 137 | addItems(pluginItem, "FilterInterface", iFilter->filters()); | 
|---|
| 138 | } | 
|---|
| 139 | } | 
|---|
| 140 | //! [1] | 
|---|
| 141 |  | 
|---|
| 142 | void PluginDialog::addItems(QTreeWidgetItem *pluginItem, | 
|---|
| 143 | const char *interfaceName, | 
|---|
| 144 | const QStringList &features) | 
|---|
| 145 | { | 
|---|
| 146 | QTreeWidgetItem *interfaceItem = new QTreeWidgetItem(pluginItem); | 
|---|
| 147 | interfaceItem->setText(0, interfaceName); | 
|---|
| 148 | interfaceItem->setIcon(0, interfaceIcon); | 
|---|
| 149 |  | 
|---|
| 150 | foreach (QString feature, features) { | 
|---|
| 151 | if (feature.endsWith("...")) | 
|---|
| 152 | feature.chop(3); | 
|---|
| 153 | QTreeWidgetItem *featureItem = new QTreeWidgetItem(interfaceItem); | 
|---|
| 154 | featureItem->setText(0, feature); | 
|---|
| 155 | featureItem->setIcon(0, featureIcon); | 
|---|
| 156 | } | 
|---|
| 157 | } | 
|---|