diff --git a/src/blackcore/actionbind.cpp b/src/blackcore/actionbind.cpp new file mode 100644 index 000000000..c89e65a14 --- /dev/null +++ b/src/blackcore/actionbind.cpp @@ -0,0 +1,37 @@ +/* Copyright (C) 2017 + * swift project community / contributors + * + * This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level + * directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project, + * including this file, may be copied, modified, propagated, or distributed except according to the terms + * contained in the LICENSE file. + */ + +#include "actionbind.h" + +namespace BlackCore +{ + CActionBind::~CActionBind() + { + unbind(); + if (m_deleteCallback) + { + m_deleteCallback(); + } + } + + void CActionBind::unbind() + { + if (m_index < 0) { return; } + auto inputManger = CInputManager::instance(); + inputManger->unbind(m_index); + m_index = -1; + } + + QString CActionBind::normalizeAction(const QString &action) + { + QString n = action.trimmed(); + if (!n.startsWith('/')) { return n.insert(0, QChar('/')); } + return n; + } +} diff --git a/src/blackcore/actionbind.h b/src/blackcore/actionbind.h index 02142b146..845951bf3 100644 --- a/src/blackcore/actionbind.h +++ b/src/blackcore/actionbind.h @@ -13,13 +13,14 @@ #define BLACKCORE_ACTIONBIND_H #include "blackcore/inputmanager.h" +#include "blackcoreexport.h" namespace BlackCore { /*! * CActionBind binds a member function to an action */ - class CActionBind + class BLACKCORE_EXPORT CActionBind { public: //! Signature of receiving member function @@ -28,24 +29,37 @@ namespace BlackCore //! Constructor template - CActionBind(const QString &action, Receiver *receiver, MembFunc slot = nullptr) + CActionBind(const QString &action, Receiver *receiver, MembFunc slot = nullptr, const std::function &deleteCallback = {}) : + m_deleteCallback(deleteCallback) { + const QString a = CActionBind::normalizeAction(action); auto inputManger = CInputManager::instance(); - inputManger->registerAction(action); - m_index = inputManger->bind(action, receiver, slot); + inputManger->registerAction(a); + m_index = inputManger->bind(a, receiver, slot); } //! Destructor - ~CActionBind() - { - auto inputManger = CInputManager::instance(); - inputManger->unbind(m_index); - } + ~CActionBind(); + + //! Unbind from BlackCore::CInputManager + void unbind(); + + //! Bound with BlackCore::CInputManager + bool isBound() const { return m_index >= 0; } + + //! Index + int getIndex() const { return m_index; } private: - int m_index; + //! normalize the name string + static QString normalizeAction(const QString &action); + + int m_index = -1; //!< action indexx (unique) + std::function m_deleteCallback; //!< called when deleted }; + + //! List of bindings + using CActionBindings = QList>; } #endif -