1
0

GradientWidget.h 2.1 KB

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