OSGEarth嵌入Qt的QGraphicsView框架(一)

近期在学习OSGEarth,看example,自己结合Qt写了一些小例子,特此记录一下。

首先,考虑到OE(OSGEarth)嵌入到QGraphicsView中,首先收到按键、鼠标事件的就是QGraphicsView,因此,我们需要先把这一庞大的事件进行处理。
代码如下:eventadapter .h

class EventAdapter : public QGraphicsView
{
public:
    EventAdapter();
    ~EventAdapter();

protected:
    // 处理按键事件
    virtual void keyPressEvent(QKeyEvent* event);
    virtual void keyReleaseEvent(QKeyEvent* event);

    // 处理鼠标单击事件
    virtual void mousePressEvent(QMouseEvent* event);
    virtual void mouseReleaseEvent(QMouseEvent* event);

    // 处理鼠标双击事件
    virtual void mouseDoubleClickEvent(QMouseEvent* event);
    virtual void mouseMoveEvent(QMouseEvent* event);

    // 处理鼠标中轮滑动事件
    virtual void wheelEvent(QWheelEvent* event);

    // 处理窗口大小改变事件
    virtual void resizeEvent(QResizeEvent *event);

    // 处理鼠标移动事件
    virtual void moveEvent(QMoveEvent* event);

    // timerEvent事件
    virtual void timerEvent(QTimerEvent *event);

private:
    void setKeyboardModifiers(QInputEvent* event);

protected:
    osgViewer::GraphicsWindow* m_pGraphicsWindow;
};

实现文件如下:eventadapter .cpp

void EventAdapter::keyPressEvent(QKeyEvent* event)
{
    QPoint pos = QCursor::pos();
    QList*> listItems = items(mapToScene(pos.x(), pos.y()).toPoint());
    if (listItems.size() <= 0)
    {
        setKeyboardModifiers(event);
        m_pGraphicsWindow->getEventQueue()->keyPress(
            (osgGA::GUIEventAdapter::KeySymbol)*(event->text().toLatin1().data()));
    }
    QGraphicsView::keyPressEvent(event);
}

void EventAdapter::keyReleaseEvent(QKeyEvent* event)
{
    setKeyboardModifiers(event);
    m_pGraphicsWindow->getEventQueue()->keyRelease(
        (osgGA::GUIEventAdapter::KeySymbol)*(event->text().toLatin1().data()));

    QGraphicsView::keyReleaseEvent(event);
}

void EventAdapter::mousePressEvent(QMouseEvent* event)
{
    QPoint pos = event->pos();
    QList*> listItems = items(mapToScene(pos.x(), pos.y()).toPoint());
    if (listItems.size() > 0)
    {
        QGraphicsView::mousePressEvent(event);
    }
    else
    {
        int button = 0;
        switch (event->button())
        {
        case Qt::LeftButton: button = 1; break;
        case Qt::MidButton: button = 2; break;
        case Qt::RightButton: button = 3; break;
        case Qt::NoButton: button = 0; break;
        default: button = 0; break;
        }
        setKeyboardModifiers(event);
        m_pGraphicsWindow->getEventQueue()->mouseButtonPress(event->x(), event->y(), button);
    }
}

void EventAdapter::mouseReleaseEvent(QMouseEvent* event)
{
    int button = 0;
    switch (event->button())
    {
    case Qt::LeftButton: button = 1; break;
    case Qt::MidButton: button = 2; break;
    case Qt::RightButton: button = 3; break;
    case Qt::NoButton: button = 0; break;
    default: button = 0; break;
    }
    setKeyboardModifiers(event);
    m_pGraphicsWindow->getEventQueue()->mouseButtonRelease(event->x(), event->y(), button);

    QGraphicsView::mouseReleaseEvent(event);
}

void EventAdapter::mouseDoubleClickEvent(QMouseEvent* event)
{
    QPoint pos = QCursor::pos();
    QList*> listItems = items(mapToScene(pos.x(), pos.y()).toPoint());
    if (listItems.size() > 0)
    {
        return;
    }
    int button = 0;
    switch (event->button())
    {
    case Qt::LeftButton: button = 1; break;
    case Qt::MidButton: button = 2; break;
    case Qt::RightButton: button = 3; break;
    case Qt::NoButton: button = 0; break;
    default: button = 0; break;
    }
    setKeyboardModifiers(event);
    m_pGraphicsWindow->getEventQueue()->mouseDoubleButtonPress(event->x(), event->y(), button);

    QGraphicsView::mouseDoubleClickEvent(event);
}

void EventAdapter::mouseMoveEvent(QMouseEvent* event)
{
    setKeyboardModifiers(event);
    m_pGraphicsWindow->getEventQueue()->mouseMotion(event->x(), event->y());
    QGraphicsView::mouseMoveEvent(event);
}

void EventAdapter::wheelEvent(QWheelEvent* event)
{
    setKeyboardModifiers(event);
    m_pGraphicsWindow->getEventQueue()->mouseScroll(
        event->orientation() == Qt::Vertical ?
        (event->delta() > 0 ? osgGA::GUIEventAdapter::SCROLL_UP : osgGA::GUIEventAdapter::SCROLL_DOWN) :
        (event->delta() > 0 ? osgGA::GUIEventAdapter::SCROLL_LEFT : osgGA::GUIEventAdapter::SCROLL_RIGHT));
    QGraphicsView::wheelEvent(event);
}

void EventAdapter::resizeEvent(QResizeEvent *event)
{
    if (scene())
    {
        scene()->setSceneRect(QRect(QPoint(0, 0), event->size()));
    }

    const QSize& size = event->size();
    m_pGraphicsWindow->resized(x(), y(), size.width(), size.height());
    m_pGraphicsWindow->getEventQueue()->windowResize(x(), y(), size.width(), size.height());
    m_pGraphicsWindow->requestRedraw();

    const QSize& oldSize = event->oldSize();
    int oldWidth = oldSize.width();
    int oldHeight = oldSize.height();

    int newWidth = size.width();
    int newHeight = size.height();

    double widthChangeRatio = double(newWidth) / double(oldWidth);
    double heigtChangeRatio = double(newHeight) / double(oldHeight);
    double aspectRatioChange = widthChangeRatio / heigtChangeRatio;
    QGraphicsView::resizeEvent(event);
}

void EventAdapter::moveEvent(QMoveEvent* event)
{
    const QPoint& pos = event->pos();
    m_pGraphicsWindow->resized(pos.x(), pos.y(), width(), height());
    m_pGraphicsWindow->getEventQueue()->windowResize(pos.x(), pos.y(), width(), height());

    QGraphicsView::moveEvent(event);
}

void EventAdapter::timerEvent(QTimerEvent *event)
{
    scene()->update();
}

另,若需要触屏操作,则需要重新实现虚函数virtual bool viewportEvent(QEvent* event);

你可能感兴趣的:(C++/Qt,OSG)