refs #313 value class for hotkey functions

This commit is contained in:
Roland Winklmeier
2014-08-04 20:12:27 +02:00
parent 98942a6395
commit c6540e8521
4 changed files with 252 additions and 0 deletions

View File

@@ -15,5 +15,6 @@
#include "blackmisc/statusmessagelist.h"
#include "blackmisc/statusmessage.h"
#include "blackmisc/audioallclasses.h"
#include "hotkeyfunction.h"
#endif // guard

View File

@@ -157,6 +157,8 @@ void BlackMisc::registerMetadata()
CIcon::registerMetadata();
CIconList::registerMetadata();
CHotkeyFunction::registerMetadata();
// sub namespaces
PhysicalQuantities::registerMetadata();
Aviation::registerMetadata();

View File

@@ -0,0 +1,106 @@
/* 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 "hotkeyfunction.h"
#include "QCoreApplication"
namespace BlackMisc
{
CHotkeyFunction::CHotkeyFunction()
: m_function(HotkeyNone)
{
}
CHotkeyFunction::CHotkeyFunction(Function function)
: m_function(function)
{
}
QString CHotkeyFunction::getFunctionAsString() const
{
switch (m_function)
{
case HotkeyNone: return "";
case HotkeyPtt: return qApp->translate("CHotkeyFunction", "PTT");
case HotkeyOpacity50: return qApp->translate("CHotkeyFunction", "Opacity 50%");
case HotkeyOpacity100: return qApp->translate("CHotkeyFunction", "Opacity 100%");
case HotkeyToggleCom1: return qApp->translate("CHotkeyFunction", "Toggle COM1");
case HotkeyToggleCom2: return qApp->translate("CHotkeyFunction", "Toggle COM2");
case HotkeyToogleWindowsStayOnTop: return qApp->translate("CHotkeyFunction", "Toogle Window on top");
default:
qFatal("Wrong function");
return "";
}
}
// Hash
uint CHotkeyFunction::getValueHash() const
{
return qHash(TupleConverter<CHotkeyFunction>::toMetaTuple(*this));
}
// To JSON
QJsonObject CHotkeyFunction::toJson() const
{
return BlackMisc::serializeJson(TupleConverter<CHotkeyFunction>::toMetaTuple(*this));
}
// From Json
void CHotkeyFunction::fromJson(const QJsonObject &json)
{
BlackMisc::deserializeJson(json, TupleConverter<CHotkeyFunction>::toMetaTuple(*this));
}
void CHotkeyFunction::registerMetadata()
{
qRegisterMetaType<CHotkeyFunction>();
qDBusRegisterMetaType<CHotkeyFunction>();
}
// Equal?
bool CHotkeyFunction::operator ==(const CHotkeyFunction &other) const
{
if (this == &other) return true;
return TupleConverter<CHotkeyFunction>::toMetaTuple(*this) == TupleConverter<CHotkeyFunction>::toMetaTuple(other);
}
QString CHotkeyFunction::convertToQString(bool /* i18n */) const
{
return getFunctionAsString();
}
int CHotkeyFunction::getMetaTypeId() const
{
return qMetaTypeId<CHotkeyFunction>();
}
bool CHotkeyFunction::isA(int metaTypeId) const
{
return (metaTypeId == qMetaTypeId<CHotkeyFunction>());
}
// Compare
int CHotkeyFunction::compareImpl(const CValueObject &otherBase) const
{
const auto &other = static_cast<const CHotkeyFunction &>(otherBase);
return compare(TupleConverter<CHotkeyFunction>::toMetaTuple(*this), TupleConverter<CHotkeyFunction>::toMetaTuple(other));
}
// Marshall to DBus
void CHotkeyFunction::marshallToDbus(QDBusArgument &argument) const
{
argument << TupleConverter<CHotkeyFunction>::toMetaTuple(*this);
}
// Unmarshall from DBus
void CHotkeyFunction::unmarshallFromDbus(const QDBusArgument &argument)
{
argument >> TupleConverter<CHotkeyFunction>::toMetaTuple(*this);
}
}

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.
*/
#ifndef BLACKMISC_HOTKEYFUNCTION_H
#define BLACKMISC_HOTKEYFUNCTION_H
#include "valueobject.h"
//! \file
namespace BlackMisc
{
//! Value object representing a hotkey function.
class CHotkeyFunction : public CValueObject
{
public:
//! Function type
enum Function
{
HotkeyNone,
HotkeyPtt,
HotkeyToggleCom1,
HotkeyToggleCom2,
HotkeyOpacity50,
HotkeyOpacity100,
HotkeyToogleWindowsStayOnTop
};
//! Default constructor
CHotkeyFunction();
//! Constructor by function
CHotkeyFunction(Function function);
//! Get function as string
QString getFunctionAsString() const;
//! Function
Function getFunction() const { return m_function; }
//! Set function
void setFunction(const Function &function) { m_function = 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 CHotkeyFunction &other) const;
//! Hotkey function is Ptt
static const CHotkeyFunction &Ptt()
{
static CHotkeyFunction hotkeyFunction(HotkeyPtt);
return hotkeyFunction;
}
//! Hotkey function is toggle Com1
static const CHotkeyFunction &ToggleCom1()
{
static CHotkeyFunction hotkeyFunction(HotkeyToggleCom1);
return hotkeyFunction;
}
//! Hotkey function is toggle Com2
static const CHotkeyFunction &ToggleCom2()
{
static CHotkeyFunction hotkeyFunction(HotkeyToggleCom2);
return hotkeyFunction;
}
//! Hotkey function is opacity 50
static const CHotkeyFunction &Opacity50()
{
static CHotkeyFunction hotkeyFunction(HotkeyOpacity50);
return hotkeyFunction;
}
//! Hotkey function is opacity 100
static const CHotkeyFunction &Opacity100()
{
static CHotkeyFunction hotkeyFunction(HotkeyOpacity100);
return hotkeyFunction;
}
//! Hotkey function is toggle windows on top
static const CHotkeyFunction &ToogleWindowsStayOnTop()
{
static CHotkeyFunction hotkeyFunction(HotkeyToogleWindowsStayOnTop);
return hotkeyFunction;
}
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(CHotkeyFunction)
Function m_function;
};
}
BLACK_DECLARE_TUPLE_CONVERSION(BlackMisc::CHotkeyFunction, (o.m_function))
Q_DECLARE_METATYPE(BlackMisc::CHotkeyFunction)
#endif //BLACKMISC_HOTKEYFUNCTION_H