File indexing completed on 2025-02-02 04:26:09

0001 /* SPDX-FileCopyrightText: 2022 Marco Martin <mart@kde.org>
0002  * SPDX-FileCopyrightText: 2022 Noah Davis <noahadvs@gmail.com>
0003  * SPDX-License-Identifier: LGPL-2.0-or-later
0004  */
0005 
0006 #pragma once
0007 
0008 #include <QColor>
0009 #include <QFont>
0010 #include <QObject>
0011 
0012 /**
0013  * This is the data structure that controls the creation of the next item. From qml its paramenter
0014  * will be set by the app toolbars, and then drawing on the screen with the mouse will lead to the
0015  * creation of a new item based on those parameters
0016  */
0017 class AnnotationTool : public QObject
0018 {
0019     Q_OBJECT
0020     Q_PROPERTY(Tool type READ type WRITE setType RESET resetType NOTIFY typeChanged)
0021     Q_PROPERTY(Options options READ options NOTIFY optionsChanged)
0022     Q_PROPERTY(int strokeWidth READ strokeWidth WRITE setStrokeWidth RESET resetStrokeWidth NOTIFY strokeWidthChanged)
0023     Q_PROPERTY(QColor strokeColor READ strokeColor WRITE setStrokeColor RESET resetStrokeColor NOTIFY strokeColorChanged)
0024     Q_PROPERTY(QColor fillColor READ fillColor WRITE setFillColor RESET resetFillColor NOTIFY fillColorChanged)
0025     Q_PROPERTY(QFont font READ font WRITE setFont RESET resetFont NOTIFY fontChanged)
0026     Q_PROPERTY(QColor fontColor READ fontColor WRITE setFontColor RESET resetFontColor NOTIFY fontColorChanged)
0027     Q_PROPERTY(int number READ number WRITE setNumber RESET resetNumber NOTIFY numberChanged)
0028     Q_PROPERTY(bool shadow READ hasShadow WRITE setShadow RESET resetShadow NOTIFY shadowChanged)
0029 
0030 public:
0031     /**
0032      * These tools are meant to be shown as selectable tool types in the UI.
0033      * They can also affect the types of traits a drawable object is allowed to have.
0034      */
0035     enum Tool {
0036         NoTool,
0037         SelectTool,
0038         FreehandTool,
0039         HighlighterTool,
0040         LineTool,
0041         ArrowTool,
0042         RectangleTool,
0043         EllipseTool,
0044         BlurTool,
0045         PixelateTool,
0046         TextTool,
0047         NumberTool,
0048     };
0049     Q_ENUM(Tool)
0050 
0051     /**
0052      * These options are meant to help control which options are visible in the UI
0053      * and what kinds of traits a drawable object should have.
0054      */
0055     enum Option {
0056         NoOptions = 0,
0057         StrokeOption = 1,
0058         FillOption = 1 << 1,
0059         FontOption = 1 << 2,
0060         TextOption = 1 << 3,
0061         NumberOption = 1 << 4,
0062         ShadowOption = 1 << 5,
0063     };
0064     Q_DECLARE_FLAGS(Options, Option)
0065     Q_FLAG(Options)
0066 
0067     AnnotationTool(QObject *parent);
0068     ~AnnotationTool();
0069 
0070     Tool type() const;
0071     void setType(Tool type);
0072     void resetType();
0073 
0074     // Whether the current tool type is for creating annotations.
0075     bool isCreationTool() const;
0076 
0077     Options options() const;
0078 
0079     int strokeWidth() const;
0080     void setStrokeWidth(int width);
0081     void resetStrokeWidth();
0082 
0083     QColor strokeColor() const;
0084     void setStrokeColor(const QColor &color);
0085     void resetStrokeColor();
0086 
0087     QColor fillColor() const;
0088     void setFillColor(const QColor &color);
0089     void resetFillColor();
0090 
0091     QFont font() const;
0092     void setFont(const QFont &font);
0093     void resetFont();
0094 
0095     QColor fontColor() const;
0096     void setFontColor(const QColor &color);
0097     void resetFontColor();
0098 
0099     int number() const;
0100     void setNumber(int number);
0101     void resetNumber();
0102 
0103     bool hasShadow() const;
0104     void setShadow(bool shadow);
0105     void resetShadow();
0106 
0107 Q_SIGNALS:
0108     void typeChanged();
0109     void optionsChanged();
0110     void strokeWidthChanged(int width);
0111     void strokeColorChanged(const QColor &color);
0112     void fillColorChanged(const QColor &color);
0113     void fontChanged(const QFont &font);
0114     void fontColorChanged(const QColor &color);
0115     void numberChanged(const int number);
0116     void shadowChanged(bool hasShadow);
0117 
0118 private:
0119     static constexpr AnnotationTool::Options optionsForType(AnnotationTool::Tool type);
0120 
0121     static constexpr int defaultStrokeWidthForType(AnnotationTool::Tool type);
0122     int strokeWidthForType(Tool type) const;
0123     void setStrokeWidthForType(int width, Tool type);
0124 
0125     static constexpr QColor defaultStrokeColorForType(AnnotationTool::Tool type);
0126     QColor strokeColorForType(Tool type) const;
0127     void setStrokeColorForType(const QColor &color, Tool type);
0128 
0129     static constexpr QColor defaultFillColorForType(AnnotationTool::Tool type);
0130     QColor fillColorForType(Tool type) const;
0131     void setFillColorForType(const QColor &color, Tool type);
0132 
0133     QFont fontForType(Tool type) const;
0134     void setFontForType(const QFont &font, Tool type);
0135 
0136     static constexpr QColor defaultFontColorForType(AnnotationTool::Tool type);
0137     QColor fontColorForType(Tool type) const;
0138     void setFontColorForType(const QColor &color, Tool type);
0139 
0140     bool typeHasShadow(Tool type) const;
0141     void setTypeHasShadow(Tool type, bool shadow);
0142 
0143     Tool m_type = Tool::NoTool;
0144     Options m_options = Option::NoOptions;
0145     int m_number = 1;
0146 };
0147 
0148 Q_DECLARE_OPERATORS_FOR_FLAGS(AnnotationTool::Options)