WidgetResizeHandler.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #pragma once
  2. #include <QObject>
  3. #include <QMouseEvent>
  4. #include <QKeyEvent>
  5. class TitleBar;
  6. class WidgetResizeHandler : public QObject
  7. {
  8. Q_OBJECT
  9. public:
  10. enum Action
  11. {
  12. Move = 0x01,
  13. Resize = 0x02,
  14. Any = Move | Resize
  15. };
  16. explicit WidgetResizeHandler(QWidget *parent, QWidget *cw = 0);
  17. void setTitleBar(TitleBar *tBar);
  18. TitleBar *getTitleBar() const;
  19. void setResizeEnabled(bool isEnabled);
  20. bool getResizeEnabled() const;
  21. void setActive(bool b)
  22. {
  23. setActive(Any, b);
  24. }
  25. void setActive(Action ac, bool b);
  26. bool isActive() const
  27. {
  28. return isActive(Any);
  29. }
  30. bool isActive(Action ac) const;
  31. void setMovingEnabled(bool b)
  32. {
  33. m_movingEnabled = b;
  34. }
  35. bool isMovingEnabled() const
  36. {
  37. return m_movingEnabled;
  38. }
  39. bool isButtonDown() const
  40. {
  41. return m_buttonDown;
  42. }
  43. void setExtraHeight(int h)
  44. {
  45. m_extrahei = h;
  46. }
  47. void setSizeProtection(bool b)
  48. {
  49. m_sizeprotect = b;
  50. }
  51. void setFrameWidth(int w)
  52. {
  53. m_fw = w;
  54. }
  55. void setTitleBarHeight(int h)
  56. {
  57. m_titleBarHeight = h;
  58. }
  59. void doResize();
  60. void doMove();
  61. Q_SIGNALS:
  62. void activate();
  63. protected:
  64. bool eventFilter(QObject *o, QEvent *e) Q_DECL_OVERRIDE;
  65. void mouseMoveEvent(QMouseEvent *e);
  66. void keyPressEvent(QKeyEvent *e);
  67. private:
  68. Q_DISABLE_COPY(WidgetResizeHandler)
  69. QSize qSmartMinSize(const QSize &sizeHint, const QSize &minSizeHint,
  70. const QSize &minSize, const QSize &maxSize,
  71. const QSizePolicy &sizePolicy);
  72. enum MousePosition
  73. {
  74. Nowhere,
  75. TopLeft, BottomRight, BottomLeft, TopRight,
  76. Top, Bottom, Left, Right,
  77. Center
  78. };
  79. QWidget *m_widget;
  80. QWidget *m_childWidget;
  81. QPoint m_moveOffset;
  82. QPoint m_invertedMoveOffset;
  83. MousePosition m_mode;
  84. int m_fw;
  85. int m_extrahei;
  86. int m_range;
  87. uint m_buttonDown = 1;
  88. uint m_moveResizeMode = 1;
  89. uint m_activeForResize = 1;
  90. uint m_sizeprotect = 1;
  91. uint m_movingEnabled = 1;
  92. uint m_activeForMove = 1;
  93. uint m_titleBarHeight = 0;
  94. TitleBar *m_titleBar;
  95. void setMouseCursor(MousePosition m);
  96. bool isMove() const
  97. {
  98. return m_moveResizeMode && m_mode == Center;
  99. }
  100. bool isResize() const
  101. {
  102. return m_moveResizeMode && !isMove();
  103. }
  104. };