From c85ed8cc7d417f43d0e25870ea40f09d50cf020a Mon Sep 17 00:00:00 2001 From: Brian Moran Date: Mon, 11 Jul 2022 15:56:55 -0700 Subject: [PATCH] show contacts by ID, scroll to insertion when sorted by tha column up or down --- models/CabrilloLog.cpp | 3 ++ widgets/AbstractLogWindow.cpp | 53 ++++++++++++++++++++++------------- widgets/CabrilloLogWindow.cpp | 3 +- 3 files changed, 39 insertions(+), 20 deletions(-) diff --git a/models/CabrilloLog.cpp b/models/CabrilloLog.cpp index b0459e190..cb13c0ec3 100644 --- a/models/CabrilloLog.cpp +++ b/models/CabrilloLog.cpp @@ -88,6 +88,7 @@ CabrilloLog::impl::impl (CabrilloLog * self, Configuration const * configuration setEditStrategy (QSqlTableModel::OnFieldChange); setTable ("cabrillo_log_v2"); + setHeaderData (fieldIndex ("id"), Qt::Horizontal, tr ("Qso #")); setHeaderData (fieldIndex ("frequency"), Qt::Horizontal, tr ("Freq(MHz)")); setHeaderData (fieldIndex ("mode"), Qt::Horizontal, tr ("Mode")); setHeaderData (fieldIndex ("when"), Qt::Horizontal, tr ("Date & Time(UTC)")); @@ -234,8 +235,10 @@ bool CabrilloLog::add_QSO (Frequency frequency, QString const& mode, QDateTime c m_->adding_row_ = true; auto ok = m_->insertRecord (-1, record); transaction.submit (); + m_->adding_row_ = false; m_->setEditStrategy (QSqlTableModel::OnFieldChange); + return ok; } diff --git a/widgets/AbstractLogWindow.cpp b/widgets/AbstractLogWindow.cpp index b8e2d7e7a..020e34bad 100644 --- a/widgets/AbstractLogWindow.cpp +++ b/widgets/AbstractLogWindow.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #include "Configuration.hpp" #include "SettingsGroup.hpp" #include "MessageBox.hpp" @@ -88,29 +89,37 @@ void AbstractLogWindow::impl::delete_QSOs () } } + AbstractLogWindow::AbstractLogWindow (QString const& settings_key, QSettings * settings , Configuration const * configuration , QWidget * parent) : QWidget {parent} - , m_ {this, settings_key, settings, configuration} -{ - // this attempt to scroll to the last new record doesn't work, some - // sort of issue with model indexes and optimized DB fetches. For - // now sorting by the same column and direction as the underlying DB - // select and that DB select being in descending order so new rows - // at the end appear at view row 0 gets the job done + , m_ {this, settings_key, settings, configuration} { + // when we're viewing the log by contact ID (visually, up/down chevron in the column heading), + // when we add a contact, scroll the list to the top or bottom, depending on the sort order. + // If the table is sorted by some other criteria, don't change anything. - // // ensure view scrolls to latest new row - // connect (&m_->model_, &QAbstractItemModel::rowsInserted, this, [this] (QModelIndex const& parent, int first, int last) { - // // note col 0 is hidden so use col 1 - // // queued connection required otherwise row may not be available - // // in time - // auto index = m_->model_.index (last, 1, parent); - // if (m_->log_view_) - // { - // m_->log_view_->scrollTo (index); - // } - // }, Qt::QueuedConnection); + connect(&m_->model_, &QAbstractItemModel::rowsInserted, this, + [this](QModelIndex const &parent, int first, int last) { + (void) (parent); // UNUSED + (void) (first); // UNUSED + (void) (last); // UNUSED + QTimer::singleShot(0, [=] { + // if we're sorting by the contact #, then show the most-recently logged contact. + // Otherwise, leave the scroll alone + auto horizontal_header = m_->log_view_->horizontalHeader (); + if (horizontal_header->sortIndicatorSection() == 0) { + if (horizontal_header->sortIndicatorOrder() == Qt::AscendingOrder) { + // we're sorting 1->N, so go to bottom + m_->log_view_->scrollToBottom(); + } else { + m_->log_view_->scrollToTop(); + } + } + + }); + } + ); } AbstractLogWindow::~AbstractLogWindow () @@ -134,11 +143,14 @@ void AbstractLogWindow::set_log_view (QTableView * log_view) log_view->setVerticalScrollMode (QAbstractItemView::ScrollPerPixel); m_->model_.setSourceModel (log_view->model ()); log_view->setModel (&m_->model_); - log_view->setColumnHidden (0, true); + log_view->setColumnHidden (0, false); // show the ID column, which is also QSO # auto horizontal_header = log_view->horizontalHeader (); + horizontal_header->setResizeContentsPrecision (0); // visible region only horizontal_header->setSectionResizeMode (QHeaderView::ResizeToContents); horizontal_header->setSectionsMovable (true); + horizontal_header->setSortIndicator(0,Qt::AscendingOrder); // sort by the contact id. show 1->N + auto vertical_header = log_view->horizontalHeader (); vertical_header->setResizeContentsPrecision (0); // visible region only vertical_header->setSectionResizeMode (QHeaderView::ResizeToContents); @@ -149,6 +161,9 @@ void AbstractLogWindow::set_log_view (QTableView * log_view) connect (delete_action, &QAction::triggered, [this] (bool /*checked*/) { m_->delete_QSOs (); }); + + // scroll to bottom, since we're showing 1-N + log_view->scrollToBottom(); } void AbstractLogWindow::set_log_view_font (QFont const& font) diff --git a/widgets/CabrilloLogWindow.cpp b/widgets/CabrilloLogWindow.cpp index 5c219fcd7..e31ed4700 100644 --- a/widgets/CabrilloLogWindow.cpp +++ b/widgets/CabrilloLogWindow.cpp @@ -70,7 +70,8 @@ CabrilloLogWindow::CabrilloLogWindow (QSettings * settings, Configuration const m_->ui_.log_table_view->setItemDelegateForColumn (3, new SQLiteDateTimeDelegate {this}); m_->ui_.log_table_view->setItemDelegateForColumn (4, new CallsignDelegate {this}); auto h_header = m_->ui_.log_table_view->horizontalHeader (); - h_header->moveSection (7, 1); // band to first column + m_->ui_.log_table_view->verticalHeader()->setVisible(false); // turn off line numbers for the table, use index + h_header->moveSection (7, 2); // band to 2nd column } CabrilloLogWindow::~CabrilloLogWindow ()