Perceptual Color

swatchbook.h
1// SPDX-FileCopyrightText: Lukas Sommer <sommerluk@gmail.com>
2// SPDX-License-Identifier: BSD-2-Clause OR MIT
3
4#ifndef SWATCHBOOK_H
5#define SWATCHBOOK_H
6
7#include "abstractdiagram.h"
8#include "constpropagatinguniquepointer.h"
9#include "helper.h"
10#include <qcolor.h>
11#include <qglobal.h>
12#include <qnamespace.h>
13#include <qsharedpointer.h>
14#include <qsize.h>
15class QEvent;
16class QKeyEvent;
17class QMouseEvent;
18class QPaintEvent;
19class QWidget;
20
21#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
22#include <qtmetamacros.h>
23#else
24#include <qobjectdefs.h>
25#include <qstring.h>
26class QObject;
27#endif
28
29namespace PerceptualColor
30{
31
32class RgbColorSpace;
33class SwatchBookPrivate;
34
35/** @internal
36 *
37 * @brief Shows colors patches.
38 *
39 * @image html SwatchBook.png "SwatchBook"
40 *
41 * The user can select a color either by mouse click or by using the keyboard.
42 *
43 * The marker used to mark the currently selected color depends
44 * on the current translation; see @ref setTranslation for details.
45 */
46class SwatchBook : public AbstractDiagram
47{
49
50 /** @brief The current color.
51 *
52 * This property can contain any valid color, including colors
53 * that are not in the swatch book.
54 *
55 * If you set this property exactly to an RGB color that is in the
56 * swatch book, this particular color patch will show a selection mark.
57 * Otherwise, no selection mark will be visible.
58 *
59 * @sa @ref currentColor() const
60 * @sa @ref setCurrentColor()
61 * @sa @ref currentColorChanged() */
62 Q_PROPERTY(QColor currentColor READ currentColor WRITE setCurrentColor NOTIFY currentColorChanged)
63
64 /**
65 * @brief Indicates whether the user can add or remove colors in the swatch
66 * book.
67 *
68 * When editable, the user can, for example, assign the @ref currentColor
69 * to empty color patches by a left mouse click or remove colors from
70 * existing patches by a right mouse click.
71 *
72 * @image html SwatchBookEditable.png "Editable SwatchBook"
73 *
74 * When not editable, the user can still select different colors, but
75 * modifications to the color patches are disabled.
76 *
77 * @sa @ref isEditable() const
78 * @sa @ref setEditable()
79 * @sa @ref editableChanged()
80 */
81 Q_PROPERTY(bool editable READ isEditable WRITE setEditable NOTIFY editableChanged)
82
83 /** @brief The swatchGrid that is displayed in the swatch book.
84 *
85 * The colors are in the current color space. The first dimension
86 * (@ref Array2D::iCount()) is interpreted as horizontal axis from
87 * left to right on LTR layouts, and the other way around on RTL
88 * layouts. The second dimension of the array (@ref Array2D::jCount())
89 * is interpreted as vertical axis, from top to bottom.
90 *
91 * @sa @ref swatchGrid() const
92 * @sa @ref setSwatchGrid()
93 * @sa @ref swatchGridChanged() */
94 Q_PROPERTY(Swatches swatchGrid READ swatchGrid WRITE setSwatchGrid NOTIFY swatchGridChanged)
95
96public:
97 Q_INVOKABLE explicit SwatchBook(const QSharedPointer<PerceptualColor::RgbColorSpace> &colorSpace,
98 const PerceptualColor::Swatches &swatchGrid,
99 Qt::Orientations wideSpacing,
100 QWidget *parent = nullptr);
101 virtual ~SwatchBook() noexcept override;
102 /** @brief Getter for property @ref currentColor
103 * @returns the property @ref currentColor */
104 [[nodiscard]] QColor currentColor() const;
105 /** @brief Getter for property @ref editable
106 * @returns the property @ref editable */
107 [[nodiscard]] bool isEditable() const;
108 [[nodiscard]] virtual QSize minimumSizeHint() const override;
109 /** @brief Getter for property @ref swatchGrid
110 * @returns the property @ref swatchGrid */
111 [[nodiscard]] Swatches swatchGrid() const;
112 [[nodiscard]] virtual QSize sizeHint() const override;
113
114public Q_SLOTS:
115 void setCurrentColor(const QColor &newCurrentColor);
116 void setEditable(const bool newEditable);
117 void setSwatchGrid(const PerceptualColor::Swatches &newSwatchGrid);
118
120 /** @brief Notify signal for property @ref currentColor.
121 *
122 * @param newCurrentColor the new @ref currentColor */
123 void currentColorChanged(const QColor &newCurrentColor);
124 /** @brief Notify signal for property @ref editable.
125 *
126 * @param newEditable the new @ref editable */
127 void editableChanged(const bool newEditable);
128 /** @brief Notify signal for property @ref swatchGrid.
129 *
130 * @param newSwatchGrid the new @ref swatchGrid */
131 void swatchGridChanged(const PerceptualColor::Swatches &newSwatchGrid);
132
133protected:
134 virtual void changeEvent(QEvent *event) override;
135 virtual void keyPressEvent(QKeyEvent *event) override;
136 virtual void mousePressEvent(QMouseEvent *event) override;
137 virtual void paintEvent(QPaintEvent *event) override;
138
139private:
140 Q_DISABLE_COPY(SwatchBook)
141
142 /** @internal
143 * @brief Declare the private implementation as friend class.
144 *
145 * This allows the private class to access the protected members and
146 * functions of instances of <em>this</em> class. */
147 friend class SwatchBookPrivate;
148 /** @brief Pointer to implementation (pimpl) */
149 ConstPropagatingUniquePointer<SwatchBookPrivate> d_pointer;
150
151 /** @internal @brief Only for unit tests. */
152 friend class TestSwatchBook;
153};
154
155} // namespace PerceptualColor
156
157#endif // SWATCHBOOK_H
Base class for LCH diagrams.
The namespace of this library.
Array2D< QColor > Swatches
Swatches organized in a grid.
Definition helper.h:262
Q_INVOKABLEQ_INVOKABLE
Q_OBJECTQ_OBJECT
Q_PROPERTY(...)
Q_SIGNALSQ_SIGNALS
Q_SLOTSQ_SLOTS
QObject * parent() const const
typedef Orientations
QWidget(QWidget *parent, Qt::WindowFlags f)
virtual bool event(QEvent *event) override
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri May 2 2025 12:04:47 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.