kestiny的专栏

抬头望天,脚踏实地

OSG点选事件的基类

由于我的例子中有多处需要用到点选操作,我不想对一段代码到处复制粘贴,所以我写了一基类对点选操作进行管理。
其功能有:
1. 管理点选事件的添加和删除操作
2. 管理点选成功后Qt信号的发送
话不多数,上代码。
handleadapter.h

class HandleAdapter : public QObject
{
    Q_OBJECT

public:
    HandleAdapter(GraphicsView* view, QObject* parent = NULL);
    ~HandleAdapter();

    void benginHandle();
    void endHandle();

private slots:
    // 虚函数的实现,有其子类来
    virtual void slotPicked(osg::Vec3d pos){}
    virtual void slotMoveing(osg::Vec3d pos){}

    virtual void slotPickedXYZ(osg::Vec3d pos){}
    virtual void slotMoveingXYZ(osg::Vec3d pos){}

    virtual void slotRightHandle(){}

public:
    GraphicsView* m_pMap3D;
    osg::Group* m_pLayerGroup;
    PickHandle* m_pEventHandle;
};

功能实现如handleadapter.cpp

HandleAdapter::HandleAdapter(GraphicsView* view, QObject* parent/* = NULL*/)
    : m_pMap3D(view), QObject(parent)
{
    m_pLayerGroup = new osg::Group;
    m_pMap3D->getRoot()->addChild(m_pLayerGroup);

    m_pEventHandle = new PickHandle(m_pMap3D->getSRS());
}

HandleAdapter::~HandleAdapter()
{

}

void HandleAdapter::benginHandle()
{
    m_pMap3D->getOSGViewer()->addEventHandler(m_pEventHandle);
    connect(m_pEventHandle, SIGNAL(signalPicked(osg::Vec3d)), this, SLOT(slotPicked(osg::Vec3d)));
    connect(m_pEventHandle, SIGNAL(signalMoving(osg::Vec3d)), this, SLOT(slotMoveing(osg::Vec3d)));
    connect(m_pEventHandle, SIGNAL(signalRightPicked()), this, SLOT(slotRightHandle()));
    connect(m_pEventHandle, SIGNAL(signalPickedXYZ(osg::Vec3d)), this, SLOT(slotPickedXYZ(osg::Vec3d)));
    connect(m_pEventHandle, SIGNAL(signalMovingXYZ(osg::Vec3d)), this, SLOT(slotMoveingXYZ(osg::Vec3d)));
}

void HandleAdapter::endHandle()
{
    disconnect(m_pEventHandle, SIGNAL(signalPicked(osg::Vec3d)), this, SLOT(slotPicked(osg::Vec3d)));
    disconnect(m_pEventHandle, SIGNAL(signalMoving(osg::Vec3d)), this, SLOT(slotMoveing(osg::Vec3d)));
    disconnect(m_pEventHandle, SIGNAL(signalRightPicked()), this, SLOT(slotRightHandle()));
    disconnect(m_pEventHandle, SIGNAL(signalPickedXYZ(osg::Vec3d)), this, SLOT(slotPickedXYZ(osg::Vec3d)));
    disconnect(m_pEventHandle, SIGNAL(signalMovingXYZ(osg::Vec3d)), this, SLOT(slotMoveingXYZ(osg::Vec3d)));
    m_pMap3D->getOSGViewer()->removeEventHandler(m_pEventHandle);
}

好了,点选操作的基本功能就完善了,下面就可以开始进行功能性操作了。请看下一篇,画线。

阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/chlk118/article/details/46821659
个人分类: C++/Qt OSG
上一篇OSG获取鼠标点选事件的位置
下一篇OSGEarth绘制Geometry:画线(一)
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