Start to take advantage of Qt 5.5 features like the new way of registering enums

git-svn-id: svn+ssh://svn.code.sf.net/p/wsjt/wsjt/branches/wsjtx@6583 ab8295b8-cf94-4d9e-aec4-7959e3be5d79
This commit is contained in:
Bill Somerville 2016-04-06 17:11:02 +00:00
parent 79a2a65030
commit 49d6ded6dd
8 changed files with 80 additions and 41 deletions

View File

@ -54,8 +54,8 @@ class QHostAddress;
class Configuration final class Configuration final
: public QObject : public QObject
{ {
Q_OBJECT; Q_OBJECT
Q_ENUMS (DataMode Type2MsgGen); Q_ENUMS (DataMode Type2MsgGen)
public: public:
using MODE = Transceiver::MODE; using MODE = Transceiver::MODE;
@ -64,7 +64,9 @@ public:
using port_type = quint16; using port_type = quint16;
enum DataMode {data_mode_none, data_mode_USB, data_mode_data}; enum DataMode {data_mode_none, data_mode_USB, data_mode_data};
Q_ENUM (DataMode)
enum Type2MsgGen {type_2_msg_1_full, type_2_msg_3_full, type_2_msg_5_only}; enum Type2MsgGen {type_2_msg_1_full, type_2_msg_3_full, type_2_msg_5_only};
Q_ENUM (Type2MsgGen)
explicit Configuration (QSettings * settings, QWidget * parent = nullptr); explicit Configuration (QSettings * settings, QWidget * parent = nullptr);
~Configuration (); ~Configuration ();
@ -219,9 +221,10 @@ private:
pimpl<impl> m_; pimpl<impl> m_;
}; };
#if QT_VERSION < 0x050500
Q_DECLARE_METATYPE (Configuration::DataMode); Q_DECLARE_METATYPE (Configuration::DataMode);
Q_DECLARE_METATYPE (Configuration::Type2MsgGen); Q_DECLARE_METATYPE (Configuration::Type2MsgGen);
#endif
#if !defined (QT_NO_DEBUG_STREAM) #if !defined (QT_NO_DEBUG_STREAM)
ENUM_QDEBUG_OPS_DECL (Configuration, DataMode); ENUM_QDEBUG_OPS_DECL (Configuration, DataMode);

View File

@ -11,6 +11,8 @@
#include <QMimeData> #include <QMimeData>
#include <QDataStream> #include <QDataStream>
#include <QByteArray> #include <QByteArray>
#include <QDebug>
#include <QDebugStateSaver>
#include "Bands.hpp" #include "Bands.hpp"
#include "pimpl_impl.hpp" #include "pimpl_impl.hpp"
@ -105,10 +107,11 @@ namespace
#if !defined (QT_NO_DEBUG_STREAM) #if !defined (QT_NO_DEBUG_STREAM)
QDebug operator << (QDebug debug, FrequencyList::Item const& item) QDebug operator << (QDebug debug, FrequencyList::Item const& item)
{ {
QDebugStateSaver saver {debug};
debug.nospace () << "FrequencyItem(" debug.nospace () << "FrequencyItem("
<< item.frequency_ << ", " << item.frequency_ << ", "
<< item.mode_ << ')'; << item.mode_ << ')';
return debug.space (); return debug;
} }
#endif #endif

View File

