qskinny/src/controls/QskShortcut.h

227 lines
7.7 KiB
C
Raw Normal View History

2017-07-21 16:21:34 +00:00
/******************************************************************************
* QSkinny - Copyright (C) 2016 Uwe Rathmann
* This file may be used under the terms of the QSkinny License, Version 1.0
*****************************************************************************/
#ifndef QSK_SHORTCUT_H
#define QSK_SHORTCUT_H
#include "QskGlobal.h"
2017-12-05 16:40:21 +00:00
#include <QQuickWindow>
2017-07-21 16:21:34 +00:00
class QQuickItem;
2017-07-21 16:21:34 +00:00
class QKeySequence;
class QSK_EXPORT QskShortcut
2017-07-21 16:21:34 +00:00
{
public:
2017-12-05 16:40:21 +00:00
static void setAutoRepeat( int, bool on );
static void setEnabled( int, bool on );
static void removeShortcut( int );
// -- traditional slots
static int addShortcut( const QKeySequence&, bool autoRepeat,
2017-12-03 16:58:18 +00:00
const QObject* receiver, const char* method );
2017-07-21 16:21:34 +00:00
static int addShortcut( QQuickWindow*, const QKeySequence&, bool autoRepeat,
2017-12-03 16:58:18 +00:00
const QObject* receiver, const char* method );
static int addShortcut( QQuickItem*, const QKeySequence&, bool autoRepeat,
const QObject* receiver, const char* method );
2017-12-03 16:58:18 +00:00
2017-12-05 16:40:21 +00:00
// -- calling a QObject method
2017-12-03 16:58:18 +00:00
template< typename Func1 >
static int addShortcut( const QKeySequence&, bool autoRepeat,
const typename QtPrivate::FunctionPointer< Func1 >::Object* receiver, Func1 slot );
2017-12-03 16:58:18 +00:00
2017-12-05 16:40:21 +00:00
template< typename Func1 >
static int addShortcut( QQuickWindow*, const QKeySequence&, bool autoRepeat,
const typename QtPrivate::FunctionPointer< Func1 >::Object* receiver, Func1 slot );
template< typename Func1 >
static int addShortcut( QQuickItem*, const QKeySequence&, bool autoRepeat,
const typename QtPrivate::FunctionPointer< Func1 >::Object* receiver, Func1 slot );
// -- calling a functor or function pointer inside a thread context
template< typename Func1 >
static int addShortcut( const QKeySequence&, bool autoRepeat,
const QObject* context, Func1 slot );
2017-12-03 16:58:18 +00:00
2017-12-05 16:40:21 +00:00
template< typename Func1 >
static int addShortcut( QQuickWindow*, const QKeySequence&, bool autoRepeat,
const QObject* context, Func1 slot );
template< typename Func1 >
static int addShortcut( QQuickItem*, const QKeySequence&, bool autoRepeat,
const QObject* context, Func1 slot );
// -- calling a functor or function pointer
2017-12-03 16:58:18 +00:00
template< typename Func1 >
static int addShortcut( const QKeySequence&, bool autoRepeat, Func1 slot );
2017-12-03 16:58:18 +00:00
2017-12-05 16:40:21 +00:00
template< typename Func1 >
static int addShortcut( QQuickWindow*, const QKeySequence&, bool autoRepeat, Func1 slot );
template< typename Func1 >
static int addShortcut( QQuickItem*, const QKeySequence&, bool autoRepeat, Func1 slot );
2017-12-03 16:58:18 +00:00
private:
QskShortcut() = delete;
~QskShortcut() = delete;
2017-12-03 16:58:18 +00:00
2017-12-05 16:40:21 +00:00
static int addMethod( QQuickItem*, const QKeySequence&, bool autoRepeat,
const QObject* receiver, const char* method );
template< typename Func1 >
static int addMemberSlotObject( QQuickItem*, const QKeySequence&, bool autoRepeat,
const typename QtPrivate::FunctionPointer< Func1 >::Object* receiver, Func1 slot );
template< typename Func1 >
static int addFunctionSlotObject( QQuickItem*, const QKeySequence&,
bool autoRepeat, const QObject* context, Func1 slot );
static int addSlotObject(
QQuickItem* item, const QKeySequence&, bool autoRepeat,
const QObject* receiver, QtPrivate::QSlotObjectBase* );
};
2017-12-03 16:58:18 +00:00
template< typename Func1 >
2017-12-05 16:40:21 +00:00
inline int QskShortcut::addFunctionSlotObject(
QQuickItem* item, const QKeySequence& key, bool autoRepeat,
const QObject* context, Func1 slot )
{
using namespace QtPrivate;
typedef FunctionPointer< Func1 > SlotType;
Q_STATIC_ASSERT_X( int( SlotType::ArgumentCount ) <= 0,
"The slot must not have any arguments.");
Q_STATIC_ASSERT_X( !SlotType::IsPointerToMemberFunction,
"The slot must be no member function." );
auto slotObj = new QFunctorSlotObject< Func1, 0,
typename List_Left< void, 0 >::Value, void >( std::move( slot ) );
return addSlotObject( item, key, autoRepeat, context, slotObj );
}
// -- traditional slots
inline int QskShortcut::addShortcut(
QQuickItem* item, const QKeySequence& key, bool autoRepeat,
const QObject* receiver, const char* method )
{
return addMethod( item, key, autoRepeat, receiver, method );
}
inline int QskShortcut::addShortcut(
const QKeySequence& key, bool autoRepeat,
const QObject* receiver, const char* method )
{
return addMethod( nullptr, key, autoRepeat, receiver, method );
}
inline int QskShortcut::addShortcut(
QQuickWindow* window, const QKeySequence& key, bool autoRepeat,
const QObject* receiver, const char* method )
{
auto item = window ? window->contentItem() : nullptr;
return addMethod( item, key, autoRepeat, receiver, method );
}
// -- calling a QObject method
template< typename Func1 >
inline int QskShortcut::addMemberSlotObject(
QQuickItem* item, const QKeySequence& key, bool autoRepeat,
const typename QtPrivate::FunctionPointer< Func1 >::Object* receiver, Func1 slot )
{
typedef QtPrivate::FunctionPointer< Func1 > SlotType;
2017-12-03 16:58:18 +00:00
Q_STATIC_ASSERT_X( int( SlotType::ArgumentCount ) == 0,
"The slot must not have any arguments.");
2017-12-03 16:58:18 +00:00
2017-12-05 16:40:21 +00:00
auto slotObject = new QtPrivate::QSlotObject< Func1,
typename SlotType::Arguments, void >(slot);
2017-12-05 16:40:21 +00:00
return addSlotObject( item, key, autoRepeat, receiver, slotObject );
}
template< typename Func1 >
inline int QskShortcut::addShortcut(
QQuickItem* item, const QKeySequence& key, bool autoRepeat,
const typename QtPrivate::FunctionPointer< Func1 >::Object* receiver, Func1 slot )
{
return addMemberSlotObject( item, key, autoRepeat, receiver, slot );
}
template< typename Func1 >
inline int QskShortcut::addShortcut(
QQuickWindow* window, const QKeySequence& key, bool autoRepeat,
const typename QtPrivate::FunctionPointer< Func1 >::Object* receiver, Func1 slot )
{
auto item = window ? window->contentItem() : nullptr;
return addMemberSlotObject( item, key, autoRepeat, receiver, slot );
}
template< typename Func1 >
inline int QskShortcut::addShortcut(
const QKeySequence& key, bool autoRepeat,
const typename QtPrivate::FunctionPointer< Func1 >::Object* receiver, Func1 slot )
{
return addMemberSlotObject( nullptr, key, autoRepeat, receiver, slot );
}
2017-12-05 16:40:21 +00:00
// -- calling a functor or function pointer with context
template< typename Func1 >
2017-12-05 16:40:21 +00:00
inline int QskShortcut::addShortcut(
QQuickItem* item, const QKeySequence& key, bool autoRepeat,
const QObject* context, Func1 slot )
{
2017-12-05 16:40:21 +00:00
return addFunctionSlotObject( item, key, autoRepeat, context, slot );
}
2017-12-05 16:40:21 +00:00
template< typename Func1 >
inline int QskShortcut::addShortcut(
QQuickWindow* window, const QKeySequence& key, bool autoRepeat,
const QObject* context, Func1 slot )
{
auto item = window ? window->contentItem() : nullptr;
return addFunctionSlotObject( item, key, autoRepeat, context, slot );
}
2017-12-05 16:40:21 +00:00
template< typename Func1 >
inline int QskShortcut::addShortcut( const QKeySequence& key, bool autoRepeat,
const QObject* context, Func1 slot )
{
return addFunctionSlotObject( nullptr, key, autoRepeat, context, slot );
}
2017-12-05 16:40:21 +00:00
// -- calling a functor or function pointer
2017-12-05 16:40:21 +00:00
template< typename Func1 >
inline int QskShortcut::addShortcut(
QQuickItem* item, const QKeySequence& key, bool autoRepeat, Func1 slot )
{
return addFunctionSlotObject( item, key, autoRepeat, nullptr, slot );
}
2017-12-05 16:40:21 +00:00
template< typename Func1 >
inline int QskShortcut::addShortcut(
QQuickWindow* window, const QKeySequence& key, bool autoRepeat, Func1 slot )
{
auto item = window ? window->contentItem() : nullptr;
return addFunctionSlotObject( item, key, autoRepeat, nullptr, autoRepeat, slot );
}
template< typename Func1 >
int QskShortcut::addShortcut( const QKeySequence& key, bool autoRepeat, Func1 slot )
{
2017-12-05 16:40:21 +00:00
return addFunctionSlotObject( nullptr, key, autoRepeat, nullptr, slot );
2017-07-21 16:21:34 +00:00
}
#endif