From 5d5d16b69fe45e39b043c550e8970da446a336ec Mon Sep 17 00:00:00 2001 From: Ladislav Foldyna Date: Mon, 16 Sep 2024 16:22:04 +0200 Subject: [PATCH] Logbook: Performance tunning - logbook shows the first 5000 QSOs - added focusing after Edit QSO and Delete QSO --- models/LogbookModel.h | 1 - ui/LogbookWidget.cpp | 64 +++++++++++++++++++++++++++++++++++++------ ui/LogbookWidget.h | 1 + 3 files changed, 56 insertions(+), 10 deletions(-) diff --git a/models/LogbookModel.h b/models/LogbookModel.h index 7762a662..3bc45352 100644 --- a/models/LogbookModel.h +++ b/models/LogbookModel.h @@ -15,7 +15,6 @@ class LogbookModel : public QSqlTableModel bool setData(const QModelIndex &index, const QVariant &value, int role) override; void updateExternalServicesUploadStatus( const QModelIndex &index, int role, bool &updateResult ); void updateUploadToModified( const QModelIndex &index, int role, int column, bool &updateResult ); - enum column_id { COLUMN_ID = 0, diff --git a/ui/LogbookWidget.cpp b/ui/LogbookWidget.cpp index 9dac4822..47715232 100644 --- a/ui/LogbookWidget.cpp +++ b/ui/LogbookWidget.cpp @@ -522,6 +522,13 @@ void LogbookWidget::deleteContact() QModelIndexList deletedRowIndexes = ui->contactTable->selectionModel()->selectedRows(); + // Since deletedRowIndexes contains indexes for columns that might be invisible, + // and scrollToIndex needs an index with a visible column, + // we obtain the column index from the first record in the table." + + int firstVisibleColumnIndex = ui->contactTable->indexAt(QPoint(0, 0)).column(); + QModelIndex previousIndex = model->index(deletedRowIndexes.first().row()-1, firstVisibleColumnIndex); + // Clublog does not accept batch DELETE operation // ask if an operator wants to continue if ( ClubLog::isUploadImmediatelyEnabled() @@ -592,6 +599,7 @@ void LogbookWidget::deleteContact() ui->contactTable->setUpdatesEnabled(true); ui->contactTable->horizontalHeader()->restoreState(state); updateTable(); + scrollToIndex(previousIndex); blockClublogSignals = false; } @@ -646,21 +654,34 @@ void LogbookWidget::updateTable() FCT_IDENTIFICATION; model->select(); + + // under normal conditions, only 256 records are loaded. + // This will increase the value of the loaded records. + while ( model->canFetchMore() && model->rowCount() < 5000 ) + model->fetchMore(); + ui->contactTable->resizeColumnsToContents(); // it is not possible to use mode->rowCount here because model contains only - // the first 256 records and rowCount has a value 256 here. Therefore, it is needed - // to run a QSL stateme with Count - QString countRecordsStmt(QLatin1String("SELECT COUNT(1) FROM contacts")); + // the first 5000 records (or more) and rowCount has a value 5000 here. Therefore, it is needed + // to run a QSL stateme with Count. Run it only in case when QTableview does not contain all + // records from model + int qsoCount = 0; + if ( model->canFetchMore() ) + { + QString countRecordsStmt(QLatin1String("SELECT COUNT(1) FROM contacts")); - if ( !model->filter().isEmpty() ) - countRecordsStmt.append(QString(" WHERE %1").arg(model->filter())); + if ( !model->filter().isEmpty() ) + countRecordsStmt.append(QString(" WHERE %1").arg(model->filter())); - QSqlQuery query(countRecordsStmt); + QSqlQuery query(countRecordsStmt); - ui->filteredQSOsLabel->setText(tr("Count: %n", "", query.first() ? query.value(0).toInt() - : 0)); - ui->contactTable->scrollToTop(); + qsoCount = query.first() ? query.value(0).toInt() : 0; + } + else + qsoCount = model->rowCount(); + + ui->filteredQSOsLabel->setText(tr("Count: %n", "", qsoCount)); emit logbookUpdated(); } @@ -761,6 +782,7 @@ void LogbookWidget::doubleClickColumn(QModelIndex modelIndex) }); dialog.exec(); updateTable(); + scrollToIndex(modelIndex); } } @@ -808,6 +830,30 @@ void LogbookWidget::sendDXCSpot() emit sendDXSpotContactReq(model->record(selectedIndexes.at(0).row())); } +void LogbookWidget::scrollToIndex(const QModelIndex &index, bool selectItem) +{ + FCT_IDENTIFICATION; + + if ( index == QModelIndex() ) + return; + + // is index visible ? + if ( ui->contactTable->visualRect(index).isEmpty() ) + { + while ( model->canFetchMore() && ui->contactTable->visualRect(index).isEmpty() ) + { + model->fetchMore(); + } + + if ( model->canFetchMore() ) + model->fetchMore(); // one more fetch + } + + ui->contactTable->scrollTo(index, QAbstractItemView::PositionAtCenter); + if ( selectItem ) + ui->contactTable->selectRow(index.row()); +} + bool LogbookWidget::eventFilter(QObject *obj, QEvent *event) { //FCT_IDENTIFICATION; diff --git a/ui/LogbookWidget.h b/ui/LogbookWidget.h index f5b6508d..b7130f51 100644 --- a/ui/LogbookWidget.h +++ b/ui/LogbookWidget.h @@ -72,6 +72,7 @@ public slots: void focusSearchCallsign(); void reloadSetting(); void sendDXCSpot(); + void scrollToIndex(const QModelIndex& index, bool select = true); private: ClubLog* clublog;