Refactored qhelpers AbstractItemView item select ()

This commit is contained in:
xarkes 2019-10-20 13:59:10 +02:00 committed by Itay Cohen
parent 3e0d5a8981
commit cae07d15db
3 changed files with 15 additions and 24 deletions

View File

@ -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. * @brief Select first item of a QAbstractItemView if not empty
* @param tw - QTreeWidget instance * @param itemView
* @return true - setCurrentItem was set, false - tree is empty * @return true if first item was selected
*/ */
bool selectFirstItem(QTreeWidget *tw) bool selectFirstItem(QAbstractItemView *itemView)
{ {
if (tw->topLevelItem(0)) { if (!itemView) {
tw->setCurrentItem(tw->topLevelItem(0)); return false;
}
auto model = itemView->model();
if (!model) {
return false;
}
if (model->hasIndex(0, 0)) {
itemView->setCurrentIndex(model->index(0, 0));
return true; return true;
} }
return false; 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) void setVerticalScrollMode(QAbstractItemView *tw)
{ {
tw->setVerticalScrollMode(scrollMode()); tw->setVerticalScrollMode(scrollMode());

View File

@ -24,7 +24,6 @@ namespace qhelpers {
QString formatBytecount(const long bytecount); QString formatBytecount(const long bytecount);
void adjustColumns(QTreeView *tv, int columnCount, int padding); void adjustColumns(QTreeView *tv, int columnCount, int padding);
void adjustColumns(QTreeWidget *tw, int padding); void adjustColumns(QTreeWidget *tw, int padding);
bool selectFirstItem(QTreeWidget *tw);
bool selectFirstItem(QAbstractItemView *itemView); bool selectFirstItem(QAbstractItemView *itemView);
QTreeWidgetItem *appendRow(QTreeWidget *tw, const QString &str, const QString &str2 = QString(), QTreeWidgetItem *appendRow(QTreeWidget *tw, const QString &str, const QString &str2 = QString(),
const QString &str3 = QString(), const QString &str4 = QString(), const QString &str5 = QString()); const QString &str3 = QString(), const QString &str4 = QString(), const QString &str5 = QString());

View File

@ -152,10 +152,8 @@ void XrefsDialog::fillRefsForAddress(RVA addr, QString name, bool whole_function
qhelpers::adjustColumns(ui->toTreeWidget, toModel.columnCount(), 0); qhelpers::adjustColumns(ui->toTreeWidget, toModel.columnCount(), 0);
// Automatically select the first line // Automatically select the first line
if (toModel.hasIndex(0, 0)) { if (!qhelpers::selectFirstItem(ui->toTreeWidget)) {
ui->toTreeWidget->setCurrentIndex(toModel.index(0, 0)); qhelpers::selectFirstItem(ui->fromTreeWidget);
} else if (fromModel.hasIndex(0, 0)) {
ui->fromTreeWidget->setCurrentIndex(fromModel.index(0, 0));
} }
} }