Fix sorting of reports

* Fixes #4976
This commit is contained in:
Jonathan White 2020-09-14 07:02:00 -04:00
parent e391dd182d
commit 3c5bd0ff6b
2 changed files with 48 additions and 2 deletions

View File

@ -76,6 +76,27 @@ namespace
QList<QSharedPointer<Item>> m_items;
bool m_anyKnownBad = false;
};
class ReportSortProxyModel : public QSortFilterProxyModel
{
public:
ReportSortProxyModel(QObject* parent)
: QSortFilterProxyModel(parent){};
~ReportSortProxyModel() override = default;
protected:
bool lessThan(const QModelIndex& left, const QModelIndex& right) const override
{
// Check if the display data is a number, convert and compare if so
bool ok = false;
int leftInt = sourceModel()->data(left).toString().toInt(&ok);
if (ok) {
return leftInt < sourceModel()->data(right).toString().toInt();
}
// Otherwise use default sorting
return QSortFilterProxyModel::lessThan(left, right);
}
};
} // namespace
Health::Health(QSharedPointer<Database> db)
@ -121,11 +142,12 @@ ReportsWidgetHealthcheck::ReportsWidgetHealthcheck(QWidget* parent)
, m_ui(new Ui::ReportsWidgetHealthcheck())
, m_errorIcon(Resources::instance()->icon("dialog-error"))
, m_referencesModel(new QStandardItemModel(this))
, m_modelProxy(new QSortFilterProxyModel(this))
, m_modelProxy(new ReportSortProxyModel(this))
{
m_ui->setupUi(this);
m_modelProxy->setSourceModel(m_referencesModel.data());
m_modelProxy->setSortLocaleAware(true);
m_ui->healthcheckTableView->setModel(m_modelProxy.data());
m_ui->healthcheckTableView->setSelectionMode(QAbstractItemView::NoSelection);
m_ui->healthcheckTableView->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
@ -256,6 +278,7 @@ void ReportsWidgetHealthcheck::calculateHealth()
} else {
m_referencesModel->setHorizontalHeaderLabels(QStringList() << tr("") << tr("Title") << tr("Path") << tr("Score")
<< tr("Reason"));
m_ui->healthcheckTableView->sortByColumn(0, Qt::AscendingOrder);
}
m_ui->healthcheckTableView->resizeRowsToContents();

View File

@ -45,17 +45,38 @@ namespace
return entry->customData()->contains(PasswordHealth::OPTION_KNOWN_BAD)
&& entry->customData()->value(PasswordHealth::OPTION_KNOWN_BAD) == TRUE_STR;
}
class ReportSortProxyModel : public QSortFilterProxyModel
{
public:
ReportSortProxyModel(QObject* parent)
: QSortFilterProxyModel(parent){};
~ReportSortProxyModel() override = default;
protected:
bool lessThan(const QModelIndex& left, const QModelIndex& right) const override
{
// Sort count column by user data
if (left.column() == 2) {
return sourceModel()->data(left, Qt::UserRole).toInt()
< sourceModel()->data(right, Qt::UserRole).toInt();
}
// Otherwise use default sorting
return QSortFilterProxyModel::lessThan(left, right);
}
};
} // namespace
ReportsWidgetHibp::ReportsWidgetHibp(QWidget* parent)
: QWidget(parent)
, m_ui(new Ui::ReportsWidgetHibp())
, m_referencesModel(new QStandardItemModel(this))
, m_modelProxy(new QSortFilterProxyModel(this))
, m_modelProxy(new ReportSortProxyModel(this))
{
m_ui->setupUi(this);
m_modelProxy->setSourceModel(m_referencesModel.data());
m_modelProxy->setSortLocaleAware(true);
m_ui->hibpTableView->setModel(m_modelProxy.data());
m_ui->hibpTableView->setSelectionMode(QAbstractItemView::NoSelection);
m_ui->hibpTableView->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
@ -167,6 +188,7 @@ void ReportsWidgetHibp::makeHibpTable()
}
row[2]->setForeground(red);
row[2]->setData(count, Qt::UserRole);
m_referencesModel->appendRow(row);
// Store entry pointer per table row (used in double click handler)
@ -198,6 +220,7 @@ void ReportsWidgetHibp::makeHibpTable()
}
m_ui->hibpTableView->resizeRowsToContents();
m_ui->hibpTableView->sortByColumn(2, Qt::DescendingOrder);
m_ui->stackedWidget->setCurrentIndex(1);
}