diff --git a/CMakeLists.txt b/CMakeLists.txt index d5d18467c..a2d6dd396 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -237,6 +237,7 @@ set (wsjt_qt_CXXSRCS models/FrequencyList.cpp models/StationList.cpp widgets/FrequencyLineEdit.cpp + widgets/FrequencyDeltaLineEdit.cpp item_delegates/CandidateKeyFilter.cpp item_delegates/ForeignKeyDelegate.cpp validators/LiveFrequencyValidator.cpp @@ -279,9 +280,12 @@ set (wsjt_qt_CXXSRCS widgets/CabrilloLogWindow.cpp item_delegates/CallsignDelegate.cpp item_delegates/MaidenheadLocatorDelegate.cpp + item_delegates/FrequencyDelegate.cpp + item_delegates/FrequencyDeltaDelegate.cpp models/CabrilloLog.cpp logbook/AD1CCty.cpp logbook/WorkedBefore.cpp + logbook/Multiplier.cpp ) set (wsjt_qtmm_CXXSRCS diff --git a/Configuration.cpp b/Configuration.cpp index 08ee2e9d3..b78739d11 100644 --- a/Configuration.cpp +++ b/Configuration.cpp @@ -167,8 +167,11 @@ #include "MetaDataRegistry.hpp" #include "SettingsGroup.hpp" #include "widgets/FrequencyLineEdit.hpp" +#include "widgets/FrequencyDeltaLineEdit.hpp" #include "item_delegates/CandidateKeyFilter.hpp" #include "item_delegates/ForeignKeyDelegate.hpp" +#include "item_delegates/FrequencyDelegate.hpp" +#include "item_delegates/FrequencyDeltaDelegate.hpp" #include "TransceiverFactory.hpp" #include "Transceiver.hpp" #include "models/Bands.hpp" @@ -1122,6 +1125,7 @@ Configuration::impl::impl (Configuration * self, QNetworkAccessManager * network ui_->frequencies_table_view->setColumnHidden (FrequencyList_v2::frequency_mhz_column, true); // delegates + ui_->frequencies_table_view->setItemDelegateForColumn (FrequencyList_v2::frequency_column, new FrequencyDelegate {this}); ui_->frequencies_table_view->setItemDelegateForColumn (FrequencyList_v2::region_column, new ForeignKeyDelegate {®ions_, 0, this}); ui_->frequencies_table_view->setItemDelegateForColumn (FrequencyList_v2::mode_column, new ForeignKeyDelegate {&modes_, 0, this}); @@ -1160,6 +1164,7 @@ Configuration::impl::impl (Configuration * self, QNetworkAccessManager * network ui_->stations_table_view->sortByColumn (StationList::band_column, Qt::AscendingOrder); // stations delegates + ui_->stations_table_view->setItemDelegateForColumn (StationList::offset_column, new FrequencyDeltaDelegate {this}); ui_->stations_table_view->setItemDelegateForColumn (StationList::band_column, new ForeignKeyDelegate {&bands_, &next_stations_, 0, StationList::band_column, this}); // stations actions diff --git a/MetaDataRegistry.cpp b/MetaDataRegistry.cpp index 32af71b4d..e66a43008 100644 --- a/MetaDataRegistry.cpp +++ b/MetaDataRegistry.cpp @@ -14,7 +14,6 @@ #include "WFPalette.hpp" #include "models/IARURegions.hpp" #include "models/DecodeHighlightingModel.hpp" -#include "widgets/FrequencyLineEdit.hpp" #include "widgets/DateTimeEdit.hpp" namespace @@ -51,9 +50,6 @@ void register_types () // used as signal/slot connection arguments since the new Qt 5.5 // Q_ENUM macro only seems to register the unqualified name - item_editor_factory->registerEditor (qMetaTypeId (), new QStandardItemEditorCreator ()); - //auto frequency_delta_type_id = qRegisterMetaType ("FrequencyDelta"); - item_editor_factory->registerEditor (qMetaTypeId (), new QStandardItemEditorCreator ()); item_editor_factory->registerEditor (qMetaTypeId (), new QStandardItemEditorCreator ()); // Frequency list model diff --git a/Radio.cpp b/Radio.cpp index 3940815f4..e5b34c35d 100644 --- a/Radio.cpp +++ b/Radio.cpp @@ -74,14 +74,14 @@ namespace Radio } - QString frequency_MHz_string (Frequency f, QLocale const& locale) + QString frequency_MHz_string (Frequency f, int precision, QLocale const& locale) { - return locale.toString (f / MHz_factor, 'f', frequency_precsion); + return locale.toString (f / MHz_factor, 'f', precision); } - QString frequency_MHz_string (FrequencyDelta d, QLocale const& locale) + QString frequency_MHz_string (FrequencyDelta d, int precision, QLocale const& locale) { - return locale.toString (d / MHz_factor, 'f', frequency_precsion); + return locale.toString (d / MHz_factor, 'f', precision); } QString pretty_frequency_MHz_string (Frequency f, QLocale const& locale) diff --git a/Radio.hpp b/Radio.hpp index 7f8b0a0f4..e63abf4b8 100644 --- a/Radio.hpp +++ b/Radio.hpp @@ -42,8 +42,8 @@ namespace Radio // // Frequency type formatting // - QString UDP_EXPORT frequency_MHz_string (Frequency, QLocale const& = QLocale ()); - QString UDP_EXPORT frequency_MHz_string (FrequencyDelta, QLocale const& = QLocale ()); + QString UDP_EXPORT frequency_MHz_string (Frequency, int precision = 6, QLocale const& = QLocale ()); + QString UDP_EXPORT frequency_MHz_string (FrequencyDelta, int precision = 6, QLocale const& = QLocale ()); QString UDP_EXPORT pretty_frequency_MHz_string (Frequency, QLocale const& = QLocale ()); QString UDP_EXPORT pretty_frequency_MHz_string (double, int scale, QLocale const& = QLocale ()); QString UDP_EXPORT pretty_frequency_MHz_string (FrequencyDelta, QLocale const& = QLocale ()); diff --git a/item_delegates/FrequencyDelegate.cpp b/item_delegates/FrequencyDelegate.cpp new file mode 100644 index 000000000..b899c0563 --- /dev/null +++ b/item_delegates/FrequencyDelegate.cpp @@ -0,0 +1,27 @@ +#include "FrequencyDelegate.hpp" + +#include "widgets/FrequencyLineEdit.hpp" + +FrequencyDelegate::FrequencyDelegate (QObject * parent) + : QStyledItemDelegate {parent} +{ +} + +QWidget * FrequencyDelegate::createEditor (QWidget * parent, QStyleOptionViewItem const& + , QModelIndex const&) const +{ + auto * editor = new FrequencyLineEdit {parent}; + editor->setFrame (false); + return editor; +} + +void FrequencyDelegate::setEditorData (QWidget * editor, QModelIndex const& index) const +{ + static_cast (editor)->frequency (index.model ()->data (index, Qt::EditRole).value ()); +} + +void FrequencyDelegate::setModelData (QWidget * editor, QAbstractItemModel * model, QModelIndex const& index) const +{ + model->setData (index, static_cast (editor)->frequency (), Qt::EditRole); +} + diff --git a/item_delegates/FrequencyDelegate.hpp b/item_delegates/FrequencyDelegate.hpp new file mode 100644 index 000000000..76fc5545d --- /dev/null +++ b/item_delegates/FrequencyDelegate.hpp @@ -0,0 +1,21 @@ +#ifndef FREQUENCY_DELEGATE_HPP_ +#define FREQUENCY_DELEGATE_HPP_ + +#include + +// +// Class FrequencyDelegate +// +// Item delegate for editing a frequency in Hertz but displayed in MHz +// +class FrequencyDelegate final + : public QStyledItemDelegate +{ +public: + explicit FrequencyDelegate (QObject * parent = nullptr); + QWidget * createEditor (QWidget * parent, QStyleOptionViewItem const&, QModelIndex const&) const override; + void setEditorData (QWidget * editor, QModelIndex const&) const override; + void setModelData (QWidget * editor, QAbstractItemModel *, QModelIndex const&) const override; +}; + +#endif diff --git a/item_delegates/FrequencyDeltaDelegate.cpp b/item_delegates/FrequencyDeltaDelegate.cpp new file mode 100644 index 000000000..40d90dc58 --- /dev/null +++ b/item_delegates/FrequencyDeltaDelegate.cpp @@ -0,0 +1,26 @@ +#include "FrequencyDeltaDelegate.hpp" + +#include "widgets/FrequencyDeltaLineEdit.hpp" + +FrequencyDeltaDelegate::FrequencyDeltaDelegate (QObject * parent) + : QStyledItemDelegate {parent} +{ +} + +QWidget * FrequencyDeltaDelegate::createEditor (QWidget * parent, QStyleOptionViewItem const& + , QModelIndex const&) const +{ + auto * editor = new FrequencyDeltaLineEdit {parent}; + editor->setFrame (false); + return editor; +} + +void FrequencyDeltaDelegate::setEditorData (QWidget * editor, QModelIndex const& index) const +{ + static_cast (editor)->frequency_delta (index.model ()->data (index, Qt::EditRole).value ()); +} + +void FrequencyDeltaDelegate::setModelData (QWidget * editor, QAbstractItemModel * model, QModelIndex const& index) const +{ + model->setData (index, static_cast (editor)->frequency_delta (), Qt::EditRole); +} diff --git a/item_delegates/FrequencyDeltaDelegate.hpp b/item_delegates/FrequencyDeltaDelegate.hpp new file mode 100644 index 000000000..121bc3d94 --- /dev/null +++ b/item_delegates/FrequencyDeltaDelegate.hpp @@ -0,0 +1,21 @@ +#ifndef FREQUENCY_DELTA_DELEGATE_HPP_ +#define FREQUENCY_DELTA_DELEGATE_HPP_ + +#include + +// +// Class FrequencyDeltaDelegate +// +// Item delegate for editing a frequency delta in Hertz but displayed in MHz +// +class FrequencyDeltaDelegate final + : public QStyledItemDelegate +{ +public: + explicit FrequencyDeltaDelegate (QObject * parent = nullptr); + QWidget * createEditor (QWidget * parent, QStyleOptionViewItem const&, QModelIndex const&) const override; + void setEditorData (QWidget * editor, QModelIndex const&) const override; + void setModelData (QWidget * editor, QAbstractItemModel *, QModelIndex const&) const override; +}; + +#endif diff --git a/item_delegates/item_delegates.pri b/item_delegates/item_delegates.pri index 026e1ca57..819cb2e0c 100644 --- a/item_delegates/item_delegates.pri +++ b/item_delegates/item_delegates.pri @@ -1,11 +1,13 @@ SOURCES += \ item_delegates/ForeignKeyDelegate.cpp \ - item_delegates/FrequencyItemDelegate.cpp \ + item_delegates/FrequencyDelegate.cpp \ + item_delegates/FrequencyDeltaDelegate.cpp \ item_delegates/CallsignDelegate.cpp \ item_delegates/MaidenheadLocatorItemDelegate.cpp HEADERS += \ item_delegates/ForeignKeyDelegate.hpp \ - item_delegates/FrequencyItemDelegate.hpp \ + item_delegates/FrequencyDelegate.hpp \ + item_delegates/FrequencyDeltaDelegate.hpp \ item_delegates/CallsignDelegate.hpp \ item_delegates/MaidenheadLocatorDelegate.hpp diff --git a/logbook/Multiplier.cpp b/logbook/Multiplier.cpp new file mode 100644 index 000000000..64f6cc6f3 --- /dev/null +++ b/logbook/Multiplier.cpp @@ -0,0 +1,45 @@ +#include "Multiplier.hpp" + +#include +#include +#include +#include "models/CabrilloLog.hpp" +#include "pimpl_impl.hpp" + +class Multiplier::impl +{ +public: + impl (AD1CCty const * countries) + : countries_ {countries} + { + } + + AD1CCty const * countries_; + worked_set entities_worked_; + worked_set grids_worked_; +}; + +Multiplier::Multiplier (AD1CCty const * countries) + : m_ {countries} +{ +} + +Multiplier::~Multiplier () +{ +} + +void Multiplier::reload (CabrilloLog const * log) +{ + m_->entities_worked_ = log->unique_DXCC_entities (m_->countries_); + qDebug () << "Entities worked:" << m_->entities_worked_; +} + +auto Multiplier::entities_worked () const -> worked_set const& +{ + return m_->entities_worked_; +} + +auto Multiplier::grids_worked () const -> worked_set const& +{ + return m_->grids_worked_; +} diff --git a/logbook/Multiplier.hpp b/logbook/Multiplier.hpp new file mode 100644 index 000000000..2271dd775 --- /dev/null +++ b/logbook/Multiplier.hpp @@ -0,0 +1,30 @@ +#ifndef MULTIPLIER_HPP_ +#define MULTIPLIER_HPP_ + +#include +#include +#include "pimpl_h.hpp" + +class QString; +class AD1CCty; +class CabrilloLog; + +class Multiplier final + : private boost::noncopyable +{ +public: + using worked_item = QPair; + using worked_set = QSet; + + explicit Multiplier (AD1CCty const *); + ~Multiplier (); + void reload (CabrilloLog const *); + worked_set const& entities_worked () const; + worked_set const& grids_worked () const; + +private: + class impl; + pimpl m_; +}; + +#endif diff --git a/logbook/WorkedBefore.cpp b/logbook/WorkedBefore.cpp index 5edc51d3e..aa65ab651 100644 --- a/logbook/WorkedBefore.cpp +++ b/logbook/WorkedBefore.cpp @@ -415,9 +415,9 @@ QString const& WorkedBefore::path () const return m_->path_; } -AD1CCty const& WorkedBefore::countries () const +AD1CCty const * WorkedBefore::countries () const { - return m_->prefixes_; + return &m_->prefixes_; } bool WorkedBefore::add (QString const& call diff --git a/logbook/WorkedBefore.hpp b/logbook/WorkedBefore.hpp index 8b115351f..1aae1aca4 100644 --- a/logbook/WorkedBefore.hpp +++ b/logbook/WorkedBefore.hpp @@ -29,7 +29,7 @@ public: , QByteArray const& ADIF_record); QString const& path () const; - AD1CCty const& countries () const; + AD1CCty const * countries () const; bool country_worked (QString const& call, QString const& mode, QString const& band) const; bool grid_worked (QString const& grid, QString const& mode, QString const& band) const; bool call_worked (QString const& call, QString const& mode, QString const& band) const; diff --git a/logbook/logbook.cpp b/logbook/logbook.cpp index bde52228c..a154664d5 100644 --- a/logbook/logbook.cpp +++ b/logbook/logbook.cpp @@ -3,6 +3,8 @@ #include #include "Configuration.hpp" #include "AD1CCty.hpp" +#include "Multiplier.hpp" +#include "logbook/AD1CCty.hpp" #include "models/CabrilloLog.hpp" #include "models/FoxLog.hpp" @@ -16,6 +18,10 @@ LogBook::LogBook (Configuration const * configuration) connect (&worked_before_, &WorkedBefore::finished_loading, this, &LogBook::finished_loading); } +LogBook::~LogBook () +{ +} + void LogBook::match (QString const& call, QString const& mode, QString const& grid, AD1CCty::Record const& looked_up, bool& callB4, @@ -144,19 +150,36 @@ QByteArray LogBook::QSOToADIF (QString const& hisCall, QString const& hisGrid, Q CabrilloLog * LogBook::contest_log () { // lazy create of Cabrillo log object instance - if (!m_contest_log) + if (!contest_log_) { - m_contest_log.reset (new CabrilloLog {config_}); + contest_log_.reset (new CabrilloLog {config_}); + if (!multiplier_) + { + multiplier_.reset (new Multiplier {countries ()}); + } + connect (contest_log_.data (), &CabrilloLog::data_changed, [this] () { + multiplier_->reload (contest_log_.data ()); + }); } - return m_contest_log.data (); + return contest_log_.data (); +} + +Multiplier const * LogBook::multiplier () const +{ + // lazy create of Multiplier object instance + if (!multiplier_) + { + multiplier_.reset (new Multiplier {countries ()}); + } + return multiplier_.data (); } FoxLog * LogBook::fox_log () { // lazy create of Fox log object instance - if (!m_fox_log) + if (!fox_log_) { - m_fox_log.reset (new FoxLog {config_}); + fox_log_.reset (new FoxLog {config_}); } - return m_fox_log.data (); + return fox_log_.data (); } diff --git a/logbook/logbook.h b/logbook/logbook.h index 2fd73bd3b..c87009e48 100644 --- a/logbook/logbook.h +++ b/logbook/logbook.h @@ -16,6 +16,7 @@ class Configuration; class QByteArray; class QDateTime; class CabrilloLog; +class Multiplier; class FoxLog; class LogBook final @@ -25,13 +26,14 @@ class LogBook final public: LogBook (Configuration const *); + ~LogBook (); QString const& path () const {return worked_before_.path ();} bool add (QString const& call , QString const& grid , QString const& band , QString const& mode , QByteArray const& ADIF_record); - AD1CCty const& countries () const {return worked_before_.countries ();} + AD1CCty const * countries () const {return worked_before_.countries ();} void rescan (); void match (QString const& call, QString const& mode, QString const& grid, AD1CCty::Record const&, bool& callB4, bool& countryB4, @@ -47,13 +49,15 @@ public: Q_SIGNAL void finished_loading (int worked_before_record_count, QString const& error) const; CabrilloLog * contest_log (); + Multiplier const * multiplier () const; FoxLog * fox_log (); private: Configuration const * config_; WorkedBefore worked_before_; - QScopedPointer m_contest_log; - QScopedPointer m_fox_log; + QScopedPointer contest_log_; + QScopedPointer mutable multiplier_; + QScopedPointer fox_log_; }; #endif diff --git a/logbook/logbook.pri b/logbook/logbook.pri index fcac62524..e67cdebd3 100644 --- a/logbook/logbook.pri +++ b/logbook/logbook.pri @@ -2,10 +2,12 @@ SOURCES += \ logbook/countriesworked.cpp \ logbook/logbook.cpp \ logbook/AD1CCty.cpp \ - logbook/WorkedBefore.cpp + logbook/WorkedBefore.cpp \ + logbook/Multiplier.cpp HEADERS += \ logbook/WorkedBefore.hpp \ logbook/logbook.h \ logbook/countriesworked.h \ - logbook/AD1CCty.hpp + logbook/AD1CCty.hpp \ + logbook/Multiplier.hpp diff --git a/main.cpp b/main.cpp index 1ea0c6a49..9070ce33f 100644 --- a/main.cpp +++ b/main.cpp @@ -380,10 +380,12 @@ int main(int argc, char *argv[]) } catch (std::exception const& e) { + MessageBox::critical_message (nullptr, QApplication::translate ("main", "Fatal error"), e.what ()); std::cerr << "Error: " << e.what () << '\n'; } catch (...) { + MessageBox::critical_message (nullptr, QApplication::translate ("main", "Unexpected fatal error")); std::cerr << "Unexpected fatal error\n"; throw; // hoping the runtime might tell us more about the exception } diff --git a/models/CabrilloLog.cpp b/models/CabrilloLog.cpp index 6a637f07d..5c3b0ef5b 100644 --- a/models/CabrilloLog.cpp +++ b/models/CabrilloLog.cpp @@ -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 (), 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 (&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 (&QSqlQuery::exec), + "CREATE TABLE cabrillo_log_backup AS SELECT * FROM cabrillo_log"); + SQL_error_check (query, static_cast (&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 (&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 (&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 (&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 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 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; } diff --git a/models/CabrilloLog.hpp b/models/CabrilloLog.hpp index ad0255297..97d8f0d88 100644 --- a/models/CabrilloLog.hpp +++ b/models/CabrilloLog.hpp @@ -1,8 +1,9 @@ #ifndef CABRILLO_LOG_HPP_ #define CABRILLO_LOG_HPP_ -#include +#include #include +#include #include #include "Radio.hpp" #include "pimpl_h.hpp" @@ -14,23 +15,29 @@ class QTextStream; class AD1CCty; class CabrilloLog final - : private boost::noncopyable + : public QObject { + Q_OBJECT + public: using Frequency = Radio::Frequency; + using worked_item = QPair; + using worked_set = QSet; - explicit CabrilloLog (Configuration const *); + explicit CabrilloLog (Configuration const *, QObject * parent = nullptr); ~CabrilloLog (); // returns false if insert fails - bool add_QSO (Frequency, QDateTime const&, QString const& call + 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; QSqlTableModel * model (); void reset (); void export_qsos (QTextStream&) const; - QSet unique_DXCC_entities (AD1CCty const&) const; + worked_set unique_DXCC_entities (AD1CCty const *) const; + + Q_SIGNAL void data_changed () const; private: class impl; diff --git a/qt_db_helpers.hpp b/qt_db_helpers.hpp index 49a082f72..dcf9d0333 100644 --- a/qt_db_helpers.hpp +++ b/qt_db_helpers.hpp @@ -6,6 +6,7 @@ #include #include #include +#include "boost/core/noncopyable.hpp" template void SQL_error_check (T&& object, Func func, Args&&... args) diff --git a/widgets/CabrilloLogWindow.cpp b/widgets/CabrilloLogWindow.cpp index 2f7b44d4a..6fd753136 100644 --- a/widgets/CabrilloLogWindow.cpp +++ b/widgets/CabrilloLogWindow.cpp @@ -1,10 +1,12 @@ #include "CabrilloLogWindow.hpp" +#include #include #include #include #include "Configuration.hpp" #include "models/Bands.hpp" +#include "item_delegates/FrequencyDelegate.hpp" #include "item_delegates/ForeignKeyDelegate.hpp" #include "item_delegates/CallsignDelegate.hpp" #include "pimpl_impl.hpp" @@ -29,7 +31,7 @@ namespace switch (index.column ()) { case 1: - case 6: + case 7: return Qt::AlignRight + Qt::AlignVCenter; default: break; @@ -63,10 +65,10 @@ CabrilloLogWindow::CabrilloLogWindow (QSettings * settings, Configuration const m_->format_model_.setSourceModel (m_->log_model_); m_->ui_.log_table_view->setModel (&m_->format_model_); set_log_view (m_->ui_.log_table_view); - m_->ui_.log_table_view->setItemDelegateForColumn (3, new CallsignDelegate {this}); - m_->ui_.log_table_view->setItemDelegateForColumn (6, new ForeignKeyDelegate {configuration->bands (), 0, this}); + m_->ui_.log_table_view->setItemDelegateForColumn (1, new FrequencyDelegate {this}); + m_->ui_.log_table_view->setItemDelegateForColumn (4, new CallsignDelegate {this}); auto h_header = m_->ui_.log_table_view->horizontalHeader (); - h_header->moveSection (6, 1); // band to first column + h_header->moveSection (7, 1); // band to first column } CabrilloLogWindow::~CabrilloLogWindow () diff --git a/widgets/FrequencyDeltaLineEdit.cpp b/widgets/FrequencyDeltaLineEdit.cpp new file mode 100644 index 000000000..b7ed144b6 --- /dev/null +++ b/widgets/FrequencyDeltaLineEdit.cpp @@ -0,0 +1,54 @@ +#include "FrequencyDeltaLineEdit.hpp" + +#include + +#include +#include +#include + +#include "moc_FrequencyDeltaLineEdit.cpp" + +namespace +{ + class MHzValidator + : public QDoubleValidator + { + public: + MHzValidator (double bottom, double top, QObject * parent = nullptr) + : QDoubleValidator {bottom, top, 6, parent} + { + } + + State validate (QString& input, int& pos) const override + { + State result = QDoubleValidator::validate (input, pos); + if (Acceptable == result) + { + bool ok; + (void)QLocale {}.toDouble (input, &ok); + if (!ok) + { + result = Intermediate; + } + } + return result; + } + }; +} + +FrequencyDeltaLineEdit::FrequencyDeltaLineEdit (QWidget * parent) + : QLineEdit (parent) +{ + setValidator (new MHzValidator {-std::numeric_limits::max () / 10.e6, + std::numeric_limits::max () / 10.e6, this}); +} + +auto FrequencyDeltaLineEdit::frequency_delta () const -> FrequencyDelta +{ + return Radio::frequency_delta (text (), 6); +} + +void FrequencyDeltaLineEdit::frequency_delta (FrequencyDelta d) +{ + setText (Radio::frequency_MHz_string (d)); +} diff --git a/widgets/FrequencyDeltaLineEdit.hpp b/widgets/FrequencyDeltaLineEdit.hpp new file mode 100644 index 000000000..caa00ecba --- /dev/null +++ b/widgets/FrequencyDeltaLineEdit.hpp @@ -0,0 +1,29 @@ +#ifndef FREQUENCY_DELTA_LINE_EDIT_HPP_ +#define FREQUENCY_DELTA_LINE_EDIT_HPP_ + +#include + +#include "Radio.hpp" + +class QWidget; + +// +// MHz frequency delta line edit with validation +// +class FrequencyDeltaLineEdit final + : public QLineEdit +{ + Q_OBJECT; + Q_PROPERTY (FrequencyDelta frequency_delta READ frequency_delta WRITE frequency_delta USER true); + +public: + using FrequencyDelta = Radio::FrequencyDelta; + + explicit FrequencyDeltaLineEdit (QWidget * parent = nullptr); + + // Property frequency_delta implementation + FrequencyDelta frequency_delta () const; + void frequency_delta (FrequencyDelta); +}; + +#endif diff --git a/widgets/FrequencyLineEdit.cpp b/widgets/FrequencyLineEdit.cpp index 78f8b2e1f..1ca2ba20b 100644 --- a/widgets/FrequencyLineEdit.cpp +++ b/widgets/FrequencyLineEdit.cpp @@ -51,21 +51,3 @@ void FrequencyLineEdit::frequency (Frequency f) { setText (Radio::frequency_MHz_string (f)); } - - -FrequencyDeltaLineEdit::FrequencyDeltaLineEdit (QWidget * parent) - : QLineEdit (parent) -{ - setValidator (new MHzValidator {-std::numeric_limits::max () / 10.e6, - std::numeric_limits::max () / 10.e6, this}); -} - -auto FrequencyDeltaLineEdit::frequency_delta () const -> FrequencyDelta -{ - return Radio::frequency_delta (text (), 6); -} - -void FrequencyDeltaLineEdit::frequency_delta (FrequencyDelta d) -{ - setText (Radio::frequency_MHz_string (d)); -} diff --git a/widgets/FrequencyLineEdit.hpp b/widgets/FrequencyLineEdit.hpp index dec0feb00..7e535f5a9 100644 --- a/widgets/FrequencyLineEdit.hpp +++ b/widgets/FrequencyLineEdit.hpp @@ -8,7 +8,7 @@ class QWidget; // -// MHz frequency line edits with validation +// MHz frequency line edit with validation // class FrequencyLineEdit final : public QLineEdit @@ -26,20 +26,4 @@ public: void frequency (Frequency); }; -class FrequencyDeltaLineEdit final - : public QLineEdit -{ - Q_OBJECT; - Q_PROPERTY (FrequencyDelta frequency_delta READ frequency_delta WRITE frequency_delta USER true); - -public: - using FrequencyDelta = Radio::FrequencyDelta; - - explicit FrequencyDeltaLineEdit (QWidget * parent = nullptr); - - // Property frequency_delta implementation - FrequencyDelta frequency_delta () const; - void frequency_delta (FrequencyDelta); -}; - #endif diff --git a/widgets/displaytext.cpp b/widgets/displaytext.cpp index c0bcd4129..53d7cd5cd 100644 --- a/widgets/displaytext.cpp +++ b/widgets/displaytext.cpp @@ -271,7 +271,7 @@ QString DisplayText::appendWorkedB4 (QString message, QString call, QString cons if(call.length()<3) return message; if(!call.contains(QRegExp("[0-9]|[A-Z]"))) return message; - auto const& looked_up = logBook.countries ().lookup (call); + auto const& looked_up = logBook.countries ()->lookup (call); logBook.match (call, currentMode, grid, looked_up, callB4, countryB4, gridB4, continentB4, CQZoneB4, ITUZoneB4); logBook.match (call, currentMode, grid, looked_up, callB4onBand, countryB4onBand, gridB4onBand, continentB4onBand, CQZoneB4onBand, ITUZoneB4onBand, currentBand); diff --git a/widgets/displaytext.h b/widgets/displaytext.h index 7f308bef8..8b580b8a2 100644 --- a/widgets/displaytext.h +++ b/widgets/displaytext.h @@ -65,6 +65,4 @@ private: int modified_vertical_scrollbar_max_; }; - extern QHash m_LoTW; - #endif // DISPLAYTEXT_H diff --git a/widgets/logqso.cpp b/widgets/logqso.cpp index a9aa18341..2eaedf116 100644 --- a/widgets/logqso.cpp +++ b/widgets/logqso.cpp @@ -157,7 +157,7 @@ void LogQSO::accept() return; // without accepting } - if (!m_log->contest_log ()->add_QSO (m_dialFreq, dateTimeOff, hisCall, xsent, xrcvd)) + if (!m_log->contest_log ()->add_QSO (m_dialFreq, mode, dateTimeOff, hisCall, xsent, xrcvd)) { show (); MessageBox::warning_message (this, tr ("Invalid QSO Data"), diff --git a/widgets/mainwindow.cpp b/widgets/mainwindow.cpp index ef1ca5705..277dbfdd7 100644 --- a/widgets/mainwindow.cpp +++ b/widgets/mainwindow.cpp @@ -8267,7 +8267,7 @@ void MainWindow::houndCallers() if(!ui->textBrowser4->toPlainText().contains(paddedHoundCall)) { if(m_loggedByFox[houndCall].contains(m_lastBand)) continue; //already logged on this band if(m_foxQSO.contains(houndCall)) continue; //still in the QSO map - auto const& entity = m_logBook.countries ().lookup (houndCall); + auto const& entity = m_logBook.countries ()->lookup (houndCall); auto const& continent = AD1CCty::continent (entity.continent); //If we are using a directed CQ, ignore Hound calls that do not comply. diff --git a/widgets/widgets.pri b/widgets/widgets.pri index eef0c7857..3c2cb2444 100644 --- a/widgets/widgets.pri +++ b/widgets/widgets.pri @@ -8,13 +8,14 @@ SOURCES += \ widgets/fastplot.cpp widgets/MessageBox.cpp \ widgets/colorhighlighting.cpp widgets/ExportCabrillo.cpp \ widgets/AbstractLogWindow.cpp \ + widgets/FrequencyLineEdit.cpp widgets/FrequencyDeltaLineEdit.cpp \ widgets/FoxLogWindow.cpp widgets/CabrilloLogWindow.cpp HEADERS += \ widgets/mainwindow.h widgets/plotter.h \ widgets/about.h widgets/widegraph.h \ widgets/displaytext.h widgets/logqso.h widgets/LettersSpinBox.hpp \ - widgets/FrequencyLineEdit.hpp widgets/signalmeter.h \ + widgets/FrequencyLineEdit.hpp widgets/FrequencyDeltaLineEdit.hpp widgets/signalmeter.h \ widgets/meterwidget.h widgets/messageaveraging.h \ widgets/echoplot.h widgets/echograph.h widgets/fastgraph.h \ widgets/fastplot.h widgets/MessageBox.hpp widgets/colorhighlighting.h \