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 const QString id;
33 QKeySequence shortcut;
34};
35
36
38 : d(new Private(id))
39{
40}
41
46
48{
49 QList<QAction *> toolActions;
50
53 QAction *action = actionRegistry->makeQAction(id(), this);
54 actionCollection->addAction(id(), action);
55 connect(action, SIGNAL(triggered()), SLOT(activateTool()));
56 //qDebug() << action << action->shortcut();
57
58
59 Q_FOREACH(QAction *action, actions) {
60 if (action->objectName().isEmpty()) {
61 qWarning() << "Tool" << id() << "tries to add an action without a name";
62 continue;
63 }
64 QAction *existingAction = actionCollection->action(action->objectName());
65 if (existingAction) {
66 delete action;
67 action = existingAction;
68 }
69
70 QStringList tools;
71 if (action->property("tool_action").isValid()) {
72 tools = action->property("tool_action").toStringList();
73 }
74 tools << id();
75 action->setProperty("tool_action", tools);
76 if (!existingAction) {
77 actionCollection->addAction(action->objectName(), action);
78 }
79 toolActions << action;
80 }
81
82 // Enable this to easily generate action files for tools
83 #if 0
84 if (toolActions.size() > 0) {
85
86 QDomDocument doc;
87 QDomElement e = doc.createElement("Actions");
88 e.setAttribute("name", id);
89 e.setAttribute("version", "2");
90 doc.appendChild(e);
91
92 Q_FOREACH (QAction *action, toolActions) {
93 QDomElement a = doc.createElement("Action");
94 a.setAttribute("name", action->objectName());
95
96 // But seriously, XML is the worst format ever designed
97 auto addElement = [&](QString title, QString content) {
98 QDomElement newNode = doc.createElement(title);
99 QDomText newText = doc.createTextNode(content);
100 newNode.appendChild(newText);
101 a.appendChild(newNode);
102 };
103
104 addElement("icon", action->icon().name());
105 addElement("text", action->text());
106 addElement("whatsThis" , action->whatsThis());
107 addElement("toolTip" , action->toolTip());
108 addElement("iconText" , action->iconText());
109 addElement("shortcut" , action->shortcut().toString());
110 addElement("isCheckable" , QString((action->isChecked() ? "true" : "false")));
111 addElement("statusTip", action->statusTip());
112 e.appendChild(a);
113 }
114 QFile f(id()z + ".action");
115 f.open(QFile::WriteOnly);
116 f.write(doc.toString().toUtf8());
117 f.close();
118
119 }
120
121 else {
122 debugFlake << "Tool" << id() << "has no actions";
123 }
124#endif
125
126// qDebug() << "Generated actions for tool factory" << id();
127// Q_FOREACH(QAction *action, toolActions) {
128// qDebug() << "\taction:" << action->objectName() << "shortcut" << action->shortcuts() << "tools" << action->property("tool_action").toStringList();
129// }
130 return toolActions;
131}
132
133QString KoToolFactoryBase::id() const
134{
135 return d->id;
136}
137
139{
140 return d->priority;
141}
142
143QString KoToolFactoryBase::section() const
144{
145 return d->section;
146}
147
149{
150 return d->tooltip;
151}
152
153QString KoToolFactoryBase::iconName() const
154{
155 return d->iconName;
156}
157
159{
160 return d->activationId;
161}
162
163QKeySequence KoToolFactoryBase::shortcut() const
164{
165 return d->shortcut;
166}
167
168void KoToolFactoryBase::setActivationShapeId(const QString &activationShapeId)
169{
170 d->activationId = activationShapeId;
171}
172
173void KoToolFactoryBase::setToolTip(const QString & tooltip)
174{
175 d->tooltip = tooltip;
176}
177
178void KoToolFactoryBase::setSection(const QString & section)
179{
180 d->section = section;
181}
182
183void KoToolFactoryBase::setIconName(const char *iconName)
184{
185 d->iconName = QLatin1String(iconName);
186}
187
188void KoToolFactoryBase::setIconName(const QString &iconName)
189{
190 d->iconName = iconName;
191}
192
194{
195 d->priority = newPriority;
196}
197
198void KoToolFactoryBase::setShortcut(const QKeySequence &shortcut)
199{
200 d->shortcut = shortcut;
201}
202
207
209{
210 KoToolManager::instance()->switchToolRequested(sender()->objectName());
211}
212
#define debugFlake
Definition FlakeDebug.h:15
connect(this, SIGNAL(optionsChanged()), this, SLOT(saveOptions()))
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
QList< QAction * > createActions(KisKActionCollection *actionCollection)
void setShortcut(const QKeySequence &shortcut)
void setSection(const QString &section)
void setActivationShapeId(const QString &activationShapeId)
void setIconName(const char *iconName)
KoToolFactoryBase(const QString &id)
void setToolTip(const QString &tooltip)
QString toolTip() const
QString activationShapeId() const
virtual QList< QAction * > createActionsImpl()
createActionsImpl should be reimplemented if the tool needs any actions. The actions should have a va...
Private(const QString &i)
void setPriority(int newPriority)
void switchToolRequested(const QString &id)
static KoToolManager * instance()
Return the toolmanager singleton.