@ -32,7 +32,7 @@ void register_types ()
// later versions. // later versions.
qRegisterMetaTypeStreamOperators<Radio::Frequencies> ("Frequencies"); qRegisterMetaTypeStreamOperators<Radio::Frequencies> ("Frequencies");
item_editor_factory ()->registerEditor (frequency_type_id, new QStandardItemEditorCreator<FrequencyLineEdit> ()); item_editor_factory ()->registerEditor (frequency_type_id, new QStandardItemEditorCreator<FrequencyLineEdit> ());
auto frequency_delta_type_id = qRegisterMetaType<Radio::FrequencyDelta> ("FrequencyDelta"); auto frequency_delta_type_id = qRegisterMetaType<Radio::FrequencyDelta> ("FrequencyDelta");
item_editor_factory ()->registerEditor (frequency_delta_type_id, new QStandardItemEditorCreator<FrequencyDeltaLineEdit> ()); item_editor_factory ()->registerEditor (frequency_delta_type_id, new QStandardItemEditorCreator<FrequencyDeltaLineEdit> ());
@ -46,33 +46,39 @@ void register_types ()
qRegisterMetaType<AudioDevice::Channel> ("AudioDevice::Channel"); qRegisterMetaType<AudioDevice::Channel> ("AudioDevice::Channel");
// Configuration // Configuration
#if QT_VERSION < 0x050500
qRegisterMetaType<Configuration::DataMode> ("Configuration::DataMode"); qRegisterMetaType<Configuration::DataMode> ("Configuration::DataMode");
qRegisterMetaTypeStreamOperators<Configuration::DataMode> ("Configuration::DataMode");
qRegisterMetaType<Configuration::Type2MsgGen> ("Configuration::Type2MsgGen"); qRegisterMetaType<Configuration::Type2MsgGen> ("Configuration::Type2MsgGen");
#endif
qRegisterMetaTypeStreamOperators<Configuration::DataMode> ("Configuration::DataMode");
qRegisterMetaTypeStreamOperators<Configuration::Type2MsgGen> ("Configuration::Type2MsgGen"); qRegisterMetaTypeStreamOperators<Configuration::Type2MsgGen> ("Configuration::Type2MsgGen");
// Station details // Station details
qRegisterMetaType<StationList::Station> ("Station"); qRegisterMetaType<StationList::Station> ("Station");
qRegisterMetaTypeStreamOperators<StationList::Station> ("Station");
qRegisterMetaType<StationList::Stations> ("Stations"); qRegisterMetaType<StationList::Stations> ("Stations");
qRegisterMetaTypeStreamOperators<StationList::Station> ("Station");
qRegisterMetaTypeStreamOperators<StationList::Stations> ("Stations"); qRegisterMetaTypeStreamOperators<StationList::Stations> ("Stations");
// Transceiver // Transceiver
qRegisterMetaType<Transceiver::TransceiverState> ("Transceiver::TransceiverState"); qRegisterMetaType<Transceiver::TransceiverState> ("Transceiver::TransceiverState");
#if QT_VERSION < 0x050500
qRegisterMetaType<Transceiver::MODE> ("Transceiver::MODE"); qRegisterMetaType<Transceiver::MODE> ("Transceiver::MODE");
#endif
// Transceiver factory // Transceiver factory
#if QT_VERSION < 0x050500
qRegisterMetaType<TransceiverFactory::DataBits> ("TransceiverFactory::DataBits"); qRegisterMetaType<TransceiverFactory::DataBits> ("TransceiverFactory::DataBits");
qRegisterMetaTypeStreamOperators<TransceiverFactory::DataBits> ("TransceiverFactory::DataBits");
qRegisterMetaType<TransceiverFactory::StopBits> ("TransceiverFactory::StopBits"); qRegisterMetaType<TransceiverFactory::StopBits> ("TransceiverFactory::StopBits");
qRegisterMetaTypeStreamOperators<TransceiverFactory::StopBits> ("TransceiverFactory::StopBits");
qRegisterMetaType<TransceiverFactory::Handshake> ("TransceiverFactory::Handshake"); qRegisterMetaType<TransceiverFactory::Handshake> ("TransceiverFactory::Handshake");
qRegisterMetaTypeStreamOperators<TransceiverFactory::Handshake> ("TransceiverFactory::Handshake");
qRegisterMetaType<TransceiverFactory::PTTMethod> ("TransceiverFactory::PTTMethod"); qRegisterMetaType<TransceiverFactory::PTTMethod> ("TransceiverFactory::PTTMethod");
qRegisterMetaTypeStreamOperators<TransceiverFactory::PTTMethod> ("TransceiverFactory::PTTMethod");
qRegisterMetaType<TransceiverFactory::TXAudioSource> ("TransceiverFactory::TXAudioSource"); qRegisterMetaType<TransceiverFactory::TXAudioSource> ("TransceiverFactory::TXAudioSource");
qRegisterMetaTypeStreamOperators<TransceiverFactory::TXAudioSource> ("TransceiverFactory::TXAudioSource");
qRegisterMetaType<TransceiverFactory::SplitMode> ("TransceiverFactory::SplitMode"); qRegisterMetaType<TransceiverFactory::SplitMode> ("TransceiverFactory::SplitMode");
#endif
qRegisterMetaTypeStreamOperators<TransceiverFactory::DataBits> ("TransceiverFactory::DataBits");
qRegisterMetaTypeStreamOperators<TransceiverFactory::StopBits> ("TransceiverFactory::StopBits");
qRegisterMetaTypeStreamOperators<TransceiverFactory::Handshake> ("TransceiverFactory::Handshake");
qRegisterMetaTypeStreamOperators<TransceiverFactory::PTTMethod> ("TransceiverFactory::PTTMethod");
qRegisterMetaTypeStreamOperators<TransceiverFactory::TXAudioSource> ("TransceiverFactory::TXAudioSource");
qRegisterMetaTypeStreamOperators<TransceiverFactory::SplitMode> ("TransceiverFactory::SplitMode"); qRegisterMetaTypeStreamOperators<TransceiverFactory::SplitMode> ("TransceiverFactory::SplitMode");
// Waterfall palette // Waterfall palette

