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 
20 #include "KoToolFactoryBase.h"
21 
22 #include "KoToolBase.h"
23 #include <kactioncollection.h>
24 
25 #include <kis_action_registry.h>
26 #include <KoToolManager.h>
27 
28 #include <QKeySequence>
29 #include <QAction>
30 #include <QDebug>
31 
32 class Q_DECL_HIDDEN KoToolFactoryBase::Private
33 {
34 public:
Private(const QString & i)35     Private(const QString &i)
36         : priority(100),
37           id(i)
38     {
39     }
40     int priority;
41     QString section;
42     QString tooltip;
43     QString activationId;
44     QString iconName;
45     const QString id;
46     QKeySequence shortcut;
47 };
48 
49 
KoToolFactoryBase(const QString & id)50 KoToolFactoryBase::KoToolFactoryBase(const QString &id)
51     : d(new Private(id))
52 {
53 }
54 
~KoToolFactoryBase()55 KoToolFactoryBase::~KoToolFactoryBase()
56 {
57     delete d;
58 }
59 
createActions(KActionCollection * actionCollection)60 QList<QAction *> KoToolFactoryBase::createActions(KActionCollection *actionCollection)
61 {
62     QList<QAction *> toolActions;
63 
64     KisActionRegistry *actionRegistry = KisActionRegistry::instance();
65     QList<QAction*> actions = createActionsImpl();
66     QAction *action = actionRegistry->makeQAction(id());
67     actionCollection->addAction(id(), action);
68     connect(action, SIGNAL(triggered()), SLOT(activateTool()));
69     //qDebug() << action << action->shortcut();
70 
71 
72     Q_FOREACH(QAction *action, actions) {
73         if (action->objectName().isEmpty()) {
74             qWarning() << "Tool" << id() << "tries to add an action without a name";
75             continue;
76         }
77         QAction *existingAction = actionCollection->action(action->objectName());
78         if (existingAction) {
79             delete action;
80             action = existingAction;
81         }
82 
83         QStringList tools;
84         if (action->property("tool_action").isValid()) {
85             tools = action->property("tool_action").toStringList();
86         }
87         tools << id();
88         action->setProperty("tool_action", tools);
89         if (!existingAction) {
90             actionCollection->addAction(action->objectName(), action);
91         }
92         toolActions << action;
93     }
94 
95     // Enable this to easily generate action files for tools
96  #if 0
97     if (toolActions.size() > 0) {
98 
99         QDomDocument doc;
100         QDomElement e = doc.createElement("Actions");
101         e.setAttribute("name", id);
102         e.setAttribute("version", "2");
103         doc.appendChild(e);
104 
105         Q_FOREACH (QAction *action, toolActions) {
106             QDomElement a = doc.createElement("Action");
107             a.setAttribute("name", action->objectName());
108 
109             // But seriously, XML is the worst format ever designed
110             auto addElement = [&](QString title, QString content) {
111                 QDomElement newNode = doc.createElement(title);
112                 QDomText    newText = doc.createTextNode(content);
113                 newNode.appendChild(newText);
114                 a.appendChild(newNode);
115             };
116 
117             addElement("icon", action->icon().name());
118             addElement("text", action->text());
119             addElement("whatsThis" , action->whatsThis());
120             addElement("toolTip" , action->toolTip());
121             addElement("iconText" , action->iconText());
122             addElement("shortcut" , action->shortcut().toString());
123             addElement("isCheckable" , QString((action->isChecked() ? "true" : "false")));
124             addElement("statusTip", action->statusTip());
125             e.appendChild(a);
126         }
127         QFile f(id()z + ".action");
128         f.open(QFile::WriteOnly);
129         f.write(doc.toString().toUtf8());
130         f.close();
131 
132     }
133 
134     else {
135         debugFlake << "Tool" << id() << "has no actions";
136     }
137 #endif
138 
139 //    qDebug() << "Generated actions for tool factory" << id();
140 //    Q_FOREACH(QAction *action, toolActions) {
141 //        qDebug() << "\taction:" << action->objectName() << "shortcut" << action->shortcuts() << "tools" << action->property("tool_action").toStringList();
142 //    }
143     return toolActions;
144 }
145 
id() const146 QString KoToolFactoryBase::id() const
147 {
148     return d->id;
149 }
150 
priority() const151 int KoToolFactoryBase::priority() const
152 {
153     return d->priority;
154 }
155 
section() const156 QString KoToolFactoryBase::section() const
157 {
158     return d->section;
159 }
160 
toolTip() const161 QString KoToolFactoryBase::toolTip() const
162 {
163     return d->tooltip;
164 }
165 
iconName() const166 QString KoToolFactoryBase::iconName() const
167 {
168     return d->iconName;
169 }
170 
activationShapeId() const171 QString KoToolFactoryBase::activationShapeId() const
172 {
173     return d->activationId;
174 }
175 
shortcut() const176 QKeySequence KoToolFactoryBase::shortcut() const
177 {
178     return d->shortcut;
179 }
180 
setActivationShapeId(const QString & activationShapeId)181 void KoToolFactoryBase::setActivationShapeId(const QString &activationShapeId)
182 {
183     d->activationId = activationShapeId;
184 }
185 
setToolTip(const QString & tooltip)186 void KoToolFactoryBase::setToolTip(const QString & tooltip)
187 {
188     d->tooltip = tooltip;
189 }
190 
setSection(const QString & section)191 void KoToolFactoryBase::setSection(const QString & section)
192 {
193     d->section = section;
194 }
195 
setIconName(const char * iconName)196 void KoToolFactoryBase::setIconName(const char *iconName)
197 {
198     d->iconName = QLatin1String(iconName);
199 }
200 
setIconName(const QString & iconName)201 void KoToolFactoryBase::setIconName(const QString &iconName)
202 {
203     d->iconName = iconName;
204 }
205 
setPriority(int newPriority)206 void KoToolFactoryBase::setPriority(int newPriority)
207 {
208     d->priority = newPriority;
209 }
210 
setShortcut(const QKeySequence & shortcut)211 void KoToolFactoryBase::setShortcut(const QKeySequence &shortcut)
212 {
213     d->shortcut = shortcut;
214 }
215 
createActionsImpl()216 QList<QAction *> KoToolFactoryBase::createActionsImpl()
217 {
218     return QList<QAction *>();
219 }
220 
activateTool()221 void KoToolFactoryBase::activateTool()
222 {
223     KoToolManager::instance()->switchToolRequested(sender()->objectName());
224 }
225 
226