RulerWidget.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #pragma once
  9. #if !defined(Q_MOC_RUN)
  10. #include "EditorCommon.h"
  11. #endif
  12. //! The RulerWidget is drawn above or to the left of the ViewportWidget
  13. class RulerWidget
  14. : public QWidget
  15. {
  16. Q_OBJECT
  17. public: // Types
  18. enum class Orientation
  19. {
  20. Horizontal,
  21. Vertical
  22. };
  23. public:
  24. RulerWidget(Orientation orientation, QWidget* parent, EditorWindow* editorWindow);
  25. //! Get the orientation of this ruler
  26. Orientation GetOrientation() { return m_orientation; }
  27. //! Get the origin position in canvas space of the ruler scale
  28. float GetOrigin() { return m_origin; }
  29. //! Tell the ruler where the cursor is in global Qt space
  30. void SetCursorPos(const QPoint& pos);
  31. //! Draw anything needed in the viewport (during adding of a guide for example)
  32. void DrawForViewport(Draw2dHelper& draw2d);
  33. public:
  34. //! Get the the desired breadth of the ruler widgets when shown
  35. static int GetRulerBreadth();
  36. protected:
  37. //! We have a custom paintEvent to draw the tick marks and labels
  38. void paintEvent(QPaintEvent* event) override;
  39. //! We handle mouse press events for adding guides
  40. void mousePressEvent(QMouseEvent* ev) override;
  41. //! We get this after pressing the mouse in the ruler, even if mouse is over viewport
  42. void mouseMoveEvent(QMouseEvent* ev) override;
  43. //! We get this after pressing the mouse in the ruler, even if mouse if over viewport
  44. void mouseReleaseEvent(QMouseEvent* ev) override;
  45. //! We need this to cancel the interaction if RMB or a Alt+char combo is pressed
  46. void focusOutEvent(QFocusEvent* ev) override;
  47. //! Draw the tick marks and text on the ruler
  48. void DrawTickMarksWithLabels(QPainter* painter, QRectF rulerRect, float translation, float scale);
  49. //! Draw one section of the ruller scale - a ssection has one major tick mark with a label plus some smaller tick marks
  50. void DrawRulerSection(QPainter* painter, QRectF rulerRect, float startInCanvasPixels, float sectionLengthInScreenPixels, int numSubdivisions, float translation, float scale);
  51. //! Draw the line on the rule that shows where the mouse is
  52. void DrawCursorPos(QPainter* painter, QRectF rulerRect);
  53. private:
  54. Orientation m_orientation; //!< The orientation of this ruler (never changes once created)
  55. float m_origin = 0.0f; //!< Where the origin of the ruler scale is in canvas space (might be modifiable in future)
  56. float m_cursorPos; //!< The current cursor position - a value along ruler in local space
  57. EditorWindow* m_editorWindow;
  58. ViewportDragInteraction* m_dragInteraction = nullptr; //! Used for adding guides
  59. };