CloudCompare2.11.0插件开发
- 一、UI文件生成
- 二、代码文件配置
- 三、资源文件配置
- 四、CMake文件配置
- 五、相关代码
- 六、编译安装效果
一、UI文件生成
1、选择Qt自带的界面设计软件Designer,因为后面编译CloudCompare插件所使用的编译器是VS2019,故选择的是MSVC 2019 64位版的
2、新建一个UI对话框界面,即Dialog对话框
3、窗口添加组件
创建好的界面如下,接下来给这个窗口添加一些我们需要的组件
添加好组件后的窗口如下,将该UI文件保存到一个文件路径下,后面需要用到这个文件
二、代码文件配置
1、将CloudCompare源码中自带的插件例子复制一份,然后改成UIPlugin(也可改成其他名字)
2、在UIPlugin文件夹中新建一个src文件夹和ui文件夹,其中src文件夹放的是源代码文件,ui文件夹下放的是前面保存的UI文件,文件结构图如下
3、在src文件夹下新建一个UIPluginParameters.cpp文件和UIPluginParameters.h文件
三、资源文件配置
1、UIPlugin.qrc文件配置
2、info.json文件配置
四、CMake文件配置
1、在src文件夹下新建一个CMakeLists.txt文件,内容配置如下
2、在ui文件夹下新建一个CMakeLists.txt文件,内容配置如下
3、UIPlugin文件夹下的CMakeLists.txt文件配置
4、在UIPlugin文件夹所在路径下的CMakeLists.txt文件中添加如下内容
5、在CMake中先点击Configure配置文件,生成UIPlugin选项,然后在Ungrouped Entries中勾选中UIPlugin,如果在Ungrouped Entries中未找到自己写的插件的话,在PLUGIN中或许能找到,勾选后点击Configure
6、点击Generate生成解决方案,点击Open Project打开解决方案
五、相关代码
1、打开解决方案后,文件结构如下
2、先对UIPlugin.h和UIPlugin.cpp文件进行如下修改
3、右键UIPlugin.ui文件,点击编译,生成ui_UIPlugin.h文件
4、在UIPluginParameters.h和UIPluginParameters.cpp文件中写入以下内容
UIPluginParameters.h文件
#ifndef UIPLUGIN_PARAMETERS_HEADER
#define UIPLUGIN_PARAMETERS_HEADER
#include <ui_UIPlugin.h>
class UIPluginParameters :public QDialog
{
Q_OBJECT
public:
explicit UIPluginParameters(QWidget* parent = nullptr);
~UIPluginParameters();
private:
Ui::Dialog* ui;
private slots:
void doConfirmBtn();
void doCancelBtn();
};
#endif
注意:如果需要使用信号槽功能的话,请一定要加上Q_OBJECT,否则编译会报错!!!
UIPluginParameters.cpp文件
#include "UIPluginParameters.h"
#include <QMessageBox>
UIPluginParameters::UIPluginParameters(QWidget* parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(doConfirmBtn()));
connect(ui->pushButton_2, SIGNAL(clicked()), this, SLOT(doCancelBtn()));
}
UIPluginParameters::~UIPluginParameters()
{
delete ui;
}
void UIPluginParameters::doConfirmBtn()
{
QMessageBox::information(this, "Tips", QString("The parameter value entered is %1").arg(ui->lineEdit->text()), QMessageBox::Ok);
this->close();
}
void UIPluginParameters::doCancelBtn()
{
QMessageBox::information(this, "Tips", QString("The cancel button has been clicked."), QMessageBox::Ok);
this->close();
}
注意:① connect中的信号函数和槽函数一定要加括号,否则按钮点击将会没有任何反应
② QString中的 %1是占位符,后面的arg中的变量为参数,若直接在QString中使用参数,则弹窗无法正确显示内容
5、在UIPlugin.cpp中添加一个头文件,同时作以下修改
//##########################################################################
//# #
//# CLOUDCOMPARE PLUGIN: ExamplePlugin #
//# #
//# This program is free software; you can redistribute it and/or modify #
//# it under the terms of the GNU General Public License as published by #
//# the Free Software Foundation; version 2 of the License. #
//# #
//# This program is distributed in the hope that it will be useful, #
//# but WITHOUT ANY WARRANTY; without even the implied warranty of #
//# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
//# GNU General Public License for more details. #
//# #
//# COPYRIGHT: XXX #
//# #
//##########################################################################
// First:
// Replace all occurrences of 'ExamplePlugin' by your own plugin class name in this file.
// This includes the resource path to info.json in the constructor.
// Second:
// Open ExamplePlugin.qrc, change the "prefix" and the icon filename for your plugin.
// Change the name of the file to <yourPluginName>.qrc
// Third:
// Open the info.json file and fill in the information about the plugin.
// "type" should be one of: "Standard", "GL", or "I/O" (required)
// "name" is the name of the plugin (required)
// "icon" is the Qt resource path to the plugin's icon (from the .qrc file)
// "description" is used as a tootip if the plugin has actions and is displayed in the plugin dialog
// "authors", "maintainers", and "references" show up in the plugin dialog as well
#include <QtGui>
#include "UIPlugin.h"
#include "UIPluginParameters.h"
// Default constructor:
// - pass the Qt resource path to the info.json file (from <yourPluginName>.qrc file)
// - constructor should mainly be used to initialize actions and other members
ExamplePlugin::ExamplePlugin( QObject *parent )
: QObject( parent )
, ccStdPluginInterface( ":/CC/plugin/UIPlugin/info.json" )
, m_action( nullptr )
{
}
// This method should enable or disable your plugin actions
// depending on the currently selected entities ('selectedEntities').
void ExamplePlugin::onNewSelection( const ccHObject::Container &selectedEntities )
{
if ( m_action == nullptr )
{
return;
}
// If you need to check for a specific type of object, you can use the methods
// in ccHObjectCaster.h or loop and check the objects' classIDs like this:
//
// for ( ccHObject *object : selectedEntities )
// {
// if ( object->getClassID() == CC_TYPES::VIEWPORT_2D_OBJECT )
// {
// // ... do something with the viewports
// }
// }
// For example - only enable our action if something is selected.
//m_action->setEnabled( !selectedEntities.empty() );
}
// This method returns all the 'actions' your plugin can perform.
// getActions() will be called only once, when plugin is loaded.
QList<QAction *> ExamplePlugin::getActions()
{
// default action (if it has not been already created, this is the moment to do it)
if ( !m_action )
{
// Here we use the default plugin name, description, and icon,
// but each action should have its own.
m_action = new QAction( getName(), this );
m_action->setToolTip( getDescription() );
m_action->setIcon( getIcon() );
// Connect appropriate signal
connect( m_action, &QAction::triggered, this, &ExamplePlugin::doAction );
}
return { m_action };
}
// This is an example of an action's method called when the corresponding action
// is triggered (i.e. the corresponding icon or menu entry is clicked in CC's
// main interface). You can access most of CC's components (database,
// 3D views, console, etc.) via the 'm_app' variable (see the ccMainAppInterface
// class in ccMainAppInterface.h).
void ExamplePlugin::doAction()
{
if ( m_app == nullptr )
{
// m_app should have already been initialized by CC when plugin is loaded
Q_ASSERT( false );
return;
}
/*** HERE STARTS THE ACTION ***/
// Put your code here
// --> you may want to start by asking for parameters (with a custom dialog, etc.)
UIPluginParameters uipp;
if (!uipp.exec())
{
return;
}
// This is how you can output messages
// Display a standard message in the console
//m_app->dispToConsole( "[ExamplePlugin] Hello world!", ccMainAppInterface::STD_CONSOLE_MESSAGE );
// Display a warning message in the console
//m_app->dispToConsole( "[ExamplePlugin] Warning: example plugin shouldn't be used as is", ccMainAppInterface::WRN_CONSOLE_MESSAGE );
// Display an error message in the console AND pop-up an error box
//m_app->dispToConsole( "Example plugin shouldn't be used - it doesn't do anything!", ccMainAppInterface::ERR_CONSOLE_MESSAGE );
/*** HERE ENDS THE ACTION ***/
}
6、右键ALL_BUILD解决方案,点击生成,对所有项目进行编译,之后右键INSTALL解决方案,点击生成,安装CloudCompare
六、编译安装效果