diff --git a/src/blackmisc/blackmiscfreefunctions.cpp b/src/blackmisc/blackmiscfreefunctions.cpp index 8c1a05293..c0bbe1476 100644 --- a/src/blackmisc/blackmiscfreefunctions.cpp +++ b/src/blackmisc/blackmiscfreefunctions.cpp @@ -144,6 +144,7 @@ void BlackMisc::Hardware::registerMetadata() { CKeyboardKey::registerMetadata(); CKeyboardKeyList::registerMetadata(); + CJoystickButton::registerMetadata(); } /* diff --git a/src/blackmisc/hwallclasses.h b/src/blackmisc/hwallclasses.h index 8345c5d50..e530bae12 100644 --- a/src/blackmisc/hwallclasses.h +++ b/src/blackmisc/hwallclasses.h @@ -7,5 +7,6 @@ #define BLACKMISC_HWALLCLASSES_H #include "blackmisc/hwkeyboardkeylist.h" +#include "blackmisc/hwjoystickbutton.h" #endif // guard diff --git a/src/blackmisc/hwjoystickbutton.cpp b/src/blackmisc/hwjoystickbutton.cpp new file mode 100644 index 000000000..091441918 --- /dev/null +++ b/src/blackmisc/hwjoystickbutton.cpp @@ -0,0 +1,182 @@ +/* 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 "hwjoystickbutton.h" +#include "blackmiscfreefunctions.h" +#include + +namespace BlackMisc +{ + namespace Hardware + { + CJoystickButton::CJoystickButton(qint32 buttonIndex) : + m_buttonIndex(buttonIndex) + {} + + /* + * Hash + */ + uint CJoystickButton::getValueHash() const + { + return qHash(TupleConverter::toMetaTuple(*this)); + } + + // To JSON + QJsonObject CJoystickButton::toJson() const + { + return BlackMisc::serializeJson(TupleConverter::toMetaTuple(*this)); + } + + // From Json + void CJoystickButton::convertFromJson(const QJsonObject &json) + { + BlackMisc::deserializeJson(json, TupleConverter::toMetaTuple(*this)); + } + + // Meta data + void CJoystickButton::registerMetadata() + { + qRegisterMetaType(); + qDBusRegisterMetaType(); + } + + void CJoystickButton::setButtonIndex(qint32 buttonIndex) + { + m_buttonIndex = buttonIndex; + } + + void CJoystickButton::setButtonObject(const CJoystickButton &button) + { + this->m_buttonIndex = button.m_buttonIndex; + } + + void CJoystickButton::setPropertyByIndex(const QVariant &variant, const CPropertyIndex &index) + { + if (index.isMyself()) + { + this->convertFromQVariant(variant); + return; + } + ColumnIndex i = index.frontCasted(); + + switch (i) + { + case IndexButton: + case IndexButtonAsString: + this->setButtonIndex(buttonIndexFromString(variant.value())); + break; + case IndeButtonObject: + this->setButtonObject(variant.value()); + break; + default: + Q_ASSERT_X(false, "CJoystickButton", "index unknown (setter)"); + break; + } + } + + QVariant CJoystickButton::propertyByIndex(const BlackMisc::CPropertyIndex &index) const + { + if (index.isMyself()) { return this->toQVariant(); } + ColumnIndex i = index.frontCasted(); + switch (i) + { + case IndexButton: + return QVariant(this->getButtonIndex()); + case IndexButtonAsString: + return QVariant(this->getButtonAsString()); + default: + break; + } + + Q_ASSERT_X(false, "CJoystickButton", "index unknown"); + QString m = QString("no property, index ").append(index.toQString()); + return QVariant::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(); + } + + /* + * Equal? + */ + bool CJoystickButton::operator ==(const CJoystickButton &other) const + { + if (this == &other) return true; + return TupleConverter::toMetaTuple(*this) == TupleConverter::toMetaTuple(other); + } + + /* + * Unequal? + */ + bool CJoystickButton::operator !=(const CJoystickButton &other) const + { + return !((*this) == other); + } + + bool CJoystickButton::operator< (CJoystickButton const &other) const + { + return TupleConverter::toMetaTuple(*this) < TupleConverter::toMetaTuple(other); + } + + QString CJoystickButton::convertToQString(bool /* i18n */) const + { + QString s = getButtonAsString(); + return s.trimmed(); + } + + int CJoystickButton::getMetaTypeId() const + { + return qMetaTypeId(); + } + + bool CJoystickButton::isA(int metaTypeId) const + { + return (metaTypeId == qMetaTypeId()); + } + + /* + * Compare + */ + int CJoystickButton::compareImpl(const CValueObject &otherBase) const + { + const auto &other = static_cast(otherBase); + return compare(TupleConverter::toMetaTuple(*this), TupleConverter::toMetaTuple(other)); + } + + /* + * Marshall to DBus + */ + void CJoystickButton::marshallToDbus(QDBusArgument &argument) const + { + argument << TupleConverter::toMetaTuple(*this); + } + + /* + * Unmarshall from DBus + */ + void CJoystickButton::unmarshallFromDbus(const QDBusArgument &argument) + { + argument >> TupleConverter::toMetaTuple(*this); + } + + } // namespace Hardware + +} // BlackMisc diff --git a/src/blackmisc/hwjoystickbutton.h b/src/blackmisc/hwjoystickbutton.h new file mode 100644 index 000000000..a28c09ebe --- /dev/null +++ b/src/blackmisc/hwjoystickbutton.h @@ -0,0 +1,139 @@ +/* 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_JOYSTICKBUTTON_H +#define BLACKMISC_JOYSTICKBUTTON_H + +#include "valueobject.h" +#include "propertyindex.h" +#include "blackmiscfreefunctions.h" + +namespace BlackMisc +{ + namespace Hardware + { + //! Value object representing a joystick button + class CJoystickButton : public CValueObject + { + public: + + //! Properties by index + enum ColumnIndex + { + IndexButton = 0, + IndexButtonAsString, + IndeButtonObject, // just for updates + }; + + //! Default constructor + CJoystickButton() {} + + //! Constructor + CJoystickButton(qint32 buttonIndex); + + //! Destructor + ~CJoystickButton() {} + + //! \copydoc CValueObject::toQVariant + virtual QVariant toQVariant() const override + { + return QVariant::fromValue(*this); + } + + //! \copydoc CValueObject::convertFromQVariant + virtual void convertFromQVariant(const QVariant &variant) override { BlackMisc::setFromQVariant(this, variant); } + + //! \copydoc CValueObject::getValueHash + virtual uint getValueHash() const override; + + //! \copydoc CValueObject::toJson + virtual QJsonObject toJson() const override; + + //! \copydoc CValueObject::convertFromJson + virtual void convertFromJson(const QJsonObject &json) override; + + //! Register metadata + static void registerMetadata(); + + //! 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 QVariant &variant, const BlackMisc::CPropertyIndex &index); + + //! \copydoc CValueObject::propertyByIndex + virtual QVariant 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; } + + //! Equal? + bool operator ==(const CJoystickButton &other) const; + + //! Unequal operator != + bool operator !=(const CJoystickButton &other) const; + + //! < + bool operator<(CJoystickButton const &other) const; + + protected: + //! \copydoc CValueObject::convertToQString + virtual QString convertToQString(bool i18n = false) const override; + + //! \copydoc CValueObject::getMetaTypeId + virtual int getMetaTypeId() const override; + + //! \copydoc CValueObject::isA + virtual bool isA(int metaTypeId) const override; + + //! \copydoc CValueObject::compareImpl(otherBase) + virtual int compareImpl(const CValueObject &otherBase) const override; + + //! \copydoc CValueObject::marshallToDbus + virtual void marshallToDbus(QDBusArgument &argument) const override; + + //! \copydoc CValueObject::unmarshallFromDbus + virtual void unmarshallFromDbus(const QDBusArgument &argument) override; + + private: + BLACK_ENABLE_TUPLE_CONVERSION(CJoystickButton) + qint32 m_buttonIndex = m_invalidIndex; //!< code similar to Qt::Key + + static const qint32 m_invalidIndex = -1; + + }; + + } // class + +} // BlackMisc + +BLACK_DECLARE_TUPLE_CONVERSION(BlackMisc::Hardware::CJoystickButton, (o.m_buttonIndex)) +Q_DECLARE_METATYPE(BlackMisc::Hardware::CJoystickButton) + +#endif // guard