1 /****************************************************************************
2 * MeshLab o o *
3 * A versatile mesh processing toolbox o o *
4 * _ O _ *
5 * Copyright(C) 2005 \/)\/ *
6 * Visual Computing Lab /\/| *
7 * ISTI - Italian National Research Council | *
8 * \ *
9 * All rights reserved. *
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * GNU General Public License (http://www.gnu.org/licenses/gpl.txt) *
20 * for more details. *
21 * *
22 ****************************************************************************/
23
24 #include "plugindialog.h"
25 #include <common/interfaces.h>
26
27 #include <QLabel>
28 #include <QTreeWidget>
29 #include <QGroupBox>
30 #include <QPushButton>
31 #include <QHeaderView>
32 #include <QGridLayout>
33 #include <QHBoxLayout>
34 #include <QStringList>
35
PluginDialog(const QString & path,const QStringList & fileNames,QWidget * parent)36 PluginDialog::PluginDialog(const QString &path, const QStringList &fileNames,QWidget *parent): QDialog(parent)
37 {
38 label = new QLabel;
39 label->setWordWrap(true);
40 QStringList headerLabels;
41 headerLabels << tr("Components");
42
43 treeWidget = new QTreeWidget;
44 treeWidget->setAlternatingRowColors(false);
45 treeWidget->setHeaderLabels(headerLabels);
46 treeWidget->header()->hide();
47
48 groupBox=new QGroupBox(tr("Info Plugin"));
49
50 okButton = new QPushButton(tr("OK"));
51 okButton->setDefault(true);
52
53 spacerItem = new QSpacerItem(363, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
54
55 labelInfo=new QLabel(groupBox);
56 labelInfo->setWordWrap(true);
57 //tedit->hide();
58
59 connect(okButton, SIGNAL(clicked()), this, SLOT(close()));
60 connect(treeWidget,SIGNAL(itemClicked(QTreeWidgetItem*,int)),this,SLOT(displayInfo(QTreeWidgetItem*,int)));
61
62 QGridLayout *mainLayout = new QGridLayout;
63 QHBoxLayout *gboxLayout = new QHBoxLayout(groupBox);
64 gboxLayout->addWidget(labelInfo);
65 //mainLayout->setColumnStretch(0, 1);
66 //mainLayout->setColumnStretch(2, 1);
67 mainLayout->addWidget(label, 0, 0, 1, 2);
68 mainLayout->addWidget(treeWidget, 1, 0, 4, 2);
69 mainLayout->addWidget(groupBox,5,0,1,2);
70 mainLayout->addItem(spacerItem, 6, 0, 1, 1);
71 mainLayout->addWidget(okButton,6,1,1,1);
72
73
74 //mainLayout->addWidget(okButton, 3, 1,Qt::AlignHCenter);
75 //mainLayout->addLayout(buttonLayout,3,1);
76 setLayout(mainLayout);
77
78 interfaceIcon.addPixmap(style()->standardPixmap(QStyle::SP_DirOpenIcon),QIcon::Normal, QIcon::On);
79 interfaceIcon.addPixmap(style()->standardPixmap(QStyle::SP_DirClosedIcon),QIcon::Normal, QIcon::Off);
80 featureIcon.addPixmap(style()->standardPixmap(QStyle::SP_FileIcon));
81
82 setWindowTitle(tr("Plugin Information"));
83 populateTreeWidget(path, fileNames);
84 pathDirectory=path;
85 }
86
computeXmlFilename(const QDir & dir,const QString & filename)87 static QString computeXmlFilename(const QDir & dir, const QString & filename)
88 {
89 QFileInfo f(dir.absoluteFilePath(filename));
90 QString tmp = f.baseName() + ".xml";
91 if (tmp.startsWith("lib")) {
92 tmp.replace("lib", "");
93 }
94 return dir.absoluteFilePath(tmp);
95 }
96
populateTreeWidget(const QString & path,const QStringList & fileNames)97 void PluginDialog::populateTreeWidget(const QString &path,const QStringList &fileNames)
98 {
99 if (fileNames.isEmpty()) {
100 label->setText(tr("Can't find any plugins in the %1 " "directory.").arg(QDir::toNativeSeparators(path)));
101 treeWidget->hide();
102 } else {
103 label->setText(tr("Found the following plugins in the %1 " "directory:").arg(QDir::toNativeSeparators(path)));
104 QDir dir(path);
105 foreach (QString fileName, fileNames) {
106 QPluginLoader loader(dir.absoluteFilePath(fileName));
107 QObject *plugin = loader.instance();
108
109 QTreeWidgetItem *pluginItem = new QTreeWidgetItem(treeWidget);
110 pluginItem->setText(0, fileName);
111 pluginItem->setIcon(0, interfaceIcon);
112 treeWidget->setItemExpanded(pluginItem, false);
113
114 QFont boldFont = pluginItem->font(0);
115 boldFont.setBold(true);
116 pluginItem->setFont(0, boldFont);
117
118 if (plugin) {
119 MeshIOInterface *iMeshIO = qobject_cast<MeshIOInterface *>(plugin);
120 if (iMeshIO){
121 QStringList Templist;
122 foreach(const MeshIOInterface::Format f,iMeshIO->importFormats()){
123 QString formats;
124 foreach(const QString s,f.extensions) formats+="Importer_"+s+" ";
125 Templist.push_back(formats);
126 }
127 foreach(const MeshIOInterface::Format f,iMeshIO->exportFormats()){
128 QString formats;
129 foreach(const QString s,f.extensions) formats+="Exporter_"+s+" ";
130 Templist.push_back(formats);
131 }
132 addItems(pluginItem,Templist);
133 }
134 MeshDecorateInterface *iDecorate = qobject_cast<MeshDecorateInterface *>(plugin);
135 if (iDecorate){
136 QStringList Templist;
137 foreach(QAction *a,iDecorate->actions()){Templist.push_back(a->text());}
138 addItems(pluginItem,Templist);
139 }
140 MeshFilterInterface *iFilter = qobject_cast<MeshFilterInterface *>(plugin);
141 if (iFilter){
142 QStringList Templist;
143 foreach(QAction *a,iFilter->actions()){Templist.push_back(a->text());}
144 addItems(pluginItem,Templist);
145 }
146 MeshLabFilterInterface *iXMLFilter = qobject_cast<MeshLabFilterInterface *>(plugin);
147 if (iXMLFilter)
148 {
149 QString xmlFile = computeXmlFilename(dir, fileName);
150 XMLMessageHandler xmlErr;
151 MeshLabXMLFilterContainer fc;
152 fc.xmlInfo = MLXMLPluginInfo::createXMLPluginInfo(xmlFile,MLXMLUtilityFunctions::xmlSchemaFile(),xmlErr);
153 if (fc.xmlInfo != NULL)
154 {
155 QStringList fn = fc.xmlInfo->filterNames();
156 addItems(pluginItem,fn);
157 }
158 MLXMLPluginInfo::destroyXMLPluginInfo(fc.xmlInfo);
159 }
160 MeshRenderInterface *iRender = qobject_cast<MeshRenderInterface *>(plugin);
161 if (iRender){
162 QStringList Templist;
163 foreach(QAction *a,iRender->actions()){Templist.push_back(a->text());}
164 addItems(pluginItem,Templist);
165 }
166 MeshEditInterfaceFactory *iEdit = qobject_cast<MeshEditInterfaceFactory *>(plugin);
167 if (iEdit){
168 QStringList Templist;
169 foreach(QAction *a,iEdit->actions()){Templist.push_back(a->text());}
170 addItems(pluginItem,Templist);
171 }
172 }
173 }
174 }
175 }
176
177
addItems(QTreeWidgetItem * pluginItem,const QStringList & features)178 void PluginDialog::addItems(QTreeWidgetItem *pluginItem,const QStringList &features){
179
180 foreach (QString feature, features) {
181 QTreeWidgetItem *featureItem = new QTreeWidgetItem(pluginItem);
182 featureItem->setText(0, feature);
183 featureItem->setIcon(0, featureIcon);
184 }
185 }
186
187
displayInfo(QTreeWidgetItem * item,int)188 void PluginDialog::displayInfo(QTreeWidgetItem* item,int /* ncolumn*/)
189 {
190 QString parent;
191 QString actionName;
192 if(item==NULL) return;
193 if (item->parent()!=NULL) {parent=item->parent()->text(0);actionName=item->text(0);}
194 else parent=item->text(0);
195 QString fileName=pathDirectory+"/"+parent;
196 QDir dir(pathDirectory);
197 QPluginLoader loader(fileName);
198 qDebug("Trying to load the plugin '%s'", qUtf8Printable(fileName));
199 QObject *plugin = loader.instance();
200 if (plugin) {
201 MeshIOInterface *iMeshIO = qobject_cast<MeshIOInterface *>(plugin);
202 if (iMeshIO){
203 foreach(const MeshIOInterface::Format f,iMeshIO->importFormats()){
204 QString formats;
205 foreach(const QString s,f.extensions) formats+="Importer_"+s+" ";
206 if (actionName==formats) labelInfo->setText(f.description);
207 }
208 foreach(const MeshIOInterface::Format f,iMeshIO->exportFormats()){
209 QString formats;
210 foreach(const QString s,f.extensions) formats+="Exporter_"+s+" ";
211 if (actionName==formats) labelInfo->setText(f.description);
212 }
213 }
214 MeshDecorateInterface *iDecorate = qobject_cast<MeshDecorateInterface *>(plugin);
215 if (iDecorate)
216 {
217 foreach(QAction *a,iDecorate->actions())
218 if (actionName==a->text())
219 labelInfo->setText(iDecorate->decorationInfo(a));
220 }
221 MeshFilterInterface *iFilter = qobject_cast<MeshFilterInterface *>(plugin);
222 if (iFilter)
223 {
224 foreach(QAction *a,iFilter->actions())
225 if (actionName==a->text()) labelInfo->setText(iFilter->filterInfo(iFilter->ID(a)));
226 }
227 MeshLabFilterInterface *iXMLFilter = qobject_cast<MeshLabFilterInterface *>(plugin);
228 if (iXMLFilter)
229 {
230 QString xmlFile = computeXmlFilename(dir, parent);
231 XMLMessageHandler xmlErr;
232 MeshLabXMLFilterContainer fc;
233 fc.xmlInfo = MLXMLPluginInfo::createXMLPluginInfo(xmlFile,MLXMLUtilityFunctions::xmlSchemaFile(),xmlErr);
234 if (fc.xmlInfo != NULL)
235 {
236 QStringList ls = fc.xmlInfo->filterNames();
237 foreach(QString fn,ls)
238 labelInfo->setText(fc.xmlInfo->filterHelp(fn));
239 }
240 MLXMLPluginInfo::destroyXMLPluginInfo(fc.xmlInfo);
241 }
242 MeshRenderInterface *iRender = qobject_cast<MeshRenderInterface *>(plugin);
243 if (iRender){
244 }
245 MeshEditInterfaceFactory *iEditFactory = qobject_cast<MeshEditInterfaceFactory *>(plugin);
246 if (iEditFactory)
247 {
248 foreach(QAction *a, iEditFactory->actions())
249 {
250 if(iEditFactory) labelInfo->setText(iEditFactory->getEditToolDescription(a));
251 }
252 }
253 }
254 }
255