File indexing completed on 2024-05-12 15:56:51
0001 /* This file is part of the KDE project 0002 * SPDX-FileCopyrightText: 2006 Thomas Zander <zander@kde.org> 0003 * 0004 * SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #include "KoToolFactoryBase.h" 0008 0009 #include "KoToolBase.h" 0010 #include <kactioncollection.h> 0011 0012 #include <kis_action_registry.h> 0013 #include <KoToolManager.h> 0014 0015 #include <QKeySequence> 0016 #include <QAction> 0017 #include <QDebug> 0018 0019 class Q_DECL_HIDDEN KoToolFactoryBase::Private 0020 { 0021 public: 0022 Private(const QString &i) 0023 : priority(100), 0024 id(i) 0025 { 0026 } 0027 int priority; 0028 QString section; 0029 QString tooltip; 0030 QString activationId; 0031 QString iconName; 0032 const QString id; 0033 QKeySequence shortcut; 0034 }; 0035 0036 0037 KoToolFactoryBase::KoToolFactoryBase(const QString &id) 0038 : d(new Private(id)) 0039 { 0040 } 0041 0042 KoToolFactoryBase::~KoToolFactoryBase() 0043 { 0044 delete d; 0045 } 0046 0047 QList<QAction *> KoToolFactoryBase::createActions(KisKActionCollection *actionCollection) 0048 { 0049 QList<QAction *> toolActions; 0050 0051 KisActionRegistry *actionRegistry = KisActionRegistry::instance(); 0052 QList<QAction*> actions = createActionsImpl(); 0053 QAction *action = actionRegistry->makeQAction(id(), this); 0054 actionCollection->addAction(id(), action); 0055 connect(action, SIGNAL(triggered()), SLOT(activateTool())); 0056 //qDebug() << action << action->shortcut(); 0057 0058 0059 Q_FOREACH(QAction *action, actions) { 0060 if (action->objectName().isEmpty()) { 0061 qWarning() << "Tool" << id() << "tries to add an action without a name"; 0062 continue; 0063 } 0064 QAction *existingAction = actionCollection->action(action->objectName()); 0065 if (existingAction) { 0066 delete action; 0067 action = existingAction; 0068 } 0069 0070 QStringList tools; 0071 if (action->property("tool_action").isValid()) { 0072 tools = action->property("tool_action").toStringList(); 0073 } 0074 tools << id(); 0075 action->setProperty("tool_action", tools); 0076 if (!existingAction) { 0077 actionCollection->addAction(action->objectName(), action); 0078 } 0079 toolActions << action; 0080 } 0081 0082 // Enable this to easily generate action files for tools 0083 #if 0 0084 if (toolActions.size() > 0) { 0085 0086 QDomDocument doc; 0087 QDomElement e = doc.createElement("Actions"); 0088 e.setAttribute("name", id); 0089 e.setAttribute("version", "2"); 0090 doc.appendChild(e); 0091 0092 Q_FOREACH (QAction *action, toolActions) { 0093 QDomElement a = doc.createElement("Action"); 0094 a.setAttribute("name", action->objectName()); 0095 0096 // But seriously, XML is the worst format ever designed 0097 auto addElement = [&](QString title, QString content) { 0098 QDomElement newNode = doc.createElement(title); 0099 QDomText newText = doc.createTextNode(content); 0100 newNode.appendChild(newText); 0101 a.appendChild(newNode); 0102 }; 0103 0104 addElement("icon", action->icon().name()); 0105 addElement("text", action->text()); 0106 addElement("whatsThis" , action->whatsThis()); 0107 addElement("toolTip" , action->toolTip()); 0108 addElement("iconText" , action->iconText()); 0109 addElement("shortcut" , action->shortcut().toString()); 0110 addElement("isCheckable" , QString((action->isChecked() ? "true" : "false"))); 0111 addElement("statusTip", action->statusTip()); 0112 e.appendChild(a); 0113 } 0114 QFile f(id()z + ".action"); 0115 f.open(QFile::WriteOnly); 0116 f.write(doc.toString().toUtf8()); 0117 f.close(); 0118 0119 } 0120 0121 else { 0122 debugFlake << "Tool" << id() << "has no actions"; 0123 } 0124 #endif 0125 0126 // qDebug() << "Generated actions for tool factory" << id(); 0127 // Q_FOREACH(QAction *action, toolActions) { 0128 // qDebug() << "\taction:" << action->objectName() << "shortcut" << action->shortcuts() << "tools" << action->property("tool_action").toStringList(); 0129 // } 0130 return toolActions; 0131 } 0132 0133 QString KoToolFactoryBase::id() const 0134 { 0135 return d->id; 0136 } 0137 0138 int KoToolFactoryBase::priority() const 0139 { 0140 return d->priority; 0141 } 0142 0143 QString KoToolFactoryBase::section() const 0144 { 0145 return d->section; 0146 } 0147 0148 QString KoToolFactoryBase::toolTip() const 0149 { 0150 return d->tooltip; 0151 } 0152 0153 QString KoToolFactoryBase::iconName() const 0154 { 0155 return d->iconName; 0156 } 0157 0158 QString KoToolFactoryBase::activationShapeId() const 0159 { 0160 return d->activationId; 0161 } 0162 0163 QKeySequence KoToolFactoryBase::shortcut() const 0164 { 0165 return d->shortcut; 0166 } 0167 0168 void KoToolFactoryBase::setActivationShapeId(const QString &activationShapeId) 0169 { 0170 d->activationId = activationShapeId; 0171 } 0172 0173 void KoToolFactoryBase::setToolTip(const QString & tooltip) 0174 { 0175 d->tooltip = tooltip; 0176 } 0177 0178 void KoToolFactoryBase::setSection(const QString & section) 0179 { 0180 d->section = section; 0181 } 0182 0183 void KoToolFactoryBase::setIconName(const char *iconName) 0184 { 0185 d->iconName = QLatin1String(iconName); 0186 } 0187 0188 void KoToolFactoryBase::setIconName(const QString &iconName) 0189 { 0190 d->iconName = iconName; 0191 } 0192 0193 void KoToolFactoryBase::setPriority(int newPriority) 0194 { 0195 d->priority = newPriority; 0196 } 0197 0198 void KoToolFactoryBase::setShortcut(const QKeySequence &shortcut) 0199 { 0200 d->shortcut = shortcut; 0201 } 0202 0203 QList<QAction *> KoToolFactoryBase::createActionsImpl() 0204 { 0205 return QList<QAction *>(); 0206 } 0207 0208 void KoToolFactoryBase::activateTool() 0209 { 0210 KoToolManager::instance()->switchToolRequested(sender()->objectName()); 0211 } 0212