/* 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