SuperMap IObjects C++组件学习笔记(一) - Hello iObjects C++

Hello iObjects C++

之前写了在qt下iObjects C++组件的环境搭建,现在可以开始使用iObjects C++进行开发了,作为一个尊重传统的程序员,我们还是从Hello iObjects开始吧。

一、纯后台处理的程序

假设我们暂时不需要UI,只希望让iObjects组件执行他的功能,比如打开一个工作空间,加载工作空间中的地图,由于没有UI,为了验证我们的地图加载成功,我们将其以图片格式输出出来。
首先创建一个类,完成我们想要的功能:
Hello.h

#ifndef HELLO_H
#define HELLO_H

#include <QObject>
#include <QWidget>
#include <QPaintEvent>
#include <QResizeEvent>

#include "Stream/ugdefs.h"
#include "Engine/ugeng.h"

namespace UGC
{
class UGWorkspace;
class UGImage;
class UGMap;
}
using namespace UGC;

class Hello
{
    Q_OBJECT
public:
    explicit Hello();
    ~Hello();

    void openMap();
    void outPutMapToPic();

private:
    //打开一张地图所需要的类
    UGWorkspace* m_pWorkspace;
    UGImage* m_pMapImage;
    UGImage* m_pOldImage;
    UGMap *m_pMap;

    int m_width;
    int m_height;
    bool m_invalidate;

private:
    void init();
};

#endif // HELLO_H

hello.cpp

#include "Hello.h"
#include "Workspace/UGWorkspace.h"
#include "Graphics/UGImage.h"
#include "Map/UGMap.h"

Hello::Hello(void)
{
    m_width = 800;
    m_height = 600;
    m_pMapImage = NULL; 
    //初始化
    init();
    //根据所需实例,使用对应方法
    openMap();
    // 调用输出地图的方法
    outPutMapToPic();
}

Hello::~Hello(void)
{
}

void Hello::init()
{
    //初始化工作空间
    m_pWorkspace = new UGWorkspace;

    //初始化一个空的地图,用于图像的输出
    m_pMap = new UGMap;
    m_pMap->SetWorkspace(m_pWorkspace);
    m_pMap->SetLineSmoothingMode(false);//设置关闭地图反走样
}

void Hello::openMap()
{
    //打开工作空间
    UGString workspacePath = _U("../Data/HelloiObjects.smwu");
    UGbool bl = m_pWorkspace->Open(workspacePath);
    // 获取到工作空间中存储的第张地图的名字
    UGString mapName = _U("HelloIObjects");

    // 打开指定的名称的地图
    UGMap *pMap = m_pMap;
    pMap->Open(mapName);
}

void MapExample::outPutMapToPic()
{
    UGMap *pMap = m_pMap;
    //调用Map类的outPut方法,指定输出位置和输出类型即可将图像输出
    //这里的输出位置是我的桌面
    pMap->OutputMapToFile(NULL,UGSize(m_width, m_height),_U("C:\\Users\\Ray\\Desktop\\outputImage.png"),UGint(UGFileType::PNG));
}

编写一个程序调用该类即可即可在桌面看法到输出的地图

二、将地图展示到窗体应用中

2.1创建自己的MapControl类

因为iObjects C++组件是一个基础的GIS组件,为了利用该组件开发可视化的GIS应用,我们需要一个窗体应用库,这也是为什么从一开始搭建环境就是在Qt下的原因,我们需要Qt的ui库来实现我们的人机交互部分。
MapControl,顾名思义,就是对地图进行展示和控制的控件,我们继承于QWidget创建我们的UGMapControl类:

class UGMapControl : public QWidget

然后我们将前面我们写的后台处理的Hello代码迁移到我们的UGMapControl类中,并添加用于显示的方法:
首先为了对地图进行控制,我们将之前声明的

UGMap *m_pMap; 替换为 UGMapEditorWnd* m_pMapEditorWnd;
以m_pMapEditorWnd->m_mapWnd.m_Map代替直接new的Map

然后重写继承自QWidget类的绘制事件和窗口大小改变事件

    virtual void paintEvent( QPaintEvent* event );
    virtual void  resizeEvent ( QResizeEvent* event );

完整代码如下:

ugmapcontrol.h

#ifndef UGMAPCONTROL_H
#define UGMAPCONTROL_H

#include <QObject>
#include <QWidget>
#include <QPaintEvent>
#include <QResizeEvent>

#include "Stream/ugdefs.h"
#include "Engine/ugeng.h"

namespace UGC
{
class UGWorkspace;
class UGMapEditorWnd;
class UGImage;
class UGGraphics;
class UGDataset;
class UGDataSource;
class UGLayer;
}
using namespace UGC;

class UGMapControl : public QWidget
{
    Q_OBJECT
public:
    explicit UGMapControl(QWidget* parent = 0);
    ~UGMapControl();

    //重写Qwidget的绘制事件
    virtual void paintEvent( QPaintEvent* event );
    //重写QWidget的窗口改变事件
    virtual void  resizeEvent ( QResizeEvent* event );

