refs #313 KeyboardKey wrapper class for current settings implementation

This commit is contained in:
Roland Winklmeier
2014-08-04 20:38:20 +02:00
parent 1adfd16ec9
commit 4d60ca3a87
6 changed files with 476 additions and 0 deletions

View File

@@ -121,6 +121,8 @@ void BlackMisc::Settings::registerMetadata()
{
CSettingsNetwork::registerMetadata();
CSettingsAudio::registerMetadata();
CSettingKeyboardHotkey::registerMetadata();
CSettingKeyboardHotkeyList::registerMetadata();
}
/*

View File

@@ -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 <QCoreApplication>
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<CSettingKeyboardHotkey>();
qDBusRegisterMetaType<CSettingKeyboardHotkey>();
}
/*
* To JSON
*/
QJsonObject CSettingKeyboardHotkey::toJson() const
{
return BlackMisc::serializeJson(TupleConverter<CSettingKeyboardHotkey>::toMetaTuple(*this));
}
/*
* From Json
*/
void CSettingKeyboardHotkey::fromJson(const QJsonObject &json)
{
BlackMisc::deserializeJson(json, TupleConverter<CSettingKeyboardHotkey>::toMetaTuple(*this));
}
QString CSettingKeyboardHotkey::convertToQString(bool /* i18n */) const
{
return m_key.toQString();
}
int CSettingKeyboardHotkey::getMetaTypeId() const
{
return qMetaTypeId<CSettingKeyboardHotkey>();
}
bool CSettingKeyboardHotkey::isA(int metaTypeId) const
{
return (metaTypeId == qMetaTypeId<CSettingKeyboardHotkey>());
}
/*
* Compare
*/
int CSettingKeyboardHotkey::compareImpl(const CValueObject &otherBase) const
{
const auto &other = static_cast<const CSettingKeyboardHotkey &>(otherBase);
return compare(TupleConverter<CSettingKeyboardHotkey>::toMetaTuple(*this), TupleConverter<CSettingKeyboardHotkey>::toMetaTuple(other));
}
/*
* Marshall to DBus
*/
void CSettingKeyboardHotkey::marshallToDbus(QDBusArgument &argument) const
{
argument << TupleConverter<CSettingKeyboardHotkey>::toMetaTuple(*this);
}
/*
* Unmarshall from DBus
*/
void CSettingKeyboardHotkey::unmarshallFromDbus(const QDBusArgument &argument)
{
argument >> TupleConverter<CSettingKeyboardHotkey>::toMetaTuple(*this);
}
/*
* Hash
*/
uint CSettingKeyboardHotkey::getValueHash() const
{
return qHash(TupleConverter<CSettingKeyboardHotkey>::toMetaTuple(*this));
}
/*
* Equal?
*/
bool CSettingKeyboardHotkey::operator ==(const CSettingKeyboardHotkey &other) const
{
if (this == &other) return true;
return TupleConverter<CSettingKeyboardHotkey>::toMetaTuple(*this) == TupleConverter<CSettingKeyboardHotkey>::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<uint>(m_key.getModifier1()));
case IndexModifier2:
return QVariant(static_cast<uint>(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<uint>();
m_hotkeyFunction.setFunction(static_cast<CHotkeyFunction::Function>(f));
break;
}
case IndexKey:
case IndexKeyAsString:
case IndexKeyAsStringRepresentation:
{
// static_cast see docu of variant.type()
if (static_cast<QMetaType::Type>(variant.type()) == QMetaType::QChar)
m_key.setKey(variant.value<QChar>());
else
m_key.setKey(variant.value<QString>());
break;
}
case IndexModifier1AsString:
m_key.setModifier1(variant.value<QString>());
break;
case IndexModifier2AsString:
m_key.setModifier2(variant.value<QString>());
break;
case IndexObject:
this->setObject(variant.value<BlackMisc::Settings::CSettingKeyboardHotkey>());
break;
default:
Q_ASSERT_X(false, "CSettingKeyboardHotkey", "index unknown (setter)");
break;
}
}
} // namespace Hardware
} // BlackMisc

View File

@@ -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 <QStringList>
#include <QKeySequence>
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

View File

@@ -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<CSettingKeyboardHotkey> &baseClass) :
CSequence<CSettingKeyboardHotkey>(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<BlackMisc::CSequence<CSettingKeyboardHotkey>>();
qDBusRegisterMetaType<BlackMisc::CSequence<CSettingKeyboardHotkey>>();
qRegisterMetaType<BlackMisc::CCollection<CSettingKeyboardHotkey>>();
qDBusRegisterMetaType<BlackMisc::CCollection<CSettingKeyboardHotkey>>();
qRegisterMetaType<CSettingKeyboardHotkeyList>();
qDBusRegisterMetaType<CSettingKeyboardHotkeyList>();
}
/*
* 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

View File

@@ -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 <QObject>
namespace BlackMisc
{
namespace Settings
{
/*!
* Value object encapsulating a list of keyboard keys.
*/
class CSettingKeyboardHotkeyList : public CSequence<CSettingKeyboardHotkey>
{
public:
//! Default constructor
CSettingKeyboardHotkeyList();
//! Construct from a base class object.
CSettingKeyboardHotkeyList(const CSequence<CSettingKeyboardHotkey> &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<BlackMisc::Settings::CSettingKeyboardHotkey>)
Q_DECLARE_METATYPE(BlackMisc::CSequence<BlackMisc::Settings::CSettingKeyboardHotkey>)
#endif //guard

View File

@@ -3,5 +3,6 @@
#include "blackmisc/setaudio.h"
#include "blackmisc/setnetwork.h"
#include "blackmisc/setkeyboardhotkeylist.h"
#endif // guard