diff options
Diffstat (limited to 'models')
| -rw-r--r--[-rwxr-xr-x] | models/Bands.cpp | 480 | ||||
| -rw-r--r--[-rwxr-xr-x] | models/Bands.hpp | 170 | ||||
| -rw-r--r--[-rwxr-xr-x] | models/CabrilloLog.cpp | 652 | ||||
| -rw-r--r--[-rwxr-xr-x] | models/CabrilloLog.hpp | 98 | ||||
| -rw-r--r--[-rwxr-xr-x] | models/DecodeHighlightingModel.cpp | 746 | ||||
| -rw-r--r--[-rwxr-xr-x] | models/DecodeHighlightingModel.hpp | 162 | ||||
| -rw-r--r--[-rwxr-xr-x] | models/FontOverrideModel.hpp | 58 | ||||
| -rw-r--r--[-rwxr-xr-x] | models/FoxLog.cpp | 708 | ||||
| -rw-r--r--[-rwxr-xr-x] | models/FoxLog.hpp | 76 | ||||
| -rw-r--r--[-rwxr-xr-x] | models/FrequencyList.cpp | 2928 | ||||
| -rw-r--r--[-rwxr-xr-x] | models/FrequencyList.hpp | 502 | ||||
| -rw-r--r--[-rwxr-xr-x] | models/IARURegions.cpp | 192 | ||||
| -rw-r--r--[-rwxr-xr-x] | models/IARURegions.hpp | 134 | ||||
| -rw-r--r--[-rwxr-xr-x] | models/Modes.cpp | 208 | ||||
| -rw-r--r--[-rwxr-xr-x] | models/Modes.hpp | 152 | ||||
| -rw-r--r--[-rwxr-xr-x] | models/StationList.cpp | 1088 | ||||
| -rw-r--r--[-rwxr-xr-x] | models/StationList.hpp | 218 | ||||
| -rw-r--r--[-rwxr-xr-x] | models/models.pri | 40 |
18 files changed, 4306 insertions, 4306 deletions
diff --git a/models/Bands.cpp b/models/Bands.cpp index 534aca7a..f6d79f2e 100755..100644 --- a/models/Bands.cpp +++ b/models/Bands.cpp @@ -1,240 +1,240 @@ -#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", 135700u, 137800u},
- {"630m", 472000u, 479000u},
- {"560m", 501000u, 504000u},
- {"160m", 1800000u, 2000000u},
- {"80m", 3500000u, 4000000u},
- {"60m", 5060000u, 5450000u},
- {"40m", 7000000u, 7300000u},
- {"30m", 10100000u, 10150000u},
- {"20m", 14000000u, 14350000u},
- {"17m", 18068000u, 18168000u},
- {"15m", 21000000u, 21450000u},
- {"12m", 24890000u, 24990000u},
- {"10m", 28000000u, 29700000u},
- {"8m", 40000000u, 45000000u},
- {"6m", 50000000u, 54000000u},
- {"5m", 54000001u, 69900000u},
- {"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];
- }
-}
-
-#include "moc_Bands.cpp"
-
-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 = 0; 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 ());
-}
+#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", 135700u, 137800u}, + {"630m", 472000u, 479000u}, + {"560m", 501000u, 504000u}, + {"160m", 1800000u, 2000000u}, + {"80m", 3500000u, 4000000u}, + {"60m", 5060000u, 5450000u}, + {"40m", 7000000u, 7300000u}, + {"30m", 10100000u, 10150000u}, + {"20m", 14000000u, 14350000u}, + {"17m", 18068000u, 18168000u}, + {"15m", 21000000u, 21450000u}, + {"12m", 24890000u, 24990000u}, + {"10m", 28000000u, 29700000u}, + {"8m", 40000000u, 45000000u}, + {"6m", 50000000u, 54000000u}, + {"5m", 54000001u, 69900000u}, + {"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]; + } +} + +#include "moc_Bands.cpp" + +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 = 0; 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 index 629e63b4..77ae5766 100755..100644 --- a/models/Bands.hpp +++ b/models/Bands.hpp @@ -1,85 +1,85 @@ -#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 frequency limit respectively.
-//
-class Bands final
- : public QAbstractTableModel
-{
- Q_OBJECT
-
-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
+#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 frequency limit respectively. +// +class Bands final + : public QAbstractTableModel +{ + Q_OBJECT + +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 index a7d9f32c..77166859 100755..100644 --- a/models/CabrilloLog.cpp +++ b/models/CabrilloLog.cpp @@ -1,326 +1,326 @@ -#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 "logbook/AD1CCty.hpp"
-#include "qt_db_helpers.hpp"
-#include "pimpl_impl.hpp"
-
-class CabrilloLog::impl final
- : public QSqlTableModel
-{
-public:
- impl (CabrilloLog *, Configuration const *);
-
- int columnCount (QModelIndex const& /*index */) const override
- {
- return QSqlTableModel::columnCount () + 1;
- }
-
- Qt::ItemFlags flags (QModelIndex const& index) const override
- {
- auto flags = QSqlTableModel::flags (index);
- if (index.isValid () && index.column () == columnCount (index) - 1)
- {
- flags = Qt::ItemIsEnabled;
- }
- return flags;
- }
-
- QVariant data (QModelIndex const& model_index, int role) const override
- {
- QVariant value;
- if (model_index.isValid () && model_index.column () == columnCount (model_index) - 1)
- { // derive band column
- if (Qt::DisplayRole == role)
- {
- value = configuration_->bands ()->find (QSqlTableModel::data (index (model_index.row (), fieldIndex ("frequency"))).toULongLong ());
- }
- }
- else
- {
- value = QSqlTableModel::data (model_index, role);
- if (Qt::DisplayRole == role)
- {
- if (model_index.column () == fieldIndex ("frequency"))
- {
- value = Radio::frequency_MHz_string (value.value<Radio::Frequency> (), 3); // kHz precision
- }
- else if (model_index.column () == fieldIndex ("when"))
- { // adjust date/time to Qt format
- QLocale locale;
- value = locale.toString (QDateTime::fromMSecsSinceEpoch (value.toULongLong () * 1000ull, Qt::UTC), locale.dateFormat (QLocale::ShortFormat) + " hh:mm:ss");
- }
- }
- }
- return value;
- }
-
- QString cabrillo_frequency_string (Radio::Frequency frequency) const;
- void create_table ();
-
- CabrilloLog * self_;
- Configuration const * configuration_;
- QSqlQuery mutable dupe_query_;
- QSqlQuery mutable export_query_;
- QSqlQuery mutable qso_count_query_;
- bool adding_row_;
- int n_qso();
-};
-
-CabrilloLog::impl::impl (CabrilloLog * self, Configuration const * configuration)
- : self_ {self}
- , configuration_ {configuration}
- , adding_row_ {false}
-{
- if (!database ().tables ().contains ("cabrillo_log_v2"))
- {
- create_table ();
- }
-
- setEditStrategy (QSqlTableModel::OnFieldChange);
- setTable ("cabrillo_log_v2");
- setHeaderData (fieldIndex ("frequency"), Qt::Horizontal, tr ("Freq(MHz)"));
- setHeaderData (fieldIndex ("mode"), Qt::Horizontal, tr ("Mode"));
- 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 (columnCount (QModelIndex {}) - 1, 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);
-
- connect (this, &CabrilloLog::impl::modelReset, self_, &CabrilloLog::data_changed);
- connect (this, &CabrilloLog::impl::dataChanged, [this] (QModelIndex const& tl, QModelIndex const& br) {
- if (!adding_row_ && !(tl == br)) // ignore single cell changes
- // as a another change for the
- // whole row will follow
- {
- Q_EMIT self_->data_changed ();
- }
- Q_EMIT self_->qso_count_changed(self_->n_qso());
- });
-
- SQL_error_check (*this, &QSqlTableModel::select);
-
- SQL_error_check (dupe_query_, &QSqlQuery::prepare,
- "SELECT "
- " frequency "
- " FROM "
- " cabrillo_log_v2 "
- " WHERE "
- " call = :call ");
-
- SQL_error_check (export_query_, &QSqlQuery::prepare,
- "SELECT "
- " frequency"
- " , \"when\""
- " , exchange_sent"
- " , call"
- " , exchange_rcvd"
- " FROM "
- " cabrillo_log_v2 "
- " ORDER BY "
- " \"when\"");
-
- SQL_error_check (qso_count_query_, &QSqlQuery::prepare,
- "SELECT COUNT(*) FROM cabrillo_log_v2");
-
-}
-
-void CabrilloLog::impl::create_table ()
-{
- QSqlQuery query;
- SQL_error_check (query, static_cast<bool (QSqlQuery::*) (QString const&)> (&QSqlQuery::exec),
- "CREATE TABLE cabrillo_log_v2 ("
- " id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,"
- " frequency INTEGER NOT NULL,"
- " mode VARCHAR(6) NOT NULL,"
- " \"when\" DATETIME NOT NULL,"
- " call VARCHAR(20) NOT NULL,"
- " exchange_sent VARCHAR(32) NOT NULL,"
- " exchange_rcvd VARCHAR(32) NOT NULL"
- ")");
-}
-
-// frequency here is in kHz
-QString CabrilloLog::impl::cabrillo_frequency_string (Radio::Frequency frequency) const
-{
- QString result;
- auto band = configuration_->bands ()->find (frequency);
- if ("1mm" == band) result = "LIGHT";
- else if ("2mm" == band) result = "241G";
- else if ("2.5mm" == band) result = "134G";
- else if ("4mm" == band) result = "75G";
- else if ("6mm" == band) result = "47G";
- else if ("1.25cm" == band) result = "24G";
- else if ("3cm" == band) result = "10G";
- else if ("6cm" == band) result = "5.7G";
- else if ("9cm" == band) result = "3.4G";
- else if ("13cm" == band) result = "2.3G";
- else if ("23cm" == band) result = "1.2G";
- else if ("33cm" == band) result = "902";
- else if ("70cm" == band) result = "432";
- else if ("1.25m" == band) result = "222";
- else if ("2m" == band) result = "144";
- else if ("4m" == band) result = "70";
- else if ("6m" == band) result = "50";
- else result = QString::number (frequency / 1000ull);
- return result;
-}
-
-#include "moc_CabrilloLog.cpp"
-
-CabrilloLog::CabrilloLog (Configuration const * configuration, QObject * parent)
- : QObject {parent}
- , m_ {this, 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, QString const& mode, QDateTime const& when, QString const& call
- , QString const& exchange_sent, QString const& exchange_received)
-{
- auto record = m_->record ();
- record.setValue ("frequency", frequency);
- record.setValue ("mode", mode);
- 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);
- if (m_->isDirty ())
- {
- m_->revert (); // discard any uncommitted changes
- }
- m_->setEditStrategy (QSqlTableModel::OnManualSubmit);
- ConditionalTransaction transaction {*m_};
- m_->adding_row_ = true;
- auto ok = m_->insertRecord (-1, record);
- transaction.submit ();
-
- m_->adding_row_ = false;
- m_->setEditStrategy (QSqlTableModel::OnFieldChange);
- Q_EMIT this->qso_count_changed(this->n_qso());
- return ok;
-}
-
-bool CabrilloLog::dupe (Frequency frequency, QString const& call) const
-{
- m_->dupe_query_.bindValue (":call", call);
- SQL_error_check (m_->dupe_query_, static_cast<bool (QSqlQuery::*) ()> (&QSqlQuery::exec));
- auto record = m_->dupe_query_.record ();
- auto frequency_index = record.indexOf ("frequency");
- while (m_->dupe_query_.next ())
- {
- if (m_->configuration_->bands ()->find (m_->dupe_query_.value (frequency_index).toULongLong ())
- == m_->configuration_->bands ()->find (frequency))
- {
- return true;
- }
- }
- return false;
-}
-
-int CabrilloLog::n_qso()
-{
- SQL_error_check (m_->qso_count_query_, static_cast<bool (QSqlQuery::*) ()> (&QSqlQuery::exec));
- m_->qso_count_query_.first();
- return m_->qso_count_query_.value(0).toInt();
-}
-
-void CabrilloLog::reset ()
-{
- // synchronize model
- while (m_->canFetchMore ()) m_->fetchMore ();
- 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);
- Q_EMIT data_changed ();
- }
-}
-
-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 mode_index = record.indexOf ("mode");
- 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 my_call = m_->configuration_->my_callsign ();
- stream << QString {"QSO: %1 DG %2 %3 %4 %5 %6\n"}
- .arg (m_->cabrillo_frequency_string (m_->export_query_.value (frequency_index).value<Radio::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);
- }
-}
-
-auto CabrilloLog::unique_DXCC_entities (AD1CCty const * countries) const -> worked_set
-{
- QSqlQuery q {"SELECT DISTINCT BAND, CALL FROM cabrillo_log_v2"};
- auto band_index = q.record ().indexOf ("band");
- auto call_index = q.record ().indexOf ("call");
- worked_set entities;
- while (q.next ())
- {
- entities << worked_item {q.value (band_index).toString ()
- , countries->lookup (q.value (call_index).toString ()).primary_prefix};
- }
- return entities;
-}
+#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 "logbook/AD1CCty.hpp" +#include "qt_db_helpers.hpp" +#include "pimpl_impl.hpp" + +class CabrilloLog::impl final + : public QSqlTableModel +{ +public: + impl (CabrilloLog *, Configuration const *); + + int columnCount (QModelIndex const& /*index */) const override + { + return QSqlTableModel::columnCount () + 1; + } + + Qt::ItemFlags flags (QModelIndex const& index) const override + { + auto flags = QSqlTableModel::flags (index); + if (index.isValid () && index.column () == columnCount (index) - 1) + { + flags = Qt::ItemIsEnabled; + } + return flags; + } + + QVariant data (QModelIndex const& model_index, int role) const override + { + QVariant value; + if (model_index.isValid () && model_index.column () == columnCount (model_index) - 1) + { // derive band column + if (Qt::DisplayRole == role) + { + value = configuration_->bands ()->find (QSqlTableModel::data (index (model_index.row (), fieldIndex ("frequency"))).toULongLong ()); + } + } + else + { + value = QSqlTableModel::data (model_index, role); + if (Qt::DisplayRole == role) + { + if (model_index.column () == fieldIndex ("frequency")) + { + value = Radio::frequency_MHz_string (value.value<Radio::Frequency> (), 3); // kHz precision + } + else if (model_index.column () == fieldIndex ("when")) + { // adjust date/time to Qt format + QLocale locale; + value = locale.toString (QDateTime::fromMSecsSinceEpoch (value.toULongLong () * 1000ull, Qt::UTC), locale.dateFormat (QLocale::ShortFormat) + " hh:mm:ss"); + } + } + } + return value; + } + + QString cabrillo_frequency_string (Radio::Frequency frequency) const; + void create_table (); + + CabrilloLog * self_; + Configuration const * configuration_; + QSqlQuery mutable dupe_query_; + QSqlQuery mutable export_query_; + QSqlQuery mutable qso_count_query_; + bool adding_row_; + int n_qso(); +}; + +CabrilloLog::impl::impl (CabrilloLog * self, Configuration const * configuration) + : self_ {self} + , configuration_ {configuration} + , adding_row_ {false} +{ + if (!database ().tables ().contains ("cabrillo_log_v2")) + { + create_table (); + } + + setEditStrategy (QSqlTableModel::OnFieldChange); + setTable ("cabrillo_log_v2"); + setHeaderData (fieldIndex ("frequency"), Qt::Horizontal, tr ("Freq(MHz)")); + setHeaderData (fieldIndex ("mode"), Qt::Horizontal, tr ("Mode")); + 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 (columnCount (QModelIndex {}) - 1, 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); + + connect (this, &CabrilloLog::impl::modelReset, self_, &CabrilloLog::data_changed); + connect (this, &CabrilloLog::impl::dataChanged, [this] (QModelIndex const& tl, QModelIndex const& br) { + if (!adding_row_ && !(tl == br)) // ignore single cell changes + // as a another change for the + // whole row will follow + { + Q_EMIT self_->data_changed (); + } + Q_EMIT self_->qso_count_changed(self_->n_qso()); + }); + + SQL_error_check (*this, &QSqlTableModel::select); + + SQL_error_check (dupe_query_, &QSqlQuery::prepare, + "SELECT " + " frequency " + " FROM " + " cabrillo_log_v2 " + " WHERE " + " call = :call "); + + SQL_error_check (export_query_, &QSqlQuery::prepare, + "SELECT " + " frequency" + " , \"when\"" + " , exchange_sent" + " , call" + " , exchange_rcvd" + " FROM " + " cabrillo_log_v2 " + " ORDER BY " + " \"when\""); + + SQL_error_check (qso_count_query_, &QSqlQuery::prepare, + "SELECT COUNT(*) FROM cabrillo_log_v2"); + +} + +void CabrilloLog::impl::create_table () +{ + QSqlQuery query; + SQL_error_check (query, static_cast<bool (QSqlQuery::*) (QString const&)> (&QSqlQuery::exec), + "CREATE TABLE cabrillo_log_v2 (" + " id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL," + " frequency INTEGER NOT NULL," + " mode VARCHAR(6) NOT NULL," + " \"when\" DATETIME NOT NULL," + " call VARCHAR(20) NOT NULL," + " exchange_sent VARCHAR(32) NOT NULL," + " exchange_rcvd VARCHAR(32) NOT NULL" + ")"); +} + +// frequency here is in kHz +QString CabrilloLog::impl::cabrillo_frequency_string (Radio::Frequency frequency) const +{ + QString result; + auto band = configuration_->bands ()->find (frequency); + if ("1mm" == band) result = "LIGHT"; + else if ("2mm" == band) result = "241G"; + else if ("2.5mm" == band) result = "134G"; + else if ("4mm" == band) result = "75G"; + else if ("6mm" == band) result = "47G"; + else if ("1.25cm" == band) result = "24G"; + else if ("3cm" == band) result = "10G"; + else if ("6cm" == band) result = "5.7G"; + else if ("9cm" == band) result = "3.4G"; + else if ("13cm" == band) result = "2.3G"; + else if ("23cm" == band) result = "1.2G"; + else if ("33cm" == band) result = "902"; + else if ("70cm" == band) result = "432"; + else if ("1.25m" == band) result = "222"; + else if ("2m" == band) result = "144"; + else if ("4m" == band) result = "70"; + else if ("6m" == band) result = "50"; + else result = QString::number (frequency / 1000ull); + return result; +} + +#include "moc_CabrilloLog.cpp" + +CabrilloLog::CabrilloLog (Configuration const * configuration, QObject * parent) + : QObject {parent} + , m_ {this, 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, QString const& mode, QDateTime const& when, QString const& call + , QString const& exchange_sent, QString const& exchange_received) +{ + auto record = m_->record (); + record.setValue ("frequency", frequency); + record.setValue ("mode", mode); + 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); + if (m_->isDirty ()) + { + m_->revert (); // discard any uncommitted changes + } + m_->setEditStrategy (QSqlTableModel::OnManualSubmit); + ConditionalTransaction transaction {*m_}; + m_->adding_row_ = true; + auto ok = m_->insertRecord (-1, record); + transaction.submit (); + + m_->adding_row_ = false; + m_->setEditStrategy (QSqlTableModel::OnFieldChange); + Q_EMIT this->qso_count_changed(this->n_qso()); + return ok; +} + +bool CabrilloLog::dupe (Frequency frequency, QString const& call) const +{ + m_->dupe_query_.bindValue (":call", call); + SQL_error_check (m_->dupe_query_, static_cast<bool (QSqlQuery::*) ()> (&QSqlQuery::exec)); + auto record = m_->dupe_query_.record (); + auto frequency_index = record.indexOf ("frequency"); + while (m_->dupe_query_.next ()) + { + if (m_->configuration_->bands ()->find (m_->dupe_query_.value (frequency_index).toULongLong ()) + == m_->configuration_->bands ()->find (frequency)) + { + return true; + } + } + return false; +} + +int CabrilloLog::n_qso() +{ + SQL_error_check (m_->qso_count_query_, static_cast<bool (QSqlQuery::*) ()> (&QSqlQuery::exec)); + m_->qso_count_query_.first(); + return m_->qso_count_query_.value(0).toInt(); +} + +void CabrilloLog::reset () +{ + // synchronize model + while (m_->canFetchMore ()) m_->fetchMore (); + 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); + Q_EMIT data_changed (); + } +} + +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 mode_index = record.indexOf ("mode"); + 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 my_call = m_->configuration_->my_callsign (); + stream << QString {"QSO: %1 DG %2 %3 %4 %5 %6\n"} + .arg (m_->cabrillo_frequency_string (m_->export_query_.value (frequency_index).value<Radio::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); + } +} + +auto CabrilloLog::unique_DXCC_entities (AD1CCty const * countries) const -> worked_set +{ + QSqlQuery q {"SELECT DISTINCT BAND, CALL FROM cabrillo_log_v2"}; + auto band_index = q.record ().indexOf ("band"); + auto call_index = q.record ().indexOf ("call"); + worked_set entities; + while (q.next ()) + { + entities << worked_item {q.value (band_index).toString () + , countries->lookup (q.value (call_index).toString ()).primary_prefix}; + } + return entities; +} diff --git a/models/CabrilloLog.hpp b/models/CabrilloLog.hpp index 8adce082..68b76fc2 100755..100644 --- a/models/CabrilloLog.hpp +++ b/models/CabrilloLog.hpp @@ -1,49 +1,49 @@ -#ifndef CABRILLO_LOG_HPP_
-#define CABRILLO_LOG_HPP_
-
-#include <QObject>
-#include <QSet>
-#include <QPair>
-#include <QString>
-#include "Radio.hpp"
-#include "pimpl_h.hpp"
-
-class Configuration;
-class QDateTime;
-class QSqlTableModel;
-class QTextStream;
-class AD1CCty;
-
-class CabrilloLog final
- : public QObject
-{
- Q_OBJECT
-
-public:
- using Frequency = Radio::Frequency;
- using worked_item = QPair<QString, QString>;
- using worked_set = QSet<worked_item>;
-
- explicit CabrilloLog (Configuration const *, QObject * parent = nullptr);
- ~CabrilloLog ();
-
- // returns false if insert fails
- bool add_QSO (Frequency, QString const& mode, QDateTime const&, QString const& call
- , QString const& report_sent, QString const& report_received);
- bool dupe (Frequency, QString const& call) const;
- int n_qso();
-
- QSqlTableModel * model ();
- void reset ();
- void export_qsos (QTextStream&) const;
- worked_set unique_DXCC_entities (AD1CCty const *) const;
-
- Q_SIGNAL void data_changed () const;
- Q_SIGNAL void qso_count_changed (int) const;
-
-private:
- class impl;
- pimpl<impl> m_;
-};
-
-#endif
+#ifndef CABRILLO_LOG_HPP_ +#define CABRILLO_LOG_HPP_ + +#include <QObject> +#include <QSet> +#include <QPair> +#include <QString> +#include "Radio.hpp" +#include "pimpl_h.hpp" + +class Configuration; +class QDateTime; +class QSqlTableModel; +class QTextStream; +class AD1CCty; + +class CabrilloLog final + : public QObject +{ + Q_OBJECT + +public: + using Frequency = Radio::Frequency; + using worked_item = QPair<QString, QString>; + using worked_set = QSet<worked_item>; + + explicit CabrilloLog (Configuration const *, QObject * parent = nullptr); + ~CabrilloLog (); + + // returns false if insert fails + bool add_QSO (Frequency, QString const& mode, QDateTime const&, QString const& call + , QString const& report_sent, QString const& report_received); + bool dupe (Frequency, QString const& call) const; + int n_qso(); + + QSqlTableModel * model (); + void reset (); + void export_qsos (QTextStream&) const; + worked_set unique_DXCC_entities (AD1CCty const *) const; + + Q_SIGNAL void data_changed () const; + Q_SIGNAL void qso_count_changed (int) const; + +private: + class impl; + pimpl<impl> m_; +}; + +#endif diff --git a/models/DecodeHighlightingModel.cpp b/models/DecodeHighlightingModel.cpp index 5b60b335..7c884b18 100755..100644 --- a/models/DecodeHighlightingModel.cpp +++ b/models/DecodeHighlightingModel.cpp @@ -1,373 +1,373 @@ -#include "DecodeHighlightingModel.hpp"
-
-#include <QString>
-#include <QVariant>
-#include <QList>
-#include <QBrush>
-#include <QColor>
-#include <QFont>
-#include <QMap>
-#include <QVector>
-#include <QTextStream>
-#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 static const defaults2_;
- 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}}
-};
-
-QList<DecodeHighlightingModel::HighlightInfo> const DecodeHighlightingModel::impl::defaults2_ = {
- {Highlight::LotW, true, {Qt::black}, {}}
- , {Highlight::MyCall, true, {{0xaa, 0x00, 0x00}}, {{0x00, 0xff, 0xff}}}
- , {Highlight::Continent, true, {{0xaa, 0x00, 0x00}}, {{0xff, 0x50, 0xff}}}
- , {Highlight::ContinentBand, true, {{0xaa, 0x00, 0x00}}, {{0xff, 0x82, 0xff}}}
- , {Highlight::DXCC, true, {{0xaa, 0x00, 0x00}}, {Qt::yellow}}
- , {Highlight::DXCCBand, true, {{0xaa, 0x00, 0x00}}, {{0xff, 0xff, 0x9b}}}
- , {Highlight::CQZone, true, {{0xaa, 0x00, 0x00}}, {{0xaa, 0xaa, 0x00}}}
- , {Highlight::CQZoneBand, true, {{0xaa, 0x00, 0x00}}, {{0xaa, 0xaa, 0x7f}}}
- , {Highlight::ITUZone, true, {{0xaa, 0x00, 0x00}}, {{0x37, 0xbd, 0xff}}}
- , {Highlight::ITUZoneBand, true, {{0xaa, 0x00, 0x00}}, {{0x91, 0xde, 0xff}}}
- , {Highlight::Grid, true, {{0xaa, 0x00, 0x00}}, {{0xff, 0xa8, 0x80}}}
- , {Highlight::GridBand, true, {{0xaa, 0x00, 0x00}}, {{0xff, 0xcd, 0x9b}}}
- , {Highlight::Call, true, {{0xaa, 0x00, 0x00}}, {{0x64, 0xff, 0x64}}}
- , {Highlight::CallBand, true, {{0xaa, 0x00, 0x00}}, {{0xb4, 0xff, 0xb4}}}
- , {Highlight::CQ, true, {{0xaa, 0x00, 0x00}}, {{0xc3, 0xc3, 0xc3}}}
- , {Highlight::Tx, true, {Qt::black}, {{0xff, 0xa5, 0xc6}}}
-};
-
-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_;
-}
-
-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)
-QDebug operator << (QDebug debug, DecodeHighlightingModel::HighlightInfo const& item)
-{
- QDebugStateSaver save {debug};
- return debug.nospace () << item.toString ();
-}
-#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 tr ("CQ in message");
- case Highlight::MyCall: return tr ("My Call in message");
- case Highlight::Tx: return tr ("Transmitted message");
- case Highlight::DXCC: return tr ("New DXCC");
- case Highlight::DXCCBand: return tr ("New DXCC on Band");
- case Highlight::Grid: return tr ("New Grid");
- case Highlight::GridBand: return tr ("New Grid on Band");
- case Highlight::Call: return tr ("New Call");
- case Highlight::CallBand: return tr ("New Call on Band");
- case Highlight::Continent: return tr ("New Continent");
- case Highlight::ContinentBand: return tr ("New Continent on Band");
- case Highlight::CQZone: return tr ("New CQ Zone");
- case Highlight::CQZoneBand: return tr ("New CQ Zone on Band");
- case Highlight::ITUZone: return tr ("New ITU Zone");
- case Highlight::ITUZoneBand: return tr ("New ITU Zone on Band");
- case Highlight::LotW: return tr ("LoTW User");
- }
- return "Unknown";
-}
-
-auto DecodeHighlightingModel::default_items () -> HighlightItems const&
-{
- return impl::defaults_;
-}
-
-auto DecodeHighlightingModel::default_items2 () -> HighlightItems const&
-{
- return impl::defaults2_;
-}
-
-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 ()];
- auto fg_unset = Qt::NoBrush == item.foreground_.style ();
- auto bg_unset = Qt::NoBrush == item.background_.style ();
- switch (role)
- {
- case Qt::CheckStateRole:
- result = item.enabled_ ? Qt::Checked : Qt::Unchecked;
- break;
- case Qt::DisplayRole:
- return QString {"%1%2%3%4%4%5%6"}
- .arg (highlight_name (item.type_))
- .arg (fg_unset || bg_unset ? QString {" ["} : QString {})
- .arg (fg_unset ? tr ("f/g unset") : QString {})
- .arg (fg_unset && bg_unset ? QString {" "} : QString {})
- .arg (bg_unset ? tr ("b/g unset") : QString {})
- .arg (fg_unset || bg_unset ? QString {"]"} : QString {});
- break;
- case Qt::ForegroundRole:
- if (!fg_unset)
- {
- result = item.foreground_;
- }
- break;
- case Qt::BackgroundRole:
- if (!bg_unset)
- {
- 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;
-}
+#include "DecodeHighlightingModel.hpp" + +#include <QString> +#include <QVariant> +#include <QList> +#include <QBrush> +#include <QColor> +#include <QFont> +#include <QMap> +#include <QVector> +#include <QTextStream> +#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 static const defaults2_; + 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}} +}; + +QList<DecodeHighlightingModel::HighlightInfo> const DecodeHighlightingModel::impl::defaults2_ = { + {Highlight::LotW, true, {Qt::black}, {}} + , {Highlight::MyCall, true, {{0xaa, 0x00, 0x00}}, {{0x00, 0xff, 0xff}}} + , {Highlight::Continent, true, {{0xaa, 0x00, 0x00}}, {{0xff, 0x50, 0xff}}} + , {Highlight::ContinentBand, true, {{0xaa, 0x00, 0x00}}, {{0xff, 0x82, 0xff}}} + , {Highlight::DXCC, true, {{0xaa, 0x00, 0x00}}, {Qt::yellow}} + , {Highlight::DXCCBand, true, {{0xaa, 0x00, 0x00}}, {{0xff, 0xff, 0x9b}}} + , {Highlight::CQZone, true, {{0xaa, 0x00, 0x00}}, {{0xaa, 0xaa, 0x00}}} + , {Highlight::CQZoneBand, true, {{0xaa, 0x00, 0x00}}, {{0xaa, 0xaa, 0x7f}}} + , {Highlight::ITUZone, true, {{0xaa, 0x00, 0x00}}, {{0x37, 0xbd, 0xff}}} + , {Highlight::ITUZoneBand, true, {{0xaa, 0x00, 0x00}}, {{0x91, 0xde, 0xff}}} + , {Highlight::Grid, true, {{0xaa, 0x00, 0x00}}, {{0xff, 0xa8, 0x80}}} + , {Highlight::GridBand, true, {{0xaa, 0x00, 0x00}}, {{0xff, 0xcd, 0x9b}}} + , {Highlight::Call, true, {{0xaa, 0x00, 0x00}}, {{0x64, 0xff, 0x64}}} + , {Highlight::CallBand, true, {{0xaa, 0x00, 0x00}}, {{0xb4, 0xff, 0xb4}}} + , {Highlight::CQ, true, {{0xaa, 0x00, 0x00}}, {{0xc3, 0xc3, 0xc3}}} + , {Highlight::Tx, true, {Qt::black}, {{0xff, 0xa5, 0xc6}}} +}; + +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_; +} + +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) +QDebug operator << (QDebug debug, DecodeHighlightingModel::HighlightInfo const& item) +{ + QDebugStateSaver save {debug}; + return debug.nospace () << item.toString (); +} +#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 tr ("CQ in message"); + case Highlight::MyCall: return tr ("My Call in message"); + case Highlight::Tx: return tr ("Transmitted message"); + case Highlight::DXCC: return tr ("New DXCC"); + case Highlight::DXCCBand: return tr ("New DXCC on Band"); + case Highlight::Grid: return tr ("New Grid"); + case Highlight::GridBand: return tr ("New Grid on Band"); + case Highlight::Call: return tr ("New Call"); + case Highlight::CallBand: return tr ("New Call on Band"); + case Highlight::Continent: return tr ("New Continent"); + case Highlight::ContinentBand: return tr ("New Continent on Band"); + case Highlight::CQZone: return tr ("New CQ Zone"); + case Highlight::CQZoneBand: return tr ("New CQ Zone on Band"); + case Highlight::ITUZone: return tr ("New ITU Zone"); + case Highlight::ITUZoneBand: return tr ("New ITU Zone on Band"); + case Highlight::LotW: return tr ("LoTW User"); + } + return "Unknown"; +} + +auto DecodeHighlightingModel::default_items () -> HighlightItems const& +{ + return impl::defaults_; +} + +auto DecodeHighlightingModel::default_items2 () -> HighlightItems const& +{ + return impl::defaults2_; +} + +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 ()]; + auto fg_unset = Qt::NoBrush == item.foreground_.style (); + auto bg_unset = Qt::NoBrush == item.background_.style (); + switch (role) + { + case Qt::CheckStateRole: + result = item.enabled_ ? Qt::Checked : Qt::Unchecked; + break; + case Qt::DisplayRole: + return QString {"%1%2%3%4%4%5%6"} + .arg (highlight_name (item.type_)) + .arg (fg_unset || bg_unset ? QString {" ["} : QString {}) + .arg (fg_unset ? tr ("f/g unset") : QString {}) + .arg (fg_unset && bg_unset ? QString {" "} : QString {}) + .arg (bg_unset ? tr ("b/g unset") : QString {}) + .arg (fg_unset || bg_unset ? QString {"]"} : QString {}); + break; + case Qt::ForegroundRole: + if (!fg_unset) + { + result = item.foreground_; + } + break; + case Qt::BackgroundRole: + if (!bg_unset) + { + 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 index b838e3b5..839b3ba4 100755..100644 --- a/models/DecodeHighlightingModel.hpp +++ b/models/DecodeHighlightingModel.hpp @@ -1,81 +1,81 @@ -#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_;
- QString toString () const;
- };
- using HighlightItems = QList<HighlightInfo>;
-
- explicit DecodeHighlightingModel (QObject * parent = 0);
- ~DecodeHighlightingModel();
-
- // access to raw items nd default items
- static HighlightItems const& default_items ();
- static HighlightItems const& default_items2 ();
- 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
+#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_; + QString toString () const; + }; + using HighlightItems = QList<HighlightInfo>; + + explicit DecodeHighlightingModel (QObject * parent = 0); + ~DecodeHighlightingModel(); + + // access to raw items nd default items + static HighlightItems const& default_items (); + static HighlightItems const& default_items2 (); + 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 index c23e119a..2bff9aa8 100755..100644 --- a/models/FontOverrideModel.hpp +++ b/models/FontOverrideModel.hpp @@ -1,29 +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
+#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 index c4fac283..960288ca 100755..100644 --- a/models/FoxLog.cpp +++ b/models/FoxLog.cpp @@ -1,354 +1,354 @@ -#include "FoxLog.hpp"
-
-#include <stdexcept>
-#include <utility>
-#include <QString>
-#include <QDateTime>
-#include <QSqlDatabase>
-#include <QSqlTableModel>
-#include <QSqlRecord>
-#include <QSqlError>
-#include <QSqlQuery>
-#include <QTextStream>
-#include <QDebug>
-#include "Logger.hpp"
-#include "Configuration.hpp"
-#include "qt_db_helpers.hpp"
-#include "pimpl_impl.hpp"
-
-class FoxLog::impl final
- : public QSqlTableModel
-{
- Q_OBJECT
-
-public:
- impl (Configuration const * configuration);
-
- QVariant data (QModelIndex const& index, int role) const
- {
- auto value = QSqlTableModel::data (index, role);
- if (index.column () == fieldIndex ("when") && Qt::DisplayRole == role)
- {
- QLocale locale;
- value = locale.toString (QDateTime::fromMSecsSinceEpoch (value.toULongLong () * 1000ull, Qt::UTC), locale.dateFormat (QLocale::ShortFormat) + " hh:mm:ss");
- }
- return value;
- }
-
- Configuration const * configuration_;
- QSqlQuery mutable dupe_query_;
- QSqlQuery mutable export_query_;
- // queries for rates
- QSqlQuery mutable rate_n_query_;
- QSqlQuery mutable rate60m_query_;
- QString rate();
-};
-
-#include "FoxLog.moc"
-
-namespace
-{
- QString const fox_log_ddl {
- "CREATE %1 TABLE fox_log%2 ("
- " 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"
- ")"
- };
-}
-
-FoxLog::impl::impl (Configuration const * configuration)
- : configuration_ {configuration}
-{
- if (!database ().tables ().contains ("fox_log"))
- {
- QSqlQuery query;
- SQL_error_check (query, static_cast<bool (QSqlQuery::*) (QString const&)> (&QSqlQuery::exec),
- fox_log_ddl.arg ("").arg (""));
- }
- else
- {
- QSqlQuery query;
- // query to check if table has a unique constraint
- SQL_error_check (query, static_cast<bool (QSqlQuery::*) (QString const&)> (&QSqlQuery::exec),
- "SELECT COUNT(*)"
- " FROM sqlite_master"
- " WHERE"
- " type = 'index' AND tbl_name = 'fox_log'");
- query.next ();
- if (query.value (0).toInt ())
- {
- // update to new schema with no dupe disallowing unique
- // constraint
- database ().transaction ();
- SQL_error_check (query, static_cast<bool (QSqlQuery::*) (QString const&)> (&QSqlQuery::exec),
- fox_log_ddl.arg ("TEMPORARY").arg ("_backup"));
- SQL_error_check (query, static_cast<bool (QSqlQuery::*) (QString const&)> (&QSqlQuery::exec),
- "INSERT INTO fox_log_backup SELECT * from fox_log");
- SQL_error_check (query, static_cast<bool (QSqlQuery::*) (QString const&)> (&QSqlQuery::exec),
- "DROP TABLE fox_log");
- SQL_error_check (query, static_cast<bool (QSqlQuery::*) (QString const&)> (&QSqlQuery::exec),
- fox_log_ddl.arg ("").arg (""));
- SQL_error_check (query, static_cast<bool (QSqlQuery::*) (QString const&)> (&QSqlQuery::exec),
- "INSERT INTO fox_log SELECT * from fox_log_backup");
- SQL_error_check (query, static_cast<bool (QSqlQuery::*) (QString const&)> (&QSqlQuery::exec),
- "DROP TABLE fox_log_backup");
- database ().commit ();
- }
- }
-
- SQL_error_check (dupe_query_, &QSqlQuery::prepare,
- "SELECT "
- " COUNT(*) "
- " FROM "
- " fox_log "
- " WHERE "
- " call = :call "
- " AND band = :band");
-
- SQL_error_check (export_query_, &QSqlQuery::prepare,
- "SELECT "
- " band"
- " , \"when\""
- " , call"
- " , grid"
- " , report_sent"
- " , report_rcvd "
- " FROM "
- " fox_log "
- " ORDER BY "
- " \"when\"");
-
- SQL_error_check (rate_n_query_, &QSqlQuery::prepare,
- "SELECT "
- " \"when\""
- " FROM "
- " fox_log "
- " ORDER BY "
- " \"when\" DESC"
- " LIMIT :limitn"
- );
-
- SQL_error_check (rate60m_query_, &QSqlQuery::prepare,
- "SELECT "
- " COUNT() "
- " FROM "
- " fox_log "
- " where \"when\" > :one_hour_ago"
- " ORDER BY "
- " \"when\" DESC"
- );
- 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);
-}
-
-QString FoxLog::rate()
-{
- return QString("Last 10: %1, Last 100: %2, Last 60m: %3").arg(QString::number(this->rate_last_n(10),'f',0),
- QString::number(this->rate_last_n(100),'f',0),
- QString::number(this->rate_60m()));
-}
-
-FoxLog::FoxLog (Configuration const * configuration)
- : m_ {configuration}
-{
-}
-
-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 ()
-{
- // synchronize model
- while (m_->canFetchMore ()) m_->fetchMore ();
- 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);
- }
-}
-
-int FoxLog::rate_60m()
-{
- int rate60m = 0;
- qlonglong const& one_hour_ago = QDateTime::currentDateTime().addSecs(-3600).toMSecsSinceEpoch () / 1000;
-
- // query the 60m rate
- m_->rate60m_query_.bindValue (":one_hour_ago", one_hour_ago);
- SQL_error_check (m_->rate60m_query_, static_cast<bool (QSqlQuery::*) ()> (&QSqlQuery::exec));
- m_->rate60m_query_.next ();
- rate60m = m_->rate60m_query_.value (0).toLongLong();
- return rate60m;
- //
-}
-
-double FoxLog::rate_last_n(int n)
-{
- double rate_interval = 0;
-
- qlonglong const& secs_now = QDateTime::currentDateTime().toMSecsSinceEpoch () / 1000;
-
- // get last n or up to n
- m_->rate_n_query_.bindValue (":limitn", n);
- SQL_error_check (m_->rate_n_query_, static_cast<bool (QSqlQuery::*) ()> (&QSqlQuery::exec));
-
- m_->rate_n_query_.next();
- if (!m_->rate_n_query_.isValid()) {
- LOG_ERROR(QString("rate_n result is not valid. Last error %1").arg(m_->rate_n_query_.lastError().text()));
- return 0.0;
- }
- // size / (time_now - time_of_first)
- m_->rate_n_query_.last();
- rate_interval = secs_now - m_->rate_n_query_.value (0).toLongLong ();
- if (rate_interval == 0) return 0.0;
-
- m_->rate_n_query_.first(); // count the records
-
- int size = 1;
- while (m_->rate_n_query_.next() && m_->rate_n_query_.isValid()) size++;
- return (size/rate_interval) * 3600;
-}
-
-namespace
-{
- struct ADIF_field
- {
- explicit ADIF_field (QString const& name, QString const& value)
- : name_ {name}
- , value_ {value}
- {
- }
-
- QString name_;
- QString value_;
- };
-
- QTextStream& operator << (QTextStream& os, ADIF_field const& field)
- {
- if (field.value_.size ())
- {
- os << QString {"<%1:%2>%3 "}.arg (field.name_).arg (field.value_.size ()).arg (field.value_);
- }
- return os;
- }
-}
-
-void FoxLog::export_qsos (QTextStream& out) const
-{
- out << "WSJT-X FT8 DXpedition Mode Fox Log\n<eoh>";
-
- SQL_error_check (m_->export_query_, static_cast<bool (QSqlQuery::*) ()> (&QSqlQuery::exec));
- auto record = m_->export_query_.record ();
- auto band_index = record.indexOf ("band");
- auto when_index = record.indexOf ("when");
- auto call_index = record.indexOf ("call");
- auto grid_index = record.indexOf ("grid");
- auto sent_index = record.indexOf ("report_sent");
- auto rcvd_index = record.indexOf ("report_rcvd");
- while (m_->export_query_.next ())
- {
- auto when = QDateTime::fromMSecsSinceEpoch (m_->export_query_.value (when_index).toULongLong () * 1000ull, Qt::UTC);
- out << '\n'
- << ADIF_field {"band", m_->export_query_.value (band_index).toString ()}
- << ADIF_field {"mode", "FT8"}
- << ADIF_field {"qso_date", when.toString ("yyyyMMdd")}
- << ADIF_field {"time_on", when.toString ("hhmmss")}
- << ADIF_field {"call", m_->export_query_.value (call_index).toString ()}
- << ADIF_field {"gridsquare", m_->export_query_.value (grid_index).toString ()}
- << ADIF_field {"rst_sent", m_->export_query_.value (sent_index).toString ()}
- << ADIF_field {"rst_rcvd", m_->export_query_.value (rcvd_index).toString ()}
- << ADIF_field {"station_callsign", m_->configuration_->my_callsign ()}
- << ADIF_field {"my_gridsquare", m_->configuration_->my_grid ()}
- << ADIF_field {"operator", m_->configuration_->opCall ()}
- << "<eor>";
- }
-#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
- out << endl;
-#else
- out << Qt::endl;
-#endif
-}
+#include "FoxLog.hpp" + +#include <stdexcept> +#include <utility> +#include <QString> +#include <QDateTime> +#include <QSqlDatabase> +#include <QSqlTableModel> +#include <QSqlRecord> +#include <QSqlError> +#include <QSqlQuery> +#include <QTextStream> +#include <QDebug> +#include "Logger.hpp" +#include "Configuration.hpp" +#include "qt_db_helpers.hpp" +#include "pimpl_impl.hpp" + +class FoxLog::impl final + : public QSqlTableModel +{ + Q_OBJECT + +public: + impl (Configuration const * configuration); + + QVariant data (QModelIndex const& index, int role) const + { + auto value = QSqlTableModel::data (index, role); + if (index.column () == fieldIndex ("when") && Qt::DisplayRole == role) + { + QLocale locale; + value = locale.toString (QDateTime::fromMSecsSinceEpoch (value.toULongLong () * 1000ull, Qt::UTC), locale.dateFormat (QLocale::ShortFormat) + " hh:mm:ss"); + } + return value; + } + + Configuration const * configuration_; + QSqlQuery mutable dupe_query_; + QSqlQuery mutable export_query_; + // queries for rates + QSqlQuery mutable rate_n_query_; + QSqlQuery mutable rate60m_query_; + QString rate(); +}; + +#include "FoxLog.moc" + +namespace +{ + QString const fox_log_ddl { + "CREATE %1 TABLE fox_log%2 (" + " 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" + ")" + }; +} + +FoxLog::impl::impl (Configuration const * configuration) + : configuration_ {configuration} +{ + if (!database ().tables ().contains ("fox_log")) + { + QSqlQuery query; + SQL_error_check (query, static_cast<bool (QSqlQuery::*) (QString const&)> (&QSqlQuery::exec), + fox_log_ddl.arg ("").arg ("")); + } + else + { + QSqlQuery query; + // query to check if table has a unique constraint + SQL_error_check (query, static_cast<bool (QSqlQuery::*) (QString const&)> (&QSqlQuery::exec), + "SELECT COUNT(*)" + " FROM sqlite_master" + " WHERE" + " type = 'index' AND tbl_name = 'fox_log'"); + query.next (); + if (query.value (0).toInt ()) + { + // update to new schema with no dupe disallowing unique + // constraint + database ().transaction (); + SQL_error_check (query, static_cast<bool (QSqlQuery::*) (QString const&)> (&QSqlQuery::exec), + fox_log_ddl.arg ("TEMPORARY").arg ("_backup")); + SQL_error_check (query, static_cast<bool (QSqlQuery::*) (QString const&)> (&QSqlQuery::exec), + "INSERT INTO fox_log_backup SELECT * from fox_log"); + SQL_error_check (query, static_cast<bool (QSqlQuery::*) (QString const&)> (&QSqlQuery::exec), + "DROP TABLE fox_log"); + SQL_error_check (query, static_cast<bool (QSqlQuery::*) (QString const&)> (&QSqlQuery::exec), + fox_log_ddl.arg ("").arg ("")); + SQL_error_check (query, static_cast<bool (QSqlQuery::*) (QString const&)> (&QSqlQuery::exec), + "INSERT INTO fox_log SELECT * from fox_log_backup"); + SQL_error_check (query, static_cast<bool (QSqlQuery::*) (QString const&)> (&QSqlQuery::exec), + "DROP TABLE fox_log_backup"); + database ().commit (); + } + } + + SQL_error_check (dupe_query_, &QSqlQuery::prepare, + "SELECT " + " COUNT(*) " + " FROM " + " fox_log " + " WHERE " + " call = :call " + " AND band = :band"); + + SQL_error_check (export_query_, &QSqlQuery::prepare, + "SELECT " + " band" + " , \"when\"" + " , call" + " , grid" + " , report_sent" + " , report_rcvd " + " FROM " + " fox_log " + " ORDER BY " + " \"when\""); + + SQL_error_check (rate_n_query_, &QSqlQuery::prepare, + "SELECT " + " \"when\"" + " FROM " + " fox_log " + " ORDER BY " + " \"when\" DESC" + " LIMIT :limitn" + ); + + SQL_error_check (rate60m_query_, &QSqlQuery::prepare, + "SELECT " + " COUNT() " + " FROM " + " fox_log " + " where \"when\" > :one_hour_ago" + " ORDER BY " + " \"when\" DESC" + ); + 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); +} + +QString FoxLog::rate() +{ + return QString("Last 10: %1, Last 100: %2, Last 60m: %3").arg(QString::number(this->rate_last_n(10),'f',0), + QString::number(this->rate_last_n(100),'f',0), + QString::number(this->rate_60m())); +} + +FoxLog::FoxLog (Configuration const * configuration) + : m_ {configuration} +{ +} + +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 () +{ + // synchronize model + while (m_->canFetchMore ()) m_->fetchMore (); + 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); + } +} + +int FoxLog::rate_60m() +{ + int rate60m = 0; + qlonglong const& one_hour_ago = QDateTime::currentDateTime().addSecs(-3600).toMSecsSinceEpoch () / 1000; + + // query the 60m rate + m_->rate60m_query_.bindValue (":one_hour_ago", one_hour_ago); + SQL_error_check (m_->rate60m_query_, static_cast<bool (QSqlQuery::*) ()> (&QSqlQuery::exec)); + m_->rate60m_query_.next (); + rate60m = m_->rate60m_query_.value (0).toLongLong(); + return rate60m; + // +} + +double FoxLog::rate_last_n(int n) +{ + double rate_interval = 0; + + qlonglong const& secs_now = QDateTime::currentDateTime().toMSecsSinceEpoch () / 1000; + + // get last n or up to n + m_->rate_n_query_.bindValue (":limitn", n); + SQL_error_check (m_->rate_n_query_, static_cast<bool (QSqlQuery::*) ()> (&QSqlQuery::exec)); + + m_->rate_n_query_.next(); + if (!m_->rate_n_query_.isValid()) { + LOG_ERROR(QString("rate_n result is not valid. Last error %1").arg(m_->rate_n_query_.lastError().text())); + return 0.0; + } + // size / (time_now - time_of_first) + m_->rate_n_query_.last(); + rate_interval = secs_now - m_->rate_n_query_.value (0).toLongLong (); + if (rate_interval == 0) return 0.0; + + m_->rate_n_query_.first(); // count the records + + int size = 1; + while (m_->rate_n_query_.next() && m_->rate_n_query_.isValid()) size++; + return (size/rate_interval) * 3600; +} + +namespace +{ + struct ADIF_field + { + explicit ADIF_field (QString const& name, QString const& value) + : name_ {name} + , value_ {value} + { + } + + QString name_; + QString value_; + }; + + QTextStream& operator << (QTextStream& os, ADIF_field const& field) + { + if (field.value_.size ()) + { + os << QString {"<%1:%2>%3 "}.arg (field.name_).arg (field.value_.size ()).arg (field.value_); + } + return os; + } +} + +void FoxLog::export_qsos (QTextStream& out) const +{ + out << "WSJT-X FT8 DXpedition Mode Fox Log\n<eoh>"; + + SQL_error_check (m_->export_query_, static_cast<bool (QSqlQuery::*) ()> (&QSqlQuery::exec)); + auto record = m_->export_query_.record (); + auto band_index = record.indexOf ("band"); + auto when_index = record.indexOf ("when"); + auto call_index = record.indexOf ("call"); + auto grid_index = record.indexOf ("grid"); + auto sent_index = record.indexOf ("report_sent"); + auto rcvd_index = record.indexOf ("report_rcvd"); + while (m_->export_query_.next ()) + { + auto when = QDateTime::fromMSecsSinceEpoch (m_->export_query_.value (when_index).toULongLong () * 1000ull, Qt::UTC); + out << '\n' + << ADIF_field {"band", m_->export_query_.value (band_index).toString ()} + << ADIF_field {"mode", "FT8"} + << ADIF_field {"qso_date", when.toString ("yyyyMMdd")} + << ADIF_field {"time_on", when.toString ("hhmmss")} + << ADIF_field {"call", m_->export_query_.value (call_index).toString ()} + << ADIF_field {"gridsquare", m_->export_query_.value (grid_index).toString ()} + << ADIF_field {"rst_sent", m_->export_query_.value (sent_index).toString ()} + << ADIF_field {"rst_rcvd", m_->export_query_.value (rcvd_index).toString ()} + << ADIF_field {"station_callsign", m_->configuration_->my_callsign ()} + << ADIF_field {"my_gridsquare", m_->configuration_->my_grid ()} + << ADIF_field {"operator", m_->configuration_->opCall ()} + << "<eor>"; + } +#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0) + out << endl; +#else + out << Qt::endl; +#endif +} diff --git a/models/FoxLog.hpp b/models/FoxLog.hpp index 850ac1f1..faf04adc 100755..100644 --- a/models/FoxLog.hpp +++ b/models/FoxLog.hpp @@ -1,38 +1,38 @@ -#ifndef FOX_LOG_HPP_
-#define FOX_LOG_HPP_
-
-#include <boost/core/noncopyable.hpp>
-#include "pimpl_h.hpp"
-
-class QDateTime;
-class QString;
-class QSqlTableModel;
-class QTextStream;
-class Configuration;
-
-class FoxLog final
- : private boost::noncopyable
-{
-public:
- explicit FoxLog (Configuration const *);
- ~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 ();
- void export_qsos (QTextStream&) const;
- QString rate();
- double rate_last_n(int n); // get the rate for the last n
- int rate_60m();
-
-private:
- class impl;
- pimpl<impl> m_;
-};
-
-#endif
+#ifndef FOX_LOG_HPP_ +#define FOX_LOG_HPP_ + +#include <boost/core/noncopyable.hpp> +#include "pimpl_h.hpp" + +class QDateTime; +class QString; +class QSqlTableModel; +class QTextStream; +class Configuration; + +class FoxLog final + : private boost::noncopyable +{ +public: + explicit FoxLog (Configuration const *); + ~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 (); + void export_qsos (QTextStream&) const; + QString rate(); + double rate_last_n(int n); // get the rate for the last n + int rate_60m(); + +private: + class impl; + pimpl<impl> m_; +}; + +#endif diff --git a/models/FrequencyList.cpp b/models/FrequencyList.cpp index be1e2e47..a0d22942 100755..100644 --- a/models/FrequencyList.cpp +++ b/models/FrequencyList.cpp @@ -1,1464 +1,1464 @@ -#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 <QTextStream>
-#include <QDataStream>
-#include <QByteArray>
-#include <QDebugStateSaver>
-#include <QJsonObject>
-#include <QJsonDocument>
-#include <QJsonArray>
-#include <QCoreApplication>
-#include <QFile>
-#include <QException>
-
-#include "Radio.hpp"
-#include "Bands.hpp"
-#include "pimpl_impl.hpp"
-#include "revision_utils.hpp"
-#include "Logger.hpp"
-
-#include "moc_FrequencyList.cpp"
-
-namespace
-{
- FrequencyList_v2_101::FrequencyItems const default_frequency_list =
- {
- {198000, Modes::FreqCal, IARURegions::R1, "","", QDateTime(), QDateTime(), false}, // BBC Radio 4 Droitwich
- {4996000, Modes::FreqCal, IARURegions::R1, "","", QDateTime(), QDateTime(), false}, // RWM time signal
- {9996000, Modes::FreqCal, IARURegions::R1, "","", QDateTime(), QDateTime(), false}, // RWM time signal
- {14996000, Modes::FreqCal, IARURegions::R1, "","", QDateTime(), QDateTime(), false}, // RWM time signal
-
- {660000, Modes::FreqCal, IARURegions::R2, "","", QDateTime(), QDateTime(), false},
- {880000, Modes::FreqCal, IARURegions::R2, "","", QDateTime(), QDateTime(), false},
- {1210000, Modes::FreqCal, IARURegions::R2, "","", QDateTime(), QDateTime(), false},
-
- {2500000, Modes::FreqCal, IARURegions::ALL, "","", QDateTime(), QDateTime(), false},
- {3330000, Modes::FreqCal, IARURegions::ALL, "","", QDateTime(), QDateTime(), false},
- {5000000, Modes::FreqCal, IARURegions::ALL, "","", QDateTime(), QDateTime(), false},
- {7850000, Modes::FreqCal, IARURegions::ALL, "","", QDateTime(), QDateTime(), false},
- {10000000, Modes::FreqCal, IARURegions::ALL, "","", QDateTime(), QDateTime(), false},
- {14670000, Modes::FreqCal, IARURegions::ALL, "","", QDateTime(), QDateTime(), false},
- {15000000, Modes::FreqCal, IARURegions::ALL, "","", QDateTime(), QDateTime(), false},
- {20000000, Modes::FreqCal, IARURegions::ALL, "","", QDateTime(), QDateTime(), false},
-
- {136000, Modes::WSPR, IARURegions::ALL, "","", QDateTime(), QDateTime(), false},
- {136000, Modes::FST4, IARURegions::ALL, "","", QDateTime(), QDateTime(), false},
- {136000, Modes::FST4W, IARURegions::ALL, "","", QDateTime(), QDateTime(), false},
- {136000, Modes::JT9, IARURegions::ALL,"","", QDateTime(), QDateTime(), false},
-
- {474200, Modes::JT9, IARURegions::ALL, "","", QDateTime(), QDateTime(), false},
- {474200, Modes::FST4, IARURegions::ALL, "","", QDateTime(), QDateTime(), false},
- {474200, Modes::WSPR, IARURegions::ALL, "","", QDateTime(), QDateTime(), false},
- {474200, Modes::FST4W, IARURegions::ALL, "","", QDateTime(), QDateTime(), false},
-
- {1836600, Modes::WSPR, IARURegions::ALL, "","", QDateTime(), QDateTime(), false},
- {1836800, Modes::FST4W, IARURegions::ALL, "","", QDateTime(), QDateTime(), false},
- {1838000, Modes::JT65, IARURegions::ALL, "","", QDateTime(), QDateTime(), false}, // squeezed allocations
- {1839000, Modes::JT9, IARURegions::ALL, "","", QDateTime(), QDateTime(), false},
- {1839000, Modes::FST4, IARURegions::ALL, "","", QDateTime(), QDateTime(), false},
- {1840000, Modes::FT8, IARURegions::ALL, "","", QDateTime(), QDateTime(), true},
-
- // Band plans (all USB dial unless stated otherwise)
- //
- // R1: 3570 - 3580 DM NB(<200Hz)
- // 3580 - 3600 DM NB(<500Hz) with 3590 - 3600 ACDS
- //
- // 3577.75 OLIVIA, Contestia, etc.
- // 3580 PSK31
- // 3583.25 OLIVIA, Contestia, etc.
- //
- // R2: 3570 - 3580 DM NB(<200Hz)
- // 3580 - 3600 DM NB(<500Hz) with 3590 - 3600 ACDS
- //
- // 3577.75 OLIVIA, Contestia, etc.
- // 3580 PSK31
- // 3583.25 OLIVIA, Contestia, etc.
- // 3590 RTTY DX
- // 3596 W1AW DM QST
- //
- // R3: 3535 - 3580 DM NB(<2000Hz)
- //
- // 3520 - 3575 DM NB(<2000Hz) JA 3535 - 3575 shared with all modes
- //
- // 3522 OLIVIA, Contestia, etc.
- // 3535 JA LSB EMCOMM
- // 3580 PSK31
- // 3600 LSB EMCOMM
- //
- {3567000, Modes::FT8, IARURegions::ALL, "","", QDateTime(), QDateTime(), false}, // DXpedition
- {3570000, Modes::JT65, IARURegions::ALL, "","", QDateTime(), QDateTime(), false}, // JA compatible
- {3572000, Modes::JT9, IARURegions::ALL, "","", QDateTime(), QDateTime(), false},
- {3573000, Modes::FT8, IARURegions::ALL, "","", QDateTime(), QDateTime(), true}, // above as below JT65 is out of DM allocation
- {3568600, Modes::WSPR, IARURegions::ALL, "","", QDateTime(), QDateTime(), false}, // needs guard marker and lock out
- {3568600, Modes::FST4W, IARURegions::ALL, "","", QDateTime(), QDateTime(), false}, // needs guard marker and lock out
- {3575000, Modes::FT4, IARURegions::ALL, "","", QDateTime(), QDateTime(), true}, // provisional
- {3568000, Modes::FT4, IARURegions::R3, "","", QDateTime(), QDateTime(), false}, // provisional
-
- {5357000, Modes::JT65, IARURegions::ALL, "","", QDateTime(), QDateTime(), false}, // provisional
- {5357000, Modes::JT9, IARURegions::ALL, "","", QDateTime(), QDateTime(), false}, // provisional
- {5357000, Modes::FT8, IARURegions::ALL, "","", QDateTime(), QDateTime(), true}, // provisional
- {5357000, Modes::FT4, IARURegions::ALL, "","", QDateTime(), QDateTime(), true}, // provisional
-
- // Band plans (all USB dial unless stated otherwise)
- //
- // R1: 7040 - 7050 DM NB(<500Hz) with 7047 - 7050 ACDS
- // 7050 - 7060 DM WB(<2700Hz) with 7050 - 7053 ACDS
- //
- // 7040 PSK31
- // 7043.25 OLIVIA, Contestia, etc. (main QRG)
- // 7070 PSK31
- // 7073.25 OLIVIA, Contestia, etc. (main QRG)
- // 7090 LSB QRP CoA
- //
- // R2: 7040 - 7050 DM NB(<500Hz) with 7047 - 7050 ACDS
- // 7050 - 7053 DM WB(<2700Hz) ACDS shared with all modes
- //
- // 7040 RTTY DX
- // 7043.25 OLIVIA, Contestia, etc. (main QRG)
- // 7070 PSK31 (also LSB EMCOMM)
- // 7073.25 OLIVIA, Contestia, etc. (main QRG)
- // 7080 - 7125 RTTY/Data
- // 7090 LSB QRP CoA
- //
- // R3: 7030 - 7060 DM NB(<2000Hz) with 7040 - 7060 NB DX all shared with phone
- //
- // 7030 - 7100 DM WB(<3000Hz) JA 7045 - 7100 shared with all modes
- //
- // 7026.25 OLIVIA, Contestia, etc. (main QRG)
- // 7035 PSK31
- // 7050 JA LSB EMCOMM
- // 7090 LSB QRP CoA
- // 7110 LSB EMCOMM
- //
- {7038600, Modes::WSPR, IARURegions::ALL, "","", QDateTime(), QDateTime(), false},
- {7038600, Modes::FST4W, IARURegions::ALL, "","", QDateTime(), QDateTime(), false},
- {7056000, Modes::FT8, IARURegions::ALL, "","", QDateTime(), QDateTime(), false}, // DXpedition
- {7074000, Modes::FT8, IARURegions::ALL, "","", QDateTime(), QDateTime(), true},
- {7076000, Modes::JT65, IARURegions::ALL, "","", QDateTime(), QDateTime(), false},
- {7078000, Modes::JT9, IARURegions::ALL, "","", QDateTime(), QDateTime(), false},
- {7047500, Modes::FT4, IARURegions::ALL, "","", QDateTime(), QDateTime(), true}, // provisional - moved
- // up 500Hz to clear
- // W1AW code practice QRG
-
- // Band plans (all USB dial unless stated otherwise)
- //
- // R1: 10130 - 10150 DM NB(<500Hz) with 10120 - 10140 shared with phone in southern Africa
- //
- // 10139.25 OLIVIA, Contestia, etc.
- // 10142 PSK31
- // 10142.25 OLIVIA, Contestia, etc.
- // 10143.25 OLIVIA, Contestia, etc. (main QRG)
- //
- // R2: 10130 - 10140 DM NB(<500Hz) shared with ACDS
- // 10140 - 10150 DM WB(<2700Hz)
- //
- // 10130 - 10140 RTTY
- // 10139.25 OLIVIA, Contestia, etc.
- // 10140 - 10150 Packet
- // 10142 PSK31
- // 10142.25 OLIVIA, Contestia, etc.
- // 10143.25 OLIVIA, Contestia, etc. (main QRG)
- //
- // R3: 10130 - 10150 DM NB(<2000Hz)
- //
- // 10139.25 OLIVIA, Contestia, etc.
- // 10142 PSK31
- // 10142.25 OLIVIA, Contestia, etc.
- // 10143.25 OLIVIA, Contestia, etc. (main QRG)
- //
- {10131000, Modes::FT8, IARURegions::ALL, "","", QDateTime(), QDateTime(), false}, // DXpedition
- {10136000, Modes::FT8, IARURegions::ALL, "","", QDateTime(), QDateTime(), true},
- {10138000, Modes::JT65, IARURegions::ALL, "","", QDateTime(), QDateTime(), false},
- {10138700, Modes::WSPR, IARURegions::ALL, "","", QDateTime(), QDateTime(), false},
- {10138700, Modes::FST4W, IARURegions::ALL, "","", QDateTime(), QDateTime(), false},
- {10140000, Modes::JT9, IARURegions::ALL, "","", QDateTime(), QDateTime(), false},
- {10140000, Modes::FT4, IARURegions::ALL, "","", QDateTime(), QDateTime(), true}, // provisional
-
- // Band plans (all USB dial unless stated otherwise)
- //
- // R1: 14070 - 14099 DM NB(<500Hz) with 14089 - 14099 ACDS
- // 14101 - 14112 DM NB(<2700Hz) ACDS
- //
- // 14070 PSK31
- // 14074.4 OLIVIA, Contestia, etc.
- // 14075.4 OLIVIA, Contestia, etc. (main QRG)
- // 14078.4 OLIVIA, Contestia, etc.
- // 14100 NCDXF beacons
- // 14105.5 OLIVIA 1000
- // 14106.5 OLIVIA 1000 (main QRG)
- //
- // R2: 14070 - 14099 DM NB(<500Hz) with 14089 - 14099 ACDS
- // 14101 - 14112 DM NB(<2700Hz) ACDS
- //
- // 14070 - 14095 RTTY
- // 14070 PSK31
- // 14074.4 OLIVIA, Contestia, etc.
- // 14075.4 OLIVIA, Contestia, etc. (main QRG)
- // 14078.4 OLIVIA, Contestia, etc.
- // 14095 - 14099.5 Packet
- // 14100 NCDXF beacons
- // 14100.5 - 14112 Packet
- // 14105.5 OLIVIA 1000
- // 14106.5 OLIVIA 1000 (main QRG)
- //
- // R3: 14070 - 14112 DM NB(<2000Hz) with ±500Hz IBP guard band at 14100
- //
- // 14070 PSK31
- // 14074.4 OLIVIA, Contestia, etc.
- // 14075.4 OLIVIA, Contestia, etc. (main QRG)
- // 14078.4 OLIVIA, Contestia, etc.
- // 14100 NCDXF beacons
- // 14105.5 OLIVIA 1000
- // 14106.5 OLIVIA 1000 (main QRG)
- //
- {14095600, Modes::WSPR, IARURegions::ALL, "","", QDateTime(), QDateTime(), false},
- {14095600, Modes::FST4W, IARURegions::ALL, "","", QDateTime(), QDateTime(), false},
- {14074000, Modes::FT8, IARURegions::ALL, "","", QDateTime(), QDateTime(), true},
- {14090000, Modes::FT8, IARURegions::ALL, "","", QDateTime(), QDateTime(), false}, // DXpedition
- {14076000, Modes::JT65, IARURegions::ALL, "","", QDateTime(), QDateTime(), false},
- {14078000, Modes::JT9, IARURegions::ALL, "","", QDateTime(), QDateTime(), false},
- {14080000, Modes::FT4, IARURegions::ALL, "","", QDateTime(), QDateTime(), true}, // provisional
-
-
- // Band plans (all USB dial unless stated otherwise)
- //
- // R1: 18095 - 18109 DM NB(<500Hz) with 18105 - 18109 ACDS
- // 18111 - 18120 DM NB(<2700Hz) ACDS
- //
- // 18100 PSK31
- // 18103.4 OLIVIA, Contestia, etc. (main QRG)
- // 18104.4 OLIVIA, Contestia, etc.
- // 18110 NCDXF beacons
- //
- // R2: 18095 - 18109 DM NB(<500Hz) with 18105 - 18109 ACDS
- // 18111 - 18120 DM NB(<2700Hz) ACDS
- //
- // 18100 - 18105 RTTY
- // 18100 PSK31
- // 18103.4 OLIVIA, Contestia, etc. (main QRG)
- // 18104.4 OLIVIA, Contestia, etc.
- // 18105 - 18110 Packet
- // 18110 NCDXF beacons
- //
- // R3: 18095 - 18120 DM NB(<2000Hz) with ±500Hz IBP guard band at 18110
- //
- // 18100 PSK31
- // 18103.4 OLIVIA, Contestia, etc. (main QRG)
- // 18104.4 OLIVIA, Contestia, etc.
- // 18110 NCDXF beacons
- //
- {18095000, Modes::FT8, IARURegions::ALL, "","", QDateTime(), QDateTime(), false}, // DXpedition
- {18100000, Modes::FT8, IARURegions::ALL, "","", QDateTime(), QDateTime(), true},
- {18102000, Modes::JT65, IARURegions::ALL, "","", QDateTime(), QDateTime(), false},
- {18104000, Modes::JT9, IARURegions::ALL, "","", QDateTime(), QDateTime(), false},
- {18104000, Modes::FT4, IARURegions::ALL, "","", QDateTime(), QDateTime(), true}, // provisional
- {18104600, Modes::WSPR, IARURegions::ALL, "","", QDateTime(), QDateTime(), false},
- {18104600, Modes::FST4W, IARURegions::ALL, "","", QDateTime(), QDateTime(), false},
-
- {21074000, Modes::FT8, IARURegions::ALL, "","", QDateTime(), QDateTime(), true},
- {21091000, Modes::FT8, IARURegions::ALL, "","", QDateTime(), QDateTime(), false}, // DXpedition
- {21076000, Modes::JT65, IARURegions::ALL, "","", QDateTime(), QDateTime(), false},
- {21078000, Modes::JT9, IARURegions::ALL, "","", QDateTime(), QDateTime(), false},
- {21094600, Modes::WSPR, IARURegions::ALL, "","", QDateTime(), QDateTime(), false},
- {21094600, Modes::FST4W, IARURegions::ALL, "","", QDateTime(), QDateTime(), false},
- {21140000, Modes::FT4, IARURegions::ALL, "","", QDateTime(), QDateTime(), true},
-
- {24911000, Modes::FT8, IARURegions::ALL, "","", QDateTime(), QDateTime(), false}, // DXpedition
- {24915000, Modes::FT8, IARURegions::ALL, "","", QDateTime(), QDateTime(), true},
- {24917000, Modes::JT65, IARURegions::ALL, "","", QDateTime(), QDateTime(), false},
- {24919000, Modes::JT9, IARURegions::ALL, "","", QDateTime(), QDateTime(), false},
- {24919000, Modes::FT4, IARURegions::ALL, "","", QDateTime(), QDateTime(), true}, // provisional
- {24924600, Modes::WSPR, IARURegions::ALL, "","", QDateTime(), QDateTime(), false},
- {24924600, Modes::FST4W, IARURegions::ALL, "","", QDateTime(), QDateTime(), false},
-
- {28074000, Modes::FT8, IARURegions::ALL, "","", QDateTime(), QDateTime(), true},
- {28091000, Modes::FT8, IARURegions::ALL, "","", QDateTime(), QDateTime(), false}, // DXpedition
- {28076000, Modes::JT65, IARURegions::ALL, "","", QDateTime(), QDateTime(), false},
- {28078000, Modes::JT9, IARURegions::ALL, "","", QDateTime(), QDateTime(), false},
- {28124600, Modes::WSPR, IARURegions::ALL, "","", QDateTime(), QDateTime(), false},
- {28124600, Modes::FST4W, IARURegions::ALL, "","", QDateTime(), QDateTime(), false},
- {28180000, Modes::FT4, IARURegions::ALL, "","", QDateTime(), QDateTime(), true},
-
- {50200000, Modes::Echo, IARURegions::ALL, "","", QDateTime(), QDateTime(), false},
- {50211000, Modes::Q65, IARURegions::ALL, "EME","", QDateTime(), QDateTime(), false},
- {50275000, Modes::Q65, IARURegions::ALL, "","", QDateTime(), QDateTime(), true},
- {50276000, Modes::JT65, IARURegions::R2, "","", QDateTime(), QDateTime(), false},
- {50276000, Modes::JT65, IARURegions::R3, "","", QDateTime(), QDateTime(), false},
- {50380000, Modes::MSK144, IARURegions::R1, "","", QDateTime(), QDateTime(), false},
- {50260000, Modes::MSK144, IARURegions::R2, "","", QDateTime(), QDateTime(), false},
- {50260000, Modes::MSK144, IARURegions::R3, "","", QDateTime(), QDateTime(), false},
- {50293000, Modes::WSPR, IARURegions::R2, "","", QDateTime(), QDateTime(), false},
- {50293000, Modes::WSPR, IARURegions::R3, "","", QDateTime(), QDateTime(), false},
- {50293000, Modes::FST4W, IARURegions::R2, "","", QDateTime(), QDateTime(), false},
- {50293000, Modes::FST4W, IARURegions::R3, "","", QDateTime(), QDateTime(), false},
- {50310000, Modes::JT65, IARURegions::ALL, "","", QDateTime(), QDateTime(), false},
- {50312000, Modes::JT9, IARURegions::ALL, "","", QDateTime(), QDateTime(), false},
- {50313000, Modes::FT8, IARURegions::ALL, "","", QDateTime(), QDateTime(), true},
- {50318000, Modes::FT4, IARURegions::ALL, "","", QDateTime(), QDateTime(), true}, // provisional
- {50323000, Modes::FT8, IARURegions::ALL, "","", QDateTime(), QDateTime(), false}, // intercontinental
-
- {70102000, Modes::JT65, IARURegions::R1, "","", QDateTime(), QDateTime(), false},
- {70104000, Modes::JT9, IARURegions::R1, "","", QDateTime(), QDateTime(), false},
- {70091000, Modes::WSPR, IARURegions::R1, "","", QDateTime(), QDateTime(), false},
- {70091000, Modes::FST4W, IARURegions::R1, "","", QDateTime(), QDateTime(), false},
- {70154000, Modes::FT8, IARURegions::R1, "","", QDateTime(), QDateTime(), true},
- {70230000, Modes::MSK144, IARURegions::R1, "","", QDateTime(), QDateTime(), false},
-
- {144116000, Modes::Q65, IARURegions::ALL, "EME","", QDateTime(), QDateTime(), false},
- {144120000, Modes::JT65, IARURegions::ALL, "","", QDateTime(), QDateTime(), false},
- {144120000, Modes::Echo, IARURegions::ALL, "","", QDateTime(), QDateTime(), false},
- {144170000, Modes::FT4, IARURegions::ALL, "","", QDateTime(), QDateTime(), true},
- {144174000, Modes::FT8, IARURegions::ALL, "","", QDateTime(), QDateTime(), true},
- {144180000, Modes::Q65, IARURegions::ALL, "","", QDateTime(), QDateTime(), true},
- {144360000, Modes::MSK144, IARURegions::R1, "","", QDateTime(), QDateTime(), false},
- {144150000, Modes::MSK144, IARURegions::R2, "","", QDateTime(), QDateTime(), false},
- {144489000, Modes::WSPR, IARURegions::ALL,"","", QDateTime(), QDateTime(), false},
- {144489000, Modes::FST4W, IARURegions::ALL,"","", QDateTime(), QDateTime(), false},
-
- {222065000, Modes::Echo, IARURegions::R2, "","", QDateTime(), QDateTime(), false},
- {222065000, Modes::JT65, IARURegions::R2, "","", QDateTime(), QDateTime(), false},
- {222065000, Modes::Q65, IARURegions::R2, "","", QDateTime(), QDateTime(), false},
- {222174000, Modes::FT8, IARURegions::R2, "","", QDateTime(), QDateTime(), true},
-
- {432065000, Modes::Echo, IARURegions::ALL, "","", QDateTime(), QDateTime(), false},
- {432065000, Modes::JT65, IARURegions::ALL, "","", QDateTime(), QDateTime(), false},
- {432174000, Modes::FT8, IARURegions::ALL, "","", QDateTime(), QDateTime(), true},
- {432300000, Modes::WSPR, IARURegions::ALL,"","", QDateTime(), QDateTime(), false},
- {432360000, Modes::MSK144, IARURegions::ALL, "","", QDateTime(), QDateTime(), false},
- {432065000, Modes::Q65, IARURegions::ALL,"EME","", QDateTime(), QDateTime(), false},
-
- {902065000, Modes::JT65, IARURegions::R2, "","", QDateTime(), QDateTime(), false},
- {902065000, Modes::Q65, IARURegions::R2, "","", QDateTime(), QDateTime(), false},
-
- {1296065000, Modes::Echo, IARURegions::ALL,"","", QDateTime(), QDateTime(), false},
- {1296065000, Modes::JT65, IARURegions::ALL,"","", QDateTime(), QDateTime(), false},
- {1296500000, Modes::WSPR, IARURegions::ALL,"","", QDateTime(), QDateTime(), false},
- {1296065000, Modes::Q65, IARURegions::ALL,"","", QDateTime(), QDateTime(), false},
- {1296174000, Modes::FT8, IARURegions::ALL, "","", QDateTime(), QDateTime(), true},
-
- {2301000000, Modes::Echo, IARURegions::ALL, "","", QDateTime(), QDateTime(), false},
- {2301065000, Modes::JT4, IARURegions::ALL,"","", QDateTime(), QDateTime(), false},
- {2301065000, Modes::JT65, IARURegions::ALL,"","", QDateTime(), QDateTime(), false},
- {2301065000, Modes::Q65, IARURegions::ALL, "","", QDateTime(), QDateTime(), false},
-
- {2304065000, Modes::Echo, IARURegions::ALL, "","", QDateTime(), QDateTime(), false},
- {2304065000, Modes::JT4, IARURegions::ALL,"","", QDateTime(), QDateTime(), false},
- {2304065000, Modes::JT65, IARURegions::ALL,"","", QDateTime(), QDateTime(), false},
- {2304065000, Modes::Q65, IARURegions::ALL,"","", QDateTime(), QDateTime(), false},
-
- {2320065000, Modes::Echo, IARURegions::ALL,"","", QDateTime(), QDateTime(), false},
- {2320065000, Modes::JT4, IARURegions::ALL,"","", QDateTime(), QDateTime(), false},
- {2320065000, Modes::JT65, IARURegions::ALL,"","", QDateTime(), QDateTime(), false},
- {2320065000, Modes::Q65, IARURegions::ALL,"","", QDateTime(), QDateTime(), false},
- {2400040000, Modes::ALL, IARURegions::ALL,"QO-100 Uplink","", QDateTime(), QDateTime(), false},
-
- {3400065000, Modes::Echo, IARURegions::ALL,"","", QDateTime(), QDateTime(), false},
- {3400065000, Modes::JT4, IARURegions::ALL,"","", QDateTime(), QDateTime(), false},
- {3400065000, Modes::JT65, IARURegions::ALL,"","", QDateTime(), QDateTime(), false},
- {3400065000, Modes::Q65, IARURegions::ALL,"","", QDateTime(), QDateTime(), false},
-
- {5760065000, Modes::Echo, IARURegions::ALL,"","", QDateTime(), QDateTime(), false},
- {5760065000, Modes::JT4, IARURegions::ALL,"","", QDateTime(), QDateTime(), false},
- {5760065000, Modes::JT65, IARURegions::ALL,"","", QDateTime(), QDateTime(), false},
- {5760200000, Modes::Q65, IARURegions::ALL,"","", QDateTime(), QDateTime(), false},
-
- {10368100000, Modes::Echo, IARURegions::ALL,"","", QDateTime(), QDateTime(), false},
- {10368200000, Modes::JT4, IARURegions::ALL,"","", QDateTime(), QDateTime(), false},
- {10368200000, Modes::Q65, IARURegions::ALL,"","", QDateTime(), QDateTime(), false},
- {10489540000, Modes::ALL, IARURegions::ALL,"QO-100 Downlink","", QDateTime(), QDateTime(), false},
-
- {24048100000, Modes::Echo, IARURegions::ALL,"","", QDateTime(), QDateTime(), false},
- {24048200000, Modes::JT4, IARURegions::ALL,"","", QDateTime(), QDateTime(), false},
- {24048200000, Modes::Q65, IARURegions::ALL,"","", QDateTime(), QDateTime(), false},
- };
-}
-
-#if !defined (QT_NO_DEBUG_STREAM)
-QDebug operator << (QDebug debug, FrequencyList_v2_101::Item const& item)
-{
- QDebugStateSaver saver {debug};
- return debug.nospace () << item.toString ();
-}
-#endif
-bool FrequencyList_v2_101::Item::isSane() const
-{
- return frequency_ > 0.0 && (!start_time_.isValid() || !end_time_.isValid() || start_time_ < end_time_)
- && (region_ == IARURegions::ALL || region_ == IARURegions::R1 || region_ == IARURegions::R2 || region_ == IARURegions::R3);
-}
-
-QString FrequencyList_v2_101::Item::toString () const
-{
- QString string;
- QTextStream qts {&string};
- qts << "FrequencyItem("
- << Radio::frequency_MHz_string (frequency_) << ", "
- << IARURegions::name (region_) << ", "
- << Modes::name (mode_) << ", "
- << start_time_.toString(Qt::ISODate) << ", "
- << end_time_.toString(Qt::ISODate) << ", "
- << description_ << ", "
- << source_ << ","
- << preferred_ << ')';
- return string;
-}
-
-QJsonObject FrequencyList_v2_101::Item::toJson() const {
- return {{"frequency", Radio::frequency_MHz_string (frequency_) },
- {"mode", Modes::name (mode_) },
- {"region", IARURegions::name (region_)},
- {"description", description_},
- {"source", source_},
- {"start_time", start_time_.toString(Qt::ISODate) },
- {"end_time", end_time_.toString(Qt::ISODate) },
- {"preferred", preferred_}};
-}
-
-QDataStream& operator << (QDataStream& os, FrequencyList_v2_101::Item const& item)
-{
- return os << item.frequency_
- << item.mode_
- << item.region_
- << item.start_time_
- << item.end_time_
- << item.description_
- << item.source_
- << item.preferred_;
-}
-
-QDataStream& operator >> (QDataStream& is, FrequencyList_v2_101::Item& item)
-{
- return is >> item.frequency_
- >> item.mode_
- >> item.region_
- >> item.start_time_
- >> item.end_time_
- >> item.description_
- >> item.source_
- >> item.preferred_;
-}
-
-class FrequencyList_v2_101::impl final
- : public QAbstractTableModel
-{
-public:
- impl (Bands const * bands, QObject * parent)
- : QAbstractTableModel {parent}
- , bands_ {bands}
- , region_filter_ {IARURegions::ALL}
- , mode_filter_ {Modes::ALL}
- , filter_on_time_ {false}
- {
- }
-
- 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;
-
- void unprefer_all_but(Item & item, int const row, QVector<int> );
-
- 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_;
- bool filter_on_time_;
-
-};
-
-FrequencyList_v2_101::FrequencyList_v2_101 (Bands const * bands, QObject * parent)
- : QSortFilterProxyModel {parent}
- , m_ {bands, parent}
-{
- setSourceModel (&*m_);
- setSortRole (SortRole);
-}
-
-FrequencyList_v2_101::~FrequencyList_v2_101 ()
-{
-}
-
-auto FrequencyList_v2_101::frequency_list (FrequencyItems frequency_list) -> FrequencyItems
-{
- return m_->frequency_list (frequency_list);
-}
-
-auto FrequencyList_v2_101::frequency_list () const -> FrequencyItems const&
-{
- return m_->frequency_list_;
-}
-
-auto FrequencyList_v2_101::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_101::frequency_list_merge (FrequencyItems const& items)
-{
- m_->add (items);
-}
-
-
-int FrequencyList_v2_101::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 the preferred one
- if (m_->frequency_list_[source_row].preferred_)
- {
- return row;
- }
- // 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_101::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)
- {
- if (m_->frequency_list_[source_row].preferred_)
- return row; // return the preferred one immediately
- result = row;
- }
- }
- }
- return result;
-}
-
-void FrequencyList_v2_101::reset_to_defaults ()
-{
- m_->frequency_list (default_frequency_list);
-}
-
-QModelIndex FrequencyList_v2_101::add (Item f)
-{
- return mapFromSource (m_->add (f));
-}
-
-bool FrequencyList_v2_101::remove (Item f)
-{
- auto row = m_->frequency_list_.indexOf (f);
-
- if (0 > row)
- {
- return false;
- }
-
- return m_->removeRow (row);
-}
-
-bool FrequencyList_v2_101::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 descending 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_101::filter (Region region, Mode mode, bool filter_on_time)
-{
- m_->region_filter_ = region;
- m_->mode_filter_ = mode;
- m_->filter_on_time_ = filter_on_time;
- invalidateFilter ();
-}
-
-void FrequencyList_v2_101::filter_refresh ()
-{
- invalidateFilter ();
-}
-
-bool FrequencyList_v2_101::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_;
- }
- if (result && m_->filter_on_time_)
- {
- result = (!item.start_time_.isValid() || item.start_time_ <= QDateTime::currentDateTimeUtc ()) &&
- (!item.end_time_.isValid() || item.end_time_ >= QDateTime::currentDateTimeUtc ());
- }
- return result;
-}
-
-
-auto FrequencyList_v2_101::impl::frequency_list (FrequencyItems frequency_list) -> FrequencyItems
-{
- beginResetModel ();
- std::swap (frequency_list_, frequency_list);
- endResetModel ();
- return frequency_list;
-}
-
-// add a frequency returning the new model index
-QModelIndex FrequencyList_v2_101::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 ();
-
- // if we added one that had a preferred frequency, unprefer everything else
- if (f.preferred_) unprefer_all_but(f, row, {Qt::DisplayRole, Qt::CheckStateRole});
-
- return index (row, 0);
- }
- return QModelIndex {};
-}
-
-void FrequencyList_v2_101::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_101::impl::rowCount (QModelIndex const& parent) const
-{
- return parent.isValid () ? 0 : frequency_list_.size ();
-}
-
-int FrequencyList_v2_101::impl::columnCount (QModelIndex const& parent) const
-{
- return parent.isValid () ? 0 : num_cols;
-}
-
-Qt::ItemFlags FrequencyList_v2_101::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;
- }
-
- if (preferred_column == column)
- {
- result |= Qt::ItemIsUserCheckable;
- }
- }
- return result;
-}
-
-QVariant FrequencyList_v2_101::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_);
- QString desc_text;
- desc_text = frequency_item.description_.isEmpty() ? "" : " \u2502 " + frequency_item.description_;
- item = (frequency_item.preferred_ ? "\u2055 " : "") +
- Radio::pretty_frequency_MHz_string(frequency_item.frequency_)
- + " MHz (" + (band.isEmpty() ? "OOB" : band) + ")" +
- (((frequency_item.start_time_.isValid() && !frequency_item.start_time_.isNull()) ||
- (frequency_item.end_time_.isValid() && !frequency_item.end_time_.isNull())) ? " \u2502 " : "")
- + desc_text;
- }
- break;
-
- case Qt::ToolTipRole:
- case Qt::AccessibleDescriptionRole:
- item = tr ("Frequency (MHz)");
- break;
-
- case Qt::TextAlignmentRole:
- item = Qt::AlignRight + Qt::AlignVCenter;
- break;
- }
- break;
-
- case description_column:
- switch (role)
- {
- case SortRole:
- case Qt::DisplayRole:
- case Qt::EditRole:
- case Qt::AccessibleTextRole:
- item = frequency_item.description_;
- break;
-
- case Qt::ToolTipRole:
- case Qt::AccessibleDescriptionRole:
- item = tr("Description");
- break;
-
- case Qt::TextAlignmentRole:
- item = Qt::AlignLeft + Qt::AlignVCenter;
- break;
- }
- break;
-
- case source_column:
- switch (role)
- {
- case SortRole:
- case Qt::DisplayRole:
- case Qt::EditRole:
- case Qt::AccessibleTextRole:
- item = frequency_item.source_;
- break;
-
- case Qt::ToolTipRole:
- case Qt::AccessibleDescriptionRole:
- item = tr ("Source");
- break;
-
- case Qt::TextAlignmentRole:
- item = Qt::AlignLeft + Qt::AlignVCenter;
- break;
- }
- break;
-
- case start_time_column:
- switch (role)
- {
- case SortRole:
- item = frequency_item.start_time_;
- break;
-
- case Qt::EditRole:
- if (frequency_item.start_time_.isNull () || !frequency_item.start_time_.isValid ())
- {
- item = QDateTime::currentDateTimeUtc ().toString (Qt::ISODate);
- }
- else
- {
- item = frequency_item.start_time_.toString(Qt::ISODate);
- }
- break;
-
- case Qt::DisplayRole:
- case Qt::AccessibleTextRole:
- item = frequency_item.start_time_.toString(Qt::ISODate);
- break;
-
- case Qt::ToolTipRole:
- case Qt::AccessibleDescriptionRole:
- item = tr ("Start Time");
- break;
-
- case Qt::TextAlignmentRole:
- item = Qt::AlignLeft + Qt::AlignVCenter;
- break;
- }
- break;
-
- case end_time_column:
- switch (role)
- {
- case SortRole:
- item = frequency_item.end_time_;
- break;
-
- case Qt::EditRole:
- if (frequency_item.end_time_.isNull () || !frequency_item.end_time_.isValid ())
- {
- item = QDateTime::currentDateTimeUtc ().toString (Qt::ISODate);
- }
- else
- {
- item = frequency_item.end_time_.toString(Qt::ISODate);
- }
- break;
-
- case Qt::DisplayRole:
- case Qt::AccessibleTextRole:
- item = frequency_item.end_time_.toString(Qt::ISODate);
- break;
-
- case Qt::ToolTipRole:
- case Qt::AccessibleDescriptionRole:
- item = tr ("End Time");
- break;
-
- case Qt::TextAlignmentRole:
- item = Qt::AlignLeft + Qt::AlignVCenter;
- break;
- }
- break;
-
- case preferred_column:
- switch (role)
- {
- case SortRole:
- item = frequency_item.preferred_;
- break;
- case Qt::DisplayRole:
- case Qt::EditRole:
- case Qt::AccessibleTextRole:
- //item = frequency_item.preferred_ ? QString("True") : QString("False");
- break;
-
- case Qt::ToolTipRole:
- case Qt::AccessibleDescriptionRole:
- item = tr ("Pref");
- break;
-
- case Qt::TextAlignmentRole:
- item = Qt::AlignHCenter + Qt::AlignVCenter;
- break;
-
- case Qt::CheckStateRole:
- item = frequency_item.preferred_ ? Qt::Checked : Qt::Unchecked;
- break;
- }
- break;
- }
- }
- return item;
-}
-
-void FrequencyList_v2_101::impl::unprefer_all_but(Item &item, int const item_row, QVector<int> roles)
-{
- // un-prefer all of the other frequencies in this band
- auto const band = bands_->find (item.frequency_);
- if (band.isEmpty ()) return; // out of any band
-
- roles << Qt::CheckStateRole;
- roles << Qt::DisplayRole;
-
- for (int row = 0; row < rowCount (); ++row)
- {
- if (row == item_row) continue;
-
- Item &i = frequency_list_[row];
- auto const &iter_band = bands_->find(i.frequency_);
-
- if (!iter_band.isEmpty() && band == iter_band && (i.region_ == item.region_) && (i.mode_ == item.mode_))
- {
- i.preferred_ = false;
- Q_EMIT dataChanged(index(row,preferred_column), index(row,preferred_column), roles);
- }
- }
-}
-
-bool FrequencyList_v2_101::impl::setData (QModelIndex const& model_index, QVariant const& value, int role)
-{
- bool changed {false};
- auto const& row = model_index.row ();
- auto& item = frequency_list_[row];
-
- QVector<int> roles;
- roles << role;
-
- if (model_index.isValid ()
- && Qt::CheckStateRole == role
- && row < frequency_list_.size ()
- && model_index.column () == preferred_column)
- {
- bool b_val = ((Qt::CheckState)value.toInt() == Qt::Checked);
- if (b_val != item.preferred_)
- {
- item.preferred_ = b_val;
- if (item.preferred_)
- {
- unprefer_all_but (item, row, roles); // un-prefer all of the other frequencies in this band
- }
- Q_EMIT dataChanged(index(row,description_column), index(row,preferred_column), roles);
- changed = true;
- }
- }
-
- if (model_index.isValid ()
- && Qt::EditRole == role
- && row < frequency_list_.size ())
- {
- 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;
-
- case description_column:
- {
- if (value.toString() != item.description_)
- {
- item.description_ = value.toString();
- Q_EMIT dataChanged(model_index, model_index, roles);
- changed = true;
- }
- }
- break;
-
- case source_column:
- {
- if (value.toString() != item.source_)
- {
- item.source_ = value.toString();
- Q_EMIT dataChanged(model_index, model_index, roles);
- changed = true;
- }
- }
- break;
-
- case start_time_column:
- {
- QDateTime start_time = QDateTime::fromString(value.toString(), Qt::ISODate);
- LOG_INFO(QString{"start_time = %1 - isEmpty %2"}.arg(value.toString()).arg(value.toString().isEmpty()));
- if (value.toString().isEmpty())
- { // empty string is valid
- start_time = QDateTime();
- }
- if (start_time.isValid() || start_time.isNull())
- {
- item.start_time_ = start_time;
- if (item.end_time_.isValid() && !item.start_time_.isNull() && item.end_time_ < item.start_time_)
- {
- item.end_time_ = item.start_time_;
- }
- Q_EMIT dataChanged(model_index, index(model_index.row(), end_time_column), roles);
- changed = true;
- }
- }
- break;
-
- case end_time_column:
- {
- QDateTime end_time = QDateTime::fromString(value.toString(), Qt::ISODate);
- if (value.toString().isEmpty())
- { // empty string is valid
- end_time = QDateTime();
- }
- if (end_time.isValid() || end_time.isNull())
- {
- item.end_time_ = end_time;
- if (item.start_time_.isValid() && !item.end_time_.isNull() && end_time <= item.start_time_)
- {
- item.start_time_ = end_time;
- }
- Q_EMIT dataChanged(index(model_index.row(), start_time_column), model_index, roles);
- changed = true;
- }
- }
- break;
-
- case preferred_column:
- {
- bool b_value = value.toBool();
- if (b_value != item.preferred_)
- {
- item.preferred_ = b_value;
- Q_EMIT dataChanged(index(model_index.row(), start_time_column), model_index, roles);
- changed = true;
- }
- }
- break;
-
- }
- }
-
- return changed;
-}
-
-QVariant FrequencyList_v2_101::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;
- case source_column: header = tr ("Source"); break;
- case start_time_column: header = tr ("Start Date/Time"); break;
- case end_time_column: header = tr ("End Date/Time"); break;
- case preferred_column: header = tr ("Pref"); break;
- case description_column: header = tr ("Description"); break;
- }
- }
- else
- {
- header = QAbstractTableModel::headerData (section, orientation, role);
- }
- return header;
-}
-
-bool FrequencyList_v2_101::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_101::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, QString(), QString(), QDateTime(), QDateTime(), false});
- }
- endInsertRows ();
- return true;
- }
- return false;
-}
-
-QStringList FrequencyList_v2_101::impl::mimeTypes () const
-{
- QStringList types;
- types << mime_type;
- return types;
-}
-
-QMimeData * FrequencyList_v2_101::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_101::const_iterator::operator * () const -> Item const&
-{
- return parent_->frequency_list ().at(parent_->mapToSource (parent_->index (row_, 0)).row ());
-}
-
-auto FrequencyList_v2_101::const_iterator::operator -> () const -> Item const *
-{
- return &parent_->frequency_list ().at(parent_->mapToSource (parent_->index (row_, 0)).row ());
-}
-
-bool FrequencyList_v2_101::const_iterator::operator != (const_iterator const& rhs) const
-{
- return parent_ != rhs.parent_ || row_ != rhs.row_;
-}
-
-bool FrequencyList_v2_101::const_iterator::operator == (const_iterator const& rhs) const
-{
- return parent_ == rhs.parent_ && row_ == rhs.row_;
-}
-
-auto FrequencyList_v2_101::const_iterator::operator ++ () -> const_iterator&
-{
- ++row_;
- return *this;
-}
-
-auto FrequencyList_v2_101::begin () const -> const_iterator
-{
- return const_iterator (this, 0);
-}
-
-auto FrequencyList_v2_101::end () const -> const_iterator
-{
- return const_iterator (this, rowCount ());
-}
-
-auto FrequencyList_v2_101::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_101::filtered_bands () const -> BandSet
-{
- BandSet result;
- for (auto const& item : *this)
- {
- result << m_->bands_->find (item.frequency_);
- }
- return result;
-}
-
-auto FrequencyList_v2_101::all_bands (Region region, Mode mode) const -> BandSet
-{
- BandSet result;
- for (auto const& item : m_->frequency_list_)
- {
- // Match frequencies that are for all regions, for the specified
- // region (which can also be "all"), and where the mode matches
- // the specified mode (which can also be "all").
- if ((region == IARURegions::ALL || item.region_ == IARURegions::ALL || item.region_ == region)
- && (mode == Modes::ALL || item.mode_ == Modes::ALL || item.mode_ == mode))
- {
- result << m_->bands_->find (item.frequency_);
- }
- }
- return result;
-}
-
-FrequencyList_v2_101::FrequencyItems FrequencyList_v2_101::from_json_file(QFile *input_file)
-{
- FrequencyList_v2_101::FrequencyItems list;
- QJsonDocument doc = QJsonDocument::fromJson(input_file->readAll());
- if (doc.isNull())
- {
- throw ReadFileException {tr ("Failed to parse JSON file")};
- }
- QJsonObject obj = doc.object();
- if (obj.isEmpty())
- {
- throw ReadFileException{tr("Information Missing")};
- }
- QJsonArray arr = obj["frequencies"].toArray();
- if (arr.isEmpty())
- {
- throw ReadFileException{tr ("No Frequencies were found")};
- }
-#ifdef DUMP_ENTRY_COUNTS
- int valid_entry_count = 0;
- int skipped_entry_count = 0;
-#endif
- for (auto const &item: arr)
- {
- QString mode_s, region_s;
- QJsonObject obj = item.toObject();
- FrequencyList_v2_101::Item freq;
- region_s = obj["region"].toString();
- mode_s = obj["mode"].toString();
-
- freq.frequency_ = obj["frequency"].toString().toDouble() * 1e6;
- freq.region_ = IARURegions::value(region_s);
- freq.mode_ = Modes::value(mode_s);
- freq.description_ = obj["description"].toString();
- freq.source_ = obj["source"].toString();
- freq.start_time_ = QDateTime::fromString(obj["start_time"].toString(), Qt::ISODate);
- freq.end_time_ = QDateTime::fromString(obj["end_time"].toString(), Qt::ISODate);
- freq.preferred_ = obj["preferred"].toBool();
-
- if ((freq.mode_ != Modes::ALL || QString::compare("ALL", mode_s)) &&
- (freq.region_ != IARURegions::ALL || QString::compare("ALL", region_s, Qt::CaseInsensitive)) &&
- freq.isSane())
- {
- list.push_back(freq);
-#ifdef DUMP_ENTRY_COUNTS
- valid_entry_count++;
-#endif
- } else {
-#ifdef DUMP_ENTRY_COUNTS
- skipped_entry_count++;
-#endif
- }
- }
-
-#ifdef DUMP_ENTRY_COUNTS
- MessageBox::information_message(this, tr("Loaded Frequencies from %1").arg(file_name),
- tr("Entries Valid/Skipped %1").arg(QString::number(valid_entry_count) + "/" +
- QString::number(skipped_entry_count)));
-#endif
- return list;
-}
-
-// write JSON format to a file
-void FrequencyList_v2_101::to_json_file(QFile *output_file, QString magic_s, QString version_s,
- FrequencyItems const &frequency_items)
-{
- QJsonObject jobject{
- {"wsjtx_file", "qrg"},
- {"wsjtx_version", QCoreApplication::applicationVersion()+" "+revision()},
- {"generated_at", QDateTime::currentDateTimeUtc ().toString (Qt::ISODate)},
- {"wsjtx_filetype", magic_s},
- {"qrg_version", version_s},
- {"frequency_count", frequency_items.count()}};
-
- QJsonArray array;
- for (auto &item: frequency_items)
- array.append(item.toJson());
- jobject["frequencies"] = array;
-
- QJsonDocument d = QJsonDocument(jobject);
- output_file->write(d.toJson());
-}
-
-// previous version 100 of the FrequencyList_v2 class
-QDataStream& operator >> (QDataStream& is, FrequencyList_v2::Item& item)
-{
- return is >> item.frequency_
- >> item.mode_
- >> item.region_;
-}
-
-QDataStream& operator << (QDataStream& os, FrequencyList_v2::Item const& item)
-{
- return os << item.frequency_
- << item.mode_
- << item.region_;
-}
-
-//
-// 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_;
-}
+#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 <QTextStream> +#include <QDataStream> +#include <QByteArray> +#include <QDebugStateSaver> +#include <QJsonObject> +#include <QJsonDocument> +#include <QJsonArray> +#include <QCoreApplication> +#include <QFile> +#include <QException> + +#include "Radio.hpp" +#include "Bands.hpp" +#include "pimpl_impl.hpp" +#include "revision_utils.hpp" +#include "Logger.hpp" + +#include "moc_FrequencyList.cpp" + +namespace +{ + FrequencyList_v2_101::FrequencyItems const default_frequency_list = + { + {198000, Modes::FreqCal, IARURegions::R1, "","", QDateTime(), QDateTime(), false}, // BBC Radio 4 Droitwich + {4996000, Modes::FreqCal, IARURegions::R1, "","", QDateTime(), QDateTime(), false}, // RWM time signal + {9996000, Modes::FreqCal, IARURegions::R1, "","", QDateTime(), QDateTime(), false}, // RWM time signal + {14996000, Modes::FreqCal, IARURegions::R1, "","", QDateTime(), QDateTime(), false}, // RWM time signal + + {660000, Modes::FreqCal, IARURegions::R2, "","", QDateTime(), QDateTime(), false}, + {880000, Modes::FreqCal, IARURegions::R2, "","", QDateTime(), QDateTime(), false}, + {1210000, Modes::FreqCal, IARURegions::R2, "","", QDateTime(), QDateTime(), false}, + + {2500000, Modes::FreqCal, IARURegions::ALL, "","", QDateTime(), QDateTime(), false}, + {3330000, Modes::FreqCal, IARURegions::ALL, "","", QDateTime(), QDateTime(), false}, + {5000000, Modes::FreqCal, IARURegions::ALL, "","", QDateTime(), QDateTime(), false}, + {7850000, Modes::FreqCal, IARURegions::ALL, "","", QDateTime(), QDateTime(), false}, + {10000000, Modes::FreqCal, IARURegions::ALL, "","", QDateTime(), QDateTime(), false}, + {14670000, Modes::FreqCal, IARURegions::ALL, "","", QDateTime(), QDateTime(), false}, + {15000000, Modes::FreqCal, IARURegions::ALL, "","", QDateTime(), QDateTime(), false}, + {20000000, Modes::FreqCal, IARURegions::ALL, "","", QDateTime(), QDateTime(), false}, + + {136000, Modes::WSPR, IARURegions::ALL, "","", QDateTime(), QDateTime(), false}, + {136000, Modes::FST4, IARURegions::ALL, "","", QDateTime(), QDateTime(), false}, + {136000, Modes::FST4W, IARURegions::ALL, "","", QDateTime(), QDateTime(), false}, + {136000, Modes::JT9, IARURegions::ALL,"","", QDateTime(), QDateTime(), false}, + + {474200, Modes::JT9, IARURegions::ALL, "","", QDateTime(), QDateTime(), false}, + {474200, Modes::FST4, IARURegions::ALL, "","", QDateTime(), QDateTime(), false}, + {474200, Modes::WSPR, IARURegions::ALL, "","", QDateTime(), QDateTime(), false}, + {474200, Modes::FST4W, IARURegions::ALL, "","", QDateTime(), QDateTime(), false}, + + {1836600, Modes::WSPR, IARURegions::ALL, "","", QDateTime(), QDateTime(), false}, + {1836800, Modes::FST4W, IARURegions::ALL, "","", QDateTime(), QDateTime(), false}, + {1838000, Modes::JT65, IARURegions::ALL, "","", QDateTime(), QDateTime(), false}, // squeezed allocations + {1839000, Modes::JT9, IARURegions::ALL, "","", QDateTime(), QDateTime(), false}, + {1839000, Modes::FST4, IARURegions::ALL, "","", QDateTime(), QDateTime(), false}, + {1840000, Modes::FT8, IARURegions::ALL, "","", QDateTime(), QDateTime(), true}, + + // Band plans (all USB dial unless stated otherwise) + // + // R1: 3570 - 3580 DM NB(<200Hz) + // 3580 - 3600 DM NB(<500Hz) with 3590 - 3600 ACDS + // + // 3577.75 OLIVIA, Contestia, etc. + // 3580 PSK31 + // 3583.25 OLIVIA, Contestia, etc. + // + // R2: 3570 - 3580 DM NB(<200Hz) + // 3580 - 3600 DM NB(<500Hz) with 3590 - 3600 ACDS + // + // 3577.75 OLIVIA, Contestia, etc. + // 3580 PSK31 + // 3583.25 OLIVIA, Contestia, etc. + // 3590 RTTY DX + // 3596 W1AW DM QST + // + // R3: 3535 - 3580 DM NB(<2000Hz) + // + // 3520 - 3575 DM NB(<2000Hz) JA 3535 - 3575 shared with all modes + // + // 3522 OLIVIA, Contestia, etc. + // 3535 JA LSB EMCOMM + // 3580 PSK31 + // 3600 LSB EMCOMM + // + {3567000, Modes::FT8, IARURegions::ALL, "","", QDateTime(), QDateTime(), false}, // DXpedition + {3570000, Modes::JT65, IARURegions::ALL, "","", QDateTime(), QDateTime(), false}, // JA compatible + {3572000, Modes::JT9, IARURegions::ALL, "","", QDateTime(), QDateTime(), false}, + {3573000, Modes::FT8, IARURegions::ALL, "","", QDateTime(), QDateTime(), true}, // above as below JT65 is out of DM allocation + {3568600, Modes::WSPR, IARURegions::ALL, "","", QDateTime(), QDateTime(), false}, // needs guard marker and lock out + {3568600, Modes::FST4W, IARURegions::ALL, "","", QDateTime(), QDateTime(), false}, // needs guard marker and lock out + {3575000, Modes::FT4, IARURegions::ALL, "","", QDateTime(), QDateTime(), true}, // provisional + {3568000, Modes::FT4, IARURegions::R3, "","", QDateTime(), QDateTime(), false}, // provisional + + {5357000, Modes::JT65, IARURegions::ALL, "","", QDateTime(), QDateTime(), false}, // provisional + {5357000, Modes::JT9, IARURegions::ALL, "","", QDateTime(), QDateTime(), false}, // provisional + {5357000, Modes::FT8, IARURegions::ALL, "","", QDateTime(), QDateTime(), true}, // provisional + {5357000, Modes::FT4, IARURegions::ALL, "","", QDateTime(), QDateTime(), true}, // provisional + + // Band plans (all USB dial unless stated otherwise) + // + // R1: 7040 - 7050 DM NB(<500Hz) with 7047 - 7050 ACDS + // 7050 - 7060 DM WB(<2700Hz) with 7050 - 7053 ACDS + // + // 7040 PSK31 + // 7043.25 OLIVIA, Contestia, etc. (main QRG) + // 7070 PSK31 + // 7073.25 OLIVIA, Contestia, etc. (main QRG) + // 7090 LSB QRP CoA + // + // R2: 7040 - 7050 DM NB(<500Hz) with 7047 - 7050 ACDS + // 7050 - 7053 DM WB(<2700Hz) ACDS shared with all modes + // + // 7040 RTTY DX + // 7043.25 OLIVIA, Contestia, etc. (main QRG) + // 7070 PSK31 (also LSB EMCOMM) + // 7073.25 OLIVIA, Contestia, etc. (main QRG) + // 7080 - 7125 RTTY/Data + // 7090 LSB QRP CoA + // + // R3: 7030 - 7060 DM NB(<2000Hz) with 7040 - 7060 NB DX all shared with phone + // + // 7030 - 7100 DM WB(<3000Hz) JA 7045 - 7100 shared with all modes + // + // 7026.25 OLIVIA, Contestia, etc. (main QRG) + // 7035 PSK31 + // 7050 JA LSB EMCOMM + // 7090 LSB QRP CoA + // 7110 LSB EMCOMM + // + {7038600, Modes::WSPR, IARURegions::ALL, "","", QDateTime(), QDateTime(), false}, + {7038600, Modes::FST4W, IARURegions::ALL, "","", QDateTime(), QDateTime(), false}, + {7056000, Modes::FT8, IARURegions::ALL, "","", QDateTime(), QDateTime(), false}, // DXpedition + {7074000, Modes::FT8, IARURegions::ALL, "","", QDateTime(), QDateTime(), true}, + {7076000, Modes::JT65, IARURegions::ALL, "","", QDateTime(), QDateTime(), false}, + {7078000, Modes::JT9, IARURegions::ALL, "","", QDateTime(), QDateTime(), false}, + {7047500, Modes::FT4, IARURegions::ALL, "","", QDateTime(), QDateTime(), true}, // provisional - moved + // up 500Hz to clear + // W1AW code practice QRG + + // Band plans (all USB dial unless stated otherwise) + // + // R1: 10130 - 10150 DM NB(<500Hz) with 10120 - 10140 shared with phone in southern Africa + // + // 10139.25 OLIVIA, Contestia, etc. + // 10142 PSK31 + // 10142.25 OLIVIA, Contestia, etc. + // 10143.25 OLIVIA, Contestia, etc. (main QRG) + // + // R2: 10130 - 10140 DM NB(<500Hz) shared with ACDS + // 10140 - 10150 DM WB(<2700Hz) + // + // 10130 - 10140 RTTY + // 10139.25 OLIVIA, Contestia, etc. + // 10140 - 10150 Packet + // 10142 PSK31 + // 10142.25 OLIVIA, Contestia, etc. + // 10143.25 OLIVIA, Contestia, etc. (main QRG) + // + // R3: 10130 - 10150 DM NB(<2000Hz) + // + // 10139.25 OLIVIA, Contestia, etc. + // 10142 PSK31 + // 10142.25 OLIVIA, Contestia, etc. + // 10143.25 OLIVIA, Contestia, etc. (main QRG) + // + {10131000, Modes::FT8, IARURegions::ALL, "","", QDateTime(), QDateTime(), false}, // DXpedition + {10136000, Modes::FT8, IARURegions::ALL, "","", QDateTime(), QDateTime(), true}, + {10138000, Modes::JT65, IARURegions::ALL, "","", QDateTime(), QDateTime(), false}, + {10138700, Modes::WSPR, IARURegions::ALL, "","", QDateTime(), QDateTime(), false}, + {10138700, Modes::FST4W, IARURegions::ALL, "","", QDateTime(), QDateTime(), false}, + {10140000, Modes::JT9, IARURegions::ALL, "","", QDateTime(), QDateTime(), false}, + {10140000, Modes::FT4, IARURegions::ALL, "","", QDateTime(), QDateTime(), true}, // provisional + + // Band plans (all USB dial unless stated otherwise) + // + // R1: 14070 - 14099 DM NB(<500Hz) with 14089 - 14099 ACDS + // 14101 - 14112 DM NB(<2700Hz) ACDS + // + // 14070 PSK31 + // 14074.4 OLIVIA, Contestia, etc. + // 14075.4 OLIVIA, Contestia, etc. (main QRG) + // 14078.4 OLIVIA, Contestia, etc. + // 14100 NCDXF beacons + // 14105.5 OLIVIA 1000 + // 14106.5 OLIVIA 1000 (main QRG) + // + // R2: 14070 - 14099 DM NB(<500Hz) with 14089 - 14099 ACDS + // 14101 - 14112 DM NB(<2700Hz) ACDS + // + // 14070 - 14095 RTTY + // 14070 PSK31 + // 14074.4 OLIVIA, Contestia, etc. + // 14075.4 OLIVIA, Contestia, etc. (main QRG) + // 14078.4 OLIVIA, Contestia, etc. + // 14095 - 14099.5 Packet + // 14100 NCDXF beacons + // 14100.5 - 14112 Packet + // 14105.5 OLIVIA 1000 + // 14106.5 OLIVIA 1000 (main QRG) + // + // R3: 14070 - 14112 DM NB(<2000Hz) with ±500Hz IBP guard band at 14100 + // + // 14070 PSK31 + // 14074.4 OLIVIA, Contestia, etc. + // 14075.4 OLIVIA, Contestia, etc. (main QRG) + // 14078.4 OLIVIA, Contestia, etc. + // 14100 NCDXF beacons + // 14105.5 OLIVIA 1000 + // 14106.5 OLIVIA 1000 (main QRG) + // + {14095600, Modes::WSPR, IARURegions::ALL, "","", QDateTime(), QDateTime(), false}, + {14095600, Modes::FST4W, IARURegions::ALL, "","", QDateTime(), QDateTime(), false}, + {14074000, Modes::FT8, IARURegions::ALL, "","", QDateTime(), QDateTime(), true}, + {14090000, Modes::FT8, IARURegions::ALL, "","", QDateTime(), QDateTime(), false}, // DXpedition + {14076000, Modes::JT65, IARURegions::ALL, "","", QDateTime(), QDateTime(), false}, + {14078000, Modes::JT9, IARURegions::ALL, "","", QDateTime(), QDateTime(), false}, + {14080000, Modes::FT4, IARURegions::ALL, "","", QDateTime(), QDateTime(), true}, // provisional + + + // Band plans (all USB dial unless stated otherwise) + // + // R1: 18095 - 18109 DM NB(<500Hz) with 18105 - 18109 ACDS + // 18111 - 18120 DM NB(<2700Hz) ACDS + // + // 18100 PSK31 + // 18103.4 OLIVIA, Contestia, etc. (main QRG) + // 18104.4 OLIVIA, Contestia, etc. + // 18110 NCDXF beacons + // + // R2: 18095 - 18109 DM NB(<500Hz) with 18105 - 18109 ACDS + // 18111 - 18120 DM NB(<2700Hz) ACDS + // + // 18100 - 18105 RTTY + // 18100 PSK31 + // 18103.4 OLIVIA, Contestia, etc. (main QRG) + // 18104.4 OLIVIA, Contestia, etc. + // 18105 - 18110 Packet + // 18110 NCDXF beacons + // + // R3: 18095 - 18120 DM NB(<2000Hz) with ±500Hz IBP guard band at 18110 + // + // 18100 PSK31 + // 18103.4 OLIVIA, Contestia, etc. (main QRG) + // 18104.4 OLIVIA, Contestia, etc. + // 18110 NCDXF beacons + // + {18095000, Modes::FT8, IARURegions::ALL, "","", QDateTime(), QDateTime(), false}, // DXpedition + {18100000, Modes::FT8, IARURegions::ALL, "","", QDateTime(), QDateTime(), true}, + {18102000, Modes::JT65, IARURegions::ALL, "","", QDateTime(), QDateTime(), false}, + {18104000, Modes::JT9, IARURegions::ALL, "","", QDateTime(), QDateTime(), false}, + {18104000, Modes::FT4, IARURegions::ALL, "","", QDateTime(), QDateTime(), true}, // provisional + {18104600, Modes::WSPR, IARURegions::ALL, "","", QDateTime(), QDateTime(), false}, + {18104600, Modes::FST4W, IARURegions::ALL, "","", QDateTime(), QDateTime(), false}, + + {21074000, Modes::FT8, IARURegions::ALL, "","", QDateTime(), QDateTime(), true}, + {21091000, Modes::FT8, IARURegions::ALL, "","", QDateTime(), QDateTime(), false}, // DXpedition + {21076000, Modes::JT65, IARURegions::ALL, "","", QDateTime(), QDateTime(), false}, + {21078000, Modes::JT9, IARURegions::ALL, "","", QDateTime(), QDateTime(), false}, + {21094600, Modes::WSPR, IARURegions::ALL, "","", QDateTime(), QDateTime(), false}, + {21094600, Modes::FST4W, IARURegions::ALL, "","", QDateTime(), QDateTime(), false}, + {21140000, Modes::FT4, IARURegions::ALL, "","", QDateTime(), QDateTime(), true}, + + {24911000, Modes::FT8, IARURegions::ALL, "","", QDateTime(), QDateTime(), false}, // DXpedition + {24915000, Modes::FT8, IARURegions::ALL, "","", QDateTime(), QDateTime(), true}, + {24917000, Modes::JT65, IARURegions::ALL, "","", QDateTime(), QDateTime(), false}, + {24919000, Modes::JT9, IARURegions::ALL, "","", QDateTime(), QDateTime(), false}, + {24919000, Modes::FT4, IARURegions::ALL, "","", QDateTime(), QDateTime(), true}, // provisional + {24924600, Modes::WSPR, IARURegions::ALL, "","", QDateTime(), QDateTime(), false}, + {24924600, Modes::FST4W, IARURegions::ALL, "","", QDateTime(), QDateTime(), false}, + + {28074000, Modes::FT8, IARURegions::ALL, "","", QDateTime(), QDateTime(), true}, + {28091000, Modes::FT8, IARURegions::ALL, "","", QDateTime(), QDateTime(), false}, // DXpedition + {28076000, Modes::JT65, IARURegions::ALL, "","", QDateTime(), QDateTime(), false}, + {28078000, Modes::JT9, IARURegions::ALL, "","", QDateTime(), QDateTime(), false}, + {28124600, Modes::WSPR, IARURegions::ALL, "","", QDateTime(), QDateTime(), false}, + {28124600, Modes::FST4W, IARURegions::ALL, "","", QDateTime(), QDateTime(), false}, + {28180000, Modes::FT4, IARURegions::ALL, "","", QDateTime(), QDateTime(), true}, + + {50200000, Modes::Echo, IARURegions::ALL, "","", QDateTime(), QDateTime(), false}, + {50211000, Modes::Q65, IARURegions::ALL, "EME","", QDateTime(), QDateTime(), false}, + {50275000, Modes::Q65, IARURegions::ALL, "","", QDateTime(), QDateTime(), true}, + {50276000, Modes::JT65, IARURegions::R2, "","", QDateTime(), QDateTime(), false}, + {50276000, Modes::JT65, IARURegions::R3, "","", QDateTime(), QDateTime(), false}, + {50380000, Modes::MSK144, IARURegions::R1, "","", QDateTime(), QDateTime(), false}, + {50260000, Modes::MSK144, IARURegions::R2, "","", QDateTime(), QDateTime(), false}, + {50260000, Modes::MSK144, IARURegions::R3, "","", QDateTime(), QDateTime(), false}, + {50293000, Modes::WSPR, IARURegions::R2, "","", QDateTime(), QDateTime(), false}, + {50293000, Modes::WSPR, IARURegions::R3, "","", QDateTime(), QDateTime(), false}, + {50293000, Modes::FST4W, IARURegions::R2, "","", QDateTime(), QDateTime(), false}, + {50293000, Modes::FST4W, IARURegions::R3, "","", QDateTime(), QDateTime(), false}, + {50310000, Modes::JT65, IARURegions::ALL, "","", QDateTime(), QDateTime(), false}, + {50312000, Modes::JT9, IARURegions::ALL, "","", QDateTime(), QDateTime(), false}, + {50313000, Modes::FT8, IARURegions::ALL, "","", QDateTime(), QDateTime(), true}, + {50318000, Modes::FT4, IARURegions::ALL, "","", QDateTime(), QDateTime(), true}, // provisional + {50323000, Modes::FT8, IARURegions::ALL, "","", QDateTime(), QDateTime(), false}, // intercontinental + + {70102000, Modes::JT65, IARURegions::R1, "","", QDateTime(), QDateTime(), false}, + {70104000, Modes::JT9, IARURegions::R1, "","", QDateTime(), QDateTime(), false}, + {70091000, Modes::WSPR, IARURegions::R1, "","", QDateTime(), QDateTime(), false}, + {70091000, Modes::FST4W, IARURegions::R1, "","", QDateTime(), QDateTime(), false}, + {70154000, Modes::FT8, IARURegions::R1, "","", QDateTime(), QDateTime(), true}, + {70230000, Modes::MSK144, IARURegions::R1, "","", QDateTime(), QDateTime(), false}, + + {144116000, Modes::Q65, IARURegions::ALL, "EME","", QDateTime(), QDateTime(), false}, + {144120000, Modes::JT65, IARURegions::ALL, "","", QDateTime(), QDateTime(), false}, + {144120000, Modes::Echo, IARURegions::ALL, "","", QDateTime(), QDateTime(), false}, + {144170000, Modes::FT4, IARURegions::ALL, "","", QDateTime(), QDateTime(), true}, + {144174000, Modes::FT8, IARURegions::ALL, "","", QDateTime(), QDateTime(), true}, + {144180000, Modes::Q65, IARURegions::ALL, "","", QDateTime(), QDateTime(), true}, + {144360000, Modes::MSK144, IARURegions::R1, "","", QDateTime(), QDateTime(), false}, + {144150000, Modes::MSK144, IARURegions::R2, "","", QDateTime(), QDateTime(), false}, + {144489000, Modes::WSPR, IARURegions::ALL,"","", QDateTime(), QDateTime(), false}, + {144489000, Modes::FST4W, IARURegions::ALL,"","", QDateTime(), QDateTime(), false}, + + {222065000, Modes::Echo, IARURegions::R2, "","", QDateTime(), QDateTime(), false}, + {222065000, Modes::JT65, IARURegions::R2, "","", QDateTime(), QDateTime(), false}, + {222065000, Modes::Q65, IARURegions::R2, "","", QDateTime(), QDateTime(), false}, + {222174000, Modes::FT8, IARURegions::R2, "","", QDateTime(), QDateTime(), true}, + + {432065000, Modes::Echo, IARURegions::ALL, "","", QDateTime(), QDateTime(), false}, + {432065000, Modes::JT65, IARURegions::ALL, "","", QDateTime(), QDateTime(), false}, + {432174000, Modes::FT8, IARURegions::ALL, "","", QDateTime(), QDateTime(), true}, + {432300000, Modes::WSPR, IARURegions::ALL,"","", QDateTime(), QDateTime(), false}, + {432360000, Modes::MSK144, IARURegions::ALL, "","", QDateTime(), QDateTime(), false}, + {432065000, Modes::Q65, IARURegions::ALL,"EME","", QDateTime(), QDateTime(), false}, + + {902065000, Modes::JT65, IARURegions::R2, "","", QDateTime(), QDateTime(), false}, + {902065000, Modes::Q65, IARURegions::R2, "","", QDateTime(), QDateTime(), false}, + + {1296065000, Modes::Echo, IARURegions::ALL,"","", QDateTime(), QDateTime(), false}, + {1296065000, Modes::JT65, IARURegions::ALL,"","", QDateTime(), QDateTime(), false}, + {1296500000, Modes::WSPR, IARURegions::ALL,"","", QDateTime(), QDateTime(), false}, + {1296065000, Modes::Q65, IARURegions::ALL,"","", QDateTime(), QDateTime(), false}, + {1296174000, Modes::FT8, IARURegions::ALL, "","", QDateTime(), QDateTime(), true}, + + {2301000000, Modes::Echo, IARURegions::ALL, "","", QDateTime(), QDateTime(), false}, + {2301065000, Modes::JT4, IARURegions::ALL,"","", QDateTime(), QDateTime(), false}, + {2301065000, Modes::JT65, IARURegions::ALL,"","", QDateTime(), QDateTime(), false}, + {2301065000, Modes::Q65, IARURegions::ALL, "","", QDateTime(), QDateTime(), false}, + + {2304065000, Modes::Echo, IARURegions::ALL, "","", QDateTime(), QDateTime(), false}, + {2304065000, Modes::JT4, IARURegions::ALL,"","", QDateTime(), QDateTime(), false}, + {2304065000, Modes::JT65, IARURegions::ALL,"","", QDateTime(), QDateTime(), false}, + {2304065000, Modes::Q65, IARURegions::ALL,"","", QDateTime(), QDateTime(), false}, + + {2320065000, Modes::Echo, IARURegions::ALL,"","", QDateTime(), QDateTime(), false}, + {2320065000, Modes::JT4, IARURegions::ALL,"","", QDateTime(), QDateTime(), false}, + {2320065000, Modes::JT65, IARURegions::ALL,"","", QDateTime(), QDateTime(), false}, + {2320065000, Modes::Q65, IARURegions::ALL,"","", QDateTime(), QDateTime(), false}, + {2400040000, Modes::ALL, IARURegions::ALL,"QO-100 Uplink","", QDateTime(), QDateTime(), false}, + + {3400065000, Modes::Echo, IARURegions::ALL,"","", QDateTime(), QDateTime(), false}, + {3400065000, Modes::JT4, IARURegions::ALL,"","", QDateTime(), QDateTime(), false}, + {3400065000, Modes::JT65, IARURegions::ALL,"","", QDateTime(), QDateTime(), false}, + {3400065000, Modes::Q65, IARURegions::ALL,"","", QDateTime(), QDateTime(), false}, + + {5760065000, Modes::Echo, IARURegions::ALL,"","", QDateTime(), QDateTime(), false}, + {5760065000, Modes::JT4, IARURegions::ALL,"","", QDateTime(), QDateTime(), false}, + {5760065000, Modes::JT65, IARURegions::ALL,"","", QDateTime(), QDateTime(), false}, + {5760200000, Modes::Q65, IARURegions::ALL,"","", QDateTime(), QDateTime(), false}, + + {10368100000, Modes::Echo, IARURegions::ALL,"","", QDateTime(), QDateTime(), false}, + {10368200000, Modes::JT4, IARURegions::ALL,"","", QDateTime(), QDateTime(), false}, + {10368200000, Modes::Q65, IARURegions::ALL,"","", QDateTime(), QDateTime(), false}, + {10489540000, Modes::ALL, IARURegions::ALL,"QO-100 Downlink","", QDateTime(), QDateTime(), false}, + + {24048100000, Modes::Echo, IARURegions::ALL,"","", QDateTime(), QDateTime(), false}, + {24048200000, Modes::JT4, IARURegions::ALL,"","", QDateTime(), QDateTime(), false}, + {24048200000, Modes::Q65, IARURegions::ALL,"","", QDateTime(), QDateTime(), false}, + }; +} + +#if !defined (QT_NO_DEBUG_STREAM) +QDebug operator << (QDebug debug, FrequencyList_v2_101::Item const& item) +{ + QDebugStateSaver saver {debug}; + return debug.nospace () << item.toString (); +} +#endif +bool FrequencyList_v2_101::Item::isSane() const +{ + return frequency_ > 0.0 && (!start_time_.isValid() || !end_time_.isValid() || start_time_ < end_time_) + && (region_ == IARURegions::ALL || region_ == IARURegions::R1 || region_ == IARURegions::R2 || region_ == IARURegions::R3); +} + +QString FrequencyList_v2_101::Item::toString () const +{ + QString string; + QTextStream qts {&string}; + qts << "FrequencyItem(" + << Radio::frequency_MHz_string (frequency_) << ", " + << IARURegions::name (region_) << ", " + << Modes::name (mode_) << ", " + << start_time_.toString(Qt::ISODate) << ", " + << end_time_.toString(Qt::ISODate) << ", " + << description_ << ", " + << source_ << "," + << preferred_ << ')'; + return string; +} + +QJsonObject FrequencyList_v2_101::Item::toJson() const { + return {{"frequency", Radio::frequency_MHz_string (frequency_) }, + {"mode", Modes::name (mode_) }, + {"region", IARURegions::name (region_)}, + {"description", description_}, + {"source", source_}, + {"start_time", start_time_.toString(Qt::ISODate) }, + {"end_time", end_time_.toString(Qt::ISODate) }, + {"preferred", preferred_}}; +} + +QDataStream& operator << (QDataStream& os, FrequencyList_v2_101::Item const& item) +{ + return os << item.frequency_ + << item.mode_ + << item.region_ + << item.start_time_ + << item.end_time_ + << item.description_ + << item.source_ + << item.preferred_; +} + +QDataStream& operator >> (QDataStream& is, FrequencyList_v2_101::Item& item) +{ + return is >> item.frequency_ + >> item.mode_ + >> item.region_ + >> item.start_time_ + >> item.end_time_ + >> item.description_ + >> item.source_ + >> item.preferred_; +} + +class FrequencyList_v2_101::impl final + : public QAbstractTableModel +{ +public: + impl (Bands const * bands, QObject * parent) + : QAbstractTableModel {parent} + , bands_ {bands} + , region_filter_ {IARURegions::ALL} + , mode_filter_ {Modes::ALL} + , filter_on_time_ {false} + { + } + + 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; + + void unprefer_all_but(Item & item, int const row, QVector<int> ); + + 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_; + bool filter_on_time_; + +}; + +FrequencyList_v2_101::FrequencyList_v2_101 (Bands const * bands, QObject * parent) + : QSortFilterProxyModel {parent} + , m_ {bands, parent} +{ + setSourceModel (&*m_); + setSortRole (SortRole); +} + +FrequencyList_v2_101::~FrequencyList_v2_101 () +{ +} + +auto FrequencyList_v2_101::frequency_list (FrequencyItems frequency_list) -> FrequencyItems +{ + return m_->frequency_list (frequency_list); +} + +auto FrequencyList_v2_101::frequency_list () const -> FrequencyItems const& +{ + return m_->frequency_list_; +} + +auto FrequencyList_v2_101::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_101::frequency_list_merge (FrequencyItems const& items) +{ + m_->add (items); +} + + +int FrequencyList_v2_101::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 the preferred one + if (m_->frequency_list_[source_row].preferred_) + { + return row; + } + // 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_101::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) + { + if (m_->frequency_list_[source_row].preferred_) + return row; // return the preferred one immediately + result = row; + } + } + } + return result; +} + +void FrequencyList_v2_101::reset_to_defaults () +{ + m_->frequency_list (default_frequency_list); +} + +QModelIndex FrequencyList_v2_101::add (Item f) +{ + return mapFromSource (m_->add (f)); +} + +bool FrequencyList_v2_101::remove (Item f) +{ + auto row = m_->frequency_list_.indexOf (f); + + if (0 > row) + { + return false; + } + + return m_->removeRow (row); +} + +bool FrequencyList_v2_101::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 descending 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_101::filter (Region region, Mode mode, bool filter_on_time) +{ + m_->region_filter_ = region; + m_->mode_filter_ = mode; + m_->filter_on_time_ = filter_on_time; + invalidateFilter (); +} + +void FrequencyList_v2_101::filter_refresh () +{ + invalidateFilter (); +} + +bool FrequencyList_v2_101::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_; + } + if (result && m_->filter_on_time_) + { + result = (!item.start_time_.isValid() || item.start_time_ <= QDateTime::currentDateTimeUtc ()) && + (!item.end_time_.isValid() || item.end_time_ >= QDateTime::currentDateTimeUtc ()); + } + return result; +} + + +auto FrequencyList_v2_101::impl::frequency_list (FrequencyItems frequency_list) -> FrequencyItems +{ + beginResetModel (); + std::swap (frequency_list_, frequency_list); + endResetModel (); + return frequency_list; +} + +// add a frequency returning the new model index +QModelIndex FrequencyList_v2_101::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 (); + + // if we added one that had a preferred frequency, unprefer everything else + if (f.preferred_) unprefer_all_but(f, row, {Qt::DisplayRole, Qt::CheckStateRole}); + + return index (row, 0); + } + return QModelIndex {}; +} + +void FrequencyList_v2_101::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_101::impl::rowCount (QModelIndex const& parent) const +{ + return parent.isValid () ? 0 : frequency_list_.size (); +} + +int FrequencyList_v2_101::impl::columnCount (QModelIndex const& parent) const +{ + return parent.isValid () ? 0 : num_cols; +} + +Qt::ItemFlags FrequencyList_v2_101::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; + } + + if (preferred_column == column) + { + result |= Qt::ItemIsUserCheckable; + } + } + return result; +} + +QVariant FrequencyList_v2_101::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_); + QString desc_text; + desc_text = frequency_item.description_.isEmpty() ? "" : " \u2502 " + frequency_item.description_; + item = (frequency_item.preferred_ ? "\u2055 " : "") + + Radio::pretty_frequency_MHz_string(frequency_item.frequency_) + + " MHz (" + (band.isEmpty() ? "OOB" : band) + ")" + + (((frequency_item.start_time_.isValid() && !frequency_item.start_time_.isNull()) || + (frequency_item.end_time_.isValid() && !frequency_item.end_time_.isNull())) ? " \u2502 " : "") + + desc_text; + } + break; + + case Qt::ToolTipRole: + case Qt::AccessibleDescriptionRole: + item = tr ("Frequency (MHz)"); + break; + + case Qt::TextAlignmentRole: + item = Qt::AlignRight + Qt::AlignVCenter; + break; + } + break; + + case description_column: + switch (role) + { + case SortRole: + case Qt::DisplayRole: + case Qt::EditRole: + case Qt::AccessibleTextRole: + item = frequency_item.description_; + break; + + case Qt::ToolTipRole: + case Qt::AccessibleDescriptionRole: + item = tr("Description"); + break; + + case Qt::TextAlignmentRole: + item = Qt::AlignLeft + Qt::AlignVCenter; + break; + } + break; + + case source_column: + switch (role) + { + case SortRole: + case Qt::DisplayRole: + case Qt::EditRole: + case Qt::AccessibleTextRole: + item = frequency_item.source_; + break; + + case Qt::ToolTipRole: + case Qt::AccessibleDescriptionRole: + item = tr ("Source"); + break; + + case Qt::TextAlignmentRole: + item = Qt::AlignLeft + Qt::AlignVCenter; + break; + } + break; + + case start_time_column: + switch (role) + { + case SortRole: + item = frequency_item.start_time_; + break; + + case Qt::EditRole: + if (frequency_item.start_time_.isNull () || !frequency_item.start_time_.isValid ()) + { + item = QDateTime::currentDateTimeUtc ().toString (Qt::ISODate); + } + else + { + item = frequency_item.start_time_.toString(Qt::ISODate); + } + break; + + case Qt::DisplayRole: + case Qt::AccessibleTextRole: + item = frequency_item.start_time_.toString(Qt::ISODate); + break; + + case Qt::ToolTipRole: + case Qt::AccessibleDescriptionRole: + item = tr ("Start Time"); + break; + + case Qt::TextAlignmentRole: + item = Qt::AlignLeft + Qt::AlignVCenter; + break; + } + break; + + case end_time_column: + switch (role) + { + case SortRole: + item = frequency_item.end_time_; + break; + + case Qt::EditRole: + if (frequency_item.end_time_.isNull () || !frequency_item.end_time_.isValid ()) + { + item = QDateTime::currentDateTimeUtc ().toString (Qt::ISODate); + } + else + { + item = frequency_item.end_time_.toString(Qt::ISODate); + } + break; + + case Qt::DisplayRole: + case Qt::AccessibleTextRole: + item = frequency_item.end_time_.toString(Qt::ISODate); + break; + + case Qt::ToolTipRole: + case Qt::AccessibleDescriptionRole: + item = tr ("End Time"); + break; + + case Qt::TextAlignmentRole: + item = Qt::AlignLeft + Qt::AlignVCenter; + break; + } + break; + + case preferred_column: + switch (role) + { + case SortRole: + item = frequency_item.preferred_; + break; + case Qt::DisplayRole: + case Qt::EditRole: + case Qt::AccessibleTextRole: + //item = frequency_item.preferred_ ? QString("True") : QString("False"); + break; + + case Qt::ToolTipRole: + case Qt::AccessibleDescriptionRole: + item = tr ("Pref"); + break; + + case Qt::TextAlignmentRole: + item = Qt::AlignHCenter + Qt::AlignVCenter; + break; + + case Qt::CheckStateRole: + item = frequency_item.preferred_ ? Qt::Checked : Qt::Unchecked; + break; + } + break; + } + } + return item; +} + +void FrequencyList_v2_101::impl::unprefer_all_but(Item &item, int const item_row, QVector<int> roles) +{ + // un-prefer all of the other frequencies in this band + auto const band = bands_->find (item.frequency_); + if (band.isEmpty ()) return; // out of any band + + roles << Qt::CheckStateRole; + roles << Qt::DisplayRole; + + for (int row = 0; row < rowCount (); ++row) + { + if (row == item_row) continue; + + Item &i = frequency_list_[row]; + auto const &iter_band = bands_->find(i.frequency_); + + if (!iter_band.isEmpty() && band == iter_band && (i.region_ == item.region_) && (i.mode_ == item.mode_)) + { + i.preferred_ = false; + Q_EMIT dataChanged(index(row,preferred_column), index(row,preferred_column), roles); + } + } +} + +bool FrequencyList_v2_101::impl::setData (QModelIndex const& model_index, QVariant const& value, int role) +{ + bool changed {false}; + auto const& row = model_index.row (); + auto& item = frequency_list_[row]; + + QVector<int> roles; + roles << role; + + if (model_index.isValid () + && Qt::CheckStateRole == role + && row < frequency_list_.size () + && model_index.column () == preferred_column) + { + bool b_val = ((Qt::CheckState)value.toInt() == Qt::Checked); + if (b_val != item.preferred_) + { + item.preferred_ = b_val; + if (item.preferred_) + { + unprefer_all_but (item, row, roles); // un-prefer all of the other frequencies in this band + } + Q_EMIT dataChanged(index(row,description_column), index(row,preferred_column), roles); + changed = true; + } + } + + if (model_index.isValid () + && Qt::EditRole == role + && row < frequency_list_.size ()) + { + 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; + + case description_column: + { + if (value.toString() != item.description_) + { + item.description_ = value.toString(); + Q_EMIT dataChanged(model_index, model_index, roles); + changed = true; + } + } + break; + + case source_column: + { + if (value.toString() != item.source_) + { + item.source_ = value.toString(); + Q_EMIT dataChanged(model_index, model_index, roles); + changed = true; + } + } + break; + + case start_time_column: + { + QDateTime start_time = QDateTime::fromString(value.toString(), Qt::ISODate); + LOG_INFO(QString{"start_time = %1 - isEmpty %2"}.arg(value.toString()).arg(value.toString().isEmpty())); + if (value.toString().isEmpty()) + { // empty string is valid + start_time = QDateTime(); + } + if (start_time.isValid() || start_time.isNull()) + { + item.start_time_ = start_time; + if (item.end_time_.isValid() && !item.start_time_.isNull() && item.end_time_ < item.start_time_) + { + item.end_time_ = item.start_time_; + } + Q_EMIT dataChanged(model_index, index(model_index.row(), end_time_column), roles); + changed = true; + } + } + break; + + case end_time_column: + { + QDateTime end_time = QDateTime::fromString(value.toString(), Qt::ISODate); + if (value.toString().isEmpty()) + { // empty string is valid + end_time = QDateTime(); + } + if (end_time.isValid() || end_time.isNull()) + { + item.end_time_ = end_time; + if (item.start_time_.isValid() && !item.end_time_.isNull() && end_time <= item.start_time_) + { + item.start_time_ = end_time; + } + Q_EMIT dataChanged(index(model_index.row(), start_time_column), model_index, roles); + changed = true; + } + } + break; + + case preferred_column: + { + bool b_value = value.toBool(); + if (b_value != item.preferred_) + { + item.preferred_ = b_value; + Q_EMIT dataChanged(index(model_index.row(), start_time_column), model_index, roles); + changed = true; + } + } + break; + + } + } + + return changed; +} + +QVariant FrequencyList_v2_101::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; + case source_column: header = tr ("Source"); break; + case start_time_column: header = tr ("Start Date/Time"); break; + case end_time_column: header = tr ("End Date/Time"); break; + case preferred_column: header = tr ("Pref"); break; + case description_column: header = tr ("Description"); break; + } + } + else + { + header = QAbstractTableModel::headerData (section, orientation, role); + } + return header; +} + +bool FrequencyList_v2_101::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_101::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, QString(), QString(), QDateTime(), QDateTime(), false}); + } + endInsertRows (); + return true; + } + return false; +} + +QStringList FrequencyList_v2_101::impl::mimeTypes () const +{ + QStringList types; + types << mime_type; + return types; +} + +QMimeData * FrequencyList_v2_101::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_101::const_iterator::operator * () const -> Item const& +{ + return parent_->frequency_list ().at(parent_->mapToSource (parent_->index (row_, 0)).row ()); +} + +auto FrequencyList_v2_101::const_iterator::operator -> () const -> Item const * +{ + return &parent_->frequency_list ().at(parent_->mapToSource (parent_->index (row_, 0)).row ()); +} + +bool FrequencyList_v2_101::const_iterator::operator != (const_iterator const& rhs) const +{ + return parent_ != rhs.parent_ || row_ != rhs.row_; +} + +bool FrequencyList_v2_101::const_iterator::operator == (const_iterator const& rhs) const +{ + return parent_ == rhs.parent_ && row_ == rhs.row_; +} + +auto FrequencyList_v2_101::const_iterator::operator ++ () -> const_iterator& +{ + ++row_; + return *this; +} + +auto FrequencyList_v2_101::begin () const -> const_iterator +{ + return const_iterator (this, 0); +} + +auto FrequencyList_v2_101::end () const -> const_iterator +{ + return const_iterator (this, rowCount ()); +} + +auto FrequencyList_v2_101::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_101::filtered_bands () const -> BandSet +{ + BandSet result; + for (auto const& item : *this) + { + result << m_->bands_->find (item.frequency_); + } + return result; +} + +auto FrequencyList_v2_101::all_bands (Region region, Mode mode) const -> BandSet +{ + BandSet result; + for (auto const& item : m_->frequency_list_) + { + // Match frequencies that are for all regions, for the specified + // region (which can also be "all"), and where the mode matches + // the specified mode (which can also be "all"). + if ((region == IARURegions::ALL || item.region_ == IARURegions::ALL || item.region_ == region) + && (mode == Modes::ALL || item.mode_ == Modes::ALL || item.mode_ == mode)) + { + result << m_->bands_->find (item.frequency_); + } + } + return result; +} + +FrequencyList_v2_101::FrequencyItems FrequencyList_v2_101::from_json_file(QFile *input_file) +{ + FrequencyList_v2_101::FrequencyItems list; + QJsonDocument doc = QJsonDocument::fromJson(input_file->readAll()); + if (doc.isNull()) + { + throw ReadFileException {tr ("Failed to parse JSON file")}; + } + QJsonObject obj = doc.object(); + if (obj.isEmpty()) + { + throw ReadFileException{tr("Information Missing")}; + } + QJsonArray arr = obj["frequencies"].toArray(); + if (arr.isEmpty()) + { + throw ReadFileException{tr ("No Frequencies were found")}; + } +#ifdef DUMP_ENTRY_COUNTS + int valid_entry_count = 0; + int skipped_entry_count = 0; +#endif + for (auto const &item: arr) + { + QString mode_s, region_s; + QJsonObject obj = item.toObject(); + FrequencyList_v2_101::Item freq; + region_s = obj["region"].toString(); + mode_s = obj["mode"].toString(); + + freq.frequency_ = obj["frequency"].toString().toDouble() * 1e6; + freq.region_ = IARURegions::value(region_s); + freq.mode_ = Modes::value(mode_s); + freq.description_ = obj["description"].toString(); + freq.source_ = obj["source"].toString(); + freq.start_time_ = QDateTime::fromString(obj["start_time"].toString(), Qt::ISODate); + freq.end_time_ = QDateTime::fromString(obj["end_time"].toString(), Qt::ISODate); + freq.preferred_ = obj["preferred"].toBool(); + + if ((freq.mode_ != Modes::ALL || QString::compare("ALL", mode_s)) && + (freq.region_ != IARURegions::ALL || QString::compare("ALL", region_s, Qt::CaseInsensitive)) && + freq.isSane()) + { + list.push_back(freq); +#ifdef DUMP_ENTRY_COUNTS + valid_entry_count++; +#endif + } else { +#ifdef DUMP_ENTRY_COUNTS + skipped_entry_count++; +#endif + } + } + +#ifdef DUMP_ENTRY_COUNTS + MessageBox::information_message(this, tr("Loaded Frequencies from %1").arg(file_name), + tr("Entries Valid/Skipped %1").arg(QString::number(valid_entry_count) + "/" + + QString::number(skipped_entry_count))); +#endif + return list; +} + +// write JSON format to a file +void FrequencyList_v2_101::to_json_file(QFile *output_file, QString magic_s, QString version_s, + FrequencyItems const &frequency_items) +{ + QJsonObject jobject{ + {"wsjtx_file", "qrg"}, + {"wsjtx_version", QCoreApplication::applicationVersion()+" "+revision()}, + {"generated_at", QDateTime::currentDateTimeUtc ().toString (Qt::ISODate)}, + {"wsjtx_filetype", magic_s}, + {"qrg_version", version_s}, + {"frequency_count", frequency_items.count()}}; + + QJsonArray array; + for (auto &item: frequency_items) + array.append(item.toJson()); + jobject["frequencies"] = array; + + QJsonDocument d = QJsonDocument(jobject); + output_file->write(d.toJson()); +} + +// previous version 100 of the FrequencyList_v2 class +QDataStream& operator >> (QDataStream& is, FrequencyList_v2::Item& item) +{ + return is >> item.frequency_ + >> item.mode_ + >> item.region_; +} + +QDataStream& operator << (QDataStream& os, FrequencyList_v2::Item const& item) +{ + return os << item.frequency_ + << item.mode_ + << item.region_; +} + +// +// 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 index 82f4946a..04b51a86 100755..100644 --- a/models/FrequencyList.hpp +++ b/models/FrequencyList.hpp @@ -1,251 +1,251 @@ -#ifndef FREQUENCY_LIST_HPP__
-#define FREQUENCY_LIST_HPP__
-
-#include "pimpl_h.hpp"
-
-#include <QList>
-#include <QSortFilterProxyModel>
-#include <QDateTime>
-#include <QJsonObject>
-#include <QJsonDocument>
-#include <QFile>
-#include <QException>
-
-#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_101 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_;
- QString description_;
- QString source_;
- QDateTime start_time_;
- QDateTime end_time_;
- bool preferred_; // preferred frequency for this band and mode
- QString toString () const;
- bool isSane() const;
- QJsonObject toJson () const;
-
- };
- using FrequencyItems = QList<Item>;
- using BandSet = QSet<QString>;
-
- enum Column {region_column, mode_column, frequency_column, frequency_mhz_column, description_column, start_time_column, end_time_column, source_column, preferred_column, SENTINAL};
-
- // an iterator that meets the requirements of the C++ for range statement
- class const_iterator
- {
- public:
- const_iterator (FrequencyList_v2_101 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_101 const * parent_;
- int row_;
- };
-
- explicit FrequencyList_v2_101 (Bands const *, QObject * parent = nullptr);
-
- ~FrequencyList_v2_101 ();
-
- // 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&);
- void to_json_file(QFile *, QString, QString, FrequencyItems const&);
- static FrequencyItems from_json_file(QFile *);
-
- // 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, bool);
-
- // 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;
-
- // Refresh the filter based on the current filter settings (underlying data may have changed)
- void filter_refresh ();
-
- // Custom roles.
- static int constexpr SortRole = Qt::UserRole;
-
-private:
- class impl;
- pimpl<impl> m_;
-};
-
-inline
-bool operator == (FrequencyList_v2_101::Item const& lhs, FrequencyList_v2_101::Item const& rhs)
-{
- return
- lhs.frequency_ == rhs.frequency_
- && lhs.region_ == rhs.region_
- && lhs.mode_ == rhs.mode_
- && lhs.description_ == rhs.description_
- && lhs.source_ == rhs.source_
- && lhs.start_time_ == rhs.start_time_
- && lhs.end_time_ == rhs.end_time_
- && lhs.preferred_ == rhs.preferred_;
-}
-
-QDataStream& operator << (QDataStream&, FrequencyList_v2_101::Item const&);
-QDataStream& operator >> (QDataStream&, FrequencyList_v2_101::Item&);
-
-#if !defined (QT_NO_DEBUG_STREAM)
-QDebug operator << (QDebug, FrequencyList_v2_101::Item const&);
-#endif
-
-Q_DECLARE_METATYPE (FrequencyList_v2_101::Item);
-Q_DECLARE_METATYPE (FrequencyList_v2_101::FrequencyItems);
-
-class FrequencyList_v2 final
-{
-
-public:
- using Frequency = Radio::Frequency;
- using Mode = Modes::Mode;
- using Region = IARURegions::Region;
-
- struct Item
- {
- Frequency frequency_;
- Mode mode_;
- Region region_;
- QString toString () const;
- };
- using FrequencyItems = QList<Item>;
-
-private:
- FrequencyItems frequency_list_;
-};
-
-QDataStream& operator << (QDataStream&, FrequencyList_v2::Item const&);
-QDataStream& operator >> (QDataStream&, FrequencyList_v2::Item&);
-
-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);
-
-class ReadFileException : public QException {
-public:
- ReadFileException (QString const& message)
- : message_ {message}
- {
- }
-
- void raise () const override { throw *this; }
- ReadFileException * clone () const override { return new ReadFileException {*this}; }
-
- QString filename_;
- QString message_;
-};
-
-#endif
+#ifndef FREQUENCY_LIST_HPP__ +#define FREQUENCY_LIST_HPP__ + +#include "pimpl_h.hpp" + +#include <QList> +#include <QSortFilterProxyModel> +#include <QDateTime> +#include <QJsonObject> +#include <QJsonDocument> +#include <QFile> +#include <QException> + +#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_101 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_; + QString description_; + QString source_; + QDateTime start_time_; + QDateTime end_time_; + bool preferred_; // preferred frequency for this band and mode + QString toString () const; + bool isSane() const; + QJsonObject toJson () const; + + }; + using FrequencyItems = QList<Item>; + using BandSet = QSet<QString>; + + enum Column {region_column, mode_column, frequency_column, frequency_mhz_column, description_column, start_time_column, end_time_column, source_column, preferred_column, SENTINAL}; + + // an iterator that meets the requirements of the C++ for range statement + class const_iterator + { + public: + const_iterator (FrequencyList_v2_101 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_101 const * parent_; + int row_; + }; + + explicit FrequencyList_v2_101 (Bands const *, QObject * parent = nullptr); + + ~FrequencyList_v2_101 (); + + // 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&); + void to_json_file(QFile *, QString, QString, FrequencyItems const&); + static FrequencyItems from_json_file(QFile *); + + // 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, bool); + + // 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; + + // Refresh the filter based on the current filter settings (underlying data may have changed) + void filter_refresh (); + + // Custom roles. + static int constexpr SortRole = Qt::UserRole; + +private: + class impl; + pimpl<impl> m_; +}; + +inline +bool operator == (FrequencyList_v2_101::Item const& lhs, FrequencyList_v2_101::Item const& rhs) +{ + return + lhs.frequency_ == rhs.frequency_ + && lhs.region_ == rhs.region_ + && lhs.mode_ == rhs.mode_ + && lhs.description_ == rhs.description_ + && lhs.source_ == rhs.source_ + && lhs.start_time_ == rhs.start_time_ + && lhs.end_time_ == rhs.end_time_ + && lhs.preferred_ == rhs.preferred_; +} + +QDataStream& operator << (QDataStream&, FrequencyList_v2_101::Item const&); +QDataStream& operator >> (QDataStream&, FrequencyList_v2_101::Item&); + +#if !defined (QT_NO_DEBUG_STREAM) +QDebug operator << (QDebug, FrequencyList_v2_101::Item const&); +#endif + +Q_DECLARE_METATYPE (FrequencyList_v2_101::Item); +Q_DECLARE_METATYPE (FrequencyList_v2_101::FrequencyItems); + +class FrequencyList_v2 final +{ + +public: + using Frequency = Radio::Frequency; + using Mode = Modes::Mode; + using Region = IARURegions::Region; + + struct Item + { + Frequency frequency_; + Mode mode_; + Region region_; + QString toString () const; + }; + using FrequencyItems = QList<Item>; + +private: + FrequencyItems frequency_list_; +}; + +QDataStream& operator << (QDataStream&, FrequencyList_v2::Item const&); +QDataStream& operator >> (QDataStream&, FrequencyList_v2::Item&); + +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); + +class ReadFileException : public QException { +public: + ReadFileException (QString const& message) + : message_ {message} + { + } + + void raise () const override { throw *this; } + ReadFileException * clone () const override { return new ReadFileException {*this}; } + + QString filename_; + QString message_; +}; + +#endif diff --git a/models/IARURegions.cpp b/models/IARURegions.cpp index f1a7dda4..3f8f1c3e 100755..100644 --- a/models/IARURegions.cpp +++ b/models/IARURegions.cpp @@ -1,96 +1,96 @@ -#include "models/IARURegions.hpp"
-
-#include <algorithm>
-
-#include <QString>
-#include <QVariant>
-#include <QModelIndex>
-#include <QMetaType>
-
-namespace
-{
- // human readable strings for each Region enumeration value
- char const * const region_names[] =
- {
- QT_TRANSLATE_NOOP ("IARURegions", "All"),
- QT_TRANSLATE_NOOP ("IARURegions", "Region 1"),
- QT_TRANSLATE_NOOP ("IARURegions", "Region 2"),
- QT_TRANSLATE_NOOP ("IARURegions", "Region 3"),
- };
- std::size_t constexpr region_names_size = sizeof (region_names) / sizeof (region_names[0]);
-}
-
-#include "moc_IARURegions.cpp"
-
-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)];
-}
-
-IARURegions::Region IARURegions::value (QString const& s)
-{
- auto end = region_names + region_names_size;
- auto p = std::find_if (region_names, end
- , [&s] (char const * const name) {
- return tr (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 = tr (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);
+#include "models/IARURegions.hpp" + +#include <algorithm> + +#include <QString> +#include <QVariant> +#include <QModelIndex> +#include <QMetaType> + +namespace +{ + // human readable strings for each Region enumeration value + char const * const region_names[] = + { + QT_TRANSLATE_NOOP ("IARURegions", "All"), + QT_TRANSLATE_NOOP ("IARURegions", "Region 1"), + QT_TRANSLATE_NOOP ("IARURegions", "Region 2"), + QT_TRANSLATE_NOOP ("IARURegions", "Region 3"), + }; + std::size_t constexpr region_names_size = sizeof (region_names) / sizeof (region_names[0]); +} + +#include "moc_IARURegions.cpp" + +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)]; +} + +IARURegions::Region IARURegions::value (QString const& s) +{ + auto end = region_names + region_names_size; + auto p = std::find_if (region_names, end + , [&s] (char const * const name) { + return tr (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 = tr (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 index a3aa0668..498846d3 100755..100644 --- a/models/IARURegions.hpp +++ b/models/IARURegions.hpp @@ -1,67 +1,67 @@ -#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
+#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 index 753af2cd..966665bb 100755..100644 --- a/models/Modes.cpp +++ b/models/Modes.cpp @@ -1,104 +1,104 @@ -#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",
- "MSK144",
- "FreqCal",
- "FT8",
- "FT4",
- "FST4",
- "FST4W",
- "Q65"
- };
- 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);
+#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", + "MSK144", + "FreqCal", + "FT8", + "FT4", + "FST4", + "FST4W", + "Q65" + }; + 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 index febef267..675f097e 100755..100644 --- a/models/Modes.hpp +++ b/models/Modes.hpp @@ -1,76 +1,76 @@ -#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,
- MSK144,
- FreqCal,
- FT8,
- FT4,
- FST4,
- FST4W,
- Q65,
- 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
+#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, + MSK144, + FreqCal, + FT8, + FT4, + FST4, + FST4W, + Q65, + 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 index 7c6de0ce..2b501259 100755..100644 --- a/models/StationList.cpp +++ b/models/StationList.cpp @@ -1,544 +1,544 @@ -#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 <QTextStream>
-#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};
- return debug.nospace () << station.toString ();
-}
-#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)
-{
- 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
-{
- Q_OBJECT
-
-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_;
-};
-
-#include "StationList.moc"
-#include "moc_StationList.cpp"
-
-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_101::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;
-}
+#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 <QTextStream> +#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}; + return debug.nospace () << station.toString (); +} +#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) +{ + 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 +{ + Q_OBJECT + +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_; +}; + +#include "StationList.moc" +#include "moc_StationList.cpp" + +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_101::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 index 5d3f8ea5..92c149fe 100755..100644 --- a/models/StationList.hpp +++ b/models/StationList.hpp @@ -1,109 +1,109 @@ -#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
-{
- Q_OBJECT
-
-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_;
- QString toString () const;
- };
-
- 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
+#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 +{ + Q_OBJECT + +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_; + QString toString () const; + }; + + 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 index 0b1219fa..5b3d038b 100755..100644 --- a/models/models.pri +++ b/models/models.pri @@ -1,20 +1,20 @@ -SOURCES += \
- models/Bands.cpp \
- models/FrequencyList.cpp \
- models/StationList.cpp \
- models/Modes.cpp \
- models/IARURegions.cpp \
- models/FoxLog.cpp \
- models/CabrilloLog.cpp \
- models/DecodeHighlightingModel.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 \
- models/DecodeHighlightingModel.hpp
+SOURCES += \ + models/Bands.cpp \ + models/FrequencyList.cpp \ + models/StationList.cpp \ + models/Modes.cpp \ + models/IARURegions.cpp \ + models/FoxLog.cpp \ + models/CabrilloLog.cpp \ + models/DecodeHighlightingModel.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 \ + models/DecodeHighlightingModel.hpp |
