Warning, file /office/calligra/libs/flake/KoToolManager_p.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /* This file is part of the KDE project
0002  * Copyright (C) 2006 Thomas Zander <zander@kde.org>
0003  *
0004  * This library is free software; you can redistribute it and/or
0005  * modify it under the terms of the GNU Library General Public
0006  * License as published by the Free Software Foundation; either
0007  * version 2 of the License, or (at your option) any later version.
0008  *
0009  * This library is distributed in the hope that it will be useful,
0010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012  * Library General Public License for more details.
0013  *
0014  * You should have received a copy of the GNU Library General Public License
0015  * along with this library; see the file COPYING.LIB.  If not, write to
0016  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017  * Boston, MA 02110-1301, USA.
0018  */
0019 
0020 #include "KoToolManager_p.h"
0021 
0022 #include <KoShapeManager.h>
0023 #include <KoSelection.h>
0024 #include <KoToolBase.h>
0025 #include <KoToolFactoryBase.h>
0026 
0027 
0028 static int newUniqueToolHelperId()
0029 {
0030     static int idCounter = 0;
0031     return ++idCounter;
0032 }
0033 
0034 /*    ************ ToolHelper **********
0035  * This class wrangles the tool factory, toolbox button and switch-tool action
0036  * for a single tool. It assumes the  will continue to live once it is created.
0037  * (Hiding the toolbox is OK.)
0038  */
0039 
0040 ToolHelper::ToolHelper(KoToolFactoryBase *tool)
0041     : m_toolFactory(tool),
0042       m_uniqueId(newUniqueToolHelperId()),
0043       m_hasCustomShortcut(false),
0044       m_toolAction(0)
0045 {
0046     // TODO: how to get an existing custom shortcut in the beginning here?
0047     // Once the first ShortcutToolAction is added to the actionCollection,
0048     // it will get any custom shortcut set by the actionCollection and
0049     // by that trigger shortcutToolActionUpdated().
0050     // But until then shortcut() will report a wrong shortcut and e.g. show
0051     // that in the tooltips of the KoToolBox.
0052 }
0053 
0054 KoToolAction *ToolHelper::toolAction()
0055 {
0056     // create lazily
0057     if (!m_toolAction) {
0058         m_toolAction = new KoToolAction(this);
0059     }
0060     return m_toolAction;
0061 }
0062 
0063 QString ToolHelper::id() const
0064 {
0065     return m_toolFactory->id();
0066 }
0067 
0068 QString ToolHelper::activationShapeId() const
0069 {
0070     return m_toolFactory->activationShapeId();
0071 }
0072 
0073 QString ToolHelper::iconName() const
0074 {
0075     return m_toolFactory->iconName();
0076 }
0077 
0078 QString ToolHelper::text() const
0079 {
0080     // TODO: add text property to KoToolFactoryBase
0081     return m_toolFactory->toolTip();
0082 }
0083 
0084 QString ToolHelper::iconText() const
0085 {
0086     // TODO: add text iconText to KoToolFactoryBase
0087     return m_toolFactory->toolTip();
0088 }
0089 
0090 QString ToolHelper::toolTip() const
0091 {
0092     return m_toolFactory->toolTip();
0093 }
0094 
0095 void ToolHelper::activate()
0096 {
0097     emit toolActivated(this);
0098 }
0099 
0100 void ToolHelper::shortcutToolActionUpdated()
0101 {
0102     ShortcutToolAction *action = static_cast<ShortcutToolAction*>(sender());
0103     // check if shortcut changed
0104     const QKeySequence actionShortcut = action->shortcut();
0105     const QKeySequence currentShortcut = shortcut();
0106     if (actionShortcut != currentShortcut) {
0107         m_hasCustomShortcut = true;
0108         m_customShortcut = actionShortcut;
0109         if (m_toolAction) {
0110             emit m_toolAction->changed();
0111         }
0112         // no need to forward the new shortcut to the other ShortcutToolAction objects,
0113         // they are synchronized behind the scenes
0114         // Thus they will also trigger this method, but due to them having
0115         // the same shortcut not result in any further action.
0116     }
0117 }
0118 
0119 KoToolBase *ToolHelper::createTool(KoCanvasBase *canvas) const
0120 {
0121     KoToolBase *tool = m_toolFactory->createTool(canvas);
0122     if (tool) {
0123         tool->setToolId(id());
0124     }
0125     return tool;
0126 }
0127 
0128 ShortcutToolAction* ToolHelper::createShortcutToolAction(QObject *parent)
0129 {
0130     ShortcutToolAction* action = new ShortcutToolAction(id(), text(), parent);
0131     action->setShortcut(shortcut());
0132 
0133     connect(action, SIGNAL(changed()), SLOT(shortcutToolActionUpdated()));
0134 
0135     return action;
0136 }
0137 
0138 QString ToolHelper::toolType() const
0139 {
0140     return m_toolFactory->toolType();
0141 }
0142 
0143 int ToolHelper::priority() const
0144 {
0145     return m_toolFactory->priority();
0146 }
0147 
0148 QKeySequence ToolHelper::shortcut() const
0149 {
0150     if (m_hasCustomShortcut) {
0151         return m_customShortcut;
0152     }
0153 
0154     return m_toolFactory->shortcut();
0155 }
0156 
0157 //   ************ Connector **********
0158 Connector::Connector(KoShapeManager *parent)
0159         : QObject(parent),
0160         m_shapeManager(parent)
0161 {
0162     connect(m_shapeManager, SIGNAL(selectionChanged()), this, SLOT(selectionChanged()));
0163 }
0164 
0165 void Connector::selectionChanged()
0166 {
0167     emit selectionChanged(m_shapeManager->selection()->selectedShapes());
0168 }
0169 
0170 //   ************ ShortcutToolAction **********
0171 ShortcutToolAction::ShortcutToolAction(const QString &id, const QString &name, QObject *parent)
0172     : QAction(name, parent)
0173     , m_toolID(id)
0174 {
0175     connect(this, SIGNAL(triggered(bool)), this, SLOT(actionTriggered()));
0176 }
0177 
0178 ShortcutToolAction::~ShortcutToolAction()
0179 {
0180 }
0181 
0182 void ShortcutToolAction::actionTriggered()
0183 {
0184     // TODO: why not ToolHelper::activate(); and thus a slightly different behaviour?
0185     KoToolManager::instance()->switchToolRequested(m_toolID);
0186 }
0187