WindowAppUiNavView.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. #include "WindowAppUiNavView.h"
  2. #include "CommonDraw.h"
  3. //
  4. //// 缩略图界面的宽高比
  5. //#define HEIGHT_SCALE 0.75
  6. // 固定UI缩略图高度
  7. #define FIXED_HEIGHT 200
  8. WindowAppUiNavView::WindowAppUiNavView(QWidget*parent)
  9. : QGraphicsView(parent)
  10. , m_mainView(nullptr)
  11. , m_mouseDown(false)
  12. , m_viewportRegion(this->rect())
  13. {
  14. // 初始化界面风格
  15. initStyle();
  16. }
  17. WindowAppUiNavView::~WindowAppUiNavView()
  18. {
  19. }
  20. /// <summary>
  21. /// 初始化界面风格
  22. /// </summary>
  23. void WindowAppUiNavView::initStyle()
  24. {
  25. // 固定高度
  26. this->setFixedHeight(FIXED_HEIGHT);
  27. // 与Scene一起设置为居中对齐
  28. this->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
  29. // 隐藏横纵向两个滚动条
  30. setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
  31. setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
  32. // 设置背景色
  33. setStyleSheet(
  34. "background-color: rgb(61,61,61);"
  35. "border-radius: 3px;"
  36. );
  37. }
  38. /// <summary>
  39. /// 绑定主视图
  40. /// </summary>
  41. /// <param name="mainView"></param>
  42. void WindowAppUiNavView::setMainView(QGraphicsView* mainView)
  43. {
  44. m_mainView = mainView;
  45. }
  46. /// <summary>
  47. /// 刷新界面坐标
  48. /// </summary>
  49. void WindowAppUiNavView::updateMainViewportRegion()
  50. {
  51. if (m_mainView != nullptr)
  52. {
  53. m_viewportRegion = mapFromScene(m_mainView->mapToScene(m_mainView->rect()));
  54. update();
  55. }
  56. }
  57. /// <summary>
  58. /// 鼠标左键点在鸟瞰图时,设置Pou界面随动
  59. /// </summary>
  60. /// <param name="event"></param>
  61. void WindowAppUiNavView::mousePressEvent(QMouseEvent* event)
  62. {
  63. m_mouseDown = true;
  64. if (m_mainView)
  65. {
  66. m_mainView->centerOn(mapToScene(event->pos()));
  67. update();
  68. }
  69. event->accept();
  70. }
  71. /// <summary>
  72. /// 鼠标移动时,设置Pou界面随动
  73. /// </summary>
  74. /// <param name="event"></param>
  75. void WindowAppUiNavView::mouseMoveEvent(QMouseEvent* event)
  76. {
  77. if (m_mouseDown && m_mainView)
  78. {
  79. m_mainView->centerOn(mapToScene(event->pos()));
  80. update();
  81. event->accept();
  82. }
  83. else
  84. {
  85. event->ignore();
  86. }
  87. }
  88. /// <summary>
  89. /// 退出Pou视图的随动状态
  90. /// </summary>
  91. /// <param name="event"></param>
  92. void WindowAppUiNavView::mouseReleaseEvent(QMouseEvent* event)
  93. {
  94. m_mouseDown = false;
  95. event->accept();
  96. }
  97. /// <summary>
  98. /// 忽略滚轮消息
  99. /// </summary>
  100. /// <param name="event"></param>
  101. void WindowAppUiNavView::wheelEvent(QWheelEvent* event)
  102. {
  103. event->ignore();
  104. return QGraphicsView::wheelEvent(event);
  105. }
  106. ///// <summary>
  107. ///// 处理缩放消息,固定宽高比
  108. ///// </summary>
  109. ///// <param name="resizeEvent"></param>
  110. //void WindowAppUiNavView::resizeEvent(QResizeEvent* resizeEvent)
  111. //{
  112. // //int nWidth = resizeEvent->size().width();
  113. //
  114. // //this->setFixedHeight(nWidth * HEIGHT_SCALE);
  115. //}
  116. /// <summary>
  117. /// 绘制预览窗口边框
  118. /// </summary>
  119. /// <param name="event"></param>
  120. void WindowAppUiNavView::paintEvent(QPaintEvent* event)
  121. {
  122. int x = (width() / 2) - (m_viewportRegion.boundingRect().width() / 2);
  123. QRect rect = m_viewportRegion.boundingRect();
  124. rect.moveLeft(x);
  125. QPainter painter(viewport());
  126. painter.setPen(QPen(Qt::gray, 1));
  127. painter.drawRect(rect);
  128. // 计算坐标
  129. int nTop = rect.top() ;
  130. int nWwidth = rect.width();
  131. int nHeight = rect.height() ;
  132. int nStepW = (nWwidth) / 3 ;
  133. int nStepH = (nHeight ) / 3;
  134. painter.setPen(UI_LINE_BK_PEN);
  135. // 画两条横线
  136. painter.drawLine(QLine(x, nStepH, nWwidth + x, nStepH));
  137. painter.drawLine(QLine(x, nStepH * 2, nWwidth + x , nStepH * 2));
  138. // 画两条竖线
  139. painter.drawLine(QLine(nStepW + x, nTop, nStepW + x, nHeight));
  140. painter.drawLine(QLine(nStepW * 2 + x, nTop, nStepW * 2 + x, nHeight));
  141. QGraphicsView::paintEvent(event);
  142. }