应用程序插件框架的内容包括:主程序App,插件Plugin。
1、实现一个应用程序插架框架关键点有:
一个插件的标准接口,在主程序中存在一个插件的集合。主程序通过循环读取每个插件,将插件对象通过多态的机制转换为插件接口,实现插件的装载。
主程序对象或者主程序接口需要作为参数传递到插件对象中,以方便插件对象调用主程序的内容,如主视图、工具栏、树视图、状态栏等。
2、开源点云处理软件CloudCompare也是一个插件框架,因此也必然包括这些内容。
插件接口:ccPluginInterface,每个插件对象有在ccPluginInterface基础上定义了一个新的接口类class ccStdPluginInterface : public ccPluginInterface
具体的插件则继承自ccStdPluginInterface 累,比如这样,class qMyPlugin : public QObject, public ccStdPluginInterface
主程序对象:class ccMainAppInterface,MainWindow主窗体类实现了ccMainAppInterface接口。
MainWindow::loadPlugins()方法负责插件的调用。(mainwindow.cpp文件中)
void MainWindow::loadPlugins()
{
menuPlugins->setEnabled(false);
menuShadersAndFilters->setEnabled(false);
toolBarPluginTools->setVisible(false);
toolBarGLFilters->setVisible(false); //"static" plugins
foreach (QObject *plugin, QPluginLoader::staticInstances())
dispatchPlugin(plugin); ccConsole::Print(QString("Application path: ")+QCoreApplication::applicationDirPath()); #if defined(Q_OS_MAC)
// plugins are in the bundle
QString path = QCoreApplication::applicationDirPath();
path.remove( "MacOS" );
m_pluginsPath = path + "Plugins/ccPlugins";
#else
//plugins are in bin/plugins
m_pluginsPath = QCoreApplication::applicationDirPath()+QString("/plugins");
#endif ccConsole::Print(QString("Plugins lookup dir.: %1").arg(m_pluginsPath)); QStringList filters;
#if defined(Q_OS_WIN)
filters << "*.dll";
#elif defined(Q_OS_LINUX)
filters << "*.so";
#elif defined(Q_OS_MAC)
filters << "*.dylib";
#endif
QDir pluginsDir(m_pluginsPath);
pluginsDir.setNameFilters(filters);
foreach (QString filename, pluginsDir.entryList(filters))
{
QPluginLoader loader(pluginsDir.absoluteFilePath(filename));
QObject* plugin = loader.instance();
if (plugin)
{
ccConsole::Print(QString("Found new plugin: '%1'").arg(filename));
if (dispatchPlugin(plugin))
{
m_pluginFileNames += filename;
}
else
{
delete plugin;
plugin = ;
ccConsole::Warning("\tUnsupported or invalid plugin type");
}
}
else
{
delete plugin;
plugin = ;
ccConsole::Warning(QString("[Plugin] %1")/*.arg(pluginsDir.absoluteFilePath(filename))*/.arg(loader.errorString()));
}
} if (menuPlugins)
{
menuPlugins->setEnabled(!m_stdPlugins.empty());
} if (toolBarPluginTools->isEnabled())
{
actionDisplayPluginTools->setEnabled(true);
actionDisplayPluginTools->setChecked(true);
}
else
{
//DGM: doesn't work :(
//actionDisplayPluginTools->setChecked(false);
} if (toolBarGLFilters->isEnabled())
{
actionDisplayGLFiltersTools->setEnabled(true);
actionDisplayGLFiltersTools->setChecked(true);
}
else
{
//DGM: doesn't work :(
//actionDisplayGLFiltersTools->setChecked(false);
}
}
主程序在加载插件时会调用插件的setMainAppInterface方法,将主程序参数传入,这样插件就可以获取主程序的内容了。
//! Sets application entry point
/** Called just after plugin creation by qCC
**/
virtual void setMainAppInterface(ccMainAppInterface* app);
3、获取主窗体中的点云图层
在doAction中的代码:
const ccHObject::Container& selectedEntities = m_app->getSelectedEntities();
size_t selNum = selectedEntities.size();
if (selNum!=)
{
m_app->dispToConsole("Select only one cloud!",ccMainAppInterface::ERR_CONSOLE_MESSAGE);
return;
} ccHObject* ent = selectedEntities[];
assert(ent);
if (!ent || !ent->isA(CC_TYPES::POINT_CLOUD))
{
m_app->dispToConsole("Select a real point cloud!",ccMainAppInterface::ERR_CONSOLE_MESSAGE);
return;
} ccPointCloud* pc = static_cast<ccPointCloud*>(ent); //input cloud
unsigned count = pc->size();
bool hasNorms = pc->hasNormals();
CCVector3 bbMin, bbMax;
pc->getBoundingBox(bbMin,bbMax);
const CCVector3d& globalShift = pc->getGlobalShift();
double globalScale = pc->getGlobalScale();