1. 头文件代码
#ifndef QGIS_HELLO_WORLD_H
#define QGIS_HELLO_WORLD_H
#include "qgisplugin.h"
#include "qgsmapcanvas.h"
#include "qgisinterface.h"
#include "qgsvectorlayer.h"
#include "qgsmessagelog.h"
#include <iostream>
#include <QAction>
#include <QList>
#include "qgsmaplayer.h"
#include <qgslayertreeview.h>
#include <qgslayertreemapcanvasbridge.h>
class HelloWorldPlugin : public QObject, public QgisPlugin
{
Q_OBJECT
public:
static const QString s_name, s_description, s_category, s_version, s_icon;
static const QgisPlugin::PluginType s_type;
HelloWorldPlugin(QgisInterface* qgis_if) : QgisPlugin(s_name, s_description, s_category, s_version, s_type), m_qgis_if(qgis_if)
{
}
// virtual methods from QgisPlugin
virtual void initGui();
virtual void unload()
{
std::cout << "HelloWorldPlugin::unload" << std::endl;
}
public slots:
void run(bool);
void addVectorLayers(); // 添加矢量图层
//void addRasterLayers(); // 添加栅格图层
private:
QgisInterface* m_qgis_if;
QAction* m_action;
QgsMapCanvas* m_mapCanvas;
QList<QgsMapLayer*> m_mapLayerSet;
//QList<QgsMapCanvasLayer> m_mapLayerSet;
//QList<int> m_a;
QgsLayerTreeView* m_layerTreeView;
QgsLayerTreeMapCanvasBridge* m_layerTreeCanvasBridge;
void initLayerTreeView();
};
#endif // #ifndef QGIS_HELLO_WORLD_H
2. cpp 文件
#include "qgis_hello_world.h"
#include <QMessageBox>
#include <qtextcodec.h>
#include <QFileDialog>
#include <qgsmaplayerstore.h>
#include <qgslogger.h>
#include <qgslayertreemodel.h>
#include <qgsproject.h>
#include <QGridLayout>
const QString HelloWorldPlugin::s_name = QObject::tr("Hello World Plugin");
const QString HelloWorldPlugin::s_description = QObject::tr("Sample Plugin");
const QString HelloWorldPlugin::s_category = QObject::tr("Plugins");
const QString HelloWorldPlugin::s_version = QObject::tr("Version 5.2.0");
const QString HelloWorldPlugin::s_icon = "";
const QgisPlugin::PluginType HelloWorldPlugin::s_type = QgisPlugin::UI;
QGISEXTERN QgisPlugin* classFactory(QgisInterface* qgis_if)
{
std::cout << "::classFactory" << std::endl;
return new HelloWorldPlugin(qgis_if);
}
QGISEXTERN QString name()
{
std::cout << "::name" << std::endl;
return HelloWorldPlugin::s_name;
}
QGISEXTERN QString description()
{
std::cout << "::description" << std::endl;
return HelloWorldPlugin::s_description;
}
QGISEXTERN QString category()
{
std::cout << "::category" << std::endl;
return HelloWorldPlugin::s_category;
}
QGISEXTERN int type()
{
std::cout << "::type" << std::endl;
return HelloWorldPlugin::s_type;
}
QGISEXTERN QString version()
{
std::cout << "::version" << std::endl;
return HelloWorldPlugin::s_version;
}
QGISEXTERN QString icon()
{
std::cout << "::icon" << std::endl;
return HelloWorldPlugin::s_icon;
}
QGISEXTERN void unload(QgisPlugin* plugin)
{
std::cout << "::unload" << std::endl;
delete plugin;
}
/*virtual*/ void HelloWorldPlugin::initGui()
{
std::cout << "HelloWorldPlugin::initGui" << std::endl;
// add an action to the menu
m_action = new QAction(QIcon("" ), tr("Hello World121212"), this);
m_action->setText(QString::fromLocal8Bit("选择矢量图层"));
m_action->setWhatsThis(tr("Draw on the map canvas."));
connect(m_action, SIGNAL(triggered(bool)), this, SLOT(run(bool)));
m_qgis_if->addRasterToolBarIcon(m_action);
m_qgis_if->addPluginToMenu(tr("&Hello World"), m_action);
m_mapCanvas = new QgsMapCanvas();
m_mapCanvas->enableAntiAliasing(true);
m_mapCanvas->setCanvasColor(QColor(255, 255, 255));
m_mapCanvas->setVisible(false);
m_layerTreeView = new QgsLayerTreeView();
//initLayerTreeView();
//! 布局
/*QWidget* centralWidget = new QWidget();
QGridLayout* centralLayout = new QGridLayout(centralWidget);
centralLayout->addWidget(m_mapCanvas, 0, 0, 1, 1);
centralLayout->addWidget(m_layerTreeView, 0, 1, 1, 1);*/
}
void HelloWorldPlugin::addVectorLayers() {
QString filename = QFileDialog::getOpenFileName(NULL, QString::fromLocal8Bit("打开矢量图层"), "", "*.shp");
QStringList temp = filename.split(QDir::separator());
QString basename = temp.at(temp.size() - 1);
QgsVectorLayer* vecLayer = new QgsVectorLayer(filename, basename, "ogr");
if (!vecLayer->isValid())
{
QMessageBox::critical(NULL, "error", "layer is invalid");
return;
}
m_mapCanvas->setVisible(true);
//QgsMapLayerRegistry::instance()->addMapLayer(vecLayer);
(new QgsMapLayerStore())->addMapLayer(vecLayer);
QgsLogger::debug(QString::fromLocal8Bit("调试信息"));
m_mapLayerSet.append(vecLayer);
m_mapCanvas->setExtent(vecLayer->extent());
//m_mapCanvas->setLayers(m_mapLayerSet);
m_mapCanvas->setVisible(true);
m_mapCanvas->freeze(false);
m_mapCanvas->refresh();
}
void HelloWorldPlugin::run(bool checked)
{
addVectorLayers();
}
void HelloWorldPlugin::initLayerTreeView()
{
QgsLayerTreeModel* model = new QgsLayerTreeModel(QgsProject::instance()->layerTreeRoot(), NULL);
model->setFlag(QgsLayerTreeModel::AllowNodeRename);
model->setFlag(QgsLayerTreeModel::AllowNodeReorder);
model->setFlag(QgsLayerTreeModel::AllowNodeChangeVisibility);
model->setFlag(QgsLayerTreeModel::ShowLegendAsTree);
model->setAutoCollapseLegendNodes(10);
m_layerTreeView->setModel(model);
// 连接地图画布和图层管理器
m_layerTreeCanvasBridge = new QgsLayerTreeMapCanvasBridge(QgsProject::instance()->layerTreeRoot(), m_mapCanvas, NULL);
connect(QgsProject::instance(), SIGNAL(writeProject(QDomDocument&)), m_layerTreeCanvasBridge, SLOT(writeProject(QDomDocument&)));
connect(QgsProject::instance(), SIGNAL(readProject(QDomDocument)), m_layerTreeCanvasBridge, SLOT(readProject(QDomDocument)));
}
3. qgis 插件模板
c++ 模板
python 模板
发表评论
评论列表, 共 0 条评论