OCC鼠标控制
.h文件
class QtWidgetsApplication1 : public QMainWindow
{
Q_OBJECT
public:
QtWidgetsApplication1(QWidget *parent = nullptr);
~QtWidgetsApplication1();
protected:
void paintEvent(QPaintEvent *event) override;
QPaintEngine *paintEngine() const;
//增加三维场景转换模式
enum Action3d
{
Action3d_Nothing,
Action3d_Panning,//平移
Action3d_Zooming,//缩放
Action3d_Rotation//旋转
};
void mousePressEvent(QMouseEvent *event) override;
void mouseReleaseEvent(QMouseEvent *event) override;
void mouseMoveEvent(QMouseEvent *event) override;
void wheelEvent(QWheelEvent *event) override;
private:
Ui::QtWidgetsApplication1Class ui;
Handle(V3d_Viewer) viewer;
Handle(V3d_View) view;
Handle(AIS_InteractiveContext) context;
Handle(WNT_Window) window;
Standard_Integer m_x;//记录鼠标平移坐标X
Standard_Integer m_y;//记录鼠标平移坐标y
Action3d m_mode;//平移、缩放、旋转
};
.cpp文件
void QtWidgetsApplication1::mousePressEvent(QMouseEvent * event)
{
//平移 鼠标右键+shift
if ((event->buttons()&Qt::RightButton) && (QApplication::keyboardModifiers() == Qt::ShiftModifier))
{
m_mode = Action3d_Panning;
m_x = event->pos().x();
m_y = event->pos().y();
}
else if (event->buttons() & Qt::MidButton)//旋转
{
m_mode = Action3d_Rotation;
//开始旋转视图围绕屏幕轴
view->StartRotation(event->pos().x(), event->pos().y());
}
}
void QtWidgetsApplication1::mouseReleaseEvent(QMouseEvent * event)
{
m_mode = Action3d_Nothing;
}
void QtWidgetsApplication1::mouseMoveEvent(QMouseEvent * event)
{
switch (m_mode)
{
case QtWidgetsApplication1::Action3d_Panning:
view->Pan(event->pos().x() - m_x, m_y - event->pos().y());
m_x = event->pos().x();
m_y = event->pos().y();
break;
case QtWidgetsApplication1::Action3d_Rotation:
view->Rotation(event->pos().x(), event->pos().y());
break;
default:
break;
}
}
//缩放
//Zoom方法通过相邻两侧鼠标位置来判断缩小或者放大,以及缩放程度。鼠标滚轮event->angleDelta().y()会返回数值,以此模拟第二次鼠标位置
void QtWidgetsApplication1::wheelEvent(QWheelEvent * event)
{
//view->Zoom(0, 0, event->angleDelta().y(), 0);//固定点缩放
//跟随鼠标位置进行缩放
view->StartZoomAtPoint(event->pos().x(), event->pos().y());
view->ZoomAtPoint(0, 0, event->angleDelta().y(), 0);
}