View File

@ -8,8 +8,8 @@
class Modes final class Modes final
: public QAbstractListModel : public QAbstractListModel
{ {
Q_OBJECT; Q_OBJECT
Q_ENUMS (Mode); Q_ENUMS (Mode)
public: public:
enum Mode enum Mode
@ -23,6 +23,7 @@ public:
ISCAT, ISCAT,
JTMSK, JTMSK,
}; };
Q_ENUM (Mode)
explicit Modes (QObject * parent = nullptr); explicit Modes (QObject * parent = nullptr);
@ -38,7 +39,9 @@ public:
QVariant headerData (int section, Qt::Orientation, int = Qt::DisplayRole) const override; QVariant headerData (int section, Qt::Orientation, int = Qt::DisplayRole) const override;
}; };
#if QT_VERSION < 0x050500
Q_DECLARE_METATYPE (Modes::Mode); Q_DECLARE_METATYPE (Modes::Mode);
#endif
#if !defined (QT_NO_DEBUG_STREAM) #if !defined (QT_NO_DEBUG_STREAM)
ENUM_QDEBUG_OPS_DECL (Modes, Mode); ENUM_QDEBUG_OPS_DECL (Modes, Mode);

View File

@ -14,6 +14,7 @@
#include <QDataStream> #include <QDataStream>
#include <QByteArray> #include <QByteArray>
#include <QDebug> #include <QDebug>
#include <QDebugStateSaver>
#include "pimpl_impl.hpp" #include "pimpl_impl.hpp"
@ -23,11 +24,12 @@
#if !defined (QT_NO_DEBUG_STREAM) #if !defined (QT_NO_DEBUG_STREAM)
QDebug operator << (QDebug debug, StationList::Station const& station) QDebug operator << (QDebug debug, StationList::Station const& station)
{ {
QDebugStateSaver saver {debug};
debug.nospace () << "Station(" debug.nospace () << "Station("
<< station.band_name_ << ", " << station.band_name_ << ", "
<< station.offset_ << ", " << station.offset_ << ", "
<< station.antenna_description_ << ')'; << station.antenna_description_ << ')';
return debug.space (); return debug;
} }
#endif #endif

View File

@ -51,8 +51,8 @@ class QString;
class Transceiver class Transceiver
: public QObject : public QObject
{ {
Q_OBJECT; Q_OBJECT
Q_ENUMS (MODE); Q_ENUMS (MODE)
public: public:
using Frequency = Radio::Frequency; using Frequency = Radio::Frequency;
@ -68,6 +68,7 @@ public:
} }
enum MODE {UNK, CW, CW_R, USB, LSB, FSK, FSK_R, DIG_U, DIG_L, AM, FM, DIG_FM}; enum MODE {UNK, CW, CW_R, USB, LSB, FSK, FSK_R, DIG_U, DIG_L, AM, FM, DIG_FM};
Q_ENUM (MODE)
// //
// Aggregation of all of the rig and PTT state accessible via this // Aggregation of all of the rig and PTT state accessible via this
@ -153,7 +154,9 @@ public:
}; };
Q_DECLARE_METATYPE (Transceiver::TransceiverState); Q_DECLARE_METATYPE (Transceiver::TransceiverState);
#if QT_VERSION < 0x050500
Q_DECLARE_METATYPE (Transceiver::MODE); Q_DECLARE_METATYPE (Transceiver::MODE);
#endif
#if !defined (QT_NO_DEBUG_STREAM) #if !defined (QT_NO_DEBUG_STREAM)
ENUM_QDEBUG_OPS_DECL (Transceiver, MODE); ENUM_QDEBUG_OPS_DECL (Transceiver, MODE);
@ -162,7 +165,6 @@ QDebug operator << (QDebug, Transceiver::TransceiverState const&);
#endif #endif
ENUM_QDATASTREAM_OPS_DECL (Transceiver, MODE); ENUM_QDATASTREAM_OPS_DECL (Transceiver, MODE);
ENUM_CONVERSION_OPS_DECL (Transceiver, MODE); ENUM_CONVERSION_OPS_DECL (Transceiver, MODE);
bool operator != (Transceiver::TransceiverState const&, Transceiver::TransceiverState const&); bool operator != (Transceiver::TransceiverState const&, Transceiver::TransceiverState const&);

