GradientWidget.h 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. alm::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 alm::Gradient& getGradient(void) const;
  33. void setGradient(alm::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. void removeSelectedHandle(void);
  53. protected:
  54. /// \brief the area in which the gradient is displayed
  55. QRect getGradientRect(void) const;
  56. /// \brief the area in which the handle with index
  57. /// \c index is displayed
  58. QRect getHandleRect(int index) const;
  59. /// \brief the area in which the handles can move around
  60. QRect getHandleArea(void) const;
  61. int handleAtPos(QPoint pos) const;
  62. float handleYToGradVal(float y) const;
  63. float gradValToHandleY(float v) const;
  64. private:
  65. static QPainterPath createSlideHandle(float w, float h);
  66. signals:
  67. void gradientChanged(void);
  68. };
  69. #endif // GRADIENTWIDGET_H