refs #317 CJoystickButton value object

This commit is contained in:
Roland Winklmeier
2014-07-31 01:14:35 +02:00
parent bf84b01ff7
commit da4da1c4b5
4 changed files with 323 additions and 0 deletions

View File

@@ -144,6 +144,7 @@ void BlackMisc::Hardware::registerMetadata()
{
CKeyboardKey::registerMetadata();
CKeyboardKeyList::registerMetadata();
CJoystickButton::registerMetadata();
}
/*

View File

@@ -7,5 +7,6 @@
#define BLACKMISC_HWALLCLASSES_H
#include "blackmisc/hwkeyboardkeylist.h"
#include "blackmisc/hwjoystickbutton.h"
#endif // guard

View File

@@ -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 <QCoreApplication>
namespace BlackMisc
{
namespace Hardware
{
CJoystickButton::CJoystickButton(qint32 buttonIndex) :
m_buttonIndex(buttonIndex)
{}
/*
* Hash
*/
uint CJoystickButton::getValueHash() const
{
return qHash(TupleConverter<CJoystickButton>::toMetaTuple(*this));
}
// To JSON
QJsonObject CJoystickButton::toJson() const
{
return BlackMisc::serializeJson(TupleConverter<CJoystickButton>::toMetaTuple(*this));
}
// From Json
void CJoystickButton::convertFromJson(const QJsonObject &json)
{
BlackMisc::deserializeJson(json, TupleConverter<CJoystickButton>::toMetaTuple(*this));
}
// Meta data
void CJoystickButton::registerMetadata()
{
qRegisterMetaType<CJoystickButton>();
qDBusRegisterMetaType<CJoystickButton>();
}
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<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;
}
}
QVariant CJoystickButton::propertyByIndex(const BlackMisc::CPropertyIndex &index) const
{
if (index.isMyself()) { return this->toQVariant(); }
ColumnIndex i = index.frontCasted<ColumnIndex>();
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<CJoystickButton>::toMetaTuple(*this) == TupleConverter<CJoystickButton>::toMetaTuple(other);
}
/*
* Unequal?
*/
bool CJoystickButton::operator !=(const CJoystickButton &other) const
{
return !((*this) == other);
}
bool CJoystickButton::operator< (CJoystickButton const &other) const
{
return TupleConverter<CJoystickButton>::toMetaTuple(*this) < TupleConverter<CJoystickButton>::toMetaTuple(other);
}
QString CJoystickButton::convertToQString(bool /* i18n */) const
{
QString s = getButtonAsString();
return s.trimmed();
}
int CJoystickButton::getMetaTypeId() const
{
return qMetaTypeId<CJoystickButton>();
}
bool CJoystickButton::isA(int metaTypeId) const
{
return (metaTypeId == qMetaTypeId<CJoystickButton>());
}
/*
* Compare
*/
int CJoystickButton::compareImpl(const CValueObject &otherBase) const
{
const auto &other = static_cast<const CJoystickButton &>(otherBase);
return compare(TupleConverter<CJoystickButton>::toMetaTuple(*this), TupleConverter<CJoystickButton>::toMetaTuple(other));
}
/*
* Marshall to DBus
*/
void CJoystickButton::marshallToDbus(QDBusArgument &argument) const
{
argument << TupleConverter<CJoystickButton>::toMetaTuple(*this);
}
/*
* Unmarshall from DBus
*/
void CJoystickButton::unmarshallFromDbus(const QDBusArgument &argument)
{
argument >> TupleConverter<CJoystickButton>::toMetaTuple(*this);
}
} // namespace Hardware
} // BlackMisc

View File

@@ -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