View File

@ -20,8 +20,8 @@ class QDir;
class TransceiverFactory class TransceiverFactory
: public QObject : public QObject
{ {
Q_OBJECT; Q_OBJECT
Q_ENUMS (DataBits StopBits Handshake PTTMethod TXAudioSource SplitMode); Q_ENUMS (DataBits StopBits Handshake PTTMethod TXAudioSource SplitMode)
public: public:
// //
@ -65,11 +65,17 @@ public:
// various Transceiver parameters // various Transceiver parameters
// //
enum DataBits {seven_data_bits = 7, eight_data_bits}; enum DataBits {seven_data_bits = 7, eight_data_bits};
Q_ENUM (DataBits)
enum StopBits {one_stop_bit = 1, two_stop_bits}; enum StopBits {one_stop_bit = 1, two_stop_bits};
Q_ENUM (StopBits)
enum Handshake {handshake_none, handshake_XonXoff, handshake_hardware}; enum Handshake {handshake_none, handshake_XonXoff, handshake_hardware};
Q_ENUM (Handshake)
enum PTTMethod {PTT_method_VOX, PTT_method_CAT, PTT_method_DTR, PTT_method_RTS}; enum PTTMethod {PTT_method_VOX, PTT_method_CAT, PTT_method_DTR, PTT_method_RTS};
Q_ENUM (PTTMethod)
enum TXAudioSource {TX_audio_source_front, TX_audio_source_rear}; enum TXAudioSource {TX_audio_source_front, TX_audio_source_rear};
Q_ENUM (TXAudioSource)
enum SplitMode {split_mode_none, split_mode_rig, split_mode_emulate}; enum SplitMode {split_mode_none, split_mode_rig, split_mode_emulate};
Q_ENUM (SplitMode)
TransceiverFactory (); TransceiverFactory ();
@ -157,12 +163,14 @@ bool operator != (TransceiverFactory::ParameterPack const& lhs, TransceiverFacto
// boilerplate routines to make enum types useable and debuggable in // boilerplate routines to make enum types useable and debuggable in
// Qt // Qt
// //
#if QT_VERSION < 0x050500
Q_DECLARE_METATYPE (TransceiverFactory::DataBits); Q_DECLARE_METATYPE (TransceiverFactory::DataBits);
Q_DECLARE_METATYPE (TransceiverFactory::StopBits); Q_DECLARE_METATYPE (TransceiverFactory::StopBits);
Q_DECLARE_METATYPE (TransceiverFactory::Handshake); Q_DECLARE_METATYPE (TransceiverFactory::Handshake);
Q_DECLARE_METATYPE (TransceiverFactory::PTTMethod); Q_DECLARE_METATYPE (TransceiverFactory::PTTMethod);
Q_DECLARE_METATYPE (TransceiverFactory::TXAudioSource); Q_DECLARE_METATYPE (TransceiverFactory::TXAudioSource);
Q_DECLARE_METATYPE (TransceiverFactory::SplitMode); Q_DECLARE_METATYPE (TransceiverFactory::SplitMode);
#endif
#if !defined (QT_NO_DEBUG_STREAM) #if !defined (QT_NO_DEBUG_STREAM)
ENUM_QDEBUG_OPS_DECL (TransceiverFactory, DataBits); ENUM_QDEBUG_OPS_DECL (TransceiverFactory, DataBits);

View File

@ -2,24 +2,20 @@
#define QT_HELPERS_HPP_ #define QT_HELPERS_HPP_
#include <stdexcept> #include <stdexcept>
#include <functional>
#include <QDataStream> #include <QString>
#include <QMetaObject> #include <QMetaObject>
#include <QHostAddress>
#include <QDataStream>
#include <QMetaType> #include <QMetaType>
#include <QMetaEnum> #include <QMetaEnum>
#include <QString>
#include <QDebug>
#include <QHostAddress>
class QVariant;
#define ENUM_QDATASTREAM_OPS_DECL(CLASS, ENUM) \ #define ENUM_QDATASTREAM_OPS_DECL(CLASS, ENUM) \
QDataStream& operator << (QDataStream&, CLASS::ENUM); \ QDataStream& operator << (QDataStream&, CLASS::ENUM const&); \
QDataStream& operator >> (QDataStream&, CLASS::ENUM&); QDataStream& operator >> (QDataStream&, CLASS::ENUM&);
#define ENUM_QDATASTREAM_OPS_IMPL(CLASS, ENUM) \ #define ENUM_QDATASTREAM_OPS_IMPL(CLASS, ENUM) \
QDataStream& operator << (QDataStream& os, CLASS::ENUM v) \ QDataStream& operator << (QDataStream& os, CLASS::ENUM const& v) \
{ \ { \
auto const& mo = CLASS::staticMetaObject; \ auto const& mo = CLASS::staticMetaObject; \
return os << mo.enumerator (mo.indexOfEnumerator (#ENUM)).valueToKey (v); \ return os << mo.enumerator (mo.indexOfEnumerator (#ENUM)).valueToKey (v); \
@ -31,7 +27,7 @@ class QVariant;
is >> buffer; \ is >> buffer; \
bool ok {false}; \ bool ok {false}; \
auto const& mo = CLASS::staticMetaObject; \ auto const& mo = CLASS::staticMetaObject; \
auto const& me = mo.enumerator (mo.indexOfEnumerator (#ENUM)); \ auto const& me = mo.enumerator (mo.indexOfEnumerator (#ENUM)); \
if (buffer) \ if (buffer) \
{ \ { \
v = static_cast<CLASS::ENUM> (me.keyToValue (buffer, &ok)); \ v = static_cast<CLASS::ENUM> (me.keyToValue (buffer, &ok)); \
@ -44,25 +40,41 @@ class QVariant;
return is; \ return is; \
} }
#define ENUM_CONVERSION_OPS_DECL(CLASS, ENUM) \
QString enum_to_qstring (CLASS::ENUM const&);
#define ENUM_CONVERSION_OPS_IMPL(CLASS, ENUM) \
QString enum_to_qstring (CLASS::ENUM const& m) \
{ \
auto const& mo = CLASS::staticMetaObject; \
return QString {mo.enumerator (mo.indexOfEnumerator (#ENUM)).valueToKey (m)}; \
}
#if QT_VERSION >= 0x050500
// Qt 5.5 now has Q_ENUM which registers enumns better
#define ENUM_QDEBUG_OPS_DECL(CLASS, ENUM)
#define ENUM_QDEBUG_OPS_IMPL(CLASS, ENUM)
#else
#define Q_ENUM(E)
#include <QDebug>
class QVariant;
#define ENUM_QDEBUG_OPS_DECL(CLASS, ENUM) \ #define ENUM_QDEBUG_OPS_DECL(CLASS, ENUM) \
QDebug operator << (QDebug, CLASS::ENUM); QDebug operator << (QDebug, CLASS::ENUM const&);
#define ENUM_QDEBUG_OPS_IMPL(CLASS, ENUM) \ #define ENUM_QDEBUG_OPS_IMPL(CLASS, ENUM) \
QDebug operator << (QDebug d, CLASS::ENUM m) \ QDebug operator << (QDebug d, CLASS::ENUM const& m) \
{ \ { \
auto const& mo = CLASS::staticMetaObject; \ auto const& mo = CLASS::staticMetaObject; \
return d << mo.enumerator (mo.indexOfEnumerator (#ENUM)).valueToKey (m); \ return d << mo.enumerator (mo.indexOfEnumerator (#ENUM)).valueToKey (m); \
} }
#define ENUM_CONVERSION_OPS_DECL(CLASS, ENUM) \ #endif
QString enum_to_qstring (CLASS::ENUM);
#define ENUM_CONVERSION_OPS_IMPL(CLASS, ENUM) \
QString enum_to_qstring (CLASS::ENUM m) \
{ \
auto const& mo = CLASS::staticMetaObject; \
return QString {mo.enumerator (mo.indexOfEnumerator (#ENUM)).valueToKey (m)}; \
}
inline inline
void throw_qstring (QString const& qs) void throw_qstring (QString const& qs)