mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-04 16:56:53 +08:00
refs #617, CActionBind extended
* delete callback when dtor is called (can use related receivers) * added cpp file * CActionBindings for multiple bindings (shared pointers) * normalize action string
This commit is contained in:
committed by
Mathew Sutcliffe
parent
eeaed099f0
commit
8c81f2bea3
37
src/blackcore/actionbind.cpp
Normal file
37
src/blackcore/actionbind.cpp
Normal file
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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 <typename Receiver>
|
||||
CActionBind(const QString &action, Receiver *receiver, MembFunc<Receiver> slot = nullptr)
|
||||
CActionBind(const QString &action, Receiver *receiver, MembFunc<Receiver> slot = nullptr, const std::function<void()> &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<void()> m_deleteCallback; //!< called when deleted
|
||||
};
|
||||
|
||||
//! List of bindings
|
||||
using CActionBindings = QList<QSharedPointer<CActionBind>>;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
Reference in New Issue
Block a user