@ -1,27 +1,20 @@
# include <QCoreApplication>
# include <QDebug>
# include <QFile>
# include <QJsonDocument>
# include <Q FileInfo >
# include <QJsonArray>
# include <QJsonDocument>
# include <QMetaEnum>
# include <QFileInfo>
# include <QCoreApplication>
# include <QDebug>
# include "keymap.h"
QString KeyMap : : s_keyMapPath = " " ;
KeyMap : : KeyMap ( QObject * parent )
: QObject ( parent )
{
KeyMap : : KeyMap ( QObject * parent ) : QObject ( parent ) { }
}
KeyMap : : ~ KeyMap ( ) { }
KeyMap : : ~ KeyMap ( )
{
}
const QString & KeyMap : : getKeyMapPath ( )
const QString & KeyMap : : getKeyMapPath ( )
{
if ( s_keyMapPath . isEmpty ( ) ) {
s_keyMapPath = QString : : fromLocal8Bit ( qgetenv ( " QTSCRCPY_KEYMAP_PATH " ) ) ;
@ -43,7 +36,7 @@ void KeyMap::loadKeyMap(const QString &json)
jsonDoc = QJsonDocument : : fromJson ( json . toUtf8 ( ) , & jsonError ) ;
if ( jsonError . error ! = QJsonParseError : : NoError ) {
if ( jsonError . error ! = QJsonParseError : : NoError ) {
errorString = QString ( " json error: %1 " ) . arg ( jsonError . errorString ( ) ) ;
goto parseError ;
}
@ -57,13 +50,13 @@ void KeyMap::loadKeyMap(const QString &json)
}
switchKey = getItemKey ( rootObj , " switchKey " ) ;
if ( switchKey . first = = AT_INVALID ) {
if ( switchKey . first = = AT_INVALID ) {
errorString = QString ( " json error: switchKey invalid " ) ;
goto parseError ;
}
m_switchKey . type = switchKey . first ;
m_switchKey . key = switchKey . second ;
m_switchKey . key = switchKey . second ;
// mouseMoveMap
if ( checkItemObject ( rootObj , " mouseMoveMap " ) ) {
@ -110,8 +103,7 @@ void KeyMap::loadKeyMap(const QString &json)
KeyMap : : KeyMapType type = getItemKeyMapType ( node , " type " ) ;
switch ( type ) {
case KeyMap : : KMT_CLICK :
{
case KeyMap : : KMT_CLICK : {
// safe check
if ( ! checkForClick ( node ) ) {
qWarning ( ) < < " json error: keyMapNodes node format error " ;
@ -129,10 +121,8 @@ void KeyMap::loadKeyMap(const QString &json)
keyMapNode . data . click . keyNode . pos = getItemPos ( node , " pos " ) ;
keyMapNode . data . click . switchMap = getItemBool ( node , " switchMap " ) ;
m_keyMapNodes . push_back ( keyMapNode ) ;
}
break ;
case KeyMap : : KMT_CLICK_TWICE :
{
} break ;
case KeyMap : : KMT_CLICK_TWICE : {
// safe check
if ( ! checkForClickTwice ( node ) ) {
qWarning ( ) < < " json error: keyMapNodes node format error " ;
@ -151,10 +141,8 @@ void KeyMap::loadKeyMap(const QString &json)
keyMapNode . data . click . keyNode . pos = getItemPos ( node , " pos " ) ;
keyMapNode . data . click . switchMap = getItemBool ( node , " switchMap " ) ;
m_keyMapNodes . push_back ( keyMapNode ) ;
}
break ;
case KeyMap : : KMT_STEER_WHEEL :
{
} break ;
case KeyMap : : KMT_STEER_WHEEL : {
// safe check
if ( ! checkForSteerWhell ( node ) ) {
qWarning ( ) < < " json error: keyMapNodes node format error " ;
@ -164,8 +152,7 @@ void KeyMap::loadKeyMap(const QString &json)
QPair < ActionType , int > rightKey = getItemKey ( node , " rightKey " ) ;
QPair < ActionType , int > upKey = getItemKey ( node , " upKey " ) ;
QPair < ActionType , int > downKey = getItemKey ( node , " downKey " ) ;
if ( leftKey . first = = AT_INVALID | | rightKey . first = = AT_INVALID
| | upKey . first = = AT_INVALID | | downKey . first = = AT_INVALID ) {
if ( leftKey . first = = AT_INVALID | | rightKey . first = = AT_INVALID | | upKey . first = = AT_INVALID | | downKey . first = = AT_INVALID ) {
if ( leftKey . first = = AT_INVALID ) {
qWarning ( ) < < " json error: keyMapNodes node invalid key: " < < node . value ( " leftKey " ) . toString ( ) ;
}
@ -184,26 +171,16 @@ void KeyMap::loadKeyMap(const QString &json)
KeyMapNode keyMapNode ;
keyMapNode . type = type ;
keyMapNode . data . steerWheel . left = { leftKey . first , leftKey . second ,
QPointF ( 0 , 0 ) , QPointF ( 0 , 0 ) ,
getItemDouble ( node , " leftOffset " ) } ;
keyMapNode . data . steerWheel . right = { rightKey . first , rightKey . second ,
QPointF ( 0 , 0 ) , QPointF ( 0 , 0 ) ,
getItemDouble ( node , " rightOffset " ) } ;
keyMapNode . data . steerWheel . up = { upKey . first , upKey . second ,
QPointF ( 0 , 0 ) , QPointF ( 0 , 0 ) ,
getItemDouble ( node , " upOffset " ) } ;
keyMapNode . data . steerWheel . down = { downKey . first , downKey . second ,
QPointF ( 0 , 0 ) , QPointF ( 0 , 0 ) ,
getItemDouble ( node , " downOffset " ) } ;
keyMapNode . data . steerWheel . left = { leftKey . first , leftKey . second , QPointF ( 0 , 0 ) , QPointF ( 0 , 0 ) , getItemDouble ( node , " leftOffset " ) } ;
keyMapNode . data . steerWheel . right = { rightKey . first , rightKey . second , QPointF ( 0 , 0 ) , QPointF ( 0 , 0 ) , getItemDouble ( node , " rightOffset " ) } ;
keyMapNode . data . steerWheel . up = { upKey . first , upKey . second , QPointF ( 0 , 0 ) , QPointF ( 0 , 0 ) , getItemDouble ( node , " upOffset " ) } ;
keyMapNode . data . steerWheel . down = { downKey . first , downKey . second , QPointF ( 0 , 0 ) , QPointF ( 0 , 0 ) , getItemDouble ( node , " downOffset " ) } ;
keyMapNode . data . steerWheel . centerPos = getItemPos ( node , " centerPos " ) ;
m_idxSteerWheel = m_keyMapNodes . size ( ) ;
m_keyMapNodes . push_back ( keyMapNode ) ;
}
break ;
case KeyMap : : KMT_DRAG :
{
} break ;
case KeyMap : : KMT_DRAG : {
// safe check
if ( ! checkForDrag ( node ) ) {
qWarning ( ) < < " json error: keyMapNodes node format error " ;
@ -241,7 +218,7 @@ parseError:
return ;
}
const KeyMap : : KeyMapNode & KeyMap : : getKeyMapNode ( int key )
const KeyMap : : KeyMapNode & KeyMap : : getKeyMapNode ( int key )
{
auto p = m_rmapKey . value ( key , & m_invalidNode ) ;
if ( p = = & m_invalidNode ) {
@ -250,12 +227,12 @@ const KeyMap::KeyMapNode& KeyMap::getKeyMapNode(int key)
return * p ;
}
const KeyMap : : KeyMapNode & KeyMap : : getKeyMapNodeKey ( int key )
const KeyMap : : KeyMapNode & KeyMap : : getKeyMapNodeKey ( int key )
{
return * m_rmapKey . value ( key , & m_invalidNode ) ;
}
const KeyMap : : KeyMapNode & KeyMap : : getKeyMapNodeMouse ( int key )
const KeyMap : : KeyMapNode & KeyMap : : getKeyMapNodeMouse ( int key )
{
return * m_rmapMouse . value ( key , & m_invalidNode ) ;
}
@ -270,7 +247,7 @@ int KeyMap::getSwitchKey()
return m_switchKey . key ;
}
const KeyMap : : KeyMapNode & KeyMap : : getMouseMoveMap ( )
const KeyMap : : KeyMapNode & KeyMap : : getMouseMoveMap ( )
{
return m_keyMapNodes [ m_idxMouseMove ] ;
}
@ -289,39 +266,31 @@ void KeyMap::makeReverseMap()
{
m_rmapKey . clear ( ) ;
m_rmapMouse . clear ( ) ;
for ( int i = 0 ; i < m_keyMapNodes . size ( ) ; + + i ) {
auto & node = m_keyMapNodes [ i ] ;
for ( int i = 0 ; i < m_keyMapNodes . size ( ) ; + + i ) {
auto & node = m_keyMapNodes [ i ] ;
switch ( node . type ) {
case KMT_CLICK :
{
QMultiHash < int , KeyMapNode * > & m = node . data . click . keyNode . type = = AT_KEY ? m_rmapKey : m_rmapMouse ;
case KMT_CLICK : {
QMultiHash < int , KeyMapNode * > & m = node . data . click . keyNode . type = = AT_KEY ? m_rmapKey : m_rmapMouse ;
m . insert ( node . data . click . keyNode . key , & node ) ;
}
break ;
case KMT_CLICK_TWICE :
{
QMultiHash < int , KeyMapNode * > & m = node . data . clickTwice . keyNode . type = = AT_KEY ? m_rmapKey : m_rmapMouse ;
} break ;
case KMT_CLICK_TWICE : {
QMultiHash < int , KeyMapNode * > & m = node . data . clickTwice . keyNode . type = = AT_KEY ? m_rmapKey : m_rmapMouse ;
m . insert ( node . data . clickTwice . keyNode . key , & node ) ;
}
break ;
case KMT_STEER_WHEEL :
{
QMultiHash < int , KeyMapNode * > & ml = node . data . steerWheel . left . type = = AT_KEY ? m_rmapKey : m_rmapMouse ;
} break ;
case KMT_STEER_WHEEL : {
QMultiHash < int , KeyMapNode * > & ml = node . data . steerWheel . left . type = = AT_KEY ? m_rmapKey : m_rmapMouse ;
ml . insert ( node . data . steerWheel . left . key , & node ) ;
QMultiHash < int , KeyMapNode * > & mr = node . data . steerWheel . right . type = = AT_KEY ? m_rmapKey : m_rmapMouse ;
QMultiHash < int , KeyMapNode * > & mr = node . data . steerWheel . right . type = = AT_KEY ? m_rmapKey : m_rmapMouse ;
mr . insert ( node . data . steerWheel . right . key , & node ) ;
QMultiHash < int , KeyMapNode * > & mu = node . data . steerWheel . up . type = = AT_KEY ? m_rmapKey : m_rmapMouse ;
QMultiHash < int , KeyMapNode * > & mu = node . data . steerWheel . up . type = = AT_KEY ? m_rmapKey : m_rmapMouse ;
mu . insert ( node . data . steerWheel . up . key , & node ) ;
QMultiHash < int , KeyMapNode * > & md = node . data . steerWheel . down . type = = AT_KEY ? m_rmapKey : m_rmapMouse ;
QMultiHash < int , KeyMapNode * > & md = node . data . steerWheel . down . type = = AT_KEY ? m_rmapKey : m_rmapMouse ;
md . insert ( node . data . steerWheel . down . key , & node ) ;
}
break ;
case KMT_DRAG :
{
QMultiHash < int , KeyMapNode * > & m = node . data . drag . keyNode . type = = AT_KEY ? m_rmapKey : m_rmapMouse ;
} break ;
case KMT_DRAG : {
QMultiHash < int , KeyMapNode * > & m = node . data . drag . keyNode . type = = AT_KEY ? m_rmapKey : m_rmapMouse ;
m . insert ( node . data . drag . keyNode . key , & node ) ;
}
break ;
} break ;
default :
break ;
}
@ -333,12 +302,12 @@ QString KeyMap::getItemString(const QJsonObject &node, const QString &name)
return node . value ( name ) . toString ( ) ;
}
double KeyMap : : getItemDouble ( const QJsonObject & node , const QString & name )
double KeyMap : : getItemDouble ( const QJsonObject & node , const QString & name )
{
return node . value ( name ) . toDouble ( ) ;
}
bool KeyMap : : getItemBool ( const QJsonObject & node , const QString & name )
bool KeyMap : : getItemBool ( const QJsonObject & node , const QString & name )
{
return node . value ( name ) . toBool ( false ) ;
}
@ -348,43 +317,43 @@ QJsonObject KeyMap::getItemObject(const QJsonObject &node, const QString &name)
return node . value ( name ) . toObject ( ) ;
}
QPointF KeyMap : : getItemPos ( const QJsonObject & node , const QString & name )
QPointF KeyMap : : getItemPos ( const QJsonObject & node , const QString & name )
{
QJsonObject pos = node . value ( name ) . toObject ( ) ;
return QPointF ( pos . value ( " x " ) . toDouble ( ) , pos . value ( " y " ) . toDouble ( ) ) ;
}
QPair < KeyMap : : ActionType , int > KeyMap : : getItemKey ( const QJsonObject & node , const QString & name )
QPair < KeyMap : : ActionType , int > KeyMap : : getItemKey ( const QJsonObject & node , const QString & name )
{
QString value = getItemString ( node , name ) ;
int key = m_metaEnumKey . keyToValue ( value . toStdString ( ) . c_str ( ) ) ;
int btn = m_metaEnumMouseButtons . keyToValue ( value . toStdString ( ) . c_str ( ) ) ;
if ( key = = - 1 & & btn = = - 1 ) {
return { AT_INVALID , - 1 } ;
return { AT_INVALID , - 1 } ;
} else if ( key ! = - 1 ) {
return { AT_KEY , key } ;
return { AT_KEY , key } ;
} else {
return { AT_MOUSE , btn } ;
return { AT_MOUSE , btn } ;
}
}
KeyMap : : KeyMapType KeyMap : : getItemKeyMapType ( const QJsonObject & node , const QString & name )
KeyMap : : KeyMapType KeyMap : : getItemKeyMapType ( const QJsonObject & node , const QString & name )
{
QString value = getItemString ( node , name ) ;
return static_cast < KeyMap : : KeyMapType > ( m_metaEnumKeyMapType . keyToValue ( value . toStdString ( ) . c_str ( ) ) ) ;
}
bool KeyMap : : checkItemString ( const QJsonObject & node , const QString & name )
bool KeyMap : : checkItemString ( const QJsonObject & node , const QString & name )
{
return node . contains ( name ) & & node . value ( name ) . isString ( ) ;
}
bool KeyMap : : checkItemDouble ( const QJsonObject & node , const QString & name )
bool KeyMap : : checkItemDouble ( const QJsonObject & node , const QString & name )
{
return node . contains ( name ) & & node . value ( name ) . isDouble ( ) ;
}
bool KeyMap : : checkItemBool ( const QJsonObject & node , const QString & name )
bool KeyMap : : checkItemBool ( const QJsonObject & node , const QString & name )
{
return node . contains ( name ) & & node . value ( name ) . isBool ( ) ;
}
@ -394,38 +363,33 @@ bool KeyMap::checkItemObject(const QJsonObject &node, const QString &name)
return node . contains ( name ) & & node . value ( name ) . isObject ( ) ;
}
bool KeyMap : : checkItemPos ( const QJsonObject & node , const QString & name )
bool KeyMap : : checkItemPos ( const QJsonObject & node , const QString & name )
{
if ( node . contains ( name ) & & node . value ( name ) . isObject ( ) ) {
QJsonObject pos = node . value ( name ) . toObject ( ) ;
return pos . contains ( " x " ) & & pos . value ( " x " ) . isDouble ( )
& & pos . contains ( " y " ) & & pos . value ( " y " ) . isDouble ( ) ;
return pos . contains ( " x " ) & & pos . value ( " x " ) . isDouble ( ) & & pos . contains ( " y " ) & & pos . value ( " y " ) . isDouble ( ) ;
}
return false ;
}
bool KeyMap : : checkForClick ( const QJsonObject & node )
bool KeyMap : : checkForClick ( const QJsonObject & node )
{
return checkForClickTwice ( node ) & & checkItemBool ( node , " switchMap " ) ;
}
bool KeyMap : : checkForClickTwice ( const QJsonObject & node )
bool KeyMap : : checkForClickTwice ( const QJsonObject & node )
{
return checkItemString ( node , " key " ) & & checkItemPos ( node , " pos " ) ;
}
bool KeyMap : : checkForSteerWhell ( const QJsonObject & node )
bool KeyMap : : checkForSteerWhell ( const QJsonObject & node )
{
return checkItemString ( node , " leftKey " ) & & checkItemString ( node , " rightKey " )
& & checkItemString ( node , " upKey " ) & & checkItemString ( node , " downKey " )
& & checkItemDouble ( node , " leftOffset " ) & & checkItemDouble ( node , " rightOffset " )
& & checkItemDouble ( node , " upOffset " ) & & checkItemDouble ( node , " downOffset " )
& & checkItemPos ( node , " centerPos " ) ;
return checkItemString ( node , " leftKey " ) & & checkItemString ( node , " rightKey " ) & & checkItemString ( node , " upKey " ) & & checkItemString ( node , " downKey " )
& & checkItemDouble ( node , " leftOffset " ) & & checkItemDouble ( node , " rightOffset " ) & & checkItemDouble ( node , " upOffset " )
& & checkItemDouble ( node , " downOffset " ) & & checkItemPos ( node , " centerPos " ) ;
}
bool KeyMap : : checkForDrag ( const QJsonObject & node )
bool KeyMap : : checkForDrag ( const QJsonObject & node )
{
return checkItemString ( node , " key " )
& & checkItemPos ( node , " startPos " ) & & checkItemPos ( node , " endPos " ) ;
return checkItemString ( node , " key " ) & & checkItemPos ( node , " startPos " ) & & checkItemPos ( node , " endPos " ) ;
}