color_preview.hpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /**
  2. * \file
  3. *
  4. * \author Mattia Basaglia
  5. *
  6. * \copyright Copyright (C) 2013-2020 Mattia Basaglia
  7. * \copyright Copyright (C) 2014 Calle Laakkonen
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Lesser General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. #ifndef COLOR_PREVIEW_HPP
  24. #define COLOR_PREVIEW_HPP
  25. #include "colorwidgets_global.hpp"
  26. #include <QWidget>
  27. namespace color_widgets {
  28. /**
  29. * Simple widget that shows a preview of a color
  30. */
  31. class QCP_EXPORT ColorPreview : public QWidget
  32. {
  33. Q_OBJECT
  34. Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged DESIGNABLE true)
  35. Q_PROPERTY(QColor comparisonColor READ comparisonColor WRITE setComparisonColor NOTIFY comparisonColorChanged DESIGNABLE true)
  36. Q_PROPERTY(DisplayMode display_mode READ displayMode WRITE setDisplayMode NOTIFY displayModeChanged DESIGNABLE true)
  37. Q_PROPERTY(QBrush background READ background WRITE setBackground NOTIFY backgroundChanged DESIGNABLE true)
  38. Q_PROPERTY(bool drawFrame READ drawFrame WRITE setDrawFrame NOTIFY drawFrameChanged DESIGNABLE true)
  39. Q_ENUMS(DisplayMode)
  40. public:
  41. enum DisplayMode
  42. {
  43. NoAlpha, ///< Show current color with no transparency
  44. AllAlpha, ///< show current color with transparency
  45. SplitAlpha, ///< Show both solid and transparent side by side
  46. SplitColor, ///< Show current and comparison colors side by side
  47. SplitColorReverse, ///< Like Split color but swapped
  48. };
  49. Q_ENUMS(DisplayMode)
  50. explicit ColorPreview(QWidget *parent = 0);
  51. ~ColorPreview();
  52. /// Get the background visible under transparent colors
  53. QBrush background() const;
  54. /// Change the background visible under transparent colors
  55. void setBackground(const QBrush &bk);
  56. /// Get color display mode
  57. DisplayMode displayMode() const;
  58. /// Set how transparent colors are handled
  59. void setDisplayMode(DisplayMode dm);
  60. /// Get current color
  61. QColor color() const;
  62. /// Get the comparison color
  63. QColor comparisonColor() const;
  64. QSize sizeHint () const Q_DECL_OVERRIDE;
  65. void paint(QPainter &painter, QRect rect) const;
  66. /// Whether to draw a frame around the color
  67. bool drawFrame() const;
  68. void setDrawFrame(bool);
  69. public Q_SLOTS:
  70. /// Set current color
  71. void setColor(const QColor &c);
  72. /// Set the comparison color
  73. void setComparisonColor(const QColor &c);
  74. Q_SIGNALS:
  75. /// Emitted when the user clicks on the widget
  76. void clicked();
  77. /// Emitted on setColor
  78. void colorChanged(QColor);
  79. void comparisonColorChanged(QColor);
  80. void displayModeChanged(DisplayMode);
  81. void backgroundChanged(const QBrush&);
  82. void drawFrameChanged(bool);
  83. protected:
  84. void paintEvent(QPaintEvent *) Q_DECL_OVERRIDE;
  85. void resizeEvent(QResizeEvent *) Q_DECL_OVERRIDE;
  86. void mouseReleaseEvent(QMouseEvent *ev) Q_DECL_OVERRIDE;
  87. void mouseMoveEvent(QMouseEvent *ev) Q_DECL_OVERRIDE;
  88. private:
  89. class Private;
  90. Private * const p;
  91. };
  92. } // namespace color_widgets
  93. Q_DECLARE_METATYPE(color_widgets::ColorPreview::DisplayMode)
  94. #endif // COLOR_PREVIEW_HPP