Conversions to QString for user-defined Qt types

This commit is contained in:
Bill Somerville 2020-09-26 13:57:21 +01:00
parent 4b85d916ec
commit db9b2d538d
No known key found for this signature in database
GPG Key ID: D864B06D1E81618F
7 changed files with 47 additions and 17 deletions

View File

@ -54,6 +54,7 @@ void register_types ()
// Frequency list model // Frequency list model
qRegisterMetaTypeStreamOperators<FrequencyList_v2::Item> ("Item_v2"); qRegisterMetaTypeStreamOperators<FrequencyList_v2::Item> ("Item_v2");
QMetaType::registerConverter<FrequencyList_v2::Item, QString> (&FrequencyList_v2::Item::toString);
qRegisterMetaTypeStreamOperators<FrequencyList_v2::FrequencyItems> ("FrequencyItems_v2"); qRegisterMetaTypeStreamOperators<FrequencyList_v2::FrequencyItems> ("FrequencyItems_v2");
// defunct old versions // defunct old versions
@ -69,6 +70,7 @@ void register_types ()
// Station details // Station details
qRegisterMetaType<StationList::Station> ("Station"); qRegisterMetaType<StationList::Station> ("Station");
QMetaType::registerConverter<StationList::Station, QString> (&StationList::Station::toString);
qRegisterMetaType<StationList::Stations> ("Stations"); qRegisterMetaType<StationList::Stations> ("Stations");
qRegisterMetaTypeStreamOperators<StationList::Station> ("Station"); qRegisterMetaTypeStreamOperators<StationList::Station> ("Station");
qRegisterMetaTypeStreamOperators<StationList::Stations> ("Stations"); qRegisterMetaTypeStreamOperators<StationList::Stations> ("Stations");
@ -92,5 +94,6 @@ void register_types ()
// DecodeHighlightingModel // DecodeHighlightingModel
qRegisterMetaTypeStreamOperators<DecodeHighlightingModel::HighlightInfo> ("HighlightInfo"); qRegisterMetaTypeStreamOperators<DecodeHighlightingModel::HighlightInfo> ("HighlightInfo");
QMetaType::registerConverter<DecodeHighlightingModel::HighlightInfo, QString> (&DecodeHighlightingModel::HighlightInfo::toString);
qRegisterMetaTypeStreamOperators<DecodeHighlightingModel::HighlightItems> ("HighlightItems"); qRegisterMetaTypeStreamOperators<DecodeHighlightingModel::HighlightItems> ("HighlightItems");
} }

View File

@ -5,9 +5,10 @@
#include <QList> #include <QList>
#include <QBrush> #include <QBrush>
#include <QColor> #include <QColor>
#include <QFont> #include <QFont>
#include <QMap> #include <QMap>
#include <QVector> #include <QVector>
#include <QTextStream>
#include <QDataStream> #include <QDataStream>
#include <QMetaType> #include <QMetaType>
#include <QDebug> #include <QDebug>
@ -72,16 +73,23 @@ QDataStream& operator >> (QDataStream& is, DecodeHighlightingModel::HighlightInf
>> item.background_; >> item.background_;
} }
QString DecodeHighlightingModel::HighlightInfo::toString () const
{
QString string;
QTextStream ots {&string};
ots << "HighlightInfo("
<< highlight_name (type_) << ", "
<< enabled_ << ", "
<< foreground_.color ().name () << ", "
<< background_.color ().name () << ')';
return string;
}
#if !defined (QT_NO_DEBUG_STREAM) #if !defined (QT_NO_DEBUG_STREAM)
QDebug operator << (QDebug debug, DecodeHighlightingModel::HighlightInfo const& item) QDebug operator << (QDebug debug, DecodeHighlightingModel::HighlightInfo const& item)
{ {
QDebugStateSaver save {debug}; QDebugStateSaver save {debug};
debug.nospace () << "HighlightInfo(" return debug.nospace () << item.toString ();
<< item.type_ << ", "
<< item.enabled_ << ", "
<< item.foreground_ << ", "
<< item.background_ << ')';
return debug;
} }
#endif #endif

View File

@ -30,6 +30,7 @@ public:
bool enabled_; bool enabled_;
QBrush foreground_; QBrush foreground_;
QBrush background_; QBrush background_;
QString toString () const;
}; };
using HighlightItems = QList<HighlightInfo>; using HighlightItems = QList<HighlightInfo>;

View File

@ -13,6 +13,7 @@
#include <QVector> #include <QVector>
#include <QStringList> #include <QStringList>
#include <QMimeData> #include <QMimeData>
#include <QTextSTream>
#include <QDataStream> #include <QDataStream>
#include <QByteArray> #include <QByteArray>
#include <QDebugStateSaver> #include <QDebugStateSaver>
@ -347,14 +348,21 @@ namespace
QDebug operator << (QDebug debug, FrequencyList_v2::Item const& item) QDebug operator << (QDebug debug, FrequencyList_v2::Item const& item)
{ {
QDebugStateSaver saver {debug}; QDebugStateSaver saver {debug};
debug.nospace () << "FrequencyItem(" return debug.nospace () << item.toString ();
<< item.frequency_ << ", "
<< item.region_ << ", "
<< item.mode_ << ')';
return debug;
} }
#endif #endif
QString FrequencyList_v2::Item::toString () const
{
QString string;
QTextStream qts {&string};
qts << "FrequencyItem("
<< Radio::frequency_MHz_string (frequency_) << ", "
<< IARURegions::name (region_) << ", "
<< Modes::name (mode_) << ')';
return string;
}
QDataStream& operator << (QDataStream& os, FrequencyList_v2::Item const& item) QDataStream& operator << (QDataStream& os, FrequencyList_v2::Item const& item)
{ {
return os << item.frequency_ return os << item.frequency_

View File

@ -52,6 +52,7 @@ public:
Frequency frequency_; Frequency frequency_;
Mode mode_; Mode mode_;
Region region_; Region region_;
QString toString () const;
}; };
using FrequencyItems = QList<Item>; using FrequencyItems = QList<Item>;
using BandSet = QSet<QString>; using BandSet = QSet<QString>;

View File

@ -13,6 +13,7 @@
#include <QMimeData> #include <QMimeData>
#include <QDataStream> #include <QDataStream>
#include <QByteArray> #include <QByteArray>
#include <QTextStream>
#include <QDebug> #include <QDebug>
#include <QDebugStateSaver> #include <QDebugStateSaver>
@ -26,14 +27,21 @@
QDebug operator << (QDebug debug, StationList::Station const& station) QDebug operator << (QDebug debug, StationList::Station const& station)
{ {
QDebugStateSaver saver {debug}; QDebugStateSaver saver {debug};
debug.nospace () << "Station(" return debug.nospace () << station.toString ();
<< station.band_name_ << ", "
<< station.offset_ << ", "
<< station.antenna_description_ << ')';
return debug;
} }
#endif #endif
QString StationList::Station::toString () const
{
QString string;
QTextStream ots {&string};
ots << "Station("
<< band_name_ << ", "
<< Radio::frequency_MHz_string (offset_) << ", "
<< antenna_description_ << ')';
return string;
}
QDataStream& operator << (QDataStream& os, StationList::Station const& station) QDataStream& operator << (QDataStream& os, StationList::Station const& station)
{ {
return os << station.band_name_ return os << station.band_name_

View File

@ -56,6 +56,7 @@ public:
QString band_name_; QString band_name_;
FrequencyDelta offset_; FrequencyDelta offset_;
QString antenna_description_; QString antenna_description_;
QString toString () const;
}; };
using Stations = QList<Station>; using Stations = QList<Station>;