mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-05 09:15:34 +08:00
refs #396 BlackMisc: nw* goes to network/, hw* goes to hardware/
This commit is contained in:
committed by
Roland Winklmeier
parent
2363fab8c1
commit
985a1caecf
16
src/blackmisc/hardware/hardware.h
Normal file
16
src/blackmisc/hardware/hardware.h
Normal file
@@ -0,0 +1,16 @@
|
||||
/* Copyright (C) 2013
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#ifndef BLACKMISC_HARDWARE_HARDWAREALL_H
|
||||
#define BLACKMISC_HARDWARE_HARDWAREALL_H
|
||||
|
||||
#include "blackmisc/hardware/keyboardkeylist.h"
|
||||
#include "blackmisc/hardware/joystickbutton.h"
|
||||
|
||||
#endif // guard
|
||||
99
src/blackmisc/hardware/joystickbutton.cpp
Normal file
99
src/blackmisc/hardware/joystickbutton.cpp
Normal file
@@ -0,0 +1,99 @@
|
||||
/* Copyright (C) 2014
|
||||
* 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 "blackmisc/hardware/joystickbutton.h"
|
||||
#include "blackmisc/blackmiscfreefunctions.h"
|
||||
#include "blackmisc/variant.h"
|
||||
#include <QCoreApplication>
|
||||
|
||||
namespace BlackMisc
|
||||
{
|
||||
namespace Hardware
|
||||
{
|
||||
CJoystickButton::CJoystickButton(qint32 buttonIndex) :
|
||||
m_buttonIndex(buttonIndex)
|
||||
{}
|
||||
|
||||
void CJoystickButton::setButtonIndex(qint32 buttonIndex)
|
||||
{
|
||||
m_buttonIndex = buttonIndex;
|
||||
}
|
||||
|
||||
void CJoystickButton::setButtonObject(const CJoystickButton &button)
|
||||
{
|
||||
this->m_buttonIndex = button.m_buttonIndex;
|
||||
}
|
||||
|
||||
void CJoystickButton::setPropertyByIndex(const CVariant &variant, const CPropertyIndex &index)
|
||||
{
|
||||
if (index.isMyself())
|
||||
{
|
||||
this->convertFromCVariant(variant);
|
||||
return;
|
||||
}
|
||||
ColumnIndex i = index.frontCasted<ColumnIndex>();
|
||||
|
||||
switch (i)
|
||||
{
|
||||
case IndexButton:
|
||||
case IndexButtonAsString:
|
||||
this->setButtonIndex(buttonIndexFromString(variant.value<QString>()));
|
||||
break;
|
||||
case IndeButtonObject:
|
||||
this->setButtonObject(variant.value<BlackMisc::Hardware::CJoystickButton>());
|
||||
break;
|
||||
default:
|
||||
Q_ASSERT_X(false, "CJoystickButton", "index unknown (setter)");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
CVariant CJoystickButton::propertyByIndex(const BlackMisc::CPropertyIndex &index) const
|
||||
{
|
||||
if (index.isMyself()) { return this->toCVariant(); }
|
||||
ColumnIndex i = index.frontCasted<ColumnIndex>();
|
||||
switch (i)
|
||||
{
|
||||
case IndexButton:
|
||||
return CVariant::from(this->getButtonIndex());
|
||||
case IndexButtonAsString:
|
||||
return CVariant::from(this->getButtonAsString());
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
Q_ASSERT_X(false, "CJoystickButton", "index unknown");
|
||||
QString m = QString("no property, index ").append(index.toQString());
|
||||
return CVariant::fromValue(m);
|
||||
}
|
||||
|
||||
QString CJoystickButton::buttonIndexToString(qint32 buttonIndex)
|
||||
{
|
||||
QString buttonString("Button");
|
||||
return buttonString.append(QString("%1").arg(buttonIndex));
|
||||
}
|
||||
|
||||
qint32 CJoystickButton::buttonIndexFromString(const QString &buttonName)
|
||||
{
|
||||
QString name("Button");
|
||||
if (!buttonName.startsWith(name)) return getInvalidIndex();
|
||||
|
||||
name.remove("Button");
|
||||
return name.toInt();
|
||||
}
|
||||
|
||||
QString CJoystickButton::convertToQString(bool /* i18n */) const
|
||||
{
|
||||
QString s = getButtonAsString();
|
||||
return s.trimmed();
|
||||
}
|
||||
|
||||
} // namespace Hardware
|
||||
|
||||
} // BlackMisc
|
||||
87
src/blackmisc/hardware/joystickbutton.h
Normal file
87
src/blackmisc/hardware/joystickbutton.h
Normal file
@@ -0,0 +1,87 @@
|
||||
/* Copyright (C) 2014
|
||||
* 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 BLACKMISC_HARDWARE_JOYSTICKBUTTON_H
|
||||
#define BLACKMISC_HARDWARE_JOYSTICKBUTTON_H
|
||||
|
||||
#include "blackmisc/valueobject.h"
|
||||
#include "blackmisc/propertyindex.h"
|
||||
#include "blackmisc/blackmiscfreefunctions.h"
|
||||
|
||||
namespace BlackMisc
|
||||
{
|
||||
namespace Hardware
|
||||
{
|
||||
//! Value object representing a joystick button
|
||||
class CJoystickButton : public CValueObject<CJoystickButton>
|
||||
{
|
||||
public:
|
||||
//! Properties by index
|
||||
enum ColumnIndex
|
||||
{
|
||||
IndexButton = 0,
|
||||
IndexButtonAsString,
|
||||
IndeButtonObject, // just for updates
|
||||
};
|
||||
|
||||
//! Default constructor
|
||||
CJoystickButton() = default;
|
||||
|
||||
//! Constructor
|
||||
CJoystickButton(qint32 buttonIndex);
|
||||
|
||||
//! Get button index
|
||||
qint32 getButtonIndex() const { return m_buttonIndex; }
|
||||
|
||||
//! Get button as String
|
||||
QString getButtonAsString() const { return buttonIndexToString(m_buttonIndex); }
|
||||
|
||||
//! Set button index
|
||||
void setButtonIndex(qint32 buttonIndex);
|
||||
|
||||
//! Is valid?
|
||||
bool isValid() const { return m_buttonIndex >= 0 ? true : false; }
|
||||
|
||||
//! Set button object
|
||||
void setButtonObject(const BlackMisc::Hardware::CJoystickButton &button);
|
||||
|
||||
//! \copydoc CValueObject::setPropertyByIndex
|
||||
virtual void setPropertyByIndex(const CVariant &variant, const BlackMisc::CPropertyIndex &index);
|
||||
|
||||
//! \copydoc CValueObject::propertyByIndex
|
||||
virtual CVariant propertyByIndex(const CPropertyIndex &index) const;
|
||||
|
||||
//! Button index to string
|
||||
static QString buttonIndexToString(qint32 buttonIndex);
|
||||
|
||||
//! Button index from string
|
||||
static qint32 buttonIndexFromString(const QString &button);
|
||||
|
||||
//! Invalid button index
|
||||
static qint32 getInvalidIndex() { return m_invalidIndex; }
|
||||
|
||||
protected:
|
||||
//! \copydoc CValueObject::convertToQString
|
||||
virtual QString convertToQString(bool i18n = false) const override;
|
||||
|
||||
private:
|
||||
BLACK_ENABLE_TUPLE_CONVERSION(CJoystickButton)
|
||||
qint32 m_buttonIndex = m_invalidIndex; //!< code similar to Qt::Key
|
||||
|
||||
static const qint32 m_invalidIndex = -1;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
BLACK_DECLARE_TUPLE_CONVERSION(BlackMisc::Hardware::CJoystickButton, (o.m_buttonIndex))
|
||||
Q_DECLARE_METATYPE(BlackMisc::Hardware::CJoystickButton)
|
||||
|
||||
#endif // guard
|
||||
314
src/blackmisc/hardware/keyboardkey.cpp
Normal file
314
src/blackmisc/hardware/keyboardkey.cpp
Normal file
@@ -0,0 +1,314 @@
|
||||
/* Copyright (C) 2013
|
||||
* 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 "blackmisc/hardware/keyboardkey.h"
|
||||
#include "blackmisc/blackmiscfreefunctions.h"
|
||||
#include "blackmisc/propertyindex.h"
|
||||
#include "blackmisc/variant.h"
|
||||
#include <QCoreApplication>
|
||||
|
||||
namespace BlackMisc
|
||||
{
|
||||
namespace Hardware
|
||||
{
|
||||
CKeyboardKey::CKeyboardKey() :
|
||||
m_qtKey(Qt::Key_unknown), m_modifier1(ModifierNone), m_modifier2(ModifierNone)
|
||||
{}
|
||||
|
||||
CKeyboardKey::CKeyboardKey(Qt::Key keyCode, Modifier modifier1, Modifier modifier2) :
|
||||
m_qtKey(keyCode), m_modifier1(modifier1), m_modifier2(modifier2)
|
||||
{}
|
||||
|
||||
QString CKeyboardKey::convertToQString(bool /* i18n */) const
|
||||
{
|
||||
QString s = this->getModifier1AsString();
|
||||
s.append(" ").append(this->getModifier2AsString()).append(" ");
|
||||
if (this->m_qtKey != 0) s.append(" ").append(this->getKeyAsStringRepresentation());
|
||||
return s.trimmed();
|
||||
}
|
||||
|
||||
QString CKeyboardKey::modifierToString(CKeyboardKey::Modifier modifier)
|
||||
{
|
||||
switch (modifier)
|
||||
{
|
||||
case ModifierNone: return "";
|
||||
case ModifierAltAny: return "Alt";
|
||||
case ModifierAltLeft: return "Alt Left";
|
||||
case ModifierAltRight: return "Alt Right";
|
||||
case ModifierShiftAny: return "Shift";
|
||||
case ModifierShiftLeft: return "Shift Left";
|
||||
case ModifierShiftRight: return "Shift Right";
|
||||
case ModifierCtrlAny: return "Ctrl";
|
||||
case ModifierCtrlLeft: return "Ctrl Left";
|
||||
case ModifierCtrlRight: return "Ctrl Right";
|
||||
case ModifierMeta: return "Meta";
|
||||
default:
|
||||
qFatal("Wrong modifier");
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
CKeyboardKey::Modifier CKeyboardKey::modifierFromString(const QString &modifier)
|
||||
{
|
||||
QString mod = modifier.toLower().replace('&', ' ');
|
||||
if (mod == "ctrl" || mod == "ctl") return ModifierCtrlAny;
|
||||
if (mod.startsWith("ctrl l") || mod.startsWith("ctl l")) return ModifierCtrlLeft;
|
||||
if (mod.startsWith("ctrl r") || mod.startsWith("ctl r")) return ModifierCtrlRight;
|
||||
|
||||
if (mod == "shift") return ModifierShiftAny;
|
||||
if (mod.startsWith("shift l")) return ModifierShiftLeft;
|
||||
if (mod.startsWith("shift r")) return ModifierShiftRight;
|
||||
|
||||
if (mod == "alt") return ModifierAltAny;
|
||||
if (mod.startsWith("alt l")) return ModifierAltLeft;
|
||||
if (mod.startsWith("alt r")) return ModifierAltRight;
|
||||
|
||||
if (mod == "meta") return ModifierMeta;
|
||||
|
||||
return ModifierNone;
|
||||
}
|
||||
|
||||
CKeyboardKey::Modifier CKeyboardKey::modifierFromQtModifier(Qt::Modifier qtModifier)
|
||||
{
|
||||
switch (qtModifier)
|
||||
{
|
||||
case Qt::META : return ModifierMeta;
|
||||
case Qt::SHIFT : return ModifierShiftAny;
|
||||
case Qt::CTRL: return ModifierShiftAny;
|
||||
case Qt::ALT: return ModifierAltAny;
|
||||
default:
|
||||
return ModifierNone;
|
||||
}
|
||||
}
|
||||
|
||||
const QStringList &CKeyboardKey::modifiers()
|
||||
{
|
||||
static QStringList modifiers;
|
||||
if (modifiers.isEmpty())
|
||||
{
|
||||
modifiers << modifierToString(ModifierNone);
|
||||
modifiers << modifierToString(ModifierCtrlAny);
|
||||
modifiers << modifierToString(ModifierCtrlLeft);
|
||||
modifiers << modifierToString(ModifierCtrlRight);
|
||||
modifiers << modifierToString(ModifierShiftAny);
|
||||
modifiers << modifierToString(ModifierShiftLeft);
|
||||
modifiers << modifierToString(ModifierShiftRight);
|
||||
modifiers << modifierToString(ModifierAltAny);
|
||||
modifiers << modifierToString(ModifierAltLeft);
|
||||
modifiers << modifierToString(ModifierAltRight);
|
||||
modifiers << modifierToString(ModifierMeta);
|
||||
}
|
||||
return modifiers;
|
||||
}
|
||||
|
||||
void CKeyboardKey::cleanup()
|
||||
{
|
||||
if (!this->hasModifier()) return;
|
||||
if (this->m_modifier1 == ModifierNone) qSwap(this->m_modifier1, this->m_modifier2);
|
||||
if (this->numberOfModifiers() == 1) return;
|
||||
if (this->m_modifier1 == this->m_modifier2)
|
||||
{
|
||||
// redundant
|
||||
this->m_modifier2 = ModifierNone;
|
||||
return;
|
||||
}
|
||||
|
||||
// order
|
||||
if (static_cast<uint>(this->m_modifier1) < static_cast<uint>(this->m_modifier2))
|
||||
qSwap(this->m_modifier1, this->m_modifier2);
|
||||
}
|
||||
|
||||
int CKeyboardKey::numberOfModifiers() const
|
||||
{
|
||||
if (!this->hasModifier()) return 0;
|
||||
if (this->m_modifier1 == ModifierNone || this->m_modifier2 == ModifierNone) return 1;
|
||||
return 2;
|
||||
}
|
||||
|
||||
void CKeyboardKey::setKey(const QString &key)
|
||||
{
|
||||
if (key.isEmpty())
|
||||
this->m_qtKey = Qt::Key_unknown;
|
||||
|
||||
QKeySequence sequence(key);
|
||||
m_qtKey = static_cast<Qt::Key>(sequence[0]);
|
||||
}
|
||||
|
||||
void CKeyboardKey::setKey(const QChar &key)
|
||||
{
|
||||
if (key.isNull())
|
||||
this->m_qtKey = Qt::Key_unknown;
|
||||
else
|
||||
{
|
||||
QKeySequence sequence(key);
|
||||
m_qtKey = static_cast<Qt::Key>(sequence[0]);
|
||||
}
|
||||
}
|
||||
|
||||
bool CKeyboardKey::addModifier(const Modifier &modifier)
|
||||
{
|
||||
bool added = false;
|
||||
|
||||
// Don't add the same modifier twice
|
||||
if (hasModifier(modifier))
|
||||
return false;
|
||||
|
||||
if (m_modifier1 == ModifierNone)
|
||||
{
|
||||
m_modifier1 = modifier;
|
||||
added = true;
|
||||
}
|
||||
else if (m_modifier2 == ModifierNone)
|
||||
{
|
||||
m_modifier2 = modifier;
|
||||
added = true;
|
||||
}
|
||||
|
||||
if (added) this->cleanup();
|
||||
return added;
|
||||
}
|
||||
|
||||
bool CKeyboardKey::addModifier(const QString &modifier)
|
||||
{
|
||||
return addModifier(modifierFromString(modifier));
|
||||
}
|
||||
|
||||
bool CKeyboardKey::removeModifier(const Modifier &modifier)
|
||||
{
|
||||
bool removed = false;
|
||||
|
||||
if (m_modifier1 == modifier)
|
||||
{
|
||||
m_modifier1 = ModifierNone;
|
||||
removed = true;
|
||||
}
|
||||
if (m_modifier2 == modifier)
|
||||
{
|
||||
m_modifier2 = ModifierNone;
|
||||
removed = true;
|
||||
}
|
||||
cleanup();
|
||||
return removed;
|
||||
}
|
||||
|
||||
bool CKeyboardKey::removeModifier(const QString &modifier)
|
||||
{
|
||||
return removeModifier(modifierFromString(modifier));
|
||||
}
|
||||
|
||||
bool CKeyboardKey::equalsModifierReleaxed(CKeyboardKey::Modifier m1, CKeyboardKey::Modifier m2)
|
||||
{
|
||||
if (m1 == m2) return true;
|
||||
if (m1 == ModifierAltAny && (m2 == ModifierAltLeft || m2 == ModifierAltRight)) return true;
|
||||
if (m1 == ModifierCtrlAny && (m2 == ModifierCtrlLeft || m2 == ModifierCtrlRight)) return true;
|
||||
if (m1 == ModifierShiftAny && (m2 == ModifierShiftLeft || m2 == ModifierShiftRight)) return true;
|
||||
|
||||
if (m2 == ModifierAltAny && (m1 == ModifierAltLeft || m1 == ModifierAltRight)) return true;
|
||||
if (m2 == ModifierCtrlAny && (m1 == ModifierCtrlLeft || m1 == ModifierCtrlRight)) return true;
|
||||
if (m2 == ModifierShiftAny && (m1 == ModifierShiftLeft || m1 == ModifierShiftRight)) return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void CKeyboardKey::setKeyObject(const CKeyboardKey &key)
|
||||
{
|
||||
this->m_modifier1 = key.m_modifier1;
|
||||
this->m_modifier2 = key.m_modifier2;
|
||||
this->m_qtKey = key.m_qtKey;
|
||||
}
|
||||
|
||||
bool CKeyboardKey::equalsWithRelaxedModifiers(const CKeyboardKey &key) const
|
||||
{
|
||||
if (key == (*this)) return true; // fully equal, not need to bother
|
||||
|
||||
// this can never be true
|
||||
if (key.m_qtKey != this->m_qtKey) return false;
|
||||
if (this->numberOfModifiers() != key.numberOfModifiers()) return false;
|
||||
|
||||
// special modifiers
|
||||
if (this->getModifier1() != key.getModifier1())
|
||||
{
|
||||
if (!CKeyboardKey::equalsModifierReleaxed(this->getModifier1(), key.getModifier1())) return false;
|
||||
}
|
||||
return CKeyboardKey::equalsModifierReleaxed(this->getModifier1(), key.getModifier1());
|
||||
}
|
||||
|
||||
QString CKeyboardKey::toStringRepresentation(int key)
|
||||
{
|
||||
if (key == 0) return "";
|
||||
QString ks = QKeySequence(key).toString();
|
||||
return ks;
|
||||
}
|
||||
|
||||
CVariant CKeyboardKey::propertyByIndex(const BlackMisc::CPropertyIndex &index) const
|
||||
{
|
||||
if (index.isMyself()) { return this->toCVariant(); }
|
||||
ColumnIndex i = index.frontCasted<ColumnIndex>();
|
||||
switch (i)
|
||||
{
|
||||
case IndexModifier1:
|
||||
return CVariant::from(static_cast<uint>(this->m_modifier1));
|
||||
case IndexModifier2:
|
||||
return CVariant::from(static_cast<uint>(this->m_modifier2));
|
||||
case IndexModifier1AsString:
|
||||
return CVariant::from(this->getModifier1AsString());
|
||||
case IndexModifier2AsString:
|
||||
return CVariant::from(this->getModifier2AsString());
|
||||
case IndexKey:
|
||||
return CVariant::from(static_cast<int>(this->m_qtKey));
|
||||
case IndexKeyAsString:
|
||||
return CVariant::from(QString(QChar(this->m_qtKey)));
|
||||
case IndexKeyAsStringRepresentation:
|
||||
return CVariant::from(this->getKeyAsStringRepresentation());
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
Q_ASSERT_X(false, "CKeyboardKey", "index unknown");
|
||||
QString m = QString("no property, index ").append(index.toQString());
|
||||
return CVariant::fromValue(m);
|
||||
}
|
||||
|
||||
void CKeyboardKey::setPropertyByIndex(const CVariant &variant, const BlackMisc::CPropertyIndex &index)
|
||||
{
|
||||
if (index.isMyself())
|
||||
{
|
||||
this->convertFromCVariant(variant);
|
||||
return;
|
||||
}
|
||||
ColumnIndex i = index.frontCasted<ColumnIndex>();
|
||||
switch (i)
|
||||
{
|
||||
case IndexKey:
|
||||
case IndexKeyAsString:
|
||||
case IndexKeyAsStringRepresentation:
|
||||
{
|
||||
// static_cast see docu of variant.type()
|
||||
if (static_cast<QMetaType::Type>(variant.type()) == QMetaType::QChar)
|
||||
this->setKey(variant.value<QChar>());
|
||||
else
|
||||
this->setKey(variant.value<QString>());
|
||||
break;
|
||||
}
|
||||
case IndexModifier1AsString:
|
||||
this->setModifier1(variant.value<QString>());
|
||||
break;
|
||||
case IndexModifier2AsString:
|
||||
this->setModifier2(variant.value<QString>());
|
||||
break;
|
||||
case IndexKeyObject:
|
||||
this->setKeyObject(variant.value<BlackMisc::Hardware::CKeyboardKey>());
|
||||
break;
|
||||
default:
|
||||
Q_ASSERT_X(false, "CKeyboardKey", "index unknown (setter)");
|
||||
break;
|
||||
}
|
||||
}
|
||||
} // namespace Hardware
|
||||
} // BlackMisc
|
||||
221
src/blackmisc/hardware/keyboardkey.h
Normal file
221
src/blackmisc/hardware/keyboardkey.h
Normal file
@@ -0,0 +1,221 @@
|
||||
/* Copyright (C) 2013
|
||||
* 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 BLACKMISC_HARDWARE_KEYBOARDKEY_H
|
||||
#define BLACKMISC_HARDWARE_KEYBOARDKEY_H
|
||||
|
||||
#include "blackmisc/propertyindex.h"
|
||||
#include "blackmisc/blackmiscfreefunctions.h"
|
||||
#include <QStringList>
|
||||
#include <QKeySequence>
|
||||
|
||||
namespace BlackMisc
|
||||
{
|
||||
namespace Hardware
|
||||
{
|
||||
//! Value object representing a keyboard key.
|
||||
class CKeyboardKey : public CValueObject<CKeyboardKey>
|
||||
{
|
||||
public:
|
||||
//! Properties by index
|
||||
enum ColumnIndex
|
||||
{
|
||||
IndexKey = 0,
|
||||
IndexKeyAsString,
|
||||
IndexKeyAsStringRepresentation,
|
||||
IndexModifier1,
|
||||
IndexModifier2,
|
||||
IndexModifier1AsString,
|
||||
IndexModifier2AsString,
|
||||
IndexKeyObject, // just for updates
|
||||
};
|
||||
|
||||
//! Modifier
|
||||
enum Modifier
|
||||
{
|
||||
ModifierNone,
|
||||
ModifierCtrlLeft,
|
||||
ModifierCtrlRight,
|
||||
ModifierCtrlAny,
|
||||
ModifierAltLeft,
|
||||
ModifierAltRight,
|
||||
ModifierAltAny,
|
||||
ModifierShiftLeft,
|
||||
ModifierShiftRight,
|
||||
ModifierShiftAny,
|
||||
ModifierMeta,
|
||||
ModifierNum
|
||||
};
|
||||
|
||||
//! Default constructor
|
||||
CKeyboardKey();
|
||||
|
||||
//! Constructor
|
||||
CKeyboardKey(Qt::Key keyCode, Modifier modifier1 = ModifierNone, Modifier modifier2 = ModifierNone);
|
||||
|
||||
//! Get key code
|
||||
Qt::Key getKey() const { return this->m_qtKey; }
|
||||
|
||||
//! Get key code
|
||||
QString getKeyAsString() const { return QString(this->getKey()); }
|
||||
|
||||
//! Get key code
|
||||
QString getKeyAsStringRepresentation() const { return CKeyboardKey::toStringRepresentation(this->m_qtKey); }
|
||||
|
||||
//! As Qt::Key
|
||||
Qt::Key getKeyAsQtKey() const { return static_cast<Qt::Key>(this->m_qtKey);}
|
||||
|
||||
//! Set key code
|
||||
void setKey(const QString &key);
|
||||
|
||||
//! Set key code
|
||||
void setKey(const QChar &key);
|
||||
|
||||
//! Set key code
|
||||
void setKey(const Qt::Key key) { this->m_qtKey = key; }
|
||||
|
||||
//! Set key code
|
||||
void setKey(int key) { this->m_qtKey = static_cast<Qt::Key>(key); }
|
||||
|
||||
/*!
|
||||
* Add modifier
|
||||
* \param modifier
|
||||
* \return True if modifier was added
|
||||
*/
|
||||
bool addModifier(const Modifier &modifier);
|
||||
|
||||
/*!
|
||||
* add modifier
|
||||
* \param modifier
|
||||
* \return True if modifier was added
|
||||
*/
|
||||
bool addModifier(const QString &modifier);
|
||||
|
||||
//! number of modifiers
|
||||
int numberOfModifiers() const;
|
||||
|
||||
//! Modifier 1
|
||||
Modifier getModifier1() const { return this->m_modifier1; }
|
||||
|
||||
//! Modifier 2
|
||||
Modifier getModifier2() const { return this->m_modifier2; }
|
||||
|
||||
//! Modifier 1
|
||||
QString getModifier1AsString() const { return modifierToString(this->m_modifier1); }
|
||||
|
||||
//! Modifier 2
|
||||
QString getModifier2AsString() const { return modifierToString(this->m_modifier2); }
|
||||
|
||||
/*!
|
||||
* Remove modifier from key
|
||||
* \param modifier
|
||||
* \return True if modifier was removed
|
||||
*/
|
||||
bool removeModifier(const Modifier &modifier);
|
||||
|
||||
/*!
|
||||
* Remove modifier from key
|
||||
* \param modifier
|
||||
* \return True if modifier was removed
|
||||
*/
|
||||
bool removeModifier(const QString &modifier);
|
||||
|
||||
//! Set modifier 1
|
||||
void setModifier1(const QString &modifier) { this->m_modifier1 = modifierFromString(modifier); }
|
||||
|
||||
//! Set modifier 1
|
||||
void setModifier1(const Modifier &modifier) { this->m_modifier1 = modifier; }
|
||||
|
||||
//! Set modifier 2
|
||||
void setModifier2(const QString &modifier) { this->m_modifier2 = modifierFromString(modifier); }
|
||||
|
||||
//! Set modifier 2
|
||||
void setModifier2(const Modifier &modifier) { this->m_modifier2 = modifier; }
|
||||
|
||||
//! Is empty, (no key, no modifier)?
|
||||
bool isEmpty() const { return !this->hasModifier() && !this->hasKey(); }
|
||||
|
||||
//! Modifier?
|
||||
bool hasModifier() const
|
||||
{
|
||||
return this->m_modifier1 != ModifierNone || this->m_modifier2 != ModifierNone;
|
||||
}
|
||||
|
||||
//! Do we have this modifier?
|
||||
bool hasModifier(Modifier modifier) const
|
||||
{
|
||||
return m_modifier1 == modifier || m_modifier2 == modifier;
|
||||
}
|
||||
|
||||
//! ALT = ALT/R or ALT/L, CTRL = CTRL/R or left, ...
|
||||
static bool equalsModifierReleaxed(Modifier m1, Modifier m2);
|
||||
|
||||
//! with key?
|
||||
bool hasKey() const
|
||||
{
|
||||
return !(this->m_qtKey == Qt::Key_unknown);
|
||||
}
|
||||
|
||||
//! Key + modifier?
|
||||
bool hasKeyAndModifier() const
|
||||
{
|
||||
return this->hasKey() && this->hasModifier();
|
||||
}
|
||||
|
||||
//! Order Modifiers
|
||||
void cleanup();
|
||||
|
||||
//! Set key object
|
||||
void setKeyObject(const BlackMisc::Hardware::CKeyboardKey &key);
|
||||
|
||||
//! CTRL will be consider equal CTRL-left/reigt, ALT = ALT-left/right ..
|
||||
bool equalsWithRelaxedModifiers(const CKeyboardKey &key) const;
|
||||
|
||||
//! \copydoc CValueObject::setPropertyByIndex
|
||||
virtual void setPropertyByIndex(const CVariant &variant, const BlackMisc::CPropertyIndex &index) override;
|
||||
|
||||
//! \copydoc CValueObject::propertyByIndex
|
||||
virtual CVariant propertyByIndex(const BlackMisc::CPropertyIndex &index) const override;
|
||||
|
||||
//! Modifier to string
|
||||
static QString modifierToString(Modifier modifier);
|
||||
|
||||
//! Modifier from string
|
||||
static Modifier modifierFromString(const QString &modifier);
|
||||
|
||||
//! Modifier from Qt::Modifier
|
||||
static Modifier modifierFromQtModifier(Qt::Modifier qtModifier);
|
||||
|
||||
//! all modifiers
|
||||
static const QStringList &modifiers();
|
||||
|
||||
/*!
|
||||
* CKeyboardKey::toStringRepresentation
|
||||
*/
|
||||
static QString toStringRepresentation(int key);
|
||||
|
||||
protected:
|
||||
//! \copydoc CValueObject::convertToQString
|
||||
virtual QString convertToQString(bool i18n = false) const override;
|
||||
|
||||
private:
|
||||
BLACK_ENABLE_TUPLE_CONVERSION(CKeyboardKey)
|
||||
Qt::Key m_qtKey; //!< code similar to Qt::Key
|
||||
Modifier m_modifier1;
|
||||
Modifier m_modifier2;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
BLACK_DECLARE_TUPLE_CONVERSION(BlackMisc::Hardware::CKeyboardKey, (o.m_qtKey, o.m_modifier1, o.m_modifier2))
|
||||
Q_DECLARE_METATYPE(BlackMisc::Hardware::CKeyboardKey)
|
||||
|
||||
#endif // guard
|
||||
36
src/blackmisc/hardware/keyboardkeylist.cpp
Normal file
36
src/blackmisc/hardware/keyboardkeylist.cpp
Normal file
@@ -0,0 +1,36 @@
|
||||
/* Copyright (C) 2013
|
||||
* 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 "blackmisc/hardware/keyboardkeylist.h"
|
||||
#include "blackmisc/predicates.h"
|
||||
|
||||
namespace BlackMisc
|
||||
{
|
||||
namespace Hardware
|
||||
{
|
||||
|
||||
CKeyboardKeyList::CKeyboardKeyList() { }
|
||||
|
||||
CKeyboardKeyList::CKeyboardKeyList(const CSequence<CKeyboardKey> &baseClass) :
|
||||
CSequence<CKeyboardKey>(baseClass)
|
||||
{ }
|
||||
|
||||
void CKeyboardKeyList::registerMetadata()
|
||||
{
|
||||
qRegisterMetaType<BlackMisc::CSequence<CKeyboardKey>>();
|
||||
qDBusRegisterMetaType<BlackMisc::CSequence<CKeyboardKey>>();
|
||||
qRegisterMetaType<BlackMisc::CCollection<CKeyboardKey>>();
|
||||
qDBusRegisterMetaType<BlackMisc::CCollection<CKeyboardKey>>();
|
||||
qRegisterMetaType<CKeyboardKeyList>();
|
||||
qDBusRegisterMetaType<CKeyboardKeyList>();
|
||||
registerMetaValueType<CKeyboardKeyList>();
|
||||
}
|
||||
|
||||
} // namespace
|
||||
} // namespace
|
||||
50
src/blackmisc/hardware/keyboardkeylist.h
Normal file
50
src/blackmisc/hardware/keyboardkeylist.h
Normal file
@@ -0,0 +1,50 @@
|
||||
/* Copyright (C) 2013
|
||||
* 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 BLACKMISC_HARDWARE_KEYBOARDKEYLIST_H
|
||||
#define BLACKMISC_HARDWARE_KEYBOARDKEYLIST_H
|
||||
|
||||
#include "blackmisc/hardware/keyboardkey.h"
|
||||
#include "blackmisc/collection.h"
|
||||
#include "blackmisc/sequence.h"
|
||||
#include <QObject>
|
||||
|
||||
namespace BlackMisc
|
||||
{
|
||||
namespace Hardware
|
||||
{
|
||||
/*!
|
||||
* Value object encapsulating a list of keyboard keys.
|
||||
*/
|
||||
class CKeyboardKeyList : public CSequence<CKeyboardKey>
|
||||
{
|
||||
public:
|
||||
//! Default constructor
|
||||
CKeyboardKeyList();
|
||||
|
||||
//! Construct from a base class object.
|
||||
CKeyboardKeyList(const CSequence<CKeyboardKey> &baseClass);
|
||||
|
||||
//! \copydoc CValueObject::toQVariant
|
||||
virtual QVariant toQVariant() const override { return QVariant::fromValue(*this); }
|
||||
|
||||
//! Register metadata
|
||||
static void registerMetadata();
|
||||
};
|
||||
|
||||
} //namespace
|
||||
} // namespace
|
||||
|
||||
Q_DECLARE_METATYPE(BlackMisc::Hardware::CKeyboardKeyList)
|
||||
Q_DECLARE_METATYPE(BlackMisc::CCollection<BlackMisc::Hardware::CKeyboardKey>)
|
||||
Q_DECLARE_METATYPE(BlackMisc::CSequence<BlackMisc::Hardware::CKeyboardKey>)
|
||||
|
||||
#endif //guard
|
||||
Reference in New Issue
Block a user