diff options
Diffstat (limited to 'models')
| -rw-r--r-- | models/Bands.cpp | 236 | ||||
| -rw-r--r-- | models/Bands.hpp | 83 | ||||
| -rw-r--r-- | models/CabrilloLog.cpp | 174 | ||||
| -rw-r--r-- | models/CabrilloLog.hpp | 37 | ||||
| -rw-r--r-- | models/DecodeHighlightingModel.cpp | 335 | ||||
| -rw-r--r-- | models/DecodeHighlightingModel.hpp | 79 | ||||
| -rw-r--r-- | models/FontOverrideModel.hpp | 29 | ||||
| -rw-r--r-- | models/FoxLog.cpp | 144 | ||||
| -rw-r--r-- | models/FoxLog.hpp | 32 | ||||
| -rw-r--r-- | models/FrequencyList.cpp | 819 | ||||
| -rw-r--r-- | models/FrequencyList.hpp | 186 | ||||
| -rw-r--r-- | models/IARURegions.cpp | 96 | ||||
| -rw-r--r-- | models/IARURegions.hpp | 66 | ||||
| -rw-r--r-- | models/Modes.cpp | 102 | ||||
| -rw-r--r-- | models/Modes.hpp | 74 | ||||
| -rw-r--r-- | models/StationList.cpp | 531 | ||||
| -rw-r--r-- | models/StationList.hpp | 106 | ||||
| -rw-r--r-- | models/models.pri | 18 |
18 files changed, 3147 insertions, 0 deletions
diff --git a/models/Bands.cpp b/models/Bands.cpp new file mode 100644 index 00000000..a70358fa --- /dev/null +++ b/models/Bands.cpp @@ -0,0 +1,236 @@ +#include "Bands.hpp" + +#include <algorithm> + +#include <QString> +#include <QVariant> + +namespace +{ + // Table of ADIF band definitions as defined in the ADIF + // specification as at ADIF v3.0.6 + struct ADIFBand + { + char const * const name_; + Radio::Frequency lower_bound_; + Radio::Frequency upper_bound_; + } constexpr ADIF_bands[] = { + {"2190m", 136000u, 137000u}, + {"630m", 472000u, 479000u}, + {"560m", 501000u, 504000u}, + {"160m", 1800000u, 2000000u}, + {"80m", 3500000u, 4000000u}, + {"60m", 5060000u, 5450000u}, + {"40m", 7000000u, 7300000u}, + {"30m", 10000000u, 10150000u}, + {"20m", 14000000u, 14350000u}, + {"17m", 18068000u, 18168000u}, + {"15m", 21000000u, 21450000u}, + {"12m", 24890000u, 24990000u}, + {"10m", 28000000u, 29700000u}, + {"6m", 50000000u, 54000000u}, + {"4m", 70000000u, 71000000u}, + {"2m", 144000000u, 148000000u}, + {"1.25m", 222000000u, 225000000u}, + {"70cm", 420000000u, 450000000u}, + {"33cm", 902000000u, 928000000u}, + {"23cm", 1240000000u, 1300000000u}, + {"13cm", 2300000000u, 2450000000u}, + {"9cm", 3300000000u, 3500000000u}, + {"6cm", 5650000000u, 5925000000u}, + {"3cm", 10000000000u, 10500000000u}, + {"1.25cm",24000000000u, 24250000000u}, + {"6mm", 47000000000u, 47200000000u}, + {"4mm", 75500000000u, 81000000000u}, + {"2.5mm", 119980000000u,120020000000u}, + {"2mm", 142000000000u,149000000000u}, + {"1mm", 241000000000u,250000000000u}, + }; + + QString const oob_name {QObject::tr ("OOB")}; + + int constexpr table_rows () + { + return sizeof (ADIF_bands) / sizeof (ADIF_bands[0]); + } +} + +Bands::Bands (QObject * parent) + : QAbstractTableModel {parent} +{ +} + +QString Bands::find (Frequency f) const +{ + QString result; + auto const& end_iter = ADIF_bands + table_rows (); + auto const& row_iter = std::find_if (ADIF_bands, end_iter, [f] (ADIFBand const& band) { + return band.lower_bound_ <= f && f <= band.upper_bound_; + }); + if (row_iter != end_iter) + { + result = row_iter->name_; + } + return result; +} + +int Bands::find (QString const& band) const +{ + int result {-1}; + for (auto i = 0u; i < table_rows (); ++i) + { + if (band == ADIF_bands[i].name_) + { + result = i; + } + } + return result; +} + +QString const& Bands::oob () +{ + return oob_name; +} + +int Bands::rowCount (QModelIndex const& parent) const +{ + return parent.isValid () ? 0 : table_rows (); +} + +int Bands::columnCount (QModelIndex const& parent) const +{ + return parent.isValid () ? 0 : 3; +} + +Qt::ItemFlags Bands::flags (QModelIndex const& index) const +{ + return QAbstractTableModel::flags (index) | Qt::ItemIsDropEnabled; +} + +QVariant Bands::data (QModelIndex const& index, int role) const +{ + QVariant item; + + if (!index.isValid ()) + { + // Hijack root for OOB string. + if (Qt::DisplayRole == role) + { + item = oob_name; + } + } + else + { + auto row = index.row (); + auto column = index.column (); + + if (row < table_rows ()) + { + switch (role) + { + case Qt::ToolTipRole: + case Qt::AccessibleDescriptionRole: + switch (column) + { + case 0: item = tr ("Band name"); break; + case 1: item = tr ("Lower frequency limit"); break; + case 2: item = tr ("Upper frequency limit"); break; + } + break; + + case SortRole: + case Qt::DisplayRole: + case Qt::EditRole: + switch (column) + { + case 0: + if (SortRole == role) + { + // band name sorts by lower bound + item = ADIF_bands[row].lower_bound_; + } + else + { + item = ADIF_bands[row].name_; + } + break; + + case 1: item = ADIF_bands[row].lower_bound_; break; + case 2: item = ADIF_bands[row].upper_bound_; break; + } + break; + + case Qt::AccessibleTextRole: + switch (column) + { + case 0: item = ADIF_bands[row].name_; break; + case 1: item = ADIF_bands[row].lower_bound_; break; + case 2: item = ADIF_bands[row].upper_bound_; break; + } + break; + + case Qt::TextAlignmentRole: + switch (column) + { + case 0: + item = Qt::AlignHCenter + Qt::AlignVCenter; + break; + + case 1: + case 2: + item = Qt::AlignRight + Qt::AlignVCenter; + break; + } + break; + } + } + } + return item; +} + +QVariant Bands::headerData (int section, Qt::Orientation orientation, int role) const +{ + QVariant result; + + if (Qt::DisplayRole == role && Qt::Horizontal == orientation) + { + switch (section) + { + case 0: result = tr ("Band"); break; + case 1: result = tr ("Lower Limit"); break; + case 2: result = tr ("Upper Limit"); break; + } + } + else + { + result = QAbstractTableModel::headerData (section, orientation, role); + } + + return result; +} + +QString Bands::const_iterator::operator * () +{ + return ADIF_bands[row_].name_; +} + +bool Bands::const_iterator::operator != (const_iterator const& rhs) const +{ + return row_ != rhs.row_; +} + +auto Bands::const_iterator::operator ++ () -> const_iterator& +{ + ++row_; + return *this; +} + +auto Bands::begin () const -> Bands::const_iterator +{ + return const_iterator (0); +} + +auto Bands::end () const -> Bands::const_iterator +{ + return const_iterator (table_rows ()); +} diff --git a/models/Bands.hpp b/models/Bands.hpp new file mode 100644 index 00000000..ed625f2f --- /dev/null +++ b/models/Bands.hpp @@ -0,0 +1,83 @@ +#ifndef BANDS_HPP__ +#define BANDS_HPP__ + +#include <QAbstractTableModel> + +#include "Radio.hpp" + +// +// Class Bands +// +// Encapsulates information about amateur radio bands as defined by +// the ADIF specification. The model is immutable. The rows are +// stored in asscending order of frequency. +// +// Responsibilities +// +// Provides a well known band name mapped to lower and upper +// frequency limits. Also provides a convenience operation to +// determine the band name for any given frequency, the result of +// which may be null if the given frequency doesn't lie within a +// recognised band. +// +// Collaborations +// +// Implements the QAbstractTableModel interface as an immutable table +// where rows are bands and columns are band name, lower frequency +// limit and, upper ferquency limit respectively. +// +class Bands final + : public QAbstractTableModel +{ +public: + using Frequency = Radio::Frequency; + + // an iterator that meets the requirements of the C++ for range statement + class const_iterator + { + public: + const_iterator (int row) + : row_ {row} + { + } + + QString operator * (); + bool operator != (const_iterator const&) const; + const_iterator& operator ++ (); + + private: + int row_; + }; + + explicit Bands (QObject * parent = nullptr); + + // + // Model API + // + QString find (Frequency) const; // find band Frequency is in + int find (QString const&) const; // find row of band (-1 if not valid) + static QString const& oob (); + + // Iterators + const_iterator begin () const; + const_iterator end () const; + + // Custom role for sorting. + static int constexpr SortRole = Qt::UserRole; + + // Implement the QAbstractTableModel interface + int rowCount (QModelIndex const& parent = QModelIndex {}) const override; + int columnCount (QModelIndex const& parent = QModelIndex {}) const override; + Qt::ItemFlags flags (QModelIndex const& = QModelIndex {}) const override; + QVariant headerData (int section, Qt::Orientation, int = Qt::DisplayRole) const override; + + // The value return for the Qt::DisplayRole role for the root of the + // model (invalid index) is a special string representing out of + // band. + // + // All columns return a number for the custom role SortRole, this + // number defines a strict frequency order for the rows. + QVariant data (QModelIndex const&, int role = Qt::DisplayRole) const override; +}; + +#endif diff --git a/models/CabrilloLog.cpp b/models/CabrilloLog.cpp new file mode 100644 index 00000000..aaedd3bd --- /dev/null +++ b/models/CabrilloLog.cpp @@ -0,0 +1,174 @@ +#include "CabrilloLog.hpp" + +#include <stdexcept> +#include <utility> +#include <QString> +#include <QDateTime> +#include <QSqlDatabase> +#include <QSqlTableModel> +#include <QSqlRecord> +#include <QSqlError> +#include <QSqlQuery> +#include <QDataStream> +#include "Configuration.hpp" +#include "Bands.hpp" +#include "qt_db_helpers.hpp" +#include "pimpl_impl.hpp" + +class CabrilloLog::impl final + : public QSqlTableModel +{ +public: + impl (Configuration const *); + + Configuration const * configuration_; + QSqlQuery mutable dupe_query_; + QSqlQuery mutable export_query_; +}; + +CabrilloLog::impl::impl (Configuration const * configuration) + : QSqlTableModel {} + , configuration_ {configuration} +{ + if (!database ().tables ().contains ("cabrillo_log")) + { + QSqlQuery query; + SQL_error_check (query, static_cast<bool (QSqlQuery::*) (QString const&)> (&QSqlQuery::exec), + "CREATE TABLE cabrillo_log (" + " id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL," + " frequency INTEGER NOT NULL," + " \"when\" DATETIME NOT NULL," + " call VARCHAR(20) NOT NULL," + " exchange_sent VARCHAR(32) NOT NULL," + " exchange_rcvd VARCHAR(32) NOT NULL," + " band VARCHAR(6) NOT NULL" + ")"); + } + + SQL_error_check (dupe_query_, &QSqlQuery::prepare, + "SELECT COUNT(*) FROM cabrillo_log WHERE call = :call AND band = :band"); + + SQL_error_check (export_query_, &QSqlQuery::prepare, + "SELECT frequency, \"when\", exchange_sent, call, exchange_rcvd FROM cabrillo_log ORDER BY \"when\""); + + setEditStrategy (QSqlTableModel::OnFieldChange); + setTable ("cabrillo_log"); + setHeaderData (fieldIndex ("frequency"), Qt::Horizontal, tr ("Freq(kHz)")); + setHeaderData (fieldIndex ("when"), Qt::Horizontal, tr ("Date & Time(UTC)")); + setHeaderData (fieldIndex ("call"), Qt::Horizontal, tr ("Call")); + setHeaderData (fieldIndex ("exchange_sent"), Qt::Horizontal, tr ("Sent")); + setHeaderData (fieldIndex ("exchange_rcvd"), Qt::Horizontal, tr ("Rcvd")); + setHeaderData (fieldIndex ("band"), Qt::Horizontal, tr ("Band")); + + // This descending order by time is important, it makes the view + // place the latest row at the top, without this the model/view + // interactions are both sluggish and unhelpful. + setSort (fieldIndex ("when"), Qt::DescendingOrder); + + SQL_error_check (*this, &QSqlTableModel::select); +} + +CabrilloLog::CabrilloLog (Configuration const * configuration) + : m_ {configuration} +{ + Q_ASSERT (configuration); +} + +CabrilloLog::~CabrilloLog () +{ +} + +QSqlTableModel * CabrilloLog::model () +{ + return &*m_; +} + +namespace +{ + void set_value_maybe_null (QSqlRecord& record, QString const& name, QString const& value) + { + if (value.size ()) + { + record.setValue (name, value); + } + else + { + record.setNull (name); + } + } +} + +bool CabrilloLog::add_QSO (Frequency frequency, QDateTime const& when, QString const& call + , QString const& exchange_sent, QString const& exchange_received) +{ + auto record = m_->record (); + record.setValue ("frequency", frequency / 1000ull); // kHz + if (!when.isNull ()) + { + record.setValue ("when", when.toMSecsSinceEpoch () / 1000ull); + } + else + { + record.setNull ("when"); + } + set_value_maybe_null (record, "call", call); + set_value_maybe_null (record, "exchange_sent", exchange_sent); + set_value_maybe_null (record, "exchange_rcvd", exchange_received); + set_value_maybe_null (record, "band", m_->configuration_->bands ()->find (frequency)); + if (m_->isDirty ()) + { + m_->revert (); // discard any uncommitted changes + } + m_->setEditStrategy (QSqlTableModel::OnManualSubmit); + ConditionalTransaction transaction {*m_}; + auto ok = m_->insertRecord (-1, record); + transaction.submit (); + m_->setEditStrategy (QSqlTableModel::OnFieldChange); + return ok; +} + +bool CabrilloLog::dupe (Frequency frequency, QString const& call) const +{ + m_->dupe_query_.bindValue (":call", call); + m_->dupe_query_.bindValue (":band", m_->configuration_->bands ()->find (frequency)); + SQL_error_check (m_->dupe_query_, static_cast<bool (QSqlQuery::*) ()> (&QSqlQuery::exec)); + m_->dupe_query_.next (); + return m_->dupe_query_.value (0).toInt (); +} + +void CabrilloLog::reset () +{ + if (m_->rowCount ()) + { + m_->setEditStrategy (QSqlTableModel::OnManualSubmit); + ConditionalTransaction transaction {*m_}; + SQL_error_check (*m_, &QSqlTableModel::removeRows, 0, m_->rowCount (), QModelIndex {}); + transaction.submit (); + m_->select (); // to refresh views + m_->setEditStrategy (QSqlTableModel::OnFieldChange); + } +} + +void CabrilloLog::export_qsos (QTextStream& stream) const +{ + SQL_error_check (m_->export_query_, static_cast<bool (QSqlQuery::*) ()> (&QSqlQuery::exec)); + auto record = m_->export_query_.record (); + auto frequency_index = record.indexOf ("frequency"); + auto when_index = record.indexOf ("when"); + auto call_index = record.indexOf ("call"); + auto sent_index = record.indexOf ("exchange_sent"); + auto rcvd_index = record.indexOf ("exchange_rcvd"); + while (m_->export_query_.next ()) + { + auto frequency = m_->export_query_.value (frequency_index).value<Radio::Frequency> (); + auto my_call = m_->configuration_->my_callsign (); + frequency = frequency > 50000000ull ? frequency / 1000ull : frequency; + stream << QString {"QSO: %1 DG %2 %3 %4 %5 %6\n"} + .arg (frequency, 5) + .arg (QDateTime::fromMSecsSinceEpoch (m_->export_query_.value (when_index).toULongLong () * 1000ull, Qt::UTC).toString ("yyyy-MM-dd hhmm")) + .arg (my_call, -12) + .arg (m_->export_query_.value (sent_index).toString (), -13) + .arg (m_->export_query_.value (call_index).toString (), -12) + .arg (m_->export_query_.value (rcvd_index).toString (), -13); + } +} diff --git a/models/CabrilloLog.hpp b/models/CabrilloLog.hpp new file mode 100644 index 00000000..43b9e8f9 --- /dev/null +++ b/models/CabrilloLog.hpp @@ -0,0 +1,37 @@ +#ifndef CABRILLO_LOG_HPP_ +#define CABRILLO_LOG_HPP_ + +#include <boost/core/noncopyable.hpp> +#include "Radio.hpp" +#include "pimpl_h.hpp" + +class Configuration; +class QDateTime; +class QString; +class QSqlTableModel; +class QTextStream; + +class CabrilloLog final + : private boost::noncopyable +{ +public: + using Frequency = Radio::Frequency; + + explicit CabrilloLog (Configuration const *); + ~CabrilloLog (); + + // returns false if insert fails + bool add_QSO (Frequency, QDateTime const&, QString const& call + , QString const& report_sent, QString const& report_received); + bool dupe (Frequency, QString const& call) const; + + QSqlTableModel * model (); + void reset (); + void export_qsos (QTextStream&) const; + +private: + class impl; + pimpl<impl> m_; +}; + +#endif diff --git a/models/DecodeHighlightingModel.cpp b/models/DecodeHighlightingModel.cpp new file mode 100644 index 00000000..6425a1a5 --- /dev/null +++ b/models/DecodeHighlightingModel.cpp @@ -0,0 +1,335 @@ +#include "DecodeHighlightingModel.hpp" + +#include <QString> +#include <QVariant> +#include <QList> +#include <QBrush> +#include <QColor> + #include <QFont> +#include <QMap> +#include <QVector> +#include <QDataStream> +#include <QMetaType> +#include <QDebug> + +#include "pimpl_impl.hpp" + +#include "moc_DecodeHighlightingModel.cpp" + +class DecodeHighlightingModel::impl final +{ +public: + explicit impl () + : data_ {defaults_} + { + } + + HighlightItems static const defaults_; + HighlightItems data_; + QFont font_; +}; + +QList<DecodeHighlightingModel::HighlightInfo> const DecodeHighlightingModel::impl::defaults_ = { + {Highlight::MyCall, true, {}, {{0xff, 0x66, 0x66}}} + , {Highlight::Continent, true, {}, {{0xff, 0x00, 0x63}}} + , {Highlight::ContinentBand, true, {}, {{0xff, 0x99, 0xc2}}} + , {Highlight::CQZone, true, {}, {{0xff, 0xbf, 0x00}}} + , {Highlight::CQZoneBand, true, {}, {{0xff, 0xe4, 0x99}}} + , {Highlight::ITUZone, false, {}, {{0xa6, 0xff, 0x00}}} + , {Highlight::ITUZoneBand, false, {}, {{0xdd, 0xff, 0x99}}} + , {Highlight::DXCC, true, {}, {{0xff, 0x00, 0xff}}} + , {Highlight::DXCCBand, true, {}, {{0xff, 0xaa, 0xff}}} + , {Highlight::Grid, false, {}, {{0xff, 0x80, 0x00}}} + , {Highlight::GridBand, false, {}, {{0xff, 0xcc, 0x99}}} + , {Highlight::Call, false, {}, {{0x00, 0xff, 0xff}}} + , {Highlight::CallBand, false, {}, {{0x99, 0xff, 0xff}}} + , {Highlight::LotW, false, {{0x99, 0x00, 0x00}}, {}} + , {Highlight::CQ, true, {}, {{0x66, 0xff, 0x66}}} + , {Highlight::Tx, true, {}, {{Qt::yellow}}} +}; + +bool operator == (DecodeHighlightingModel::HighlightInfo const& lhs, DecodeHighlightingModel::HighlightInfo const& rhs) +{ + return lhs.type_ == rhs.type_ + && lhs.enabled_ == rhs.enabled_ + && lhs.foreground_ == rhs.foreground_ + && lhs.background_ == rhs.background_; +} + +QDataStream& operator << (QDataStream& os, DecodeHighlightingModel::HighlightInfo const& item) +{ + return os << item.type_ + << item.enabled_ + << item.foreground_ + << item.background_; +} + +QDataStream& operator >> (QDataStream& is, DecodeHighlightingModel::HighlightInfo& item) +{ + return is >> item.type_ + >> item.enabled_ + >> item.foreground_ + >> item.background_; +} + +#if !defined (QT_NO_DEBUG_STREAM) +QDebug operator << (QDebug debug, DecodeHighlightingModel::HighlightInfo const& item) +{ + QDebugStateSaver save {debug}; + debug.nospace () << "HighlightInfo(" + << item.type_ << ", " + << item.enabled_ << ", " + << item.foreground_ << ", " + << item.background_ << ')'; + return debug; +} +#endif + +ENUM_QDATASTREAM_OPS_IMPL (DecodeHighlightingModel, Highlight); +ENUM_CONVERSION_OPS_IMPL (DecodeHighlightingModel, Highlight); + +DecodeHighlightingModel::DecodeHighlightingModel (QObject * parent) + : QAbstractListModel {parent} +{ +} + +DecodeHighlightingModel::~DecodeHighlightingModel () +{ +} + +QString DecodeHighlightingModel::highlight_name (Highlight h) +{ + switch (h) + { + case Highlight::CQ: return "CQ in message"; + case Highlight::MyCall: return "My Call in message"; + case Highlight::Tx: return "Transmitted message"; + case Highlight::DXCC: return "New DXCC"; + case Highlight::DXCCBand: return "New DXCC on Band"; + case Highlight::Grid: return "New Grid"; + case Highlight::GridBand: return "New Grid on Band"; + case Highlight::Call: return "New Call"; + case Highlight::CallBand: return "New Call on Band"; + case Highlight::Continent: return "New Continent"; + case Highlight::ContinentBand: return "New Continent on Band"; + case Highlight::CQZone: return "New CQ Zone"; + case Highlight::CQZoneBand: return "New CQ Zone on Band"; + case Highlight::ITUZone: return "New ITU Zone"; + case Highlight::ITUZoneBand: return "New ITU Zone on Band"; + case Highlight::LotW: return "LotW User"; + } + return "Unknown"; +} + +auto DecodeHighlightingModel::default_items () -> HighlightItems const& +{ + return impl::defaults_; +} + +auto DecodeHighlightingModel::items () const -> HighlightItems const& +{ + return m_->data_; +} + +void DecodeHighlightingModel::items (HighlightItems const& items) +{ + m_->data_ = items; + QVector<int> roles; + roles << Qt::CheckStateRole << Qt::ForegroundRole << Qt::BackgroundRole; + Q_EMIT dataChanged (index (0, 0), index (rowCount () - 1, 0), roles); +} + +void DecodeHighlightingModel::set_font (QFont const& font) +{ + m_->font_ = font; +} + +int DecodeHighlightingModel::rowCount (const QModelIndex& parent) const +{ + return parent.isValid () ? 0 : m_->data_.size (); +} + +QVariant DecodeHighlightingModel::data (const QModelIndex& index, int role) const +{ + QVariant result; + if (index.isValid () && index.row () < rowCount ()) + { + auto const& item = m_->data_[index.row ()]; + switch (role) + { + case Qt::CheckStateRole: + result = item.enabled_ ? Qt::Checked : Qt::Unchecked; + break; + case Qt::DisplayRole: + return QString {"%1 [f/g:%2, b/g:%3]"} + .arg (highlight_name (item.type_)) + .arg (item.foreground_.style () != Qt::NoBrush ? QString {"#%1"}.arg (item.foreground_.color ().rgb () & 0xffffff, 6, 16, QChar {'0'}) : QString {"unset"}) + .arg (item.background_.style () != Qt::NoBrush ? QString {"#%1"}.arg (item.background_.color ().rgb () & 0xffffff, 6, 16, QChar {'0'}) : QString {"unset"}); + break; + case Qt::ForegroundRole: + if (Qt::NoBrush != item.foreground_.style ()) + { + result = item.foreground_; + } + break; + case Qt::BackgroundRole: + if (Qt::NoBrush != item.background_.style ()) + { + result = item.background_; + } + break; + case Qt::FontRole: + result = m_->font_; + break; + case TypeRole: + result = static_cast<int> (item.type_); + break; + case EnabledDefaultRole: + for (auto const& default_item : impl::defaults_) + { + if (default_item.type_ == item.type_) + { + result = default_item.enabled_ ? Qt::Checked : Qt::Unchecked; + } + } + break; + case ForegroundDefaultRole: + for (auto const& default_item : impl::defaults_) + { + if (default_item.type_ == item.type_) + { + result = default_item.foreground_; + } + } + break; + case BackgroundDefaultRole: + for (auto const& default_item : impl::defaults_) + { + if (default_item.type_ == item.type_) + { + result = default_item.background_; + } + } + break; + } + } + return result; +} + +// Override QAbstractItemModel::itemData() as it is used by the +// default mime encode routine used in drag'n'drop operations and we +// want to transport the type role, this is because the display role +// is derived from the type role. +QMap<int, QVariant> DecodeHighlightingModel::itemData (QModelIndex const& index) const +{ + auto roles = QAbstractListModel::itemData (index); + QVariant variantData = data (index, TypeRole); + if (variantData.isValid ()) + { + roles.insert (TypeRole, variantData); + } + return roles; +} + +QVariant DecodeHighlightingModel::headerData (int /*section*/, Qt::Orientation orientation, int role) const +{ + QVariant header; + if (Qt::DisplayRole == role && Qt::Horizontal == orientation) + { + header = tr ("Highlight Type"); + } + return header; +} + +Qt::ItemFlags DecodeHighlightingModel::flags (QModelIndex const& index) const +{ + auto flags = QAbstractListModel::flags (index) | Qt::ItemIsDragEnabled; + if (index.isValid ()) + { + flags |= Qt::ItemIsUserCheckable; + } + else + { + flags |= Qt::ItemIsDropEnabled; + } + return flags; +} + +bool DecodeHighlightingModel::setData (QModelIndex const& index, QVariant const& value, int role) +{ + bool ok {false}; + if (index.isValid () && index.row () < rowCount ()) + { + auto& item = m_->data_[index.row ()]; + QVector<int> roles; + roles << role; + switch (role) + { + case Qt::DisplayRole: + case Qt::FontRole: + ok = true; + break; + case Qt::CheckStateRole: + if (item.enabled_ != (Qt::Checked == value)) + { + item.enabled_ = Qt::Checked == value; + Q_EMIT dataChanged (index, index, roles); + } + ok = true; + break; + case Qt::ForegroundRole: + if (item.foreground_ != value.value<QBrush> ()) + { + item.foreground_ = value.value<QBrush> (); + Q_EMIT dataChanged (index, index, roles); + } + ok = true; + break; + case Qt::BackgroundRole: + if (item.background_ != value.value<QBrush> ()) + { + item.background_ = value.value<QBrush> (); + Q_EMIT dataChanged (index, index, roles); + } + ok = true; + break; + case TypeRole: + if (item.type_ != static_cast<Highlight> (value.toInt ())) + { + item.type_ = static_cast<Highlight> (value.toInt ()); + roles << Qt::DisplayRole; + Q_EMIT dataChanged (index, index, roles); + } + ok = true; + break; + } + } + return ok; +} + +Qt::DropActions DecodeHighlightingModel::supportedDropActions () const +{ + return Qt::MoveAction; +} + +bool DecodeHighlightingModel::insertRows (int row, int count, QModelIndex const& parent) +{ + beginInsertRows (parent, row, row + count - 1); + for (int index = 0; index < count; ++index) + { + m_->data_.insert (row, HighlightInfo {Highlight::CQ, false, {}, {}}); + } + endInsertRows (); + return true; +} + +bool DecodeHighlightingModel::removeRows (int row, int count, QModelIndex const& parent) +{ + beginRemoveRows (parent, row, row + count - 1); + for (int index = 0; index < count; ++index) + { + m_->data_.removeAt (row); + } + endRemoveRows (); + return true; +} diff --git a/models/DecodeHighlightingModel.hpp b/models/DecodeHighlightingModel.hpp new file mode 100644 index 00000000..afb66276 --- /dev/null +++ b/models/DecodeHighlightingModel.hpp @@ -0,0 +1,79 @@ +#ifndef DECODE_HIGHLIGHTING_MODEL_HPP_ +#define DECODE_HIGHLIGHTING_MODEL_HPP_ + +#include <QAbstractListModel> +#include <QBrush> +#include <QList> + +#include "qt_helpers.hpp" +#include "pimpl_h.hpp" + +class QObject; +class QFont; +class QDataStream; +class QDebug; + +class DecodeHighlightingModel final + : public QAbstractListModel +{ + Q_OBJECT +public: + enum class Highlight : char {CQ, MyCall, Tx, DXCC, DXCCBand, Grid, GridBand, Call, CallBand + , Continent, ContinentBand, CQZone, CQZoneBand, ITUZone, ITUZoneBand + , LotW}; + Q_ENUM (Highlight) + static QString highlight_name (Highlight h); + + struct HighlightInfo final + { + Highlight type_; + bool enabled_; + QBrush foreground_; + QBrush background_; + }; + using HighlightItems = QList<HighlightInfo>; + + explicit DecodeHighlightingModel (QObject * parent = 0); + ~DecodeHighlightingModel(); + + // access to raw items nd default items + static HighlightItems const& default_items (); + HighlightItems const& items () const; + void items (HighlightItems const&); + + void set_font (QFont const&); + + enum DefaultRoles {TypeRole = Qt::UserRole, EnabledDefaultRole, ForegroundDefaultRole, BackgroundDefaultRole}; + +private: + // implement the QAbstractListModel interface + int rowCount (QModelIndex const& parent = QModelIndex()) const override; + QVariant data (QModelIndex const&, int role) const override; + QVariant headerData (int section, Qt::Orientation, int role = Qt::DisplayRole) const override; + Qt::ItemFlags flags (QModelIndex const&) const override; + bool setData (QModelIndex const& index, QVariant const& value, int role) override; + Qt::DropActions supportedDropActions () const override; + bool insertRows (int row, int count, QModelIndex const& parent = QModelIndex {}) override; + bool removeRows (int row, int count, QModelIndex const& parent = QModelIndex {}) override; + QMap<int, QVariant> itemData (QModelIndex const&) const override; + + class impl; + pimpl<impl> m_; +}; + +bool operator == (DecodeHighlightingModel::HighlightInfo const&, DecodeHighlightingModel::HighlightInfo const&); + +QDataStream& operator << (QDataStream&, DecodeHighlightingModel::HighlightInfo const&); +QDataStream& operator >> (QDataStream&, DecodeHighlightingModel::HighlightInfo&); + +#if !defined (QT_NO_DEBUG_STREAM) +QDebug operator << (QDebug, DecodeHighlightingModel::HighlightInfo const&); +#endif + +ENUM_QDATASTREAM_OPS_DECL (DecodeHighlightingModel, Highlight); +ENUM_CONVERSION_OPS_DECL (DecodeHighlightingModel, Highlight); + +Q_DECLARE_METATYPE (DecodeHighlightingModel::HighlightInfo); +Q_DECLARE_METATYPE (DecodeHighlightingModel::HighlightItems); + +#endif diff --git a/models/FontOverrideModel.hpp b/models/FontOverrideModel.hpp new file mode 100644 index 00000000..2bff9aa8 --- /dev/null +++ b/models/FontOverrideModel.hpp @@ -0,0 +1,29 @@ +#ifndef FONT_OVERRIDE_MODEL_HPP_ +#define FONT_OVERRIDE_MODEL_HPP_ + +#include <QIdentityProxyModel> +#include <QFont> + +// fix up font display as header font changes don't currently work +// from views (I think fixed in Qt 5.11.1) +class FontOverrideModel final + : public QIdentityProxyModel +{ +public: + FontOverrideModel (QObject * parent = nullptr) : QIdentityProxyModel {parent} {} + void set_font (QFont const& font) {font_ = font;} + QVariant data (QModelIndex const& index, int role) const override + { + if (Qt::FontRole == role) return font_; + return QIdentityProxyModel::data (index, role); + } + QVariant headerData (int section, Qt::Orientation orientation, int role) const override + { + if (Qt::FontRole == role) return font_; + return QIdentityProxyModel::headerData (section, orientation, role); + } +private: + QFont font_; +}; + +#endif diff --git a/models/FoxLog.cpp b/models/FoxLog.cpp new file mode 100644 index 00000000..1f985f62 --- /dev/null +++ b/models/FoxLog.cpp @@ -0,0 +1,144 @@ +#include "FoxLog.hpp" + +#include <stdexcept> +#include <utility> +#include <QString> +#include <QDateTime> +#include <QSqlDatabase> +#include <QSqlTableModel> +#include <QSqlRecord> +#include <QSqlError> +#include <QSqlQuery> +#include <QDebug> +#include "qt_db_helpers.hpp" +#include "pimpl_impl.hpp" + +class FoxLog::impl final + : public QSqlTableModel +{ +public: + impl (); + + QSqlQuery mutable dupe_query_; +}; + +FoxLog::impl::impl () +{ + if (!database ().tables ().contains ("fox_log")) + { + QSqlQuery query; + SQL_error_check (query, static_cast<bool (QSqlQuery::*) (QString const&)> (&QSqlQuery::exec), + "CREATE TABLE fox_log (" + " id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL," + " \"when\" DATETIME NOT NULL," + " call VARCHAR(20) NOT NULL," + " grid VARCHAR(4)," + " report_sent VARCHAR(3)," + " report_rcvd VARCHAR(3)," + " band VARCHAR(6) NOT NULL," + " CONSTRAINT no_dupes UNIQUE (call, band)" + ")"); + } + + SQL_error_check (dupe_query_, &QSqlQuery::prepare, + "SELECT COUNT(*) FROM fox_log WHERE call = :call AND band = :band"); + + setEditStrategy (QSqlTableModel::OnFieldChange); + setTable ("fox_log"); + setHeaderData (fieldIndex ("when"), Qt::Horizontal, tr ("Date & Time(UTC)")); + setHeaderData (fieldIndex ("call"), Qt::Horizontal, tr ("Call")); + setHeaderData (fieldIndex ("grid"), Qt::Horizontal, tr ("Grid")); + setHeaderData (fieldIndex ("report_sent"), Qt::Horizontal, tr ("Sent")); + setHeaderData (fieldIndex ("report_rcvd"), Qt::Horizontal, tr ("Rcvd")); + setHeaderData (fieldIndex ("band"), Qt::Horizontal, tr ("Band")); + + // This descending order by time is important, it makes the view + // place the latest row at the top, without this the model/view + // interactions are both sluggish and unhelpful. + setSort (fieldIndex ("when"), Qt::DescendingOrder); + + SQL_error_check (*this, &QSqlTableModel::select); +} + +FoxLog::FoxLog () +{ +} + +FoxLog::~FoxLog () +{ +} + +QSqlTableModel * FoxLog::model () +{ + return &*m_; +} + +namespace +{ + void set_value_maybe_null (QSqlRecord& record, QString const& name, QString const& value) + { + if (value.size ()) + { + record.setValue (name, value); + } + else + { + record.setNull (name); + } + } +} + +bool FoxLog::add_QSO (QDateTime const& when, QString const& call, QString const& grid + , QString const& report_sent, QString const& report_received + , QString const& band) +{ + auto record = m_->record (); + if (!when.isNull ()) + { + record.setValue ("when", when.toMSecsSinceEpoch () / 1000); + } + else + { + record.setNull ("when"); + } + set_value_maybe_null (record, "call", call); + set_value_maybe_null (record, "grid", grid); + set_value_maybe_null (record, "report_sent", report_sent); + set_value_maybe_null (record, "report_rcvd", report_received); + set_value_maybe_null (record, "band", band); + if (m_->isDirty ()) + { + m_->revert (); // discard any uncommitted changes + } + m_->setEditStrategy (QSqlTableModel::OnManualSubmit); + ConditionalTransaction transaction {*m_}; + auto ok = m_->insertRecord (-1, record); + if (ok) + { + ok = transaction.submit (false); + } + m_->setEditStrategy (QSqlTableModel::OnFieldChange); + return ok; +} + +bool FoxLog::dupe (QString const& call, QString const& band) const +{ + m_->dupe_query_.bindValue (":call", call); + m_->dupe_query_.bindValue (":band", band); + SQL_error_check (m_->dupe_query_, static_cast<bool (QSqlQuery::*) ()> (&QSqlQuery::exec)); + m_->dupe_query_.next (); + return m_->dupe_query_.value (0).toInt (); +} + +void FoxLog::reset () +{ + if (m_->rowCount ()) + { + m_->setEditStrategy (QSqlTableModel::OnManualSubmit); + ConditionalTransaction transaction {*m_}; + SQL_error_check (*m_, &QSqlTableModel::removeRows, 0, m_->rowCount (), QModelIndex {}); + transaction.submit (); + m_->select (); // to refresh views + m_->setEditStrategy (QSqlTableModel::OnFieldChange); + } +} diff --git a/models/FoxLog.hpp b/models/FoxLog.hpp new file mode 100644 index 00000000..caa8e358 --- /dev/null +++ b/models/FoxLog.hpp @@ -0,0 +1,32 @@ +#ifndef FOX_LOG_HPP_ +#define FOX_LOG_HPP_ + +#include <boost/core/noncopyable.hpp> +#include "pimpl_h.hpp" + +class QDateTime; +class QString; +class QSqlTableModel; + +class FoxLog final + : private boost::noncopyable +{ +public: + explicit FoxLog (); + ~FoxLog (); + + // returns false if insert fails, dupe call+band + bool add_QSO (QDateTime const&, QString const& call, QString const& grid + , QString const& report_sent, QString const& report_received + , QString const& band); + bool dupe (QString const& call, QString const& band) const; + + QSqlTableModel * model (); + void reset (); + +private: + class impl; + pimpl<impl> m_; +}; + +#endif diff --git a/models/FrequencyList.cpp b/models/FrequencyList.cpp new file mode 100644 index 00000000..55844bdf --- /dev/null +++ b/models/FrequencyList.cpp @@ -0,0 +1,819 @@ +#include "FrequencyList.hpp" + +#include <cstdlib> +#include <utility> +#include <limits> +#include <algorithm> + +#include <QMetaType> +#include <QAbstractTableModel> +#include <QString> +#include <QList> +#include <QListIterator> +#include <QVector> +#include <QStringList> +#include <QMimeData> +#include <QDataStream> +#include <QByteArray> +#include <QDebugStateSaver> + +#include "Radio.hpp" +#include "Bands.hpp" +#include "pimpl_impl.hpp" + +#include "moc_FrequencyList.cpp" + +namespace +{ + FrequencyList_v2::FrequencyItems const default_frequency_list = + { + {198000, Modes::FreqCal, IARURegions::R1}, // BBC Radio 4 Droitwich + {4996000, Modes::FreqCal, IARURegions::R1}, // RWM time signal + {9996000, Modes::FreqCal, IARURegions::R1}, // RWM time signal + {14996000, Modes::FreqCal, IARURegions::R1}, // RWM time signal + + {660000, Modes::FreqCal, IARURegions::R2}, + {880000, Modes::FreqCal, IARURegions::R2}, + {1210000, Modes::FreqCal, IARURegions::R2}, + + {2500000, Modes::FreqCal, IARURegions::ALL}, + {3330000, Modes::FreqCal, IARURegions::ALL}, + {5000000, Modes::FreqCal, IARURegions::ALL}, + {7850000, Modes::FreqCal, IARURegions::ALL}, + {10000000, Modes::FreqCal, IARURegions::ALL}, + {14670000, Modes::FreqCal, IARURegions::ALL}, + {15000000, Modes::FreqCal, IARURegions::ALL}, + {20000000, Modes::FreqCal, IARURegions::ALL}, + + {136000, Modes::WSPR, IARURegions::ALL}, + {136130, Modes::JT65, IARURegions::ALL}, + {136130, Modes::JT9, IARURegions::ALL}, + + {474200, Modes::JT65, IARURegions::ALL}, + {474200, Modes::JT9, IARURegions::ALL}, + {474200, Modes::WSPR, IARURegions::ALL}, + + {1836600, Modes::WSPR, IARURegions::ALL}, + {1838000, Modes::JT65, IARURegions::ALL}, // squeezed allocations + {1839000, Modes::JT9, IARURegions::ALL}, + {1840000, Modes::FT8, IARURegions::ALL}, + + {3570000, Modes::JT65, IARURegions::ALL}, // JA compatible + {3572000, Modes::JT9, IARURegions::ALL}, + {3573000, Modes::FT8, IARURegions::ALL}, // above as below JT65 + // is out of DM allocation + {3568600, Modes::WSPR, IARURegions::ALL}, // needs guard marker + // and lock out + + {7038600, Modes::WSPR, IARURegions::ALL}, + {7074000, Modes::FT8, IARURegions::ALL}, + {7076000, Modes::JT65, IARURegions::ALL}, + {7078000, Modes::FT8, IARURegions::ALL}, + {7078000, Modes::JT9, IARURegions::ALL}, + + {10136000, Modes::FT8, IARURegions::ALL}, + {10138000, Modes::JT65, IARURegions::ALL}, + {10138700, Modes::WSPR, IARURegions::ALL}, + {10140000, Modes::JT9, IARURegions::ALL}, + + {14095600, Modes::WSPR, IARURegions::ALL}, + {14074000, Modes::FT8, IARURegions::ALL}, + {14076000, Modes::JT65, IARURegions::ALL}, + {14078000, Modes::FT8, IARURegions::ALL}, + {14078000, Modes::JT9, IARURegions::ALL}, + + {18100000, Modes::FT8, IARURegions::ALL}, + {18102000, Modes::JT65, IARURegions::ALL}, + {18104000, Modes::JT9, IARURegions::ALL}, + {18104600, Modes::WSPR, IARURegions::ALL}, + + {21074000, Modes::FT8, IARURegions::ALL}, + {21076000, Modes::JT65, IARURegions::ALL}, + {21078000, Modes::JT9, IARURegions::ALL}, + {21094600, Modes::WSPR, IARURegions::ALL}, + + {24915000, Modes::FT8, IARURegions::ALL}, + {24917000, Modes::JT65, IARURegions::ALL}, + {24919000, Modes::JT9, IARURegions::ALL}, + {24924600, Modes::WSPR, IARURegions::ALL}, + + {28074000, Modes::FT8, IARURegions::ALL}, + {28076000, Modes::JT65, IARURegions::ALL}, + {28078000, Modes::JT9, IARURegions::ALL}, + {28124600, Modes::WSPR, IARURegions::ALL}, + + {50200000, Modes::Echo, IARURegions::ALL}, + {50276000, Modes::JT65, IARURegions::R2}, + {50276000, Modes::JT65, IARURegions::R3}, + {50380000, Modes::MSK144, IARURegions::R1}, + {50280000, Modes::MSK144, IARURegions::R2}, + {50280000, Modes::MSK144, IARURegions::R3}, + {50293000, Modes::WSPR, IARURegions::R2}, + {50293000, Modes::WSPR, IARURegions::R3}, + {50310000, Modes::JT65, IARURegions::ALL}, + {50312000, Modes::JT9, IARURegions::ALL}, + {50313000, Modes::FT8, IARURegions::ALL}, + {50323000, Modes::FT8, IARURegions::ALL}, + + {70100000, Modes::FT8, IARURegions::R1}, + {70102000, Modes::JT65, IARURegions::R1}, + {70104000, Modes::JT9, IARURegions::R1}, + {70091000, Modes::WSPR, IARURegions::R1}, + {70230000, Modes::MSK144, IARURegions::R1}, + + {144120000, Modes::JT65, IARURegions::ALL}, + {144120000, Modes::Echo, IARURegions::ALL}, + {144174000, Modes::FT8, IARURegions::ALL}, + {144360000, Modes::MSK144, IARURegions::R1}, + {144150000, Modes::MSK144, IARURegions::R2}, + {144489000, Modes::WSPR, IARURegions::ALL}, + {144120000, Modes::QRA64, IARURegions::ALL}, + + {222065000, Modes::Echo, IARURegions::R2}, + {222065000, Modes::JT65, IARURegions::R2}, + {222065000, Modes::QRA64, IARURegions::R2}, + + {432065000, Modes::Echo, IARURegions::ALL}, + {432065000, Modes::JT65, IARURegions::ALL}, + {432300000, Modes::WSPR, IARURegions::ALL}, + {432360000, Modes::MSK144, IARURegions::ALL}, + {432065000, Modes::QRA64, IARURegions::ALL}, + + {902065000, Modes::JT65, IARURegions::R2}, + {902065000, Modes::QRA64, IARURegions::R2}, + + {1296065000, Modes::Echo, IARURegions::ALL}, + {1296065000, Modes::JT65, IARURegions::ALL}, + {1296500000, Modes::WSPR, IARURegions::ALL}, + {1296065000, Modes::QRA64, IARURegions::ALL}, + + {2301000000, Modes::Echo, IARURegions::ALL}, + {2301065000, Modes::JT4, IARURegions::ALL}, + {2301065000, Modes::JT65, IARURegions::ALL}, + {2301065000, Modes::QRA64, IARURegions::ALL}, + + {2304065000, Modes::Echo, IARURegions::ALL}, + {2304065000, Modes::JT4, IARURegions::ALL}, + {2304065000, Modes::JT65, IARURegions::ALL}, + {2304065000, Modes::QRA64, IARURegions::ALL}, + + {2320065000, Modes::Echo, IARURegions::ALL}, + {2320065000, Modes::JT4, IARURegions::ALL}, + {2320065000, Modes::JT65, IARURegions::ALL}, + {2320065000, Modes::QRA64, IARURegions::ALL}, + + {3400065000, Modes::Echo, IARURegions::ALL}, + {3400065000, Modes::JT4, IARURegions::ALL}, + {3400065000, Modes::JT65, IARURegions::ALL}, + {3400065000, Modes::QRA64, IARURegions::ALL}, + + {5760065000, Modes::Echo, IARURegions::ALL}, + {5760065000, Modes::JT4, IARURegions::ALL}, + {5760065000, Modes::JT65, IARURegions::ALL}, + {5760200000, Modes::QRA64, IARURegions::ALL}, + + {10368100000, Modes::Echo, IARURegions::ALL}, + {10368200000, Modes::JT4, IARURegions::ALL}, + {10368200000, Modes::QRA64, IARURegions::ALL}, + + {24048100000, Modes::Echo, IARURegions::ALL}, + {24048200000, Modes::JT4, IARURegions::ALL}, + {24048200000, Modes::QRA64, IARURegions::ALL}, + }; +} + +#if !defined (QT_NO_DEBUG_STREAM) +QDebug operator << (QDebug debug, FrequencyList_v2::Item const& item) +{ + QDebugStateSaver saver {debug}; + debug.nospace () << "FrequencyItem(" + << item.frequency_ << ", " + << item.region_ << ", " + << item.mode_ << ')'; + return debug; +} +#endif + +QDataStream& operator << (QDataStream& os, FrequencyList_v2::Item const& item) +{ + return os << item.frequency_ + << item.mode_ + << item.region_; +} + +QDataStream& operator >> (QDataStream& is, FrequencyList_v2::Item& item) +{ + return is >> item.frequency_ + >> item.mode_ + >> item.region_; +} + +class FrequencyList_v2::impl final + : public QAbstractTableModel +{ +public: + impl (Bands const * bands, QObject * parent) + : QAbstractTableModel {parent} + , bands_ {bands} + , region_filter_ {IARURegions::ALL} + , mode_filter_ {Modes::ALL} + { + } + + FrequencyItems frequency_list (FrequencyItems); + QModelIndex add (Item); + void add (FrequencyItems); + + // Implement the QAbstractTableModel interface + int rowCount (QModelIndex const& parent = QModelIndex {}) const override; + int columnCount (QModelIndex const& parent = QModelIndex {}) const override; + Qt::ItemFlags flags (QModelIndex const& = QModelIndex {}) const override; + QVariant data (QModelIndex const&, int role = Qt::DisplayRole) const override; + bool setData (QModelIndex const&, QVariant const& value, int role = Qt::EditRole) override; + QVariant headerData (int section, Qt::Orientation, int = Qt::DisplayRole) const override; + bool removeRows (int row, int count, QModelIndex const& parent = QModelIndex {}) override; + bool insertRows (int row, int count, QModelIndex const& parent = QModelIndex {}) override; + QStringList mimeTypes () const override; + QMimeData * mimeData (QModelIndexList const&) const override; + + static int constexpr num_cols {SENTINAL}; + static auto constexpr mime_type = "application/wsjt.Frequencies"; + + Bands const * bands_; + FrequencyItems frequency_list_; + Region region_filter_; + Mode mode_filter_; +}; + +FrequencyList_v2::FrequencyList_v2 (Bands const * bands, QObject * parent) + : QSortFilterProxyModel {parent} + , m_ {bands, parent} +{ + setSourceModel (&*m_); + setSortRole (SortRole); +} + +FrequencyList_v2::~FrequencyList_v2 () +{ +} + +auto FrequencyList_v2::frequency_list (FrequencyItems frequency_list) -> FrequencyItems +{ + return m_->frequency_list (frequency_list); +} + +auto FrequencyList_v2::frequency_list () const -> FrequencyItems const& +{ + return m_->frequency_list_; +} + +auto FrequencyList_v2::frequency_list (QModelIndexList const& model_index_list) const -> FrequencyItems +{ + FrequencyItems list; + Q_FOREACH (auto const& index, model_index_list) + { + list << m_->frequency_list_[mapToSource (index).row ()]; + } + return list; +} + +void FrequencyList_v2::frequency_list_merge (FrequencyItems const& items) +{ + m_->add (items); +} + +int FrequencyList_v2::best_working_frequency (Frequency f) const +{ + int result {-1}; + auto const& target_band = m_->bands_->find (f); + if (!target_band.isEmpty ()) + { + Radio::FrequencyDelta delta {std::numeric_limits<Radio::FrequencyDelta>::max ()}; + // find a frequency in the same band that is allowed + for (int row = 0; row < rowCount (); ++row) + { + auto const& source_row = mapToSource (index (row, 0)).row (); + auto const& candidate_frequency = m_->frequency_list_[source_row].frequency_; + auto const& band = m_->bands_->find (candidate_frequency); + if (band == target_band) + { + // take closest band match + Radio::FrequencyDelta new_delta = f - candidate_frequency; + if (std::abs (new_delta) < std::abs (delta)) + { + delta = new_delta; + result = row; + } + } + } + } + return result; +} + +int FrequencyList_v2::best_working_frequency (QString const& target_band) const +{ + int result {-1}; + if (!target_band.isEmpty ()) + { + // find a frequency in the same band that is allowed + for (int row = 0; row < rowCount (); ++row) + { + auto const& source_row = mapToSource (index (row, 0)).row (); + auto const& band = m_->bands_->find (m_->frequency_list_[source_row].frequency_); + if (band == target_band) + { + return row; + } + } + } + return result; +} + +void FrequencyList_v2::reset_to_defaults () +{ + m_->frequency_list (default_frequency_list); +} + +QModelIndex FrequencyList_v2::add (Item f) +{ + return mapFromSource (m_->add (f)); +} + +bool FrequencyList_v2::remove (Item f) +{ + auto row = m_->frequency_list_.indexOf (f); + + if (0 > row) + { + return false; + } + + return m_->removeRow (row); +} + +bool FrequencyList_v2::removeDisjointRows (QModelIndexList rows) +{ + bool result {true}; + + // We must work with source model indexes because we don't want row + // removes to invalidate model indexes we haven't yet processed. We + // achieve that by processing them in decending row order. + for (int r = 0; r < rows.size (); ++r) + { + rows[r] = mapToSource (rows[r]); + } + + // reverse sort by row + std::sort (rows.begin (), rows.end (), [] (QModelIndex const& lhs, QModelIndex const& rhs) + { + return rhs.row () < lhs.row (); // reverse row ordering + }); + Q_FOREACH (auto index, rows) + { + if (result && !m_->removeRow (index.row ())) + { + result = false; + } + } + return result; +} + +void FrequencyList_v2::filter (Region region, Mode mode) +{ + m_->region_filter_ = region; + m_->mode_filter_ = mode; + invalidateFilter (); +} + +bool FrequencyList_v2::filterAcceptsRow (int source_row, QModelIndex const& /* parent */) const +{ + bool result {true}; + auto const& item = m_->frequency_list_[source_row]; + if (m_->region_filter_ != IARURegions::ALL) + { + result = IARURegions::ALL == item.region_ || m_->region_filter_ == item.region_; + } + if (result && m_->mode_filter_ != Modes::ALL) + { + // we pass ALL mode rows unless filtering for FreqCal mode + result = (Modes::ALL == item.mode_ && m_->mode_filter_ != Modes::FreqCal) + || m_->mode_filter_ == item.mode_; + } + return result; +} + + +auto FrequencyList_v2::impl::frequency_list (FrequencyItems frequency_list) -> FrequencyItems +{ + beginResetModel (); + std::swap (frequency_list_, frequency_list); + endResetModel (); + return frequency_list; +} + +QModelIndex FrequencyList_v2::impl::add (Item f) +{ + // Any Frequency that isn't in the list may be added + if (!frequency_list_.contains (f)) + { + auto row = frequency_list_.size (); + + beginInsertRows (QModelIndex {}, row, row); + frequency_list_.append (f); + endInsertRows (); + + return index (row, 0); + } + return QModelIndex {}; +} + +void FrequencyList_v2::impl::add (FrequencyItems items) +{ + // Any Frequency that isn't in the list may be added + for (auto p = items.begin (); p != items.end ();) + { + if (frequency_list_.contains (*p)) + { + p = items.erase (p); + } + else + { + ++p; + } + } + + if (items.size ()) + { + auto row = frequency_list_.size (); + + beginInsertRows (QModelIndex {}, row, row + items.size () - 1); + frequency_list_.append (items); + endInsertRows (); + } +} + +int FrequencyList_v2::impl::rowCount (QModelIndex const& parent) const +{ + return parent.isValid () ? 0 : frequency_list_.size (); +} + +int FrequencyList_v2::impl::columnCount (QModelIndex const& parent) const +{ + return parent.isValid () ? 0 : num_cols; +} + +Qt::ItemFlags FrequencyList_v2::impl::flags (QModelIndex const& index) const +{ + auto result = QAbstractTableModel::flags (index) | Qt::ItemIsDropEnabled; + auto row = index.row (); + auto column = index.column (); + if (index.isValid () + && row < frequency_list_.size () + && column < num_cols) + { + if (frequency_mhz_column != column) + { + result |= Qt::ItemIsEditable | Qt::ItemIsDragEnabled; + } + } + return result; +} + +QVariant FrequencyList_v2::impl::data (QModelIndex const& index, int role) const +{ + QVariant item; + + auto const& row = index.row (); + auto const& column = index.column (); + + if (index.isValid () + && row < frequency_list_.size () + && column < num_cols) + { + auto const& frequency_item = frequency_list_.at (row); + switch (column) + { + case region_column: + switch (role) + { + case SortRole: + case Qt::DisplayRole: + case Qt::EditRole: + case Qt::AccessibleTextRole: + item = IARURegions::name (frequency_item.region_); + break; + + case Qt::ToolTipRole: + case Qt::AccessibleDescriptionRole: + item = tr ("IARU Region"); + break; + + case Qt::TextAlignmentRole: + item = Qt::AlignHCenter + Qt::AlignVCenter; + break; + } + break; + + case mode_column: + switch (role) + { + case SortRole: + case Qt::DisplayRole: + case Qt::EditRole: + case Qt::AccessibleTextRole: + item = Modes::name (frequency_item.mode_); + break; + + case Qt::ToolTipRole: + case Qt::AccessibleDescriptionRole: + item = tr ("Mode"); + break; + + case Qt::TextAlignmentRole: + item = Qt::AlignHCenter + Qt::AlignVCenter; + break; + } + break; + + case frequency_column: + switch (role) + { + case SortRole: + case Qt::EditRole: + case Qt::AccessibleTextRole: + item = frequency_item.frequency_; + break; + + case Qt::DisplayRole: + { + auto const& band = bands_->find (frequency_item.frequency_); + item = Radio::pretty_frequency_MHz_string (frequency_item.frequency_) + + " MHz (" + (band.isEmpty () ? "OOB" : band) + ')'; + } + break; + + case Qt::ToolTipRole: + case Qt::AccessibleDescriptionRole: + item = tr ("Frequency"); + break; + + case Qt::TextAlignmentRole: + item = Qt::AlignRight + Qt::AlignVCenter; + break; + } + break; + + case frequency_mhz_column: + switch (role) + { + case Qt::EditRole: + case Qt::AccessibleTextRole: + item = Radio::frequency_MHz_string (frequency_item.frequency_); + break; + + case Qt::DisplayRole: + { + auto const& band = bands_->find (frequency_item.frequency_); + item = Radio::pretty_frequency_MHz_string (frequency_item.frequency_) + + " MHz (" + (band.isEmpty () ? "OOB" : band) + ')'; + } + break; + + case Qt::ToolTipRole: + case Qt::AccessibleDescriptionRole: + item = tr ("Frequency (MHz)"); + break; + + case Qt::TextAlignmentRole: + item = Qt::AlignRight + Qt::AlignVCenter; + break; + } + break; + } + } + return item; +} + +bool FrequencyList_v2::impl::setData (QModelIndex const& model_index, QVariant const& value, int role) +{ + bool changed {false}; + + auto const& row = model_index.row (); + if (model_index.isValid () + && Qt::EditRole == role + && row < frequency_list_.size ()) + { + QVector<int> roles; + roles << role; + + auto& item = frequency_list_[row]; + switch (model_index.column ()) + { + case region_column: + { + auto region = IARURegions::value (value.toString ()); + if (region != item.region_) + { + item.region_ = region; + Q_EMIT dataChanged (model_index, model_index, roles); + changed = true; + } + } + break; + + case mode_column: + { + auto mode = Modes::value (value.toString ()); + if (mode != item.mode_) + { + item.mode_ = mode; + Q_EMIT dataChanged (model_index, model_index, roles); + changed = true; + } + } + break; + + case frequency_column: + if (value.canConvert<Frequency> ()) + { + Radio::Frequency frequency {qvariant_cast <Radio::Frequency> (value)}; + if (frequency != item.frequency_) + { + item.frequency_ = frequency; + // mark derived column (1) changed as well + Q_EMIT dataChanged (index (model_index.row (), 1), model_index, roles); + changed = true; + } + } + break; + } + } + + return changed; +} + +QVariant FrequencyList_v2::impl::headerData (int section, Qt::Orientation orientation, int role) const +{ + QVariant header; + if (Qt::DisplayRole == role + && Qt::Horizontal == orientation + && section < num_cols) + { + switch (section) + { + case region_column: header = tr ("IARU Region"); break; + case mode_column: header = tr ("Mode"); break; + case frequency_column: header = tr ("Frequency"); break; + case frequency_mhz_column: header = tr ("Frequency (MHz)"); break; + } + } + else + { + header = QAbstractTableModel::headerData (section, orientation, role); + } + return header; +} + +bool FrequencyList_v2::impl::removeRows (int row, int count, QModelIndex const& parent) +{ + if (0 < count && (row + count) <= rowCount (parent)) + { + beginRemoveRows (parent, row, row + count - 1); + for (auto r = 0; r < count; ++r) + { + frequency_list_.removeAt (row); + } + endRemoveRows (); + return true; + } + return false; +} + +bool FrequencyList_v2::impl::insertRows (int row, int count, QModelIndex const& parent) +{ + if (0 < count) + { + beginInsertRows (parent, row, row + count - 1); + for (auto r = 0; r < count; ++r) + { + frequency_list_.insert (row, Item {0, Mode::ALL, IARURegions::ALL}); + } + endInsertRows (); + return true; + } + return false; +} + +QStringList FrequencyList_v2::impl::mimeTypes () const +{ + QStringList types; + types << mime_type; + return types; +} + +QMimeData * FrequencyList_v2::impl::mimeData (QModelIndexList const& items) const +{ + QMimeData * mime_data = new QMimeData {}; + QByteArray encoded_data; + QDataStream stream {&encoded_data, QIODevice::WriteOnly}; + + Q_FOREACH (auto const& item, items) + { + if (item.isValid () && frequency_column == item.column ()) + { + stream << frequency_list_.at (item.row ()); + } + } + + mime_data->setData (mime_type, encoded_data); + return mime_data; +} + +auto FrequencyList_v2::const_iterator::operator * () const -> Item const& +{ + return parent_->frequency_list ().at(parent_->mapToSource (parent_->index (row_, 0)).row ()); +} + +auto FrequencyList_v2::const_iterator::operator -> () const -> Item const * +{ + return &parent_->frequency_list ().at(parent_->mapToSource (parent_->index (row_, 0)).row ()); +} + +bool FrequencyList_v2::const_iterator::operator != (const_iterator const& rhs) const +{ + return parent_ != rhs.parent_ || row_ != rhs.row_; +} + +bool FrequencyList_v2::const_iterator::operator == (const_iterator const& rhs) const +{ + return parent_ == rhs.parent_ && row_ == rhs.row_; +} + +auto FrequencyList_v2::const_iterator::operator ++ () -> const_iterator& +{ + ++row_; + return *this; +} + +auto FrequencyList_v2::begin () const -> const_iterator +{ + return const_iterator (this, 0); +} + +auto FrequencyList_v2::end () const -> const_iterator +{ + return const_iterator (this, rowCount ()); +} + +auto FrequencyList_v2::find (Frequency f) const -> const_iterator +{ + int row {0}; + for (; row < rowCount (); ++row) + { + if (m_->frequency_list_[mapToSource (index (row, 0)).row ()].frequency_ == f) + { + break; + } + } + return const_iterator (this, row); +} + +auto FrequencyList_v2::filtered_bands () const -> BandSet +{ + BandSet result; + for (auto const& item : *this) + { + result << m_->bands_->find (item.frequency_); + } + return result; +} + +auto FrequencyList_v2::all_bands (Region region, Mode mode) const -> BandSet +{ + BandSet result; + for (auto const& item : m_->frequency_list_) + { + if (region == IARURegions::ALL || item.region_ == region + || mode == Modes::ALL || item.mode_ == mode) + { + result << m_->bands_->find (item.frequency_); + } + } + return result; +} + +// +// Obsolete version of FrequencyList no longer used but needed to +// allow loading and saving of old settings contents without damage +// +QDataStream& operator << (QDataStream& os, FrequencyList::Item const& item) +{ + return os << item.frequency_ + << item.mode_; +} + +QDataStream& operator >> (QDataStream& is, FrequencyList::Item& item) +{ + return is >> item.frequency_ + >> item.mode_; +} diff --git a/models/FrequencyList.hpp b/models/FrequencyList.hpp new file mode 100644 index 00000000..aa93352a --- /dev/null +++ b/models/FrequencyList.hpp @@ -0,0 +1,186 @@ +#ifndef FREQUENCY_LIST_HPP__ +#define FREQUENCY_LIST_HPP__ + +#include "pimpl_h.hpp" + +#include <QList> +#include <QSortFilterProxyModel> + +#include "Radio.hpp" +#include "IARURegions.hpp" +#include "Modes.hpp" + +class Bands; + +// +// Class FrequencyList_v2 +// +// Encapsulates a collection of frequencies with associated modes. +// The implementation is a table containing the list of IARU region, +// Frequency and mode tuples which are editable. A third column is +// modeled in the model which is an immutable double representation +// of the corresponding Frequency item scaled to mega-Hertz. +// +// The list is ordered. A filter on IARU region and mode is +// available and is set by the filter(Region, Mode) method. The +// Region value IARURegions::ALL and the Mode value Modes::ALL may be +// optionally given which passes all rows in the filtered column. +// +// Responsibilities +// +// Stores internally a list of unique region, frequency mode tuples. +// Provides methods to add and delete list elements. Provides range +// iterators for a filtered view of the underlying table. +// +// Collaborations +// +// Implements the QSortFilterProxyModel interface for a list of spot +// frequencies. +// +class FrequencyList_v2 final + : public QSortFilterProxyModel +{ + Q_OBJECT; + +public: + using Region = IARURegions::Region; + using Frequency = Radio::Frequency; + using Mode = Modes::Mode; + + struct Item + { + Frequency frequency_; + Mode mode_; + Region region_; + }; + using FrequencyItems = QList<Item>; + using BandSet = QSet<QString>; + + enum Column {region_column, mode_column, frequency_column, frequency_mhz_column, SENTINAL}; + + // an iterator that meets the requirements of the C++ for range statement + class const_iterator + { + public: + const_iterator (FrequencyList_v2 const * parent, int row) + : parent_ {parent} + , row_ {row} + { + } + + Item const& operator * () const; + Item const * operator -> () const; + bool operator != (const_iterator const&) const; + bool operator == (const_iterator const&) const; + const_iterator& operator ++ (); + + private: + FrequencyList_v2 const * parent_; + int row_; + }; + + explicit FrequencyList_v2 (Bands const *, QObject * parent = nullptr); + ~FrequencyList_v2 (); + + // Load and store underlying items + FrequencyItems frequency_list (FrequencyItems); + FrequencyItems const& frequency_list () const; + FrequencyItems frequency_list (QModelIndexList const&) const; + void frequency_list_merge (FrequencyItems const&); + + // Iterators for the sorted and filtered items + // + // Note that these iterators are on the final sorted and filtered + // rows, if you need to access the underlying unfiltered and + // unsorted frequencies then use the frequency_list() member to + // access the underlying list of rows. + const_iterator begin () const; + const_iterator end () const; + + // Find a row with a given frequency + const_iterator find (Frequency) const; + + // Bands of the frequencies + BandSet all_bands (Region = IARURegions::ALL, Mode = Modes::ALL) const; + BandSet filtered_bands () const; + + // Find the row of the nearest best working frequency given a + // frequency. Returns -1 if no suitable working frequency is found + // in the list. + int best_working_frequency (Frequency) const; + + // Find the row of the nearest best working frequency given a band + // name. Returns -1 if no suitable working frequency is found in the + // list. + int best_working_frequency (QString const& band) const; + + // Set filter + Q_SLOT void filter (Region, Mode); + + // Reset + Q_SLOT void reset_to_defaults (); + + // Model API + QModelIndex add (Item); + bool remove (Item); + bool removeDisjointRows (QModelIndexList); + + // Proxy API + bool filterAcceptsRow (int source_row, QModelIndex const& parent) const override; + + // Custom roles. + static int constexpr SortRole = Qt::UserRole; + +private: + class impl; + pimpl<impl> m_; +}; + +inline +bool operator == (FrequencyList_v2::Item const& lhs, FrequencyList_v2::Item const& rhs) +{ + return + lhs.frequency_ == rhs.frequency_ + && lhs.region_ == rhs.region_ + && lhs.mode_ == rhs.mode_; +} + +QDataStream& operator << (QDataStream&, FrequencyList_v2::Item const&); +QDataStream& operator >> (QDataStream&, FrequencyList_v2::Item&); + +#if !defined (QT_NO_DEBUG_STREAM) +QDebug operator << (QDebug, FrequencyList_v2::Item const&); +#endif + +Q_DECLARE_METATYPE (FrequencyList_v2::Item); +Q_DECLARE_METATYPE (FrequencyList_v2::FrequencyItems); + + +// +// Obsolete version of FrequencyList no longer used but needed to +// allow loading and saving of old settings contents without damage +// +class FrequencyList final +{ +public: + using Frequency = Radio::Frequency; + using Mode = Modes::Mode; + + struct Item + { + Frequency frequency_; + Mode mode_; + }; + using FrequencyItems = QList<Item>; + +private: + FrequencyItems frequency_list_; +}; + +QDataStream& operator << (QDataStream&, FrequencyList::Item const&); +QDataStream& operator >> (QDataStream&, FrequencyList::Item&); + +Q_DECLARE_METATYPE (FrequencyList::Item); +Q_DECLARE_METATYPE (FrequencyList::FrequencyItems); + +#endif diff --git a/models/IARURegions.cpp b/models/IARURegions.cpp new file mode 100644 index 00000000..25450ab4 --- /dev/null +++ b/models/IARURegions.cpp @@ -0,0 +1,96 @@ +#include "models/IARURegions.hpp" + +#include <algorithm> + +#include <QString> +#include <QVariant> +#include <QModelIndex> +#include <QMetaType> + +#include "moc_IARURegions.cpp" + +namespace +{ + // human readable strings for each Region enumeration value + char const * const region_names[] = + { + "All", + "Region 1", + "Region 2", + "Region 3", + }; + std::size_t constexpr region_names_size = sizeof (region_names) / sizeof (region_names[0]); +} + +IARURegions::IARURegions (QObject * parent) + : QAbstractListModel {parent} +{ + static_assert (region_names_size == SENTINAL, + "region_names array must match Region enumeration"); +} + +char const * IARURegions::name (Region r) +{ + return region_names[static_cast<int> (r)]; +} + +auto IARURegions::value (QString const& s) -> Region +{ + auto end = region_names + region_names_size; + auto p = std::find_if (region_names, end + , [&s] (char const * const name) { + return name == s; + }); + return p != end ? static_cast<Region> (p - region_names) : ALL; +} + +QVariant IARURegions::data (QModelIndex const& index, int role) const +{ + QVariant item; + + if (index.isValid ()) + { + auto const& row = index.row (); + switch (role) + { + case Qt::ToolTipRole: + case Qt::AccessibleDescriptionRole: + item = tr ("IARU Region"); + break; + + case Qt::EditRole: + item = static_cast<Region> (row); + break; + + case Qt::DisplayRole: + case Qt::AccessibleTextRole: + item = region_names[row]; + break; + + case Qt::TextAlignmentRole: + item = Qt::AlignHCenter + Qt::AlignVCenter; + break; + } + } + + return item; +} + +QVariant IARURegions::headerData (int section, Qt::Orientation orientation, int role) const +{ + QVariant result; + + if (Qt::DisplayRole == role && Qt::Horizontal == orientation) + { + result = tr ("IARU Region"); + } + else + { + result = QAbstractListModel::headerData (section, orientation, role); + } + + return result; +} + +ENUM_QDATASTREAM_OPS_IMPL (IARURegions, Region); +ENUM_CONVERSION_OPS_IMPL (IARURegions, Region); diff --git a/models/IARURegions.hpp b/models/IARURegions.hpp new file mode 100644 index 00000000..d849d97b --- /dev/null +++ b/models/IARURegions.hpp @@ -0,0 +1,66 @@ +#ifndef IARU_REGIONS_HPP__ +#define IARU_REGIONS_HPP__ + +#include <QAbstractListModel> + +#include "qt_helpers.hpp" + +class QString; +class QVariant; +class QModelIndex; + +// +// Class IARURegions - Qt model that implements the list of IARU regions +// +// +// Responsibilities +// +// Provides a single column list model that contains the human +// readable string version of the data region in the display +// role. Also provided is a translatable column header string and +// tool tip string. +// +// +// Collaborations +// +// Implements a concrete sub-class of the QAbstractListModel class. +// +class IARURegions final + : public QAbstractListModel +{ + Q_OBJECT +public: + // + // This enumeration contains the supported regions, to complement + // this an array of human readable strings in the implementation + // (IARURegions.cpp) must be maintained in parallel. + // + enum Region + { + ALL, // matches with all regions + R1, + R2, + R3, + SENTINAL // this must be last + }; + Q_ENUM (Region) + + explicit IARURegions (QObject * parent = nullptr); + + // translate between enumeration and human readable strings + static char const * name (Region); + static Region value (QString const&); + + // Implement the QAbstractListModel interface + int rowCount (QModelIndex const& parent = QModelIndex {}) const override + { + return parent.isValid () ? 0 : SENTINAL; // Number of regionss in Region enumeration class + } + QVariant data (QModelIndex const&, int role = Qt::DisplayRole) const override; + QVariant headerData (int section, Qt::Orientation, int = Qt::DisplayRole) const override; +}; + +ENUM_QDATASTREAM_OPS_DECL (IARURegions, Region); +ENUM_CONVERSION_OPS_DECL (IARURegions, Region); + +#endif diff --git a/models/Modes.cpp b/models/Modes.cpp new file mode 100644 index 00000000..9d21459c --- /dev/null +++ b/models/Modes.cpp @@ -0,0 +1,102 @@ +#include "Modes.hpp" + +#include <algorithm> + +#include <QString> +#include <QVariant> +#include <QModelIndex> + +#include "moc_Modes.cpp" + +namespace +{ + // human readable strings for each Mode enumeration value + char const * const mode_names[] = + { + "All", + "JT65", + "JT9", + "JT4", + "WSPR", + "Echo", + "ISCAT", + "MSK144", + "QRA64", + "FreqCal", + "FT8" + }; + std::size_t constexpr mode_names_size = sizeof (mode_names) / sizeof (mode_names[0]); +} + +Modes::Modes (QObject * parent) + : QAbstractListModel {parent} +{ + static_assert (mode_names_size == MODES_END_SENTINAL_AND_COUNT, + "mode_names array must match Mode enumeration"); +} + +char const * Modes::name (Mode m) +{ + return mode_names[static_cast<int> (m)]; +} + +auto Modes::value (QString const& s) -> Mode +{ + auto end = mode_names + mode_names_size; + auto p = std::find_if (mode_names, end + , [&s] (char const * const name) { + return name == s; + }); + return p != end ? static_cast<Mode> (p - mode_names) : ALL; +} + +QVariant Modes::data (QModelIndex const& index, int role) const +{ + QVariant item; + + if (index.isValid ()) + { + auto const& row = index.row (); + switch (role) + { + case Qt::ToolTipRole: + case Qt::AccessibleDescriptionRole: + item = tr ("Mode"); + break; + + case Qt::EditRole: + item = static_cast<Mode> (row); + break; + + case Qt::DisplayRole: + case Qt::AccessibleTextRole: + item = mode_names[row]; + break; + + case Qt::TextAlignmentRole: + item = Qt::AlignHCenter + Qt::AlignVCenter; + break; + } + } + + return item; +} + +QVariant Modes::headerData (int section, Qt::Orientation orientation, int role) const +{ + QVariant result; + + if (Qt::DisplayRole == role && Qt::Horizontal == orientation) + { + result = tr ("Mode"); + } + else + { + result = QAbstractListModel::headerData (section, orientation, role); + } + + return result; +} + +ENUM_QDATASTREAM_OPS_IMPL (Modes, Mode); +ENUM_CONVERSION_OPS_IMPL (Modes, Mode); diff --git a/models/Modes.hpp b/models/Modes.hpp new file mode 100644 index 00000000..cdf29974 --- /dev/null +++ b/models/Modes.hpp @@ -0,0 +1,74 @@ +#ifndef MODES_HPP__ +#define MODES_HPP__ + +#include <QAbstractListModel> + +#include "qt_helpers.hpp" + +class QString; +class QVariant; +class QModelIndex; + +// +// Class Modes - Qt model that implements a list of data modes +// +// +// Responsibilities +// +// Provides a single column list model that contains the human +// readable string version of the data mode in the display role. Also +// provided is a translatable column header string and tool tip +// string. +// +// +// Collaborations +// +// Implements a concrete sub-class of the QAbstractListModel class. +// +class Modes final + : public QAbstractListModel +{ + Q_OBJECT + +public: + // + // This enumeration contains the supported modes, to complement this + // an array of human readable strings in the implementation + // (Modes.cpp) must be maintained in parallel. + // + enum Mode + { + ALL, // matches with all modes + JT65, + JT9, + JT4, + WSPR, + Echo, + ISCAT, + MSK144, + QRA64, + FreqCal, + FT8, + MODES_END_SENTINAL_AND_COUNT // this must be last + }; + Q_ENUM (Mode) + + explicit Modes (QObject * parent = nullptr); + + // translate between enumeration and human readable strings + static char const * name (Mode); + static Mode value (QString const&); + + // Implement the QAbstractListModel interface + int rowCount (QModelIndex const& parent = QModelIndex {}) const override + { + return parent.isValid () ? 0 : MODES_END_SENTINAL_AND_COUNT; // Number of modes in Mode enumeration class + } + QVariant data (QModelIndex const&, int role = Qt::DisplayRole) const override; + QVariant headerData (int section, Qt::Orientation, int = Qt::DisplayRole) const override; +}; + +ENUM_QDATASTREAM_OPS_DECL (Modes, Mode); +ENUM_CONVERSION_OPS_DECL (Modes, Mode); + +#endif diff --git a/models/StationList.cpp b/models/StationList.cpp new file mode 100644 index 00000000..eeeb4d60 --- /dev/null +++ b/models/StationList.cpp @@ -0,0 +1,531 @@ +#include "StationList.hpp" + +#include <utility> +#include <algorithm> +#include <cmath> + +#include <QMetaType> +#include <QAbstractTableModel> +#include <QObject> +#include <QString> +#include <QVector> +#include <QStringList> +#include <QMimeData> +#include <QDataStream> +#include <QByteArray> +#include <QDebug> +#include <QDebugStateSaver> + +#include "pimpl_impl.hpp" + +#include "Radio.hpp" +#include "Bands.hpp" +#include "FrequencyList.hpp" + +#if !defined (QT_NO_DEBUG_STREAM) +QDebug operator << (QDebug debug, StationList::Station const& station) +{ + QDebugStateSaver saver {debug}; + debug.nospace () << "Station(" + << station.band_name_ << ", " + << station.offset_ << ", " + << station.antenna_description_ << ')'; + return debug; +} +#endif + +QDataStream& operator << (QDataStream& os, StationList::Station const& station) +{ + return os << station.band_name_ + << station.offset_ + << station.antenna_description_; +} + +QDataStream& operator >> (QDataStream& is, StationList::Station& station) +{ + return is >> station.band_name_ + >> station.offset_ + >> station.antenna_description_; +} + + +class StationList::impl final + : public QAbstractTableModel +{ +public: + impl (Bands const * bands, Stations stations, QObject * parent) + : QAbstractTableModel {parent} + , bands_ {bands} + , stations_ {stations} + { + } + + Stations station_list (Stations); + QModelIndex add (Station); + FrequencyDelta offset (Frequency) const; + + // Implement the QAbstractTableModel interface. + int rowCount (QModelIndex const& parent = QModelIndex {}) const override; + int columnCount (QModelIndex const& parent = QModelIndex {}) const override; + Qt::ItemFlags flags (QModelIndex const& = QModelIndex {}) const override; + QVariant data (QModelIndex const&, int role) const override; + QVariant headerData (int section, Qt::Orientation, int = Qt::DisplayRole) const override; + bool setData (QModelIndex const&, QVariant const& value, int role = Qt::EditRole) override; + bool removeRows (int row, int count, QModelIndex const& parent = QModelIndex {}) override; + bool insertRows (int row, int count, QModelIndex const& parent = QModelIndex {}) override; + Qt::DropActions supportedDropActions () const override; + QStringList mimeTypes () const override; + QMimeData * mimeData (QModelIndexList const&) const override; + bool dropMimeData (QMimeData const *, Qt::DropAction, int row, int column, QModelIndex const& parent) override; + + // Helper method for band validation. + QModelIndex first_matching_band (QString const& band_name) const + { + // find first exact match in bands + auto matches = bands_->match (bands_->index (0, 0) + , Qt::DisplayRole + , band_name + , 1 + , Qt::MatchExactly); + return matches.isEmpty () ? QModelIndex {} : matches.first (); + } + + static int constexpr num_columns {3}; + static auto constexpr mime_type = "application/wsjt.antenna-descriptions"; + + Bands const * bands_; + Stations stations_; +}; + +StationList::StationList (Bands const * bands, QObject * parent) + : StationList {bands, {}, parent} +{ +} + +StationList::StationList (Bands const * bands, Stations stations, QObject * parent) + : QSortFilterProxyModel {parent} + , m_ {bands, stations, parent} +{ + setSourceModel (&*m_); + setSortRole (SortRole); +} + +StationList::~StationList () +{ +} + +auto StationList::station_list (Stations stations) -> Stations +{ + return m_->station_list (stations); +} + +auto StationList::station_list () const -> Stations const& +{ + return m_->stations_; +} + +QModelIndex StationList::add (Station s) +{ + return mapFromSource (m_->add (s)); +} + +bool StationList::remove (Station s) +{ + auto row = m_->stations_.indexOf (s); + if (0 > row) + { + return false; + } + return removeRow (row); +} + +bool StationList::removeDisjointRows (QModelIndexList rows) +{ + bool result {true}; + + // We must work with source model indexes because we don't want row + // removes to invalidate model indexes we haven't yet processed. We + // achieve that by processing them in decending row order. + for (int r = 0; r < rows.size (); ++r) + { + rows[r] = mapToSource (rows[r]); + } + + // reverse sort by row + std::sort (rows.begin (), rows.end (), [] (QModelIndex const& lhs, QModelIndex const& rhs) + { + return rhs.row () < lhs.row (); // reverse row ordering + }); + Q_FOREACH (auto index, rows) + { + if (result && !m_->removeRow (index.row ())) + { + result = false; + } + } + + return result; +} + +auto StationList::offset (Frequency f) const -> FrequencyDelta +{ + return m_->offset (f); +} + + +auto StationList::impl::station_list (Stations stations) -> Stations +{ + beginResetModel (); + std::swap (stations_, stations); + endResetModel (); + return stations; +} + +QModelIndex StationList::impl::add (Station s) +{ + // Any band that isn't in the list may be added + if (!stations_.contains (s)) + { + auto row = stations_.size (); + + beginInsertRows (QModelIndex {}, row, row); + stations_.append (s); + endInsertRows (); + + return index (row, 0); + } + + return QModelIndex {}; +} + +auto StationList::impl::offset (Frequency f) const -> FrequencyDelta +{ + // Lookup band for frequency + auto const& band = bands_->find (f); + if (!band.isEmpty ()) + { + // Lookup station for band + for (int i = 0; i < stations_.size (); ++i) + { + if (stations_[i].band_name_ == band) + { + return stations_[i].offset_; + } + } + } + return 0; // no offset +} + +int StationList::impl::rowCount (QModelIndex const& parent) const +{ + return parent.isValid () ? 0 : stations_.size (); +} + +int StationList::impl::columnCount (QModelIndex const& parent) const +{ + return parent.isValid () ? 0 : num_columns; +} + +Qt::ItemFlags StationList::impl::flags (QModelIndex const& index) const +{ + auto result = QAbstractTableModel::flags (index); + + auto row = index.row (); + auto column = index.column (); + + if (index.isValid () + && row < stations_.size () + && column < num_columns) + { + if (description_column == column) + { + result |= Qt::ItemIsEditable | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled; + } + else + { + result |= Qt::ItemIsEditable | Qt::ItemIsDropEnabled; + } + } + else + { + result |= Qt::ItemIsDropEnabled; + } + + return result; +} + +QVariant StationList::impl::data (QModelIndex const& index, int role) const +{ + QVariant item; + + auto row = index.row (); + auto column = index.column (); + + if (index.isValid () + && row < stations_.size ()) + { + switch (column) + { + case band_column: + switch (role) + { + case SortRole: + { + // Lookup band. + auto band_index = first_matching_band (stations_.at (row).band_name_); + // Use the sort role value of the band. + item = band_index.data (Bands::SortRole); + } + break; + + case Qt::DisplayRole: + case Qt::EditRole: + case Qt::AccessibleTextRole: + item = stations_.at (row).band_name_; + break; + + case Qt::ToolTipRole: + case Qt::AccessibleDescriptionRole: + item = tr ("Band name"); + break; + + case Qt::TextAlignmentRole: + item = Qt::AlignHCenter + Qt::AlignVCenter; + break; + } + break; + + case offset_column: + { + auto frequency_offset = stations_.at (row).offset_; + switch (role) + { + case SortRole: + case Qt::EditRole: + case Qt::AccessibleTextRole: + item = frequency_offset; + break; + + case Qt::DisplayRole: + item = Radio::pretty_frequency_MHz_string (frequency_offset) + " MHz"; + break; + + case Qt::ToolTipRole: + case Qt::AccessibleDescriptionRole: + item = tr ("Frequency offset"); + break; + + case Qt::TextAlignmentRole: + item = Qt::AlignRight + Qt::AlignVCenter; + break; + } + } + break; + + case description_column: + switch (role) + { + case SortRole: + case Qt::EditRole: + case Qt::DisplayRole: + case Qt::AccessibleTextRole: + item = stations_.at (row).antenna_description_; + break; + + case Qt::ToolTipRole: + case Qt::AccessibleDescriptionRole: + item = tr ("Antenna description"); + break; + + case Qt::TextAlignmentRole: + item = Qt::AlignLeft + Qt::AlignVCenter; + break; + } + break; + } + } + + return item; +} + +QVariant StationList::impl::headerData (int section, Qt::Orientation orientation, int role) const +{ + QVariant header; + + if (Qt::DisplayRole == role && Qt::Horizontal == orientation) + { + switch (section) + { + case band_column: header = tr ("Band"); break; + case offset_column: header = tr ("Offset"); break; + case description_column: header = tr ("Antenna Description"); break; + } + } + else + { + header = QAbstractTableModel::headerData (section, orientation, role); + } + + return header; +} + +bool StationList::impl::setData (QModelIndex const& model_index, QVariant const& value, int role) +{ + bool changed {false}; + + auto row = model_index.row (); + auto size = stations_.size (); + if (model_index.isValid () + && Qt::EditRole == role + && row < size) + { + QVector<int> roles; + roles << role; + + switch (model_index.column ()) + { + case band_column: + { + // Check if band name is valid. + auto band_index = first_matching_band (value.toString ()); + if (band_index.isValid ()) + { + stations_[row].band_name_ = band_index.data ().toString (); + Q_EMIT dataChanged (model_index, model_index, roles); + changed = true; + } + } + break; + + case offset_column: + { + if (value.canConvert<FrequencyDelta> ()) + { + FrequencyDelta offset {qvariant_cast<Radio::FrequencyDelta> (value)}; + if (offset != stations_[row].offset_) + { + stations_[row].offset_ = offset; + Q_EMIT dataChanged (model_index, model_index, roles); + changed = true; + } + } + } + break; + + case description_column: + stations_[row].antenna_description_ = value.toString (); + Q_EMIT dataChanged (model_index, model_index, roles); + changed = true; + break; + } + } + + return changed; +} + +bool StationList::impl::removeRows (int row, int count, QModelIndex const& parent) +{ + if (0 < count && (row + count) <= rowCount (parent)) + { + beginRemoveRows (parent, row, row + count - 1); + for (auto r = 0; r < count; ++r) + { + stations_.removeAt (row); + } + endRemoveRows (); + return true; + } + + return false; +} + +bool StationList::impl::insertRows (int row, int count, QModelIndex const& parent) +{ + if (0 < count) + { + beginInsertRows (parent, row, row + count - 1); + for (auto r = 0; r < count; ++r) + { + stations_.insert (row, Station ()); + } + endInsertRows (); + return true; + } + + return false; +} + +Qt::DropActions StationList::impl::supportedDropActions () const +{ + return Qt::CopyAction | Qt::MoveAction; +} + +QStringList StationList::impl::mimeTypes () const +{ + QStringList types; + types << mime_type; + types << "application/wsjt.Frequencies"; + return types; +} + +QMimeData * StationList::impl::mimeData (QModelIndexList const& items) const +{ + QMimeData * mime_data = new QMimeData {}; + QByteArray encoded_data; + QDataStream stream {&encoded_data, QIODevice::WriteOnly}; + + Q_FOREACH (auto const& item, items) + { + if (item.isValid ()) + { + stream << QString {data (item, Qt::DisplayRole).toString ()}; + } + } + + mime_data->setData (mime_type, encoded_data); + return mime_data; +} + +bool StationList::impl::dropMimeData (QMimeData const * data, Qt::DropAction action, int /* row */, int /* column */, QModelIndex const& parent) +{ + if (Qt::IgnoreAction == action) + { + return true; + } + if (parent.isValid () + && description_column == parent.column () + && data->hasFormat (mime_type)) + { + QByteArray encoded_data {data->data (mime_type)}; + QDataStream stream {&encoded_data, QIODevice::ReadOnly}; + auto dest_index = parent; + while (!stream.atEnd ()) + { + QString text; + stream >> text; + setData (dest_index, text); + dest_index = index (dest_index.row () + 1, dest_index.column (), QModelIndex {}); + } + return true; + } + else if (data->hasFormat ("application/wsjt.Frequencies")) + { + QByteArray encoded_data {data->data ("application/wsjt.Frequencies")}; + QDataStream stream {&encoded_data, QIODevice::ReadOnly}; + while (!stream.atEnd ()) + { + FrequencyList_v2::Item item; + stream >> item; + auto const& band = bands_->find (item.frequency_); + if (stations_.cend () == std::find_if (stations_.cbegin () + , stations_.cend () + , [&band] (Station const& s) {return s.band_name_ == band;})) + { + // not found so add it + add (Station {band, 0, QString {}}); + } + } + return true; + } + return false; +} diff --git a/models/StationList.hpp b/models/StationList.hpp new file mode 100644 index 00000000..25bf428f --- /dev/null +++ b/models/StationList.hpp @@ -0,0 +1,106 @@ +#ifndef STATION_LIST_HPP__ +#define STATION_LIST_HPP__ + +#include <QSortFilterProxyModel> +#include <QString> +#include <QList> + +#include "pimpl_h.hpp" + +#include "Radio.hpp" + +class Bands; + +// +// Class StationList +// +// Encapsulates information about a collection of unique operating +// stations per band. The implementation is a table model with the +// first column being the unique (within the table rows) band name +// and, the second the frequency offset for transverter usage and, +// the third the antenna description. All are editable. +// +// Responsibilities +// +// Stores internally an unordered table of bands. +// +// If an ordered representaion is required then wrapping with an +// appropriate proxy model is sufficient +// e.g. QSortFilterProxyModel. A custom SortRole role is provided for +// the band name column which returns a numeric value (Bands lower +// frequency limit) which gives a strict frequency ordering by band. +// +// Collaborations +// +// Implements the QAbstractTableModel interface for a grid of bands +// with offset frequencies and antenna descriptions. +// +// Uses the Bands model to lookup band information. +// +class StationList final + : public QSortFilterProxyModel +{ +public: + using Frequency = Radio::Frequency; + using FrequencyDelta = Radio::FrequencyDelta; + + // + // Struct Station + // + // Aggregation of fields that describe a radio station on a band. + // + struct Station + { + QString band_name_; + FrequencyDelta offset_; + QString antenna_description_; + }; + + using Stations = QList<Station>; + + enum Column {band_column, offset_column, description_column}; + + explicit StationList (Bands const * bands, QObject * parent = nullptr); + explicit StationList (Bands const * bands, Stations, QObject * parent = nullptr); + ~StationList (); + + // Load and query contents. + Stations station_list (Stations); + Stations const& station_list () const; + + // + // Model API + // + QModelIndex add (Station); // Add a new Station + bool remove (Station); // Remove a Station + bool removeDisjointRows (QModelIndexList); // Remove one or more stations + FrequencyDelta offset (Frequency) const; // Return the offset to be used for a Frequency + + // Custom sort role. + static int constexpr SortRole = Qt::UserRole; + +private: + class impl; + pimpl<impl> m_; +}; + +// Station equivalence +inline +bool operator == (StationList::Station const& lhs, StationList::Station const& rhs) +{ + return lhs.band_name_ == rhs.band_name_ + && lhs.offset_ == rhs.offset_ + && lhs.antenna_description_ == rhs.antenna_description_; +} + +QDataStream& operator << (QDataStream&, StationList::Station const&); +QDataStream& operator >> (QDataStream&, StationList::Station&); + +#if !defined (QT_NO_DEBUG_STREAM) +QDebug operator << (QDebug debug, StationList::Station const&); +#endif + +Q_DECLARE_METATYPE (StationList::Station); +Q_DECLARE_METATYPE (StationList::Stations); + +#endif diff --git a/models/models.pri b/models/models.pri new file mode 100644 index 00000000..e0c69e81 --- /dev/null +++ b/models/models.pri @@ -0,0 +1,18 @@ +SOURCES += \ + models/Bands.cpp \ + models/FrequencyList.cpp \ + models/StationList.cpp \ + models/Modes.cpp \ + models/IARURegions.cpp \ + models/FoxLog.cpp \ + models/CabrilloLog.cpp + +HEADERS += \ + models/Bands.hpp \ + models/FrequencyList.hpp \ + models/StationList.hpp \ + models/Modes.hpp \ + models/IARURegions.hpp \ + models/FoxLog.hpp \ + models/CabrilloLog.hpp \ + models/FontOverrideModel.hpp |
