1
0

GradientWidget.h 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #ifndef GRADIENTWIDGET_H
  2. #define GRADIENTWIDGET_H
  3. #include <QWidget>
  4. #include <QLinearGradient>
  5. #include <QPainterPath>
  6. #include <QVector>
  7. #include <QPair>
  8. class GradientWidget :
  9. public QWidget
  10. {
  11. Q_OBJECT
  12. QVector<QPair<float, QColor>> points;
  13. bool dragging;
  14. int selectedHandle;
  15. float selectOffsetY;
  16. int mouseOver;
  17. int handleWidth = 40;
  18. int handleHeight = 24;
  19. public:
  20. enum HandleState
  21. {
  22. HANDLE_NORMAL = 0x00,
  23. HANDLE_MOUSEOVER = 0x01,
  24. HANDLE_DOWN = 0x02,
  25. HANDLE_SELECTED = 0x04
  26. };
  27. explicit GradientWidget(QWidget *parent = nullptr);
  28. const QVector<QPair<float, QColor>>& getGradient(void) const;
  29. void setGradient(QVector<QPair<float, QColor>>);
  30. QColor colorAtY(float y);
  31. virtual void paintEvent(QPaintEvent* e) override;
  32. virtual void paintHandle(QPainter& painter, const QRectF &pos, QColor c, int handleState);
  33. virtual void mousePressEvent(QMouseEvent* e) override;
  34. virtual void mouseReleaseEvent(QMouseEvent* e) override;
  35. virtual void mouseMoveEvent(QMouseEvent* e) override;
  36. virtual void mouseDoubleClickEvent(QMouseEvent* e) override;
  37. QSize minimumSizeHint(void) const override;
  38. QSize sizeHint(void) const override;
  39. protected:
  40. /// \brief the area in which the gradient is displayed
  41. QRect getGradientRect(void) const;
  42. /// \brief the area in which the handle with index
  43. /// \c index is displayed
  44. QRect getHandleRect(int index) const;
  45. /// \brief the area in which the handles can move around
  46. QRect getHandleArea(void) const;
  47. int handleAtPos(QPoint pos) const;
  48. float handleYToGradVal(float y) const;
  49. float gradValToHandleY(float v) const;
  50. private:
  51. static QPainterPath createSlideHandle(float w, float h);
  52. signals:
  53. };
  54. #endif // GRADIENTWIDGET_H