Perceptual Color

colorpatch.h
1// SPDX-FileCopyrightText: Lukas Sommer <sommerluk@gmail.com>
2// SPDX-License-Identifier: BSD-2-Clause OR MIT
3
4#ifndef COLORPATCH_H
5#define COLORPATCH_H
6
7#include "abstractdiagram.h"
8#include "constpropagatinguniquepointer.h"
9#include "importexport.h"
10#include <qcolor.h>
11#include <qglobal.h>
12#include <qsize.h>
13class QDragEnterEvent;
14class QDropEvent;
15class QMouseEvent;
16class QResizeEvent;
17class QWidget;
18
19#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
20#include <qtmetamacros.h>
21#else
22#include <qobjectdefs.h>
23#include <qstring.h>
24class QObject;
25#endif
26
27namespace PerceptualColor
28{
29class ColorPatchPrivate;
30
31/** @brief A color display widget.
32 *
33 * This widget simply displays a color. And it provides drag-and-drop support;
34 * it emits also @ref colorChanged on drop events if the color has changed.
35 * Useful for showing a given color. The instantiation and usage is simple.
36 * Example:
37 * @snippet testcolorpatch.cpp ColorPatch Create widget
38 *
39 * @image html ColorPatch.png "ColorPatch" width=50
40 *
41 * This widget is also able to display transparency (the pattern will be
42 * mirrored on right-to-left layouts):
43 *
44 * @image html ColorPatchSemiTransparent.png "ColorPatch with 50% transparency" width=50
45 *
46 * There is also a simple representation
47 * for <tt>QColor::isValid() == false</tt>:
48 *
49 * @image html ColorPatchInvalid.png "ColorPatch with invalid color" width=50
50 *
51 * The default minimum size of this widget is similar to a
52 * <tt>QToolButton</tt>. Depending on your use case, you might
53 * want to set a bigger minimum size:
54 * @snippet testcolorpatch.cpp ColorPatch Bigger minimum size
55 *
56 * @note This class is API-compatible with the legacy
57 * <a href="https://api.kde.org/legacy/4.12-api/kdelibs-apidocs/kdeui/html/classKColorPatch.html">
58 * KColorPatch</a> class that was available in KDElibs4 (and still in KF5
59 * through KDELibs4Support).
60 *
61 * @internal
62 *
63 * @note This class intentionally does not reimplement the paint event,
64 * but uses a child QLabel to display the color. QLabel integrates by
65 * default nicely with QStyle: Even round frames that are cutting slightly
66 * the pixmap (like in the Breeze style), are possible. So we rely entirely
67 * on QLabel for the actual display, and only implement @ref sizeHint() and
68 * @ref minimumSizeHint() ourselves. */
70{
71 Q_OBJECT
72
73 /** @brief The color that is displayed
74 *
75 * Default value is an invalid color.
76 *
77 * - If the color is valid, the widget frame is filled with this color.
78 * If this color is not fully opaque, the background behind the color
79 * will be a special background pattern (and <em>not</em> the default
80 * widget background).
81 * - If the color is invalid, this is represented by a a special
82 * appearance of the widget.
83 *
84 * @note No color management is applied. The color is used as-is to
85 * paint on the canvas provided by the operation system.
86 *
87 * @sa @ref color() const
88 * @sa @ref setColor()
89 * @sa @ref colorChanged() */
90 Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged)
91
92public:
93 Q_INVOKABLE explicit ColorPatch(QWidget *parent = nullptr);
94 virtual ~ColorPatch() noexcept override;
95 /** @brief Getter for property @ref color
96 * @returns the property @ref color */
97 [[nodiscard]] QColor color() const;
98 [[nodiscard]] virtual QSize minimumSizeHint() const override;
99 [[nodiscard]] virtual QSize sizeHint() const override;
100
101public Q_SLOTS:
102 void setColor(const QColor &newColor);
103
104Q_SIGNALS:
105 /** @brief Notify signal for property @ref color.
106 *
107 * @param color the new color */
108 void colorChanged(const QColor &color);
109
110protected:
111 virtual void dragEnterEvent(QDragEnterEvent *event) override;
112 virtual void dropEvent(QDropEvent *event) override;
113 virtual void mouseMoveEvent(QMouseEvent *event) override;
114 virtual void mousePressEvent(QMouseEvent *event) override;
115 virtual void resizeEvent(QResizeEvent *event) override;
116
117private:
118 Q_DISABLE_COPY(ColorPatch)
119
120 /** @internal
121 * @brief Declare the private implementation as friend class.
122 *
123 * This allows the private class to access the protected members and
124 * functions of instances of <em>this</em> class. */
125 friend class ColorPatchPrivate;
126 /** @brief Pointer to implementation (pimpl) */
127 ConstPropagatingUniquePointer<ColorPatchPrivate> d_pointer;
128
129 /** @internal @brief Only for unit tests. */
130 friend class TestColorPatch;
131};
132
133} // namespace PerceptualColor
134
135#endif // COLORPATCH_H
Base class for LCH diagrams.
A color display widget.
Definition colorpatch.h:70
This file provides support for C++ symbol import and export.
#define PERCEPTUALCOLOR_IMPORTEXPORT
A macro that either exports dynamic library symbols or imports dynamic library symbols or does nothin...
The namespace of this library.
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:57:18 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.