selectwidget.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #ifndef SELECTWIDGET_H
  2. #define SELECTWIDGET_H
  3. /**
  4. * 描点跟随窗体控件,并且增加了VisionPlus相关的业务逻辑
  5. * 1:可设置是否绘制描点
  6. * 2:可设置边距
  7. * 3:可设置描点颜色
  8. * 4:可设置描点尺寸
  9. * 5:可设置描点样式 正方形+圆形
  10. * 6:可设置选中边框宽度
  11. * 7:支持上下左右按键移动窗体
  12. * 8:支持delete键删除窗体
  13. * 9:支持八个描点改变窗体大小尺寸
  14. * 10:支持拖动窗体
  15. */
  16. #include "Common.h"
  17. #include "CommonDraw.h"
  18. #ifdef quc
  19. #if (QT_VERSION < QT_VERSION_CHECK(5,7,0))
  20. #include <QtDesigner/QDesignerExportWidget>
  21. #else
  22. #include <QtUiPlugin/QDesignerExportWidget>
  23. #endif
  24. class QDESIGNER_WIDGET_EXPORT SelectWidget : public QWidget
  25. #else
  26. class SelectWidget : public QWidget
  27. #endif
  28. {
  29. Q_OBJECT
  30. Q_ENUMS(PointStyle)
  31. Q_PROPERTY(bool drawPoint READ getDrawPoint WRITE setDrawPoint)
  32. Q_PROPERTY(int padding READ getPadding WRITE setPadding)
  33. Q_PROPERTY(int borderWidth READ getBorderWidth WRITE setBorderWidth)
  34. Q_PROPERTY(int pointSize READ getPointSize WRITE setPointSize)
  35. Q_PROPERTY(QColor pointColor READ getPointColor WRITE setPointColor)
  36. Q_PROPERTY(PointStyle pointStyle READ getPointStyle WRITE setPointStyle)
  37. public:
  38. enum class PointStyle {
  39. PointStyle_Rect = 0, //正方形
  40. PointStyle_Circle = 1 //圆形
  41. };
  42. explicit SelectWidget(QWidget *parent = 0);
  43. QWidget* m_pWidget; // 主控件
  44. VALUE_TYPE m_Type; // 增加:控件的类型
  45. CONTROL_PROPERTY* m_pProperty; // 增加:控件的属性
  46. CONTROL_PROPERTY_EX* m_pPropertyEx; // 2021-12-11 增加:控件的扩展属性(仅复杂控件才有)
  47. QString m_strID; // 2022-10-6,控件的ID,用于Undo体系
  48. protected:
  49. bool eventFilter(QObject *watched, QEvent *event);
  50. void resizeEvent(QResizeEvent *);
  51. void mouseMoveEvent(QMouseEvent *);
  52. void paintEvent(QPaintEvent *);
  53. void drawRect(QPainter *painter);
  54. void drawCircle(QPainter *painter);
  55. void drawBorder(QPainter *painter);
  56. // 自动对齐控件
  57. void alignToGrid(QPoint& pt);
  58. int alignToGrid(int x);
  59. private:
  60. bool drawPoint; //绘制描点
  61. int padding; //窗体的边距
  62. int borderWidth; //边框宽度
  63. int pointSize; //描点的尺寸
  64. QColor pointColor; //描点的颜色
  65. PointStyle pointStyle; //描点的形状
  66. bool m_Hitted; //2022-9-8 鼠标按下或者拖拽中的任何一种状态
  67. bool pressed; //鼠标按下
  68. bool pressedLeft; //鼠标按下左侧
  69. bool pressedRight; //鼠标按下右侧
  70. bool pressedTop; //鼠标按下上侧
  71. bool pressedBottom; //鼠标按下下侧
  72. bool pressedLeftTop; //鼠标按下左上侧
  73. bool pressedRightTop; //鼠标按下右上侧
  74. bool pressedLeftBottom; //鼠标按下左下侧
  75. bool pressedRightBottom; //鼠标按下右下侧
  76. int rectX, rectY, rectW, rectH; //窗体坐标+宽高
  77. QPoint lastPos; //鼠标按下处坐标
  78. //// 2022-3-20 ,保存上一次的矩形区域位置,防止无效的矩形区域位置变动通知,提高效率
  79. //QRectF oldGeometry;
  80. QRectF rectLeft; //左侧区域
  81. QRectF rectRight; //右侧区域
  82. QRectF rectTop; //上侧区域
  83. QRectF rectBottom; //下侧区域
  84. QRectF rectLeftTop; //左上侧区域
  85. QRectF rectRightTop; //右上侧区域
  86. QRectF rectLeftBottom; //左下侧区域
  87. QRectF rectRightBottom; //右下侧区域
  88. // 控件被拖动的方向(如果被拖动了的话)
  89. STRETCH_DIRECTION m_StretchDir;
  90. public:
  91. bool getDrawPoint() const;
  92. int getPadding() const;
  93. int getBorderWidth() const;
  94. int getPointSize() const;
  95. QColor getPointColor() const;
  96. PointStyle getPointStyle() const;
  97. QWidget *getWidget() const;
  98. QSize sizeHint() const;
  99. QSize minimumSizeHint() const;
  100. // 2022-10-21 直接移动控件和外边框的位置(目前用于Redo体系中直接移动控件)
  101. void updatePos(const QPoint& newPos);
  102. // 2022-10-21 直接缩放控件和外边框(目前用于Redo体系中直接缩放控件)
  103. void updateGeometry(const QRect& newGeometry);
  104. public Q_SLOTS:
  105. //设置是否绘制描点
  106. void setDrawPoint(bool drawPoint);
  107. //设置跟随窗体的边距
  108. void setPadding(int padding);
  109. //设置线条宽度
  110. void setBorderWidth(int borderWidth);
  111. //设置描点的尺寸
  112. void setPointSize(int pointSize);
  113. //设置描点的颜色
  114. void setPointColor(const QColor &pointColor);
  115. //设置描点的样式
  116. void setPointStyle(const PointStyle &pointStyle);
  117. // 设置绑定窗体以及本窗体的数据类型
  118. void setWidget(QWidget *widget);
  119. Q_SIGNALS:
  120. void widgetPressed(QWidget *widget);
  121. void widgetRelease(QWidget *widget);
  122. void widgetDelete(QWidget *widget);
  123. // 2022-2-13 增加控件移动信号
  124. void widgetMove(QWidget* widget);
  125. // 2022-9-15 增加控件拉伸信号(为了绘制等宽线)
  126. void widgetStretch(QWidget* widget, STRETCH_DIRECTION dir);
  127. };
  128. #endif // SELECTWIDGET_H