Krita Source Code Documentation
Loading...
Searching...
No Matches
KoToolFactoryBase.cpp
Go to the documentation of this file.
1/* This file is part of the KDE project
2 * SPDX-FileCopyrightText: 2006 Thomas Zander <zander@kde.org>
3 *
4 * SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6
7#include "KoToolFactoryBase.h"
8
9#include "KoToolBase.h"
10#include <kactioncollection.h>
11
12#include <kis_action_registry.h>
13#include <KoToolManager.h>
14
15#include <QKeySequence>
16#include <QAction>
17#include <QDebug>
18
19class Q_DECL_HIDDEN KoToolFactoryBase::Private
20{
21public:
22 Private(const QString &i)
23 : priority(100),
24 id(i)
25 {
26 }
28 QString section;
29 QString tooltip;
30 QString activationId;
31 QString iconName;
32 QString toolName;
33 const QString id;
34 QKeySequence shortcut;
35};
36
37
39 : d(new Private(id))
40{
41}
42
47
49{
50 QList<QAction *> toolActions;
51
53 QList<QAction*> actions = createActionsImpl(toolManager);
54 QAction *action = actionRegistry->makeQAction(id(), this);
55 actionCollection->addAction(id(), action);
56 connect(action, &QAction::triggered, toolManager, [toolId = id(), toolManager] () {
57 toolManager->switchToolRequested(toolId);
58 });
59
60 Q_FOREACH(QAction *action, actions) {
61 if (action->objectName().isEmpty()) {
62 qWarning() << "Tool" << id() << "tries to add an action without a name";
63 continue;
64 }
65 QAction *existingAction = actionCollection->action(action->objectName());
66 if (existingAction) {
67 delete action;
68 action = existingAction;
69 }
70
71 QStringList tools;
72 if (action->property("tool_action").isValid()) {
73 tools = action->property("tool_action").toStringList();
74 }
75 tools << id();
76 action->setProperty("tool_action", tools);
77 if (!existingAction) {
78 actionCollection->addAction(action->objectName(), action);
79 }
80 toolActions << action;
81 }
82
83 // Enable this to easily generate action files for tools
84 #if 0
85 if (toolActions.size() > 0) {
86
87 QDomDocument doc;
88 QDomElement e = doc.createElement("Actions");
89 e.setAttribute("name", id);
90 e.setAttribute("version", "2");
91 doc.appendChild(e);
92
93 Q_FOREACH (QAction *action, toolActions) {
94 QDomElement a = doc.createElement("Action");
95 a.setAttribute("name", action->objectName());
96
97 // But seriously, XML is the worst format ever designed
98 auto addElement = [&](QString title, QString content) {
99 QDomElement newNode = doc.createElement(title);
100 QDomText newText = doc.createTextNode(content);
101 newNode.appendChild(newText);
102 a.appendChild(newNode);
103 };
104
105 addElement("icon", action->icon().name());
106 addElement("text", action->text());
107 addElement("whatsThis" , action->whatsThis());
108 addElement("toolTip" , action->toolTip());
109 addElement("iconText" , action->iconText());
110 addElement("shortcut" , action->shortcut().toString());
111 addElement("isCheckable" , QString((action->isChecked() ? "true" : "false")));
112 addElement("statusTip", action->statusTip());
113 e.appendChild(a);
114 }
115 QFile f(id()z + ".action");
116 f.open(QFile::WriteOnly);
117 f.write(doc.toString().toUtf8());
118 f.close();
119
120 }
121
122 else {
123 debugFlake << "Tool" << id() << "has no actions";
124 }
125#endif
126
127// qDebug() << "Generated actions for tool factory" << id();
128// Q_FOREACH(QAction *action, toolActions) {
129// qDebug() << "\taction:" << action->objectName() << "shortcut" << action->shortcuts() << "tools" << action->property("tool_action").toStringList();
130// }
131 return toolActions;
132}
133
134QString KoToolFactoryBase::id() const
135{
136 return d->id;
137}
138
140{
141 return d->priority;
142}
143
144QString KoToolFactoryBase::section() const
145{
146 return d->section;
147}
148
150{
151 return d->tooltip;
152}
153
154QString KoToolFactoryBase::toolName() const
155{
156 return d->toolName;
157}
158
159QString KoToolFactoryBase::iconName() const
160{
161 return d->iconName;
162}
163
165{
166 return d->activationId;
167}
168
169QKeySequence KoToolFactoryBase::shortcut() const
170{
171 return d->shortcut;
172}
173
174void KoToolFactoryBase::setActivationShapeId(const QString &activationShapeId)
175{
176 d->activationId = activationShapeId;
177}
178
179void KoToolFactoryBase::setToolTip(const QString & tooltip)
180{
181 d->tooltip = tooltip;
182}
183
184void KoToolFactoryBase::setToolName(const QString & toolName)
185{
186 d->toolName = toolName;
187}
188
189void KoToolFactoryBase::setSection(const QString & section)
190{
191 d->section = section;
192}
193
194void KoToolFactoryBase::setIconName(const char *iconName)
195{
196 d->iconName = QLatin1String(iconName);
197}
198
199void KoToolFactoryBase::setIconName(const QString &iconName)
200{
201 d->iconName = iconName;
202}
203
205{
206 d->priority = newPriority;
207}
208
209void KoToolFactoryBase::setShortcut(const QKeySequence &shortcut)
210{
211 d->shortcut = shortcut;
212}
213
215{
216 Q_UNUSED(toolManager);
217 return QList<QAction *>();
218}
#define debugFlake
Definition FlakeDebug.h:15
QAction * makeQAction(const QString &name, QObject *parent=0)
static KisActionRegistry * instance()
A container for a set of QAction objects.
Q_INVOKABLE QAction * addAction(const QString &name, QAction *action)
QAction * action(int index) const
void setShortcut(const QKeySequence &shortcut)
void setSection(const QString &section)
void setActivationShapeId(const QString &activationShapeId)
QList< QAction * > createActions(KisKActionCollection *actionCollection, KoToolManager *toolManager)
void setToolName(const QString &toolName)
void setIconName(const char *iconName)
KoToolFactoryBase(const QString &id)
void setToolTip(const QString &tooltip)
QString toolTip() const
virtual QList< QAction * > createActionsImpl(KoToolManager *toolManager)
createActionsImpl should be reimplemented if the tool needs any actions. The actions should have a va...
QString activationShapeId() const
Private(const QString &i)
void setPriority(int newPriority)
void switchToolRequested(const QString &id)