File indexing completed on 2024-05-19 04:29:03

0001 /*
0002  *  SPDX-FileCopyrightText: 2009 Cyrille Berger <cberger@cberger.net>
0003  *  SPDX-FileCopyrightText: 2017 Scott Petrovic <scottpetrovic@gmail.com>
0004  *
0005  *  SPDX-License-Identifier: GPL-2.0-or-later
0006  */
0007 
0008 #ifndef _KIS_PAINTING_ASSISTANTS_MANAGER_H_
0009 #define _KIS_PAINTING_ASSISTANTS_MANAGER_H_
0010 
0011 #include <QPointF>
0012 #include <QColor>
0013 
0014 #include "KoPointerEvent.h"
0015 #include "KoSnapGuide.h"
0016 #include "kis_icon_utils.h"
0017 #include "canvas/kis_canvas_decoration.h"
0018 #include "kis_painting_assistant.h"
0019 #include <kritaui_export.h>
0020 
0021 class KisView;
0022 
0023 class KisPaintingAssistantsDecoration;
0024 typedef KisSharedPtr<KisPaintingAssistantsDecoration> KisPaintingAssistantsDecorationSP;
0025 
0026 // Data for editor widget. This is shared between the decoration and assistant tool which needs hit box information
0027 struct AssistantEditorData {
0028     //button count to loop over
0029     unsigned int buttoncount = 6;
0030     //button icon size
0031     const int buttonSize = 24;
0032     //boolean values track which buttons are enabled within the editor widget
0033     bool moveButtonActivated = true;
0034     bool snapButtonActivated = true;
0035     bool lockButtonActivated = true;
0036     bool duplicateButtonActivated = true;
0037     bool deleteButtonActivated = true;
0038     bool widgetActivated = true;
0039     //padding for dynamic positioning between buttons
0040     const int buttonPadding = 7;
0041     //button positions
0042     QPointF moveIconPosition = QPointF(0, 0);
0043     QPointF snapIconPosition = QPointF(0, 0);
0044     QPointF lockedIconPosition = QPointF(0, 0);
0045     QPointF duplicateIconPosition = QPointF(0, 0);
0046     QPointF deleteIconPosition = QPointF(0, 0);
0047     QSize boundingSize = QSize(0, 0);
0048     //size of the side drag decoration
0049     const int dragDecorationWidth = 15;
0050     //QPixMaps representing icons for buttons
0051     const QPixmap m_iconMove = KisIconUtils::loadIcon("transform-move").pixmap(buttonSize+10, buttonSize+10);
0052     const QPixmap m_iconSnapOn = KisIconUtils::loadIcon("visible").pixmap(buttonSize, buttonSize);
0053     const QPixmap m_iconSnapOff = KisIconUtils::loadIcon("novisible").pixmap(buttonSize, buttonSize);
0054     const QPixmap m_iconLockOn = KisIconUtils::loadIcon("layer-locked").pixmap(buttonSize, buttonSize);
0055     const QPixmap m_iconLockOff = KisIconUtils::loadIcon("layer-unlocked").pixmap(buttonSize, buttonSize);
0056     const QPixmap m_iconDuplicate = KisIconUtils::loadIcon("duplicateitem").pixmap(buttonSize, buttonSize);
0057     const QPixmap m_iconDelete = KisIconUtils::loadIcon("deletelayer").pixmap(buttonSize, buttonSize);
0058     //how many buttons fit horizontally before extending to the next row
0059     const int horizontalButtonLimit = 4;
0060 };
0061 
0062 /**
0063  * KisPaintingAssistantsDecoration draws the assistants stored in the document on
0064  * the canvas.
0065  * In the application flow, each canvas holds one of these classes to manage the assistants
0066  * There is an assistants manager, but that is higher up in the flow and makes sure each view gets one of these
0067  * Since this is off the canvas level, the decoration can be seen across all tools. The contents from here will be in
0068  * front of the kis_assistant_tool, which hold and displays the editor controls.
0069  *
0070  * Many of the events this receives such as adding and removing assistants comes from kis_assistant_tool
0071  */
0072 class KRITAUI_EXPORT KisPaintingAssistantsDecoration : public KisCanvasDecoration
0073 {
0074     Q_OBJECT
0075 public:
0076     KisPaintingAssistantsDecoration(QPointer<KisView> parent);
0077     ~KisPaintingAssistantsDecoration() override;
0078     void addAssistant(KisPaintingAssistantSP assistant);
0079     void removeAssistant(KisPaintingAssistantSP assistant);
0080     void removeAll();
0081     void setAssistants(const QList<KisPaintingAssistantSP> &assistants);
0082     QPointF adjustPosition(const QPointF& point, const QPointF& strokeBegin);
0083     void adjustLine(QPointF &point, QPointF& strokeBegin);
0084     void setAdjustedBrushPosition(const QPointF position);
0085     void endStroke();
0086     QList<KisPaintingAssistantHandleSP> handles();
0087     QList<KisPaintingAssistantSP> assistants() const;
0088     //store the editor data to be used to control the render/interaction of the editor widget.
0089     struct AssistantEditorData globalEditorWidgetData;
0090     bool hasPaintableAssistants() const;
0091 
0092 
0093     /// getter and setter functions for what assistant is currently selected
0094     /// this is used to control some tool options that are specific to a assistant
0095     KisPaintingAssistantSP selectedAssistant();
0096     void setSelectedAssistant(KisPaintingAssistantSP assistant);
0097     void deselectAssistant();
0098 
0099 
0100     /// called when assistant editor is activated
0101     /// right now this happens when the assistants tool is selected
0102     void activateAssistantsEditor();
0103 
0104 
0105     /// called when assistant editor is deactivated
0106     /// right now this happens when the assistants tool is un-selected
0107     void deactivateAssistantsEditor();
0108 
0109     /// brings back if we are currently editing assistants or not
0110     /// useful for some assistants (like spline) that draw bezier curves
0111     bool isEditingAssistants();
0112 
0113 
0114     /// sets whether the main assistant is visible
0115     void setAssistantVisible(bool set);
0116 
0117     /// sets whether the preview is visible
0118     void setOutlineVisible(bool set);
0119 
0120     /// sets whether we snap to only one assistant
0121     void setOnlyOneAssistantSnap(bool assistant);
0122 
0123     /// sets whether eraser brushes snap
0124     void setEraserSnap(bool assistant);
0125 
0126     /// returns assistant visibility
0127     bool assistantVisibility();
0128 
0129     /// returns preview visibility
0130     bool outlineVisibility();
0131 
0132     /// uncache all assistants
0133     void uncache();
0134 
0135     int handleSize();
0136     void setHandleSize(int handleSize);
0137 
0138     QColor globalAssistantsColor();
0139     void setGlobalAssistantsColor(QColor color);
0140 
0141 Q_SIGNALS:
0142     void assistantChanged();
0143     void selectedAssistantChanged();
0144 
0145 public Q_SLOTS:
0146 
0147     /// toggles whether the assistant is active or not
0148     void toggleAssistantVisible();
0149 
0150     /// toggles whether there will be a preview of the assistant result when painting
0151     void toggleOutlineVisible();
0152     QPointF snapToGuide(KoPointerEvent *e, const QPointF &offset, bool useModifiers);
0153     QPointF snapToGuide(const QPointF& pt, const QPointF &offset);
0154 
0155     void slotUpdateDecorationVisibility();
0156     void slotConfigChanged();
0157 
0158 protected:
0159     void drawDecoration(QPainter& gc, const QRectF& updateRect, const KisCoordinatesConverter *converter,KisCanvas2* canvas) override;
0160     void drawHandles(KisPaintingAssistantSP assistant, QPainter& gc, const KisCoordinatesConverter *converter);
0161     void drawEditorWidget(KisPaintingAssistantSP assistant, QPainter& gc, const KisCoordinatesConverter *converter);
0162 
0163 private:
0164     struct Private;
0165     Private* const d;
0166 };
0167 
0168 #endif