1 // Copyright (C) 2012-2019 The VPaint Developers.
2 // See the COPYRIGHT file at the top-level directory of this distribution
3 // and at https://github.com/dalboris/vpaint/blob/master/COPYRIGHT
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 
17 #ifndef GLOBAL_H
18 #define GLOBAL_H
19 
20 // Define objects that we want accessible globally
21 //
22 // Example: global()->mainWindow()->update();
23 //          double w = global()->preferences().edgeWidth();
24 
25 #include "Settings.h"
26 
27 #include <QObject>
28 #include <QVector>
29 #include <QAction>
30 #include <QDoubleSpinBox>
31 #include "SpinBox.h"
32 #include "TimeDef.h"
33 #include <Eigen/Core>
34 #include <QLabel>
35 #include <QDir>
36 
37 class QHBoxLayout;
38 class DevSettings;
39 class MainWindow;
40 class QToolBar;
41 class QAction;
42 class ToolModeAction;
43 class ColorSelector;
44 class QColor;
45 class SettingsDialog;
46 class View;
47 class Timeline;
48 class Scene;
49 
50 namespace VectorAnimationComplex
51 {
52 class VAC;
53 }
54 
55 class Global: public QObject
56 {
57     Q_OBJECT
58 
59 public:
60     // Initialization
61     static void initialize(MainWindow * w);
62     Global(MainWindow * w);
63 
64     // Tool Mode
65     void createToolBars();
66     enum ToolMode {
67         // Used for array indexes, don't change the numbers!
68         SELECT = 0,
69         SKETCH,
70         PAINT,
71         SCULPT,
72         //CUT,
73         NUMBER_OF_TOOL_MODES, // Keep this one last
74         EDIT_CANVAS_SIZE // This one is below "Number of tools" as it's not a mode interface-wise
75     };
76     ToolMode toolMode() const;
77 
78     // Menus
79     void addSelectionActions(QMenu * selectionMenu);
80 
81     // Keyboard state
82     Qt::KeyboardModifiers keyboardModifiers();
83 
84     // Tablet pressure
85     bool useTabletPressure() const;
86 
87     // Edge width
88     double edgeWidth() const;
89     void setEdgeWidth(double w);
90 
91     // Planar map mode
92     bool planarMapMode() const;
93 
94     // snapping
95     bool snapMode() const;
96     double snapThreshold() const;
97     void setSnapThreshold(double newSnapThreshold);
98 
99     // Sculpting
100     double sculptRadius() const;
101     void setSculptRadius(double newRadius);
102 
103     // Automatic topological cleaning
104     bool deleteIsolatedVertices();
105     bool deleteShortEdges();
106 
107     // Cursor position
108     Eigen::Vector2d sceneCursorPos() const;
109     void setSceneCursorPos(const Eigen::Vector2d & pos);
110 
111     // Colors
112     QColor edgeColor();
113     QColor faceColor();
114 
115     // Display modes
116     enum DisplayMode {
117         ILLUSTRATION,
118         OUTLINE,
119         ILLUSTRATION_OUTLINE
120     };
121     DisplayMode displayMode() const;
122     void setDisplayMode(DisplayMode mode);
123     bool showCanvas() const;
124 
125     // Active View and time
126     View * activeView() const;
127     View * hoveredView() const;
128     Time activeTime() const;
129     Timeline * timeline() const;
130 
131     // Other getters
132     MainWindow * mainWindow() const;
133     Scene * scene() const;
134     Settings & settings();
135     DevSettings * devSettings();
136 
137     // Settings ( = user settings + application state )
138     void readSettings();
139     void writeSettings();
140 
141     // GUI elements owned by global
142     QToolBar * toolModeToolBar() const;
143     QToolBar * toolBar() const;
144 
145     // Directory from which paths in document are relative to
146     void setDocumentDir(const QDir & dir);
147     QDir documentDir() const;
148 
149 signals:
150     void keyboardModifiersChanged();
151 
152 public slots:
153     void setToolMode(Global::ToolMode mode);
154     void togglePlanarMapMode();
155     void toggleSnapping();
156     void toggleStylusPressure();
157 
158     void setScalingCorner(bool b);
159     void setScalingEdge(bool b);
160     void setRotating(bool b);
161     void setDragAndDropping(bool b);
162     void setDraggingPivot(bool b);
163 
164     // Open preference dialog
165     void openPreferencesDialog();
166 
167     // Update widgets
168     void updateWidgetValuesFromPreferences();
169 
170     // Help message
171     void updateStatusBarHelp();
172 
173 protected:
174     // Global event filter
175     bool eventFilter(QObject * watched, QEvent * event);
176 
177     // Update keyboard modifiers
178     void updateModifiers();
179 
180     // Resolve ambiguous shortcuts
181     void resolveAmbiguousShortcuts(const QKeySequence & key);
182 
183 private slots:
184     void setEdgeWidth_(double w);
185 
186 
187 private:
188     // Tools
189     ToolModeAction * toolModeActions [NUMBER_OF_TOOL_MODES];
190 
191     // Color selector
192     QAction * colorSelectorAction_;
193 
194     // Tool Mode
195     ToolMode toolMode_;
196     QToolBar * toolBar_;
197 
198     // Tool options
199     QToolBar * toolModeToolBar_;
200 
201     // Is a selection being transformed?
202     bool isScalingCorner_;
203     bool isScalingEdge_;
204     bool isRotating_;
205     bool isDragAndDropping_;
206     bool isDraggingPivot_;
207 
208     // Select
209     QAction * actionChangeColor_;
210     QAction * actionChangeEdgeWidth_;
211     QAction * actionCreateFace_;
212     QAction * actionAddCycles_;
213     QAction * actionRemoveCycles_;
214     QAction * actionGlue_;
215     QAction * actionUnglue_;
216     QAction * actionUncut_;
217     // Sketch
218     QAction * actionPlanarMapMode_;
219     QAction * actionSnapMode_;
220     SpinBox * edgeWidth_;
221     QAction * actionEdgeWidth_;
222     SpinBox * snapThreshold_;
223     QAction * actionSnapThreshold_;
224     QAction * actionUseTabletPressure_;
225     // Sculpt
226     SpinBox * sculptRadius_;
227     QAction * actionSculptRadius_;
228 
229     // Separators
230     QAction * separatorSelect1_;
231     QAction * separatorSelect2_;
232     QAction * separatorSketch1_;
233     QAction * separatorSketch2_;
234     QAction * separatorSketch3_;
235 
236     // Scene cursor pos
237     double xSceneCursorPos_, ySceneCursorPos_;
238 
239     // Colors
240     ColorSelector * currentColor_;
241 
242     // Display modes
243     DisplayMode currentDisplayMode_;
244     DisplayMode switchToDisplayMode_;
245     DisplayMode otherDisplayMode_;
246     QAction * actionSwitchDisplayMode_;
247     QAction * actionSwitchToOtherDisplayMode_;
248 
249     // Others
250     MainWindow * mainWindow_;
251     Settings preferences_;
252     SettingsDialog * preferencesDialog_;
253     DevSettings * settings_;
254     Qt::KeyboardModifiers keyboardModifiers_;
255     QDir documentDir_;
256 
257     // Status bar help
258     QLabel * statusBarHelp_;
259 };
260 
261 class ToolModeAction: public QAction
262 {
263     Q_OBJECT
264 
265 public:
266     ToolModeAction(Global::ToolMode mode, QObject * parent = 0);
267 
268 signals:
269     void triggered(Global::ToolMode mode);
270 
271 private slots:
272     void emitSpecializedTriggered();
273 
274 private:
275     Global::ToolMode toolMode;
276 };
277 
278 Global * global();
279 
280 #endif // GLOBAL_H
281