From cae07d15db970ea1c9e685e12033b057af4a4ab0 Mon Sep 17 00:00:00 2001 From: xarkes Date: Sun, 20 Oct 2019 13:59:10 +0200 Subject: [PATCH] Refactored qhelpers AbstractItemView item select (#1850) --- src/common/Helpers.cpp | 32 +++++++++++++------------------- src/common/Helpers.h | 1 - src/dialogs/XrefsDialog.cpp | 6 ++---- 3 files changed, 15 insertions(+), 24 deletions(-) diff --git a/src/common/Helpers.cpp b/src/common/Helpers.cpp index 25339648..826790f4 100644 --- a/src/common/Helpers.cpp +++ b/src/common/Helpers.cpp @@ -75,32 +75,26 @@ QTreeWidgetItem *appendRow(QTreeWidget *tw, const QString &str, const QString &s } /** - * @brief select first item of QTreeWidget if tree is not empty. - * @param tw - QTreeWidget instance - * @return true - setCurrentItem was set, false - tree is empty + * @brief Select first item of a QAbstractItemView if not empty + * @param itemView + * @return true if first item was selected */ -bool selectFirstItem(QTreeWidget *tw) +bool selectFirstItem(QAbstractItemView *itemView) { - if (tw->topLevelItem(0)) { - tw->setCurrentItem(tw->topLevelItem(0)); + if (!itemView) { + return false; + } + auto model = itemView->model(); + if (!model) { + return false; + } + if (model->hasIndex(0, 0)) { + itemView->setCurrentIndex(model->index(0, 0)); return true; } return false; } - -bool selectFirstItem(QAbstractItemView *itemView) -{ - auto selectionModel = itemView->selectionModel(); - auto model = itemView->model(); - if (model->hasChildren()) { - selectionModel->setCurrentIndex(model->index(0, 0), QItemSelectionModel::SelectCurrent); - return true; - } else { - return false; - } -} - void setVerticalScrollMode(QAbstractItemView *tw) { tw->setVerticalScrollMode(scrollMode()); diff --git a/src/common/Helpers.h b/src/common/Helpers.h index e3d0f6fe..9275faea 100644 --- a/src/common/Helpers.h +++ b/src/common/Helpers.h @@ -24,7 +24,6 @@ namespace qhelpers { QString formatBytecount(const long bytecount); void adjustColumns(QTreeView *tv, int columnCount, int padding); void adjustColumns(QTreeWidget *tw, int padding); -bool selectFirstItem(QTreeWidget *tw); bool selectFirstItem(QAbstractItemView *itemView); QTreeWidgetItem *appendRow(QTreeWidget *tw, const QString &str, const QString &str2 = QString(), const QString &str3 = QString(), const QString &str4 = QString(), const QString &str5 = QString()); diff --git a/src/dialogs/XrefsDialog.cpp b/src/dialogs/XrefsDialog.cpp index 4c9c93fb..6d98564f 100644 --- a/src/dialogs/XrefsDialog.cpp +++ b/src/dialogs/XrefsDialog.cpp @@ -152,10 +152,8 @@ void XrefsDialog::fillRefsForAddress(RVA addr, QString name, bool whole_function qhelpers::adjustColumns(ui->toTreeWidget, toModel.columnCount(), 0); // Automatically select the first line - if (toModel.hasIndex(0, 0)) { - ui->toTreeWidget->setCurrentIndex(toModel.index(0, 0)); - } else if (fromModel.hasIndex(0, 0)) { - ui->fromTreeWidget->setCurrentIndex(fromModel.index(0, 0)); + if (!qhelpers::selectFirstItem(ui->toTreeWidget)) { + qhelpers::selectFirstItem(ui->fromTreeWidget); } }