当前位置: 首页 > article >正文

opencascade鼠标拖拽框选功能

1.首先在OccView中添加用于显示矩形框的类    

//! rubber rectangle for the mouse selection.
    Handle(AIS_RubberBand) mRectBand;

2.设置框选的属性

mRectBand = new AIS_RubberBand();
//设置属性
mRectBand->SetLineType(Aspect_TOL_SOLID); //设置变宽线型为实线
mRectBand->SetLineColor(Quantity_NOC_RED);

3.在鼠标移动的事件中添加新的事件

void OccView::mouseMoveEvent(QMouseEvent* theEvent)
{
    if ((theEvent->buttons() & Qt::LeftButton) && (theEvent->buttons() & Qt::RightButton))
    {
        //鼠标左右按键一起按,执行平移
        myView->Pan(theEvent->pos().x() - myXmax, myYmax - theEvent->pos().y());//沿视图投影的x和y轴平移视图中心,可用于交互式平移操作
        myXmax = theEvent->x();
        myYmax = theEvent->y();
    }
    else if (theEvent->buttons() & Qt::MiddleButton)
    {
        //鼠标滚轮按键
        if (qApp->keyboardModifiers() == Qt::ShiftModifier) //且按下shift按键
        {
            //鼠标滚轮按键执行平移
            myView->Pan(theEvent->pos().x() - myXmax, myYmax - theEvent->pos().y());
            myXmax = theEvent->x();
            myYmax = theEvent->y();
        }
        else
        {
            myView->Rotation(theEvent->x(), theEvent->y());
        }
    }
    else if (theEvent->buttons() & Qt::LeftButton)
    {
        //绘制选择框
        //获得视口尺寸
        int winViewWidth = 0;
        int winViewHeight = 0;
        myView->Window()->Size(winViewWidth, winViewHeight);
        //设置矩形边界
        mRectBand->SetRectangle(myXmax, winViewHeight - myYmax, theEvent->pos().x(), winViewHeight - theEvent->pos().y());
        //更新显示
        if (myContext->IsDisplayed(mRectBand))
        {
            myContext->Redisplay(mRectBand, true);
        }
        else
        {
            myContext->Display(mRectBand, true);
        }
        myContext->SelectRectangle(Graphic3d_Vec2i(myXmax, myYmax), Graphic3d_Vec2i(theEvent->pos().x(), theEvent->pos().y()), myView);
    }
    else
    {
        //将鼠标位置传递到交互环境
        myContext->MoveTo(theEvent->pos().x(), theEvent->pos().y(), myView, Standard_Boolean(true));
    }
}

4.在鼠标释放事件中清空框选

void OccView::mouseReleaseEvent(QMouseEvent* theEvent)
{
    std::cout << myContext->IsDisplayed(mRectBand) << std::endl;
    if (myContext->IsDisplayed(mRectBand))
    {
        myContext->Remove(mRectBand, Standard_True);
        myView->Update();
    }

    //将鼠标位置传递到交互环境
    myContext->MoveTo(theEvent->pos().x(), theEvent->pos().y(), myView, Standard_Boolean(true));
}

效果图:


http://www.kler.cn/news/340957.html

相关文章:

  • Java | Leetcode Java题解之第468题验证IP地址
  • Windows10的MinGW安装和VS Code配置C/C++编译环境
  • mermaid 图表相关
  • 408算法题leetcode--第29天
  • 如何让你的Mac右键菜单栏更加的丰富多样
  • Vue vben admin开源库中table组件tips
  • 数据结构进阶:二叉搜索树_C++
  • YOLO11 实例分割模型做行人分割
  • 【10086网上营业厅-注册/登录安全分析报告】
  • 【网络篇】计算机网络——应用层详述(笔记)
  • 华为S5735交换机配置脚本
  • vue 的属性绑定
  • APP的命令和monkey压力测试
  • js基础速成14-错误处理
  • 鸿蒙HarmonyOS中Image图片组件以及HarmonyOs图标库完全解析
  • 社工字典生成工具 —— CeWL 使用手册
  • RabbitMQ初识
  • JavaScript-API(倒计时的实现)
  • 【漏洞复现】宏景-HCM KhFieldTree Sql注入漏洞
  • MySQL 是否支持 XML