1 /*
2  *  Copyright (c) 2009 Cyrille Berger <cberger@cberger.net>
3  *  Copyright (c) 2017 Scott Petrovic <scottpetrovic@gmail.com>
4  *
5  *  This program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2 of the License, or
8  *  (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program; if not, write to the Free Software
17  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19 
20 #ifndef _KIS_PAINTING_ASSISTANTS_MANAGER_H_
21 #define _KIS_PAINTING_ASSISTANTS_MANAGER_H_
22 
23 #include <QPointF>
24 #include <QColor>
25 
26 #include "KoPointerEvent.h"
27 #include "KoSnapGuide.h"
28 
29 #include "canvas/kis_canvas_decoration.h"
30 #include "kis_painting_assistant.h"
31 #include <kritaui_export.h>
32 
33 class KisView;
34 
35 class KisPaintingAssistantsDecoration;
36 typedef KisSharedPtr<KisPaintingAssistantsDecoration> KisPaintingAssistantsDecorationSP;
37 
38 /// data for editor widget. This is shared between the decoration and assistant tool which needs hit box information
39 struct AssistantEditorData {
40     const int moveIconSize = 32;
41     const int deleteIconSize = 24;
42     const int snapIconSize = 20;
43     const QPointF moveIconPosition = QPointF(15, 15);
44     const QPointF snapIconPosition = QPointF(54, 20);
45     const QPointF deleteIconPosition = QPointF(83, 18);
46     const QSize boundingSize = QSize(110, 40);
47 };
48 
49 /**
50  * KisPaintingAssistantsDecoration draws the assistants stored in the document on
51  * the canvas.
52  * In the application flow, each canvas holds one of these classes to manage the assistants
53  * There is an assistants manager, but that is higher up in the flow and makes sure each view gets one of these
54  * Since this is off the canvas level, the decoration can be seen across all tools. The contents from here will be in
55  * front of the kis_assistant_tool, which hold and displays the editor controls.
56  *
57  * Many of the events this receives such as adding and removing assistants comes from kis_assistant_tool
58  */
59 class KRITAUI_EXPORT KisPaintingAssistantsDecoration : public KisCanvasDecoration
60 {
61     Q_OBJECT
62 public:
63     KisPaintingAssistantsDecoration(QPointer<KisView> parent);
64     ~KisPaintingAssistantsDecoration() override;
65     void addAssistant(KisPaintingAssistantSP assistant);
66     void removeAssistant(KisPaintingAssistantSP assistant);
67     void removeAll();
68     void setAssistants(const QList<KisPaintingAssistantSP> &assistants);
69     QPointF adjustPosition(const QPointF& point, const QPointF& strokeBegin);
70     void setAdjustedBrushPosition(const QPointF position);
71     void endStroke();
72     QList<KisPaintingAssistantHandleSP> handles();
73     QList<KisPaintingAssistantSP> assistants() const;
74 
75     bool hasPaintableAssistants() const;
76 
77 
78     /// getter and setter functions for what assistant is currently selected
79     /// this is used to control some tool options that are specific to a assistant
80     KisPaintingAssistantSP selectedAssistant();
81     void setSelectedAssistant(KisPaintingAssistantSP assistant);
82     void deselectAssistant();
83 
84 
85     /// called when assistant editor is activated
86     /// right now this happens when the assistants tool is selected
87     void activateAssistantsEditor();
88 
89 
90     /// called when assistant editor is deactivated
91     /// right now this happens when the assistants tool is un-selected
92     void deactivateAssistantsEditor();
93 
94     /// brings back if we are currently editing assistants or not
95     /// useful for some assistants (like spline) that draw bezier curves
96     bool isEditingAssistants();
97 
98 
99     /// sets whether the main assistant is visible
100     void setAssistantVisible(bool set);
101 
102     /// sets whether the preview is visible
103     void setOutlineVisible(bool set);
104 
105     /// sets whether we snap to only one assistant
106     void setOnlyOneAssistantSnap(bool assistant);
107 
108     /// returns assistant visibility
109     bool assistantVisibility();
110 
111     /// returns preview visibility
112     bool outlineVisibility();
113 
114     /// uncache all assistants
115     void uncache();
116 
117     int handleSize();
118     void setHandleSize(int handleSize);
119 
120     QColor globalAssistantsColor();
121     void setGlobalAssistantsColor(QColor color);
122 
123 Q_SIGNALS:
124     void assistantChanged();
125     void selectedAssistantChanged();
126 
127 public Q_SLOTS:
128 
129     /// toggles whether the assistant is active or not
130     void toggleAssistantVisible();
131 
132     /// toggles whether there will be a preview of the assistant result when painting
133     void toggleOutlineVisible();
134     QPointF snapToGuide(KoPointerEvent *e, const QPointF &offset, bool useModifiers);
135     QPointF snapToGuide(const QPointF& pt, const QPointF &offset);
136 
137     void slotUpdateDecorationVisibility();
138 
139 protected:
140     void drawDecoration(QPainter& gc, const QRectF& updateRect, const KisCoordinatesConverter *converter,KisCanvas2* canvas) override;
141     void drawHandles(KisPaintingAssistantSP assistant, QPainter& gc, const KisCoordinatesConverter *converter);
142     void drawEditorWidget(KisPaintingAssistantSP assistant, QPainter& gc, const KisCoordinatesConverter *converter);
143 
144 private:
145     struct Private;
146     Private* const d;
147 };
148 
149 #endif
150