Fix filesize sorting in preview dialog

PR #21563.
Closes #21510.
This commit is contained in:
DoubleSpicy 2024-10-23 14:03:25 +08:00 committed by GitHub
parent 5dd41f506e
commit c080fc3aa0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -48,6 +48,11 @@
#define SETTINGS_KEY(name) u"PreviewSelectDialog/" name
enum Roles
{
SortRole = Qt::UserRole
};
PreviewSelectDialog::PreviewSelectDialog(QWidget *parent, const BitTorrent::Torrent *torrent)
: QDialog(parent)
, m_ui {new Ui::PreviewSelectDialog}
@ -75,7 +80,6 @@ PreviewSelectDialog::PreviewSelectDialog(QWidget *parent, const BitTorrent::Torr
m_ui->previewList->setAlternatingRowColors(pref->useAlternatingRowColors());
m_ui->previewList->setUniformRowHeights(true);
m_ui->previewList->setModel(previewListModel);
m_ui->previewList->hideColumn(FILE_INDEX);
auto *listDelegate = new PreviewListDelegate(this);
m_ui->previewList->setItemDelegate(listDelegate);
@ -89,13 +93,19 @@ PreviewSelectDialog::PreviewSelectDialog(QWidget *parent, const BitTorrent::Torr
{
int row = previewListModel->rowCount();
previewListModel->insertRow(row);
previewListModel->setData(previewListModel->index(row, NAME), filePath.filename());
previewListModel->setData(previewListModel->index(row, SIZE), Utils::Misc::friendlyUnit(torrent->fileSize(i)));
previewListModel->setData(previewListModel->index(row, PROGRESS), fp[i]);
previewListModel->setData(previewListModel->index(row, FILE_INDEX), i);
previewListModel->setData(previewListModel->index(row, NAME), filePath.filename(), Qt::DisplayRole);
previewListModel->setData(previewListModel->index(row, NAME), filePath.filename(), SortRole);
// Sorting file size by bytes, while displaying by human readable format
previewListModel->setData(previewListModel->index(row, SIZE), Utils::Misc::friendlyUnit(torrent->fileSize(i)), Qt::DisplayRole);
previewListModel->setData(previewListModel->index(row, SIZE), torrent->fileSize(i), SortRole);
previewListModel->setData(previewListModel->index(row, PROGRESS), fp[i], Qt::DisplayRole);
previewListModel->setData(previewListModel->index(row, PROGRESS), fp[i], SortRole);
previewListModel->setData(previewListModel->index(row, FILE_INDEX), i, Qt::DisplayRole);
previewListModel->setData(previewListModel->index(row, FILE_INDEX), i, SortRole);
}
}
previewListModel->setSortRole(SortRole);
previewListModel->sort(NAME);
m_ui->previewList->header()->setContextMenuPolicy(Qt::CustomContextMenu);
m_ui->previewList->header()->setFirstSectionMovable(true);
@ -106,6 +116,7 @@ PreviewSelectDialog::PreviewSelectDialog(QWidget *parent, const BitTorrent::Torr
// Restore dialog state
loadWindowState();
m_ui->previewList->hideColumn(FILE_INDEX);
}
PreviewSelectDialog::~PreviewSelectDialog()