1 /* This file is part of the KDE project
2  * Copyright (C) 2006 Thomas Zander <zander@kde.org>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public License
15  * along with this library; see the file COPYING.LIB.  If not, write to
16  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19 #ifndef KO_TOOL_MANAGER_P
20 #define KO_TOOL_MANAGER_P
21 
22 #include <QList>
23 #include <QObject>
24 #include <QString>
25 #include <QHash>
26 
27 #include <QKeySequence>
28 #include <QAction>
29 
30 #include "KoInputDevice.h"
31 #include "KoToolManager.h"
32 
33 class KoToolFactoryBase;
34 class KoShapeManager;
35 class KoCanvasBase;
36 class KoToolBase;
37 class KoShape;
38 class KoToolManager;
39 class KoCanvasController;
40 class KoShapeLayer;
41 class ToolHelper;
42 class CanvasData;
43 class KoToolProxy;
44 
45 class Q_DECL_HIDDEN KoToolManager::Private
46 {
47 public:
48     Private(KoToolManager *qq);
49     ~Private();
50 
51     void setup();
52 
53     void connectActiveTool();
54     void disconnectActiveTool();
55     void switchTool(KoToolBase *tool, bool temporary);
56     void switchTool(const QString &id, bool temporary);
57     void postSwitchTool(bool temporary);
58     void switchCanvasData(CanvasData *cd);
59 
60     bool eventFilter(QObject *object, QEvent *event);
61     void toolActivated(ToolHelper *tool);
62 
63     void detachCanvas(KoCanvasController *controller);
64     void attachCanvas(KoCanvasController *controller);
65     void movedFocus(QWidget *from, QWidget *to);
66     void updateCursor(const QCursor &cursor);
67     void switchBackRequested();
68     void selectionChanged(const QList<KoShape*> &shapes);
69     void currentLayerChanged(const KoShapeLayer *layer);
70     void updateToolForProxy();
71     void switchToolTemporaryRequested(const QString &id);
72     CanvasData *createCanvasData(KoCanvasController *controller, const KoInputDevice &device);
73 
74     /**
75      * Request a switch from to the param input device.
76      * This will cause the tool for that device to be selected.
77      */
78     void switchInputDevice(const KoInputDevice &device);
79 
80     /**
81      * Whenever a new tool proxy class is instantiated, it will use this method to register itself
82      * so the toolManager can update it to the latest active tool.
83      * @param proxy the proxy to register.
84      * @param canvas which canvas the proxy is associated with; whenever a new tool is selected for that canvas,
85      *        the proxy gets an update.
86      */
87     void registerToolProxy(KoToolProxy *proxy, KoCanvasBase *canvas);
88 
89     KoToolManager *q;
90 
91     QList<ToolHelper*> tools; // list of all available tools via their factories.
92 
93     QHash<KoToolBase*, int> uniqueToolIds; // for the changedTool signal
94     QHash<KoCanvasController*, QList<CanvasData*> > canvasses;
95     QHash<KoCanvasBase*, KoToolProxy*> proxies;
96 
97     CanvasData *canvasData; // data about the active canvas.
98 
99     KoInputDevice inputDevice;
100 
101     bool layerExplicitlyDisabled;
102 };
103 
104 /// \internal
105 class ToolHelper : public QObject
106 {
107     Q_OBJECT
108 public:
109     explicit ToolHelper(KoToolFactoryBase *tool);
110     KoToolAction *toolAction();
111     /// wrapper around KoToolFactoryBase::id();
112     QString id() const;
113     /// wrapper around KoToolFactoryBase::iconName();
114     QString iconName() const;
115     /// descriptive text, as ;
116     QString text() const;
117     /// descriptive icon text, e.g. use on a button next to an icon or without one;
118     QString iconText() const;
119     /// tooltip of the tool, e.g. for tooltip of a button;
120     QString toolTip() const;
121     /// wrapper around KoToolFactoryBase::toolType();
122     QString section() const;
123     /// wrapper around KoToolFactoryBase::activationShapeId();
124     QString activationShapeId() const;
125     /// wrapper around KoToolFactoryBase::priority();
126     int priority() const;
127     KoToolBase *createTool(KoCanvasBase *canvas) const;
128     /// unique id, >= 0
uniqueId()129     int uniqueId() const {
130         return m_uniqueId;
131     }
132     /// QAction->shortcut() if it exists, otherwise KoToolFactoryBase::shortcut()
133     QKeySequence shortcut() const;
134 
135 public Q_SLOTS:
136     void activate();
137 
138 Q_SIGNALS:
139     /// Emitted when the tool should be activated, e.g. by pressing the tool's assigned button in the toolbox
140     void toolActivated(ToolHelper *tool);
141 
142 private:
143     KoToolFactoryBase * const m_toolFactory;
144     const int m_uniqueId;
145     QKeySequence m_customShortcut;
146     bool m_hasCustomShortcut;
147     KoToolAction *m_toolAction;
148 };
149 
150 /// \internal
151 /// Helper class to transform a simple signal selection changed into a signal with a parameter
152 class Connector : public QObject
153 {
154     Q_OBJECT
155 public:
156     explicit Connector(KoShapeManager *parent);
157 
158 public Q_SLOTS:
159     void selectionChanged();
160 
161 Q_SIGNALS:
162     void selectionChanged(const QList<KoShape*> &shape);
163 
164 private:
165     KoShapeManager *m_shapeManager;
166 };
167 
168 #endif
169