color_line_edit.hpp 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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_WIDGETS_COLOR_LINE_EDIT_HPP
  23. #define COLOR_WIDGETS_COLOR_LINE_EDIT_HPP
  24. #include "colorwidgets_global.hpp"
  25. #include <QLineEdit>
  26. #include <QColor>
  27. namespace color_widgets {
  28. /**
  29. * \brief A line edit used to define a color name
  30. *
  31. * Supported string formats:
  32. * * Short hex strings #f00
  33. * * Long hex strings #ff0000
  34. * * Color names red
  35. * * Function-like rgb(255,0,0)
  36. *
  37. * Additional string formats supported when showAlpha is true:
  38. * * Long hex strings #ff0000ff
  39. * * Function like rgba(255,0,0,255)
  40. */
  41. class QCP_EXPORT ColorLineEdit : public QLineEdit
  42. {
  43. Q_OBJECT
  44. Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged)
  45. /**
  46. * \brief Whether the widget displays and edits the alpha channel
  47. */
  48. Q_PROPERTY(bool showAlpha READ showAlpha WRITE setShowAlpha NOTIFY showAlphaChanged)
  49. /**
  50. * \brief If \b true, the background of the widget is changed to show the color
  51. */
  52. Q_PROPERTY(bool previewColor READ previewColor WRITE setPreviewColor NOTIFY previewColorChanged)
  53. public:
  54. explicit ColorLineEdit(QWidget* parent = nullptr);
  55. ~ColorLineEdit();
  56. QColor color() const;
  57. bool showAlpha() const;
  58. bool previewColor() const;
  59. public Q_SLOTS:
  60. void setColor(const QColor& color);
  61. void setShowAlpha(bool showAlpha);
  62. void setPreviewColor(bool previewColor);
  63. Q_SIGNALS:
  64. /**
  65. * \brief Emitted when the color is changed by any means
  66. */
  67. void colorChanged(const QColor& color);
  68. /**
  69. * \brief Emitted when the user is typing a color but has not finished yet
  70. */
  71. void colorEdited(const QColor& color);
  72. /**
  73. * \brief Emitted when the user finished to edit a string
  74. */
  75. void colorEditingFinished(const QColor& color);
  76. void showAlphaChanged(bool showAlpha);
  77. void previewColorChanged(bool previewColor);
  78. protected:
  79. void dragEnterEvent(QDragEnterEvent *event) Q_DECL_OVERRIDE;
  80. void dropEvent(QDropEvent * event) Q_DECL_OVERRIDE;
  81. void paintEvent(QPaintEvent* event) Q_DECL_OVERRIDE;
  82. private:
  83. class Private;
  84. Private* p;
  85. };
  86. } // namespace color_widgets
  87. #endif // COLOR_WIDGETS_COLOR_LINE_EDIT_HPP