mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-17 19:05:31 +08:00
refs #617, CGuiActionBindHandler to use QAction with hotkeys
* QMenu can be automatically turned into a list of CActionBind objects for hotkeys * trigger QAction from hotkeys
This commit is contained in:
committed by
Mathew Sutcliffe
parent
8c81f2bea3
commit
5be38086b8
85
src/blackgui/guiactionbind.cpp
Normal file
85
src/blackgui/guiactionbind.cpp
Normal file
@@ -0,0 +1,85 @@
|
|||||||
|
/* 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 "guiactionbind.h"
|
||||||
|
#include "blackmisc/fileutils.h"
|
||||||
|
|
||||||
|
using namespace BlackMisc;
|
||||||
|
using namespace BlackCore;
|
||||||
|
|
||||||
|
namespace BlackGui
|
||||||
|
{
|
||||||
|
CGuiActionBindHandler::CGuiActionBindHandler(QAction *action) : QObject(action)
|
||||||
|
{
|
||||||
|
this->setAction(action);
|
||||||
|
}
|
||||||
|
|
||||||
|
CGuiActionBindHandler::~CGuiActionBindHandler()
|
||||||
|
{
|
||||||
|
this->unbind();
|
||||||
|
}
|
||||||
|
|
||||||
|
CActionBindings CGuiActionBindHandler::bindMenu(QMenu *menu, const QString &path)
|
||||||
|
{
|
||||||
|
CActionBindings boundActions;
|
||||||
|
if (!menu || menu->isEmpty()) { return boundActions; }
|
||||||
|
for (QAction *action : menu->actions())
|
||||||
|
{
|
||||||
|
if (action->text().isEmpty()) { continue; }
|
||||||
|
if (action->isSeparator()) { continue; }
|
||||||
|
|
||||||
|
const QString pathNew = CGuiActionBindHandler::appendPath(path, action->text()).remove('&'); // remove E&xit key codes
|
||||||
|
if (action->menu())
|
||||||
|
{
|
||||||
|
CGuiActionBindHandler::bindMenu(action->menu(), pathNew);
|
||||||
|
}
|
||||||
|
|
||||||
|
CGuiActionBindHandler *bindHandler = new CGuiActionBindHandler(action);
|
||||||
|
QSharedPointer<CActionBind> actionBind(new CActionBind(pathNew, bindHandler, &CGuiActionBindHandler::boundFunction, [bindHandler]() { bindHandler->deleteLater(); }));
|
||||||
|
bindHandler->m_index = actionBind->getIndex();
|
||||||
|
boundActions.append(actionBind); // takes ownership
|
||||||
|
}
|
||||||
|
return boundActions;
|
||||||
|
}
|
||||||
|
|
||||||
|
void CGuiActionBindHandler::setAction(QAction *action)
|
||||||
|
{
|
||||||
|
this->m_action = action;
|
||||||
|
|
||||||
|
// if the action is destroyed from somewhere else I unbind myself
|
||||||
|
QObject::connect(action, &QAction::destroyed, [ = ]
|
||||||
|
{
|
||||||
|
this->unbind();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
void CGuiActionBindHandler::unbind()
|
||||||
|
{
|
||||||
|
if (!this->m_action) { return; }
|
||||||
|
this->m_action = nullptr;
|
||||||
|
if (CInputManager::instance())
|
||||||
|
{
|
||||||
|
CInputManager::instance()->unbind(this->m_index);
|
||||||
|
m_index = -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void CGuiActionBindHandler::boundFunction(bool enabled)
|
||||||
|
{
|
||||||
|
if (!enabled) { return; }
|
||||||
|
if (!m_action) { return; }
|
||||||
|
if (m_index < 0) { return; }
|
||||||
|
m_action->trigger();
|
||||||
|
}
|
||||||
|
|
||||||
|
QString CGuiActionBindHandler::appendPath(const QString &path, const QString &name)
|
||||||
|
{
|
||||||
|
return CFileUtils::appendFilePaths(path, name);
|
||||||
|
}
|
||||||
|
} // namespace
|
||||||
60
src/blackgui/guiactionbind.h
Normal file
60
src/blackgui/guiactionbind.h
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
/* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
//! \file
|
||||||
|
|
||||||
|
#ifndef BLACKGUI_GUIACTIONBIND_H
|
||||||
|
#define BLACKGUI_GUIACTIONBIND_H
|
||||||
|
|
||||||
|
#include "blackgui/blackguiexport.h"
|
||||||
|
#include "blackcore/actionbind.h"
|
||||||
|
|
||||||
|
#include <QMenu>
|
||||||
|
#include <QList>
|
||||||
|
#include <QScopedPointer>
|
||||||
|
|
||||||
|
namespace BlackGui
|
||||||
|
{
|
||||||
|
//! QObject derived handler to be registered with BlackCore::CActionBind
|
||||||
|
class BLACKGUI_EXPORT CGuiActionBindHandler : public QObject
|
||||||
|
{
|
||||||
|
Q_OBJECT
|
||||||
|
|
||||||
|
public:
|
||||||
|
//! Constructor
|
||||||
|
CGuiActionBindHandler(QAction *action);
|
||||||
|
|
||||||
|
//! Destructor
|
||||||
|
virtual ~CGuiActionBindHandler();
|
||||||
|
|
||||||
|
//! Bound function for BlackCore::CActionBind
|
||||||
|
void boundFunction(bool enabled);
|
||||||
|
|
||||||
|
//! Bind whole menu
|
||||||
|
static BlackCore::CActionBindings bindMenu(QMenu *menu, const QString &path = {});
|
||||||
|
|
||||||
|
private:
|
||||||
|
//! Corresponding action destroyed
|
||||||
|
void destroyed();
|
||||||
|
|
||||||
|
//! Set the action
|
||||||
|
void setAction(QAction *action);
|
||||||
|
|
||||||
|
//! Unbind this action
|
||||||
|
void unbind();
|
||||||
|
|
||||||
|
//! Append path for action
|
||||||
|
static QString appendPath(const QString &path, const QString &name);
|
||||||
|
|
||||||
|
int m_index = -1;
|
||||||
|
QAction *m_action = nullptr;
|
||||||
|
};
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
#endif // guard
|
||||||
Reference in New Issue
Block a user