From 4d60ca3a87a0abe2c0a11b0431847176d6cf5158 Mon Sep 17 00:00:00 2001 From: Roland Winklmeier Date: Mon, 4 Aug 2014 20:38:20 +0200 Subject: [PATCH] refs #313 KeyboardKey wrapper class for current settings implementation --- src/blackmisc/blackmiscfreefunctions.cpp | 2 + src/blackmisc/setkeyboardhotkey.cpp | 200 +++++++++++++++++++++++ src/blackmisc/setkeyboardhotkey.h | 143 ++++++++++++++++ src/blackmisc/setkeyboardhotkeylist.cpp | 71 ++++++++ src/blackmisc/setkeyboardhotkeylist.h | 59 +++++++ src/blackmisc/settingsblackmiscclasses.h | 1 + 6 files changed, 476 insertions(+) create mode 100644 src/blackmisc/setkeyboardhotkey.cpp create mode 100644 src/blackmisc/setkeyboardhotkey.h create mode 100644 src/blackmisc/setkeyboardhotkeylist.cpp create mode 100644 src/blackmisc/setkeyboardhotkeylist.h diff --git a/src/blackmisc/blackmiscfreefunctions.cpp b/src/blackmisc/blackmiscfreefunctions.cpp index 0fb837512..374a9f0ff 100644 --- a/src/blackmisc/blackmiscfreefunctions.cpp +++ b/src/blackmisc/blackmiscfreefunctions.cpp @@ -121,6 +121,8 @@ void BlackMisc::Settings::registerMetadata() { CSettingsNetwork::registerMetadata(); CSettingsAudio::registerMetadata(); + CSettingKeyboardHotkey::registerMetadata(); + CSettingKeyboardHotkeyList::registerMetadata(); } /* diff --git a/src/blackmisc/setkeyboardhotkey.cpp b/src/blackmisc/setkeyboardhotkey.cpp new file mode 100644 index 000000000..110c8494f --- /dev/null +++ b/src/blackmisc/setkeyboardhotkey.cpp @@ -0,0 +1,200 @@ +/* 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 "setkeyboardhotkey.h" +#include "blackmiscfreefunctions.h" +#include + +namespace BlackMisc +{ + namespace Settings + { + CSettingKeyboardHotkey::CSettingKeyboardHotkey(const CHotkeyFunction &function) : + m_hotkeyFunction(function) + {} + + CSettingKeyboardHotkey::CSettingKeyboardHotkey(const Hardware::CKeyboardKey &key, const CHotkeyFunction &function) : + m_key(key), m_hotkeyFunction(function) + {} + + void CSettingKeyboardHotkey::registerMetadata() + { + qRegisterMetaType(); + qDBusRegisterMetaType(); + } + + /* + * To JSON + */ + QJsonObject CSettingKeyboardHotkey::toJson() const + { + return BlackMisc::serializeJson(TupleConverter::toMetaTuple(*this)); + } + + /* + * From Json + */ + void CSettingKeyboardHotkey::fromJson(const QJsonObject &json) + { + BlackMisc::deserializeJson(json, TupleConverter::toMetaTuple(*this)); + } + + + QString CSettingKeyboardHotkey::convertToQString(bool /* i18n */) const + { + return m_key.toQString(); + } + + int CSettingKeyboardHotkey::getMetaTypeId() const + { + return qMetaTypeId(); + } + + bool CSettingKeyboardHotkey::isA(int metaTypeId) const + { + return (metaTypeId == qMetaTypeId()); + } + + /* + * Compare + */ + int CSettingKeyboardHotkey::compareImpl(const CValueObject &otherBase) const + { + const auto &other = static_cast(otherBase); + return compare(TupleConverter::toMetaTuple(*this), TupleConverter::toMetaTuple(other)); + } + + /* + * Marshall to DBus + */ + void CSettingKeyboardHotkey::marshallToDbus(QDBusArgument &argument) const + { + argument << TupleConverter::toMetaTuple(*this); + } + + /* + * Unmarshall from DBus + */ + void CSettingKeyboardHotkey::unmarshallFromDbus(const QDBusArgument &argument) + { + argument >> TupleConverter::toMetaTuple(*this); + } + + /* + * Hash + */ + uint CSettingKeyboardHotkey::getValueHash() const + { + return qHash(TupleConverter::toMetaTuple(*this)); + } + + /* + * Equal? + */ + bool CSettingKeyboardHotkey::operator ==(const CSettingKeyboardHotkey &other) const + { + if (this == &other) return true; + return TupleConverter::toMetaTuple(*this) == TupleConverter::toMetaTuple(other); + } + + /* + * Unequal? + */ + bool CSettingKeyboardHotkey::operator !=(const CSettingKeyboardHotkey &other) const + { + return !((*this) == other); + } + + bool CSettingKeyboardHotkey::operator< (CSettingKeyboardHotkey const &other) const + { + if (this->getKey() < other.getKey()) + return true; + else + return false; + } + + void CSettingKeyboardHotkey::setKey(const Hardware::CKeyboardKey &key) + { + m_key = key; + } + + void CSettingKeyboardHotkey::setObject(const CSettingKeyboardHotkey &obj) + { + m_hotkeyFunction = obj.m_hotkeyFunction; + m_key = obj.m_key; + } + + QVariant CSettingKeyboardHotkey::propertyByIndex(int index) const + { + switch (index) + { + case IndexFunction: + return QVariant(m_hotkeyFunction.getFunction()); + case IndexFunctionAsString: + return QVariant(m_hotkeyFunction.getFunctionAsString()); + case IndexModifier1: + return QVariant(static_cast(m_key.getModifier1())); + case IndexModifier2: + return QVariant(static_cast(m_key.getModifier2())); + case IndexModifier1AsString: + return QVariant(m_key.getModifier1AsString()); + case IndexModifier2AsString: + return QVariant(m_key.getModifier2AsString()); + case IndexKey: + return QVariant(m_key.getKeyAsQtKey()); + case IndexKeyAsString: + return QVariant(QString(QChar(m_key.getKeyAsQtKey()))); + case IndexKeyAsStringRepresentation: + return QVariant(m_key.getKeyAsStringRepresentation()); + default: + break; + } + + Q_ASSERT_X(false, "CSettingKeyboardHotkey", "index unknown"); + QString m = QString("no property, index ").append(QString::number(index)); + return QVariant::fromValue(m); + } + + void CSettingKeyboardHotkey::setPropertyByIndex(const QVariant &variant, int index) + { + switch (index) + { + case IndexFunction: + { + uint f = variant.value(); + m_hotkeyFunction.setFunction(static_cast(f)); + break; + } + case IndexKey: + case IndexKeyAsString: + case IndexKeyAsStringRepresentation: + { + // static_cast see docu of variant.type() + if (static_cast(variant.type()) == QMetaType::QChar) + m_key.setKey(variant.value()); + else + m_key.setKey(variant.value()); + break; + } + case IndexModifier1AsString: + m_key.setModifier1(variant.value()); + break; + case IndexModifier2AsString: + m_key.setModifier2(variant.value()); + break; + case IndexObject: + this->setObject(variant.value()); + break; + default: + Q_ASSERT_X(false, "CSettingKeyboardHotkey", "index unknown (setter)"); + break; + } + } + } // namespace Hardware +} // BlackMisc diff --git a/src/blackmisc/setkeyboardhotkey.h b/src/blackmisc/setkeyboardhotkey.h new file mode 100644 index 000000000..eecaa20d6 --- /dev/null +++ b/src/blackmisc/setkeyboardhotkey.h @@ -0,0 +1,143 @@ +/* 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_SETTINGS_KEYBOARDHOTKEY_H +#define BLACKMISC_SETTINGS_KEYBOARDHOTKEY_H + +#include "valueobject.h" +#include "hwkeyboardkey.h" +#include "hotkeyfunction.h" +#include +#include + +namespace BlackMisc +{ + namespace Settings + { + //! Value object encapsulating the keyboard hotkey assignment + class CSettingKeyboardHotkey : public CValueObject + { + public: + + //! Properties by index + enum ColumnIndex + { + IndexKey = 0, + IndexKeyAsString, + IndexKeyAsStringRepresentation, + IndexModifier1, + IndexModifier2, + IndexModifier1AsString, + IndexModifier2AsString, + IndexFunction, + IndexFunctionAsString, + IndexObject, // just for updates + }; + + //! Default constructor + CSettingKeyboardHotkey() {} + + //! Constructor + CSettingKeyboardHotkey(const CHotkeyFunction &function); + + //! Constructor + CSettingKeyboardHotkey(const Hardware::CKeyboardKey &key, const CHotkeyFunction &function); + + //! \copydoc CValueObject::toQVariant + virtual QVariant toQVariant() const override + { + return QVariant::fromValue(*this); + } + + //! \copydoc CValueObject::getValueHash + virtual uint getValueHash() const override; + + //! \copydoc CValueObject::toJson + virtual QJsonObject toJson() const override; + + //! \copydoc CValueObject::fromJson + void fromJson(const QJsonObject &json) override; + + //! Register metadata + static void registerMetadata(); + + //! Equal? + bool operator ==(const CSettingKeyboardHotkey &other) const; + + //! Unequal operator != + bool operator !=(const CSettingKeyboardHotkey &other) const; + + //! < + bool operator<(CSettingKeyboardHotkey const &other) const; + + //! Get key code + const Hardware::CKeyboardKey &getKey() const { return m_key; } + + //! Set key code + void setKey(const Hardware::CKeyboardKey &key); + + //! Function + CHotkeyFunction getFunction() const { return m_hotkeyFunction; } + + //! Set function + void setFunction(const CHotkeyFunction &function) { this->m_hotkeyFunction = function; } + + //! Set object + void setObject(const CSettingKeyboardHotkey &obj); + + void cleanup() { m_key.cleanup(); } + + QString getModifier1AsString() const { return m_key.getModifier1AsString(); } + + QString getModifier2AsString() const { return m_key.getModifier1AsString(); } + + static QStringList modifiers() { return Hardware::CKeyboardKey::modifiers(); } + + static QString toStringRepresentation(int key) { return Hardware::CKeyboardKey::toStringRepresentation(key); } + + //! \copydoc CValueObject::setPropertyByIndex + virtual void setPropertyByIndex(const QVariant &variant, int index); + + //! \copydoc CValueObject::propertyByIndex + virtual QVariant propertyByIndex(int index) 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(CSettingKeyboardHotkey) + Hardware::CKeyboardKey m_key; //!< code similar to Qt::Key + CHotkeyFunction m_hotkeyFunction; //!< hotkey function + + }; + } // class +} // BlackMisc + +BLACK_DECLARE_TUPLE_CONVERSION(BlackMisc::Settings::CSettingKeyboardHotkey, (o.m_key, o.m_hotkeyFunction)) +Q_DECLARE_METATYPE(BlackMisc::Settings::CSettingKeyboardHotkey) + +#endif // guard diff --git a/src/blackmisc/setkeyboardhotkeylist.cpp b/src/blackmisc/setkeyboardhotkeylist.cpp new file mode 100644 index 000000000..f413326bd --- /dev/null +++ b/src/blackmisc/setkeyboardhotkeylist.cpp @@ -0,0 +1,71 @@ +/* Copyright (C) 2013 VATSIM Community / authors + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "setkeyboardhotkeylist.h" +#include "predicates.h" + +namespace BlackMisc +{ + using namespace Hardware; + + namespace Settings + { + /* + * Constructor + */ + CSettingKeyboardHotkeyList::CSettingKeyboardHotkeyList() { } + + /* + * Construct from base class object + */ + CSettingKeyboardHotkeyList::CSettingKeyboardHotkeyList(const CSequence &baseClass) : + CSequence(baseClass) + { } + + /* + * Contains this hotkey function? + */ + bool CSettingKeyboardHotkeyList::containsFunction(const CHotkeyFunction &function) const + { + return CSequence::contains(&CSettingKeyboardHotkey::getFunction, function); + } + + /* + * Key for function + */ + CSettingKeyboardHotkey CSettingKeyboardHotkeyList::keyForFunction(const CHotkeyFunction &function) const + { + return this->findBy(&CSettingKeyboardHotkey::getFunction, function).frontOrDefault(); + } + + /* + * Register metadata + */ + void CSettingKeyboardHotkeyList::registerMetadata() + { + qRegisterMetaType>(); + qDBusRegisterMetaType>(); + qRegisterMetaType>(); + qDBusRegisterMetaType>(); + qRegisterMetaType(); + qDBusRegisterMetaType(); + } + + /* + * Hotkey list + */ + void CSettingKeyboardHotkeyList::initAsHotkeyList(bool reset) + { + if (reset) this->clear(); + if (!this->containsFunction(CHotkeyFunction::Ptt())) push_back(CSettingKeyboardHotkey(CHotkeyFunction::Ptt())); + if (!this->containsFunction(CHotkeyFunction::ToggleCom1())) push_back(CSettingKeyboardHotkey(CHotkeyFunction::ToggleCom1())); + if (!this->containsFunction(CHotkeyFunction::ToggleCom2())) push_back(CSettingKeyboardHotkey(CHotkeyFunction::ToggleCom2())); + if (!this->containsFunction(CHotkeyFunction::Opacity50())) push_back(CSettingKeyboardHotkey(CHotkeyFunction::Opacity50())); + if (!this->containsFunction(CHotkeyFunction::Opacity100())) push_back(CSettingKeyboardHotkey(CHotkeyFunction::Opacity100())); + if (!this->containsFunction(CHotkeyFunction::ToogleWindowsStayOnTop())) push_back(CSettingKeyboardHotkey(CHotkeyFunction::ToogleWindowsStayOnTop())); + } + + } // namespace +} // namespace diff --git a/src/blackmisc/setkeyboardhotkeylist.h b/src/blackmisc/setkeyboardhotkeylist.h new file mode 100644 index 000000000..1cf99435b --- /dev/null +++ b/src/blackmisc/setkeyboardhotkeylist.h @@ -0,0 +1,59 @@ +/* Copyright (C) 2013 VATSIM Community / authors + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +//! \file + +#ifndef BLACKMISC_SETTINGS_KEYBOARDHOTKEYLIST_H +#define BLACKMISC_SETTINGS_KEYBOARDHOTKEYLIST_H + +#include "hwkeyboardkey.h" +#include "setkeyboardhotkey.h" +#include "collection.h" +#include "sequence.h" +#include + +namespace BlackMisc +{ + namespace Settings + { + /*! + * Value object encapsulating a list of keyboard keys. + */ + class CSettingKeyboardHotkeyList : public CSequence + { + public: + //! Default constructor + CSettingKeyboardHotkeyList(); + + //! Construct from a base class object. + CSettingKeyboardHotkeyList(const CSequence &baseClass); + + //! \copydoc CValueObject::toQVariant + virtual QVariant toQVariant() const override { return QVariant::fromValue(*this); } + + //! Contains given hotkey function? + bool containsFunction(const CHotkeyFunction &function) const; + + //! Key for given function + BlackMisc::Settings::CSettingKeyboardHotkey keyForFunction(const CHotkeyFunction &function) const; + + //! Register metadata + static void registerMetadata(); + + /*! + * Fill the list with hotkeys + * \param reset true, list will be be reset, otherwise values will not be overridde + */ + void initAsHotkeyList(bool reset = true); + }; + + } //namespace +} // namespace + +Q_DECLARE_METATYPE(BlackMisc::Settings::CSettingKeyboardHotkeyList) +Q_DECLARE_METATYPE(BlackMisc::CCollection) +Q_DECLARE_METATYPE(BlackMisc::CSequence) + +#endif //guard diff --git a/src/blackmisc/settingsblackmiscclasses.h b/src/blackmisc/settingsblackmiscclasses.h index 00f65a27c..786fc3818 100644 --- a/src/blackmisc/settingsblackmiscclasses.h +++ b/src/blackmisc/settingsblackmiscclasses.h @@ -3,5 +3,6 @@ #include "blackmisc/setaudio.h" #include "blackmisc/setnetwork.h" +#include "blackmisc/setkeyboardhotkeylist.h" #endif // guard