1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
|
#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_;
bool adding_row_;
};
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 ();
}
});
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\"");
}
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);
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;
}
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;
}
|