    void openMap();

private:
    UGWorkspace* m_pWorkspace;
    UGMapEditorWnd* m_pMapEditorWnd;
    UGImage* m_pMapImage;
    UGImage* m_pOldImage;

    QImage* m_pQimage;

    int m_width;
    int m_height;
    bool m_invalidate;

private:
    void init();
    void CreateUGGrpaphics(UGGraphics* &pGraphics);
    void ReleaseUGGraphics(UGGraphics* &pGraphics);
    void PaintToQPainter();
    void Invalidate();
};

#endif // UGMAPCONTROL_H

ugmapcontrol.cpp

#include "ugmapcontrol.h"
#include "MapEditor/UGMapEditorWnd.h"//打开地图用到
#include "Graphics/UGGraphicsManager.h"//绘制图像用到
#include "Engine/UGDataSourceManager.h"
#include "Engine/UGDataSources.h"
#include "Engine/UGDataSource.h"
#include "Engine/UGDataset.h"
#include "Engine/UGDatasetVector.h"

#include <QPainter>

UGMapControl::UGMapControl(QWidget *parent)
{
    m_width = parent->width();
    m_height = parent->height();
    m_pMapImage = NULL;
    init();
    //openMap();
}

UGMapControl::~UGMapControl()
{

}

void UGMapControl::paintEvent(QPaintEvent *event)
{
    if (m_invalidate)
    {
        m_invalidate = FALSE;

        UGGraphics* pGraphics = NULL;
        CreateUGGrpaphics(pGraphics);

        UGRect rcInvalid(0, 0, m_width, m_height);
        m_pMapEditorWnd->OnDraw(pGraphics, rcInvalid, rcInvalid, false, false);

        ReleaseUGGraphics(pGraphics);
    }

    PaintToQPainter();
}

void UGMapControl::resizeEvent(QResizeEvent *event)
{
    QWidget::resizeEvent(event);
    m_width = this->width();
    m_height = this->height();

    m_pMapEditorWnd->OnSize(NULL,UGRect(0, 0, m_width, m_height));

    delete m_pMapImage;
    m_pMapImage = UGGraphicsManager::NewImage();
    m_pMapImage->CreateCompatibleImage(NULL, m_width, m_height);
    this->Invalidate();
}

void UGMapControl::openMap()
{
    UGString workspacePath = _U("../Data/HelloiObjects.smwu");
    UGbool bl = m_pWorkspace->Open(workspacePath);
    // 获取到工作空间中存储的第张地图的名字
    UGString mapName = _U("HelloIObjects");//m_pWorkspace->m_MapStorages.GetNameAt(0);

    // 打开指定的名称的地图
    UGMap *pMap = (UGMap*)&(m_pMapEditorWnd->m_mapWnd.m_Map);
    pMap->Open(mapName);

    // 全副显示地图
    m_pMapEditorWnd->ViewEntire();
    m_pMapEditorWnd->Refresh();

    Invalidate();
}

void UGMapControl::init()
{
    UGGraphicsManager::SetCurGraphicsType(UGGraphics::GT_Windows);
    //初始化地图控件
    m_pMapEditorWnd = new UGMapEditorWnd();
    m_pMapEditorWnd->Initialize();
    m_pMapEditorWnd->SetUserAction(UGDrawParamaters::uaPointModeSelect);


    //初始化工作空间
    m_pWorkspace = new UGWorkspace;


    //将工作空间绑定到地图控件的地图渲染窗口中的地图上,使地图控件可以对该工作空间上的地图进行控制
    UGMap *pMap = (UGMap*)&(m_pMapEditorWnd->m_mapWnd.m_Map);
    pMap->SetWorkspace(m_pWorkspace);

    pMap->SetLineSmoothingMode(false);//设置关闭地图反走样
}

void UGMapControl::CreateUGGrpaphics(UGGraphics *&pGraphics)
{
    pGraphics = UGC::UGGraphicsManager::NewGraphics();
    pGraphics->CreateCompatibleGraphics(NULL);

    m_pOldImage = pGraphics->SelectImage(m_pMapImage);
}

void UGMapControl::ReleaseUGGraphics(UGGraphics *&pGraphics)
{
    pGraphics->SelectImage(m_pOldImage);
    delete pGraphics;
    pGraphics = NULL;
}

void UGMapControl::PaintToQPainter()
{
    m_pQimage = new QImage((UGC::UGbyte*)m_pMapImage->GetColors(), m_width, m_height, QImage::Format_ARGB32);

    QPainter paint;
    paint.begin(this);
    QColor backGound(255, 255, 255);
    paint.fillRect(0, 0, m_width, m_height, backGound);
    paint.drawPixmap( QRect(0, 0, m_width, m_height),QPixmap::fromImage(m_pQimage->scaled(size())));
    //paint.drawImage(QRectF(0, 0, m_width, m_height), *m_pQimage);
    paint.end();

    if (m_pQimage != NULL)
    {
        delete m_pQimage;
        m_pQimage = NULL;
    }
}

void UGMapControl::Invalidate()
{
    m_invalidate = TRUE;
    this->update();
}

你可能感兴趣的:(C++)