File indexing completed on 2024-05-12 04:44:36

0001 // SPDX-FileCopyrightText: Lukas Sommer <sommerluk@gmail.com>
0002 // SPDX-License-Identifier: BSD-2-Clause OR MIT
0003 
0004 #ifndef SWATCHBOOK_H
0005 #define SWATCHBOOK_H
0006 
0007 #include "abstractdiagram.h"
0008 #include "constpropagatinguniquepointer.h"
0009 #include <qcolor.h>
0010 #include <qglobal.h>
0011 #include <qnamespace.h>
0012 #include <qsharedpointer.h>
0013 #include <qsize.h>
0014 class QEvent;
0015 class QKeyEvent;
0016 class QMouseEvent;
0017 class QPaintEvent;
0018 class QWidget;
0019 
0020 #if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
0021 #include <qtmetamacros.h>
0022 #else
0023 #include <qobjectdefs.h>
0024 #include <qstring.h>
0025 class QObject;
0026 #endif
0027 
0028 namespace PerceptualColor
0029 {
0030 
0031 template<typename T>
0032 class Array2D;
0033 class RgbColorSpace;
0034 class SwatchBookPrivate;
0035 
0036 /** @internal
0037  *
0038  * @brief Shows colors swatches.
0039  *
0040  * @image html SwatchBook.png "SwatchBook"
0041  *
0042  * The user can select a color either by mouse click or by using the keyboard.
0043  *
0044  * The marker used to mark the currently selected color depends
0045  * on the current translation; see @ref setTranslation for details.
0046  *
0047  * @internal
0048  *
0049  * @todo A design question: Should we draw margins around each individual
0050  * color patch? Maybe rely on @ref ColorPatch somehow?
0051  *
0052  * @todo A design question: Should the size of the individual color patches
0053  * be responsive, adopting the the widget size? */
0054 class SwatchBook : public AbstractDiagram
0055 {
0056     Q_OBJECT
0057 
0058     /** @brief The current color.
0059      *
0060      * This property can contain any valid color, including colors
0061      * that are not in the swatch book.
0062      *
0063      * If you set this property exactly to an RGB color that is in the
0064      * swatch book, this particular swatch will show a selection mark.
0065      * Otherwise, no selection mark will be visible.
0066      *
0067      * @sa @ref currentColor() const
0068      * @sa @ref setCurrentColor()
0069      * @sa @ref currentColorChanged() */
0070     Q_PROPERTY(QColor currentColor READ currentColor WRITE setCurrentColor NOTIFY currentColorChanged)
0071 
0072 public:
0073     Q_INVOKABLE explicit SwatchBook(const QSharedPointer<PerceptualColor::RgbColorSpace> &colorSpace,
0074                                     const Array2D<QColor> &swatches,
0075                                     Qt::Orientations wideSpacing,
0076                                     QWidget *parent = nullptr);
0077     virtual ~SwatchBook() noexcept override;
0078     /** @brief Getter for property @ref currentColor
0079      *  @returns the property @ref currentColor */
0080     [[nodiscard]] QColor currentColor() const;
0081     [[nodiscard]] virtual QSize minimumSizeHint() const override;
0082     [[nodiscard]] virtual QSize sizeHint() const override;
0083 
0084 public Q_SLOTS:
0085     void setCurrentColor(const QColor &newCurrentColor);
0086 
0087 Q_SIGNALS:
0088     /** @brief Notify signal for property @ref currentColor.
0089      *
0090      * @param newCurrentColor the new @ref currentColor */
0091     void currentColorChanged(const QColor &newCurrentColor);
0092 
0093 protected:
0094     virtual void changeEvent(QEvent *event) override;
0095     virtual void keyPressEvent(QKeyEvent *event) override;
0096     virtual void mousePressEvent(QMouseEvent *event) override;
0097     virtual void paintEvent(QPaintEvent *event) override;
0098 
0099 private:
0100     Q_DISABLE_COPY(SwatchBook)
0101 
0102     /** @internal
0103      * @brief Declare the private implementation as friend class.
0104      *
0105      * This allows the private class to access the protected members and
0106      * functions of instances of <em>this</em> class. */
0107     friend class SwatchBookPrivate;
0108     /** @brief Pointer to implementation (pimpl) */
0109     ConstPropagatingUniquePointer<SwatchBookPrivate> d_pointer;
0110 
0111     /** @internal @brief Only for unit tests. */
0112     friend class TestSwatchBook;
0113 };
0114 
0115 } // namespace PerceptualColor
0116 
0117 #endif // SWATCHBOOK_H