color_dialog.hpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /**
  2. * \file
  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 COLOR_DIALOG_HPP
  23. #define COLOR_DIALOG_HPP
  24. #include "colorwidgets_global.hpp"
  25. #include "color_preview.hpp"
  26. #include "color_wheel.hpp"
  27. #include <QDialog>
  28. class QAbstractButton;
  29. namespace color_widgets {
  30. class QCP_EXPORT ColorDialog : public QDialog
  31. {
  32. Q_OBJECT
  33. Q_ENUMS(ButtonMode)
  34. Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged DESIGNABLE true)
  35. Q_PROPERTY(ColorWheel::ShapeEnum wheelShape READ wheelShape WRITE setWheelShape NOTIFY wheelShapeChanged)
  36. Q_PROPERTY(ColorWheel::ColorSpaceEnum colorSpace READ colorSpace WRITE setColorSpace NOTIFY colorSpaceChanged)
  37. Q_PROPERTY(bool wheelRotating READ wheelRotating WRITE setWheelRotating NOTIFY wheelRotatingChanged)
  38. /**
  39. * \brief whether the color alpha channel can be edited.
  40. *
  41. * If alpha is disabled, the selected color's alpha will always be 255.
  42. */
  43. Q_PROPERTY(bool alphaEnabled READ alphaEnabled WRITE setAlphaEnabled NOTIFY alphaEnabledChanged)
  44. public:
  45. enum ButtonMode {
  46. OkCancel,
  47. OkApplyCancel,
  48. Close
  49. };
  50. explicit ColorDialog(QWidget *parent = 0, Qt::WindowFlags f = {});
  51. ~ColorDialog();
  52. /**
  53. * Get currently selected color
  54. */
  55. QColor color() const;
  56. /**
  57. * Set the display mode for the color preview
  58. */
  59. void setPreviewDisplayMode(ColorPreview::DisplayMode mode);
  60. /**
  61. * Get the color preview diplay mode
  62. */
  63. ColorPreview::DisplayMode previewDisplayMode() const;
  64. bool alphaEnabled() const;
  65. /**
  66. * Select which dialog buttons to show
  67. *
  68. * There are three predefined modes:
  69. * OkCancel - this is useful when the dialog is modal and we just want to return a color
  70. * OkCancelApply - this is for non-modal dialogs
  71. * Close - for non-modal dialogs with direct color updates via colorChanged signal
  72. */
  73. void setButtonMode(ButtonMode mode);
  74. ButtonMode buttonMode() const;
  75. QSize sizeHint() const Q_DECL_OVERRIDE;
  76. ColorWheel::ShapeEnum wheelShape() const;
  77. ColorWheel::ColorSpaceEnum colorSpace() const;
  78. bool wheelRotating() const;
  79. int exec() Q_DECL_OVERRIDE;
  80. public Q_SLOTS:
  81. /**
  82. * Change color
  83. */
  84. void setColor(const QColor &c);
  85. /**
  86. * Set the current color and show the dialog
  87. */
  88. void showColor(const QColor &oldcolor);
  89. void setWheelShape(ColorWheel::ShapeEnum shape);
  90. void setColorSpace(ColorWheel::ColorSpaceEnum space);
  91. void setWheelRotating(bool rotating);
  92. /**
  93. * Set whether the color alpha channel can be edited.
  94. * If alpha is disabled, the selected color's alpha will always be 255.
  95. */
  96. void setAlphaEnabled(bool a);
  97. Q_SIGNALS:
  98. /**
  99. * The current color was changed
  100. */
  101. void colorChanged(QColor);
  102. /**
  103. * The user selected the new color by pressing Ok/Apply
  104. */
  105. void colorSelected(QColor);
  106. void wheelShapeChanged(ColorWheel::ShapeEnum shape);
  107. void colorSpaceChanged(ColorWheel::ColorSpaceEnum space);
  108. void wheelRotatingChanged(bool rotating);
  109. void alphaEnabledChanged(bool alphaEnabled);
  110. private Q_SLOTS:
  111. /// Update all the Ui elements to match the selected color
  112. void setColorInternal(const QColor &color);
  113. /// Update from HSV sliders
  114. void set_hsv();
  115. /// Update from RGB sliders
  116. void set_rgb();
  117. /// Update from Alpha slider
  118. void set_alpha();
  119. void on_edit_hex_colorChanged(const QColor& color);
  120. void on_edit_hex_colorEditingFinished(const QColor& color);
  121. void on_buttonBox_clicked(QAbstractButton*);
  122. protected:
  123. void dragEnterEvent(QDragEnterEvent *event) Q_DECL_OVERRIDE;
  124. void dropEvent(QDropEvent * event) Q_DECL_OVERRIDE;
  125. void mouseReleaseEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
  126. void mouseMoveEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
  127. void keyReleaseEvent(QKeyEvent *event) Q_DECL_OVERRIDE;
  128. private:
  129. class Private;
  130. Private * const p;
  131. };
  132. } // namespace color_widgets
  133. #endif // COLOR_DIALOG_HPP