Moving toward multiplier and dupe detection for contest modes

There's not  much to show  for this so far  but some of  the necessary
infrastructure is in place.
This commit is contained in:
Bill Somerville
2019-05-30 22:20:09 +01:00
parent 425b0e89a8
commit 19c46774b4
31 changed files with 473 additions and 128 deletions
+131 -52
View File
@@ -20,59 +20,122 @@ class CabrilloLog::impl final
: public QSqlTableModel
{
public:
impl (Configuration const *);
impl (CabrilloLog *, Configuration const *);
QVariant data (QModelIndex const& index, int role) const
int columnCount (QModelIndex const& /*index */) const override
{
auto value = QSqlTableModel::data (index, role);
if (index.column () == fieldIndex ("when")
&& (Qt::DisplayRole == role || Qt::EditRole == role))
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)
{
auto t = QDateTime::fromMSecsSinceEpoch (value.toULongLong () * 1000ull, Qt::UTC);
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)
{
QLocale locale;
return locale.toString (t, locale.dateFormat (QLocale::ShortFormat) + " hh:mm:ss");
value = configuration_->bands ()->find (QSqlTableModel::data (index (model_index.row (), fieldIndex ("frequency"))).toULongLong ());
}
}
else
{
value = QSqlTableModel::data (model_index, role);
if (model_index.column () == fieldIndex ("frequency") && Qt::DisplayRole == role)
{
value = Radio::frequency_MHz_string (value.value<Radio::Frequency> (), 3); // kHz precision
}
else if (model_index.column () == fieldIndex ("when")
&& (Qt::DisplayRole == role || Qt::EditRole == role))
{ // adjust date/time to Qt format
auto t = QDateTime::fromMSecsSinceEpoch (value.toULongLong () * 1000ull, Qt::UTC);
if (Qt::DisplayRole == role)
{
QLocale locale;
return locale.toString (t, locale.dateFormat (QLocale::ShortFormat) + " hh:mm:ss");
}
value = t;
}
value = t;
}
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_;
bool adding_row_;
};
CabrilloLog::impl::impl (Configuration const * configuration)
: QSqlTableModel {}
CabrilloLog::impl::impl (CabrilloLog * self, Configuration const * configuration)
: self_ {self}
, configuration_ {configuration}
, adding_row_ {false}
{
if (!database ().tables ().contains ("cabrillo_log"))
{
create_table ();
}
setEditStrategy (QSqlTableModel::OnFieldChange);
setTable ("cabrillo_log");
if (-1 != fieldIndex ("band")) // schema out of date
{
QSqlQuery query;
SQL_error_check (query, static_cast<bool (QSqlQuery::*) (QString const&)> (&QSqlQuery::exec),
"CREATE TABLE cabrillo_log ("
" id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,"
" frequency INTEGER NOT NULL,"
" \"when\" DATETIME NOT NULL,"
" call VARCHAR(20) NOT NULL,"
" exchange_sent VARCHAR(32) NOT NULL,"
" exchange_rcvd VARCHAR(32) NOT NULL,"
" band VARCHAR(6) NOT NULL"
")");
"DROP TABLE IF EXISTS cabrillo_log_backup");
SQL_error_check (query, static_cast<bool (QSqlQuery::*) (QString const&)> (&QSqlQuery::exec),
"CREATE TABLE cabrillo_log_backup AS SELECT * FROM cabrillo_log");
SQL_error_check (query, static_cast<bool (QSqlQuery::*) (QString const&)> (&QSqlQuery::exec),
"DROP TABLE cabrillo_log");
create_table ();
setTable ("cabrillo_log");
}
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 ();
}
});
SQL_error_check (*this, &QSqlTableModel::select);
SQL_error_check (dupe_query_, &QSqlQuery::prepare,
"SELECT "
" COUNT(*) "
" frequency "
" FROM "
" cabrillo_log "
" WHERE "
" call = :call "
" AND band = :band");
" call = :call ");
SQL_error_check (export_query_, &QSqlQuery::prepare,
"SELECT "
@@ -85,29 +148,28 @@ CabrilloLog::impl::impl (Configuration const * configuration)
" cabrillo_log "
" ORDER BY "
" \"when\"");
setEditStrategy (QSqlTableModel::OnFieldChange);
setTable ("cabrillo_log");
setHeaderData (fieldIndex ("frequency"), Qt::Horizontal, tr ("Freq(kHz)"));
setHeaderData (fieldIndex ("when"), Qt::Horizontal, tr ("Date & Time(UTC)"));
setHeaderData (fieldIndex ("call"), Qt::Horizontal, tr ("Call"));
setHeaderData (fieldIndex ("exchange_sent"), Qt::Horizontal, tr ("Sent"));
setHeaderData (fieldIndex ("exchange_rcvd"), Qt::Horizontal, tr ("Rcvd"));
setHeaderData (fieldIndex ("band"), Qt::Horizontal, tr ("Band"));
}
// This descending order by time is important, it makes the view
// place the latest row at the top, without this the model/view
// interactions are both sluggish and unhelpful.
setSort (fieldIndex ("when"), Qt::DescendingOrder);
SQL_error_check (*this, &QSqlTableModel::select);
void CabrilloLog::impl::create_table ()
{
QSqlQuery query;
SQL_error_check (query, static_cast<bool (QSqlQuery::*) (QString const&)> (&QSqlQuery::exec),
"CREATE TABLE cabrillo_log ("
" id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,"
" frequency INTEGER NOT NULL,"
" 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 * 1000ull);
auto band = configuration_->bands ()->find (frequency);
if ("1mm" == band) result = "LIGHT";
else if ("2mm" == band) result = "241G";
else if ("2.5mm" == band) result = "134G";
@@ -125,12 +187,15 @@ QString CabrilloLog::impl::cabrillo_frequency_string (Radio::Frequency frequency
else if ("2m" == band) result = "144";
else if ("4m" == band) result = "70";
else if ("6m" == band) result = "50";
else result = QString::number (frequency);
else result = QString::number (frequency / 1000ull);
return result;
}
CabrilloLog::CabrilloLog (Configuration const * configuration)
: m_ {configuration}
#include "moc_CabrilloLog.cpp"
CabrilloLog::CabrilloLog (Configuration const * configuration, QObject * parent)
: QObject {parent}
, m_ {this, configuration}
{
Q_ASSERT (configuration);
}
@@ -159,11 +224,12 @@ namespace
}
}
bool CabrilloLog::add_QSO (Frequency frequency, QDateTime const& when, QString const& call
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 / 1000ull); // kHz
record.setValue ("frequency", frequency);
record.setValue ("mode", mode);
if (!when.isNull ())
{
record.setValue ("when", when.toMSecsSinceEpoch () / 1000ull);
@@ -175,15 +241,16 @@ bool CabrilloLog::add_QSO (Frequency frequency, QDateTime const& when, QString c
set_value_maybe_null (record, "call", call);
set_value_maybe_null (record, "exchange_sent", exchange_sent);
set_value_maybe_null (record, "exchange_rcvd", exchange_received);
set_value_maybe_null (record, "band", m_->configuration_->bands ()->find (frequency));
if (m_->isDirty ())
{
m_->revert (); // discard any uncommitted changes
}
m_->setEditStrategy (QSqlTableModel::OnManualSubmit);
ConditionalTransaction transaction {*m_};
m_->adding_row_ = true;
auto ok = m_->insertRecord (-1, record);
transaction.submit ();
m_->adding_row_ = false;
m_->setEditStrategy (QSqlTableModel::OnFieldChange);
return ok;
}
@@ -191,10 +258,18 @@ bool CabrilloLog::add_QSO (Frequency frequency, QDateTime const& when, QString c
bool CabrilloLog::dupe (Frequency frequency, QString const& call) const
{
m_->dupe_query_.bindValue (":call", call);
m_->dupe_query_.bindValue (":band", m_->configuration_->bands ()->find (frequency));
SQL_error_check (m_->dupe_query_, static_cast<bool (QSqlQuery::*) ()> (&QSqlQuery::exec));
m_->dupe_query_.next ();
return m_->dupe_query_.value (0).toInt ();
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;
}
void CabrilloLog::reset ()
@@ -209,6 +284,7 @@ void CabrilloLog::reset ()
transaction.submit ();
m_->select (); // to refresh views
m_->setEditStrategy (QSqlTableModel::OnFieldChange);
Q_EMIT data_changed ();
}
}
@@ -217,6 +293,7 @@ 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");
@@ -234,14 +311,16 @@ void CabrilloLog::export_qsos (QTextStream& stream) const
}
}
QSet<QString> CabrilloLog::unique_DXCC_entities (AD1CCty const& countries) const
auto CabrilloLog::unique_DXCC_entities (AD1CCty const * countries) const -> worked_set
{
QSqlQuery q {"SELECT UNIQUE CALL FROM cabrillo_log"};
QSqlQuery q {"SELECT DISTINCT BAND, CALL FROM cabrillo_log"};
auto band_index = q.record ().indexOf ("band");
auto call_index = q.record ().indexOf ("call");
QSet<QString> entities;
worked_set entities;
while (q.next ())
{
entities << countries.lookup (q.value(call_index).toString ()).primary_prefix;
entities << worked_item {q.value (band_index).toString ()
, countries->lookup (q.value (call_index).toString ()).primary_prefix};
}
return entities;
}