gradient_editor.hpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /**
  2. * \file gradient_editor.hpp
  3. *
  4. * \author Mattia Basaglia
  5. *
  6. * \copyright Copyright (C) 2013-2020 Mattia Basaglia
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Lesser General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #ifndef GRADIENT_EDITOR_HPP
  23. #define GRADIENT_EDITOR_HPP
  24. #include "colorwidgets_global.hpp"
  25. #include <QWidget>
  26. #include <QGradient>
  27. namespace color_widgets {
  28. class ColorDialog;
  29. /**
  30. * \brief A slider that moves on top of a gradient
  31. */
  32. class QCP_EXPORT GradientEditor : public QWidget
  33. {
  34. Q_OBJECT
  35. Q_PROPERTY(QBrush background READ background WRITE setBackground NOTIFY backgroundChanged)
  36. Q_PROPERTY(QGradientStops stops READ stops WRITE setStops NOTIFY stopsChanged)
  37. Q_PROPERTY(QLinearGradient gradient READ gradient WRITE setGradient)
  38. Q_PROPERTY(Qt::Orientation orientation READ orientation WRITE setOrientation)
  39. Q_PROPERTY(int selectedStop READ selectedStop WRITE setSelectedStop NOTIFY selectedStopChanged)
  40. Q_PROPERTY(QColor selectedColor READ selectedColor WRITE setSelectedColor)
  41. public:
  42. explicit GradientEditor(QWidget *parent = 0);
  43. explicit GradientEditor(Qt::Orientation orientation, QWidget *parent = 0);
  44. ~GradientEditor();
  45. QSize sizeHint() const override;
  46. /// Get the background, it's visible for transparent gradient stops
  47. QBrush background() const;
  48. /// Set the background, it's visible for transparent gradient stops
  49. void setBackground(const QBrush &bg);
  50. /// Get the colors that make up the gradient
  51. QGradientStops stops() const;
  52. /// Set the colors that make up the gradient
  53. void setStops(const QGradientStops &colors);
  54. /// Get the gradient
  55. QLinearGradient gradient() const;
  56. /// Set the gradient
  57. void setGradient(const QLinearGradient &gradient);
  58. Qt::Orientation orientation() const;
  59. /**
  60. * \brief Dialog shown when double clicking a stop
  61. */
  62. ColorDialog* dialog() const;
  63. /**
  64. * \brief Index of the currently selected gradient stop (or -1 if there is no selection)
  65. */
  66. int selectedStop() const;
  67. /**
  68. * \brief Color of the selected stop
  69. */
  70. QColor selectedColor() const;
  71. public Q_SLOTS:
  72. void setOrientation(Qt::Orientation);
  73. void setSelectedStop(int stop);
  74. void setSelectedColor(const QColor& color);
  75. void addStop();
  76. void removeStop();
  77. Q_SIGNALS:
  78. void backgroundChanged(const QBrush&);
  79. void stopsChanged(const QGradientStops&);
  80. void selectedStopChanged(int);
  81. protected:
  82. void paintEvent(QPaintEvent *ev) override;
  83. void mousePressEvent(QMouseEvent *ev) override;
  84. void mouseMoveEvent(QMouseEvent *ev) override;
  85. void mouseReleaseEvent(QMouseEvent *ev) override;
  86. void leaveEvent(QEvent * event) override;
  87. void mouseDoubleClickEvent(QMouseEvent *event) override;
  88. void dragEnterEvent(QDragEnterEvent *event) override;
  89. void dragMoveEvent(QDragMoveEvent* event) override;
  90. void dragLeaveEvent(QDragLeaveEvent *event) override;
  91. void dropEvent(QDropEvent* event) override;
  92. private Q_SLOTS:
  93. void dialogUpdate(const QColor& c);
  94. private:
  95. class Private;
  96. Private * const p;
  97. };
  98. } // namespace color_widgets
  99. #endif // GRADIENT_EDITOR_HPP