Always use QStyledItemDelegate as base of delegate classes

PR #19340.
This commit is contained in:
Vladimir Golovnev 2023-07-21 08:37:11 +03:00 committed by GitHub
parent 0f862fcf9f
commit d554f4d44a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 33 additions and 39 deletions

View file

@ -1,5 +1,6 @@
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2023 Vladimir Golovnev <glassez@yandex.ru>
* Copyright (C) 2006 Christophe Dumez <chris@qbittorrent.org>
*
* This program is free software; you can redistribute it and/or
@ -30,13 +31,12 @@
#include <QModelIndex>
#include <QPainter>
#include <QStyleOptionViewItem>
#include "base/utils/misc.h"
#include "base/utils/string.h"
#include "previewselectdialog.h"
PreviewListDelegate::PreviewListDelegate(QObject *parent)
: QItemDelegate(parent)
: QStyledItemDelegate(parent)
{
}
@ -44,15 +44,8 @@ void PreviewListDelegate::paint(QPainter *painter, const QStyleOptionViewItem &o
{
painter->save();
QStyleOptionViewItem opt = QItemDelegate::setOptions(index, option);
drawBackground(painter, opt, index);
switch (index.column())
{
case PreviewSelectDialog::SIZE:
QItemDelegate::drawDisplay(painter, opt, option.rect, Utils::Misc::friendlyUnit(index.data().toLongLong()));
break;
case PreviewSelectDialog::PROGRESS:
{
const qreal progress = (index.data().toReal() * 100);
@ -65,7 +58,7 @@ void PreviewListDelegate::paint(QPainter *painter, const QStyleOptionViewItem &o
break;
default:
QItemDelegate::paint(painter, option, index);
QStyledItemDelegate::paint(painter, option, index);
break;
}

View file

@ -1,5 +1,6 @@
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2023 Vladimir Golovnev <glassez@yandex.ru>
* Copyright (C) 2006 Christophe Dumez <chris@qbittorrent.org>
*
* This program is free software; you can redistribute it and/or
@ -28,11 +29,11 @@
#pragma once
#include <QItemDelegate>
#include <QStyledItemDelegate>
#include "progressbarpainter.h"
class PreviewListDelegate final : public QItemDelegate
class PreviewListDelegate final : public QStyledItemDelegate
{
Q_OBJECT
Q_DISABLE_COPY_MOVE(PreviewListDelegate)

View file

@ -1,5 +1,6 @@
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2023 Vladimir Golovnev <glassez@yandex.ru>
* Copyright (C) 2011 Christophe Dumez <chris@qbittorrent.org>
*
* This program is free software; you can redistribute it and/or
@ -49,10 +50,10 @@
PreviewSelectDialog::PreviewSelectDialog(QWidget *parent, const BitTorrent::Torrent *torrent)
: QDialog(parent)
, m_ui(new Ui::PreviewSelectDialog)
, m_torrent(torrent)
, m_storeDialogSize(SETTINGS_KEY(u"Size"_s))
, m_storeTreeHeaderState(u"GUI/Qt6/" SETTINGS_KEY(u"HeaderState"_s))
, m_ui {new Ui::PreviewSelectDialog}
, m_torrent {torrent}
, m_storeDialogSize {SETTINGS_KEY(u"Size"_s)}
, m_storeTreeHeaderState {u"GUI/Qt6/" SETTINGS_KEY(u"HeaderState"_s)}
{
m_ui->setupUi(this);
@ -66,16 +67,19 @@ PreviewSelectDialog::PreviewSelectDialog(QWidget *parent, const BitTorrent::Torr
const Preferences *pref = Preferences::instance();
// Preview list
m_previewListModel = new QStandardItemModel(0, NB_COLUMNS, this);
m_previewListModel->setHeaderData(NAME, Qt::Horizontal, tr("Name"));
m_previewListModel->setHeaderData(SIZE, Qt::Horizontal, tr("Size"));
m_previewListModel->setHeaderData(PROGRESS, Qt::Horizontal, tr("Progress"));
auto *previewListModel = new QStandardItemModel(0, NB_COLUMNS, this);
previewListModel->setHeaderData(NAME, Qt::Horizontal, tr("Name"));
previewListModel->setHeaderData(SIZE, Qt::Horizontal, tr("Size"));
previewListModel->setHeaderData(PROGRESS, Qt::Horizontal, tr("Progress"));
m_ui->previewList->setAlternatingRowColors(pref->useAlternatingRowColors());
m_ui->previewList->setModel(m_previewListModel);
m_ui->previewList->setUniformRowHeights(true);
m_ui->previewList->setModel(previewListModel);
m_ui->previewList->hideColumn(FILE_INDEX);
m_listDelegate = new PreviewListDelegate(this);
m_ui->previewList->setItemDelegate(m_listDelegate);
auto *listDelegate = new PreviewListDelegate(this);
m_ui->previewList->setItemDelegate(listDelegate);
// Fill list in
const QVector<qreal> fp = torrent->filesProgress();
for (int i = 0; i < torrent->filesCount(); ++i)
@ -83,20 +87,20 @@ PreviewSelectDialog::PreviewSelectDialog(QWidget *parent, const BitTorrent::Torr
const Path filePath = torrent->filePath(i);
if (Utils::Misc::isPreviewable(filePath))
{
int row = m_previewListModel->rowCount();
m_previewListModel->insertRow(row);
m_previewListModel->setData(m_previewListModel->index(row, NAME), filePath.filename());
m_previewListModel->setData(m_previewListModel->index(row, SIZE), torrent->fileSize(i));
m_previewListModel->setData(m_previewListModel->index(row, PROGRESS), fp[i]);
m_previewListModel->setData(m_previewListModel->index(row, FILE_INDEX), i);
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);
}
}
m_previewListModel->sort(NAME);
previewListModel->sort(NAME);
m_ui->previewList->header()->setContextMenuPolicy(Qt::CustomContextMenu);
m_ui->previewList->header()->setFirstSectionMovable(true);
m_ui->previewList->header()->setSortIndicator(0, Qt::AscendingOrder);
m_ui->previewList->selectionModel()->select(m_previewListModel->index(0, NAME), QItemSelectionModel::Select | QItemSelectionModel::Rows);
m_ui->previewList->selectionModel()->select(previewListModel->index(0, NAME), QItemSelectionModel::Select | QItemSelectionModel::Rows);
connect(m_ui->previewList->header(), &QWidget::customContextMenuRequested, this, &PreviewSelectDialog::displayColumnHeaderMenu);
@ -125,7 +129,7 @@ void PreviewSelectDialog::previewButtonClicked()
// File
if (!path.exists())
{
const bool isSingleFile = (m_previewListModel->rowCount() == 1);
const bool isSingleFile = (m_ui->previewList->model()->rowCount() == 1);
QWidget *parent = isSingleFile ? this->parentWidget() : this;
QMessageBox::critical(parent, tr("Preview impossible")
, tr("Sorry, we can't preview this file: \"%1\".").arg(path.toString()));
@ -195,6 +199,6 @@ void PreviewSelectDialog::showEvent(QShowEvent *event)
}
// Only one file, no choice
if (m_previewListModel->rowCount() <= 1)
if (m_ui->previewList->model()->rowCount() <= 1)
previewButtonClicked();
}

View file

@ -1,5 +1,6 @@
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2023 Vladimir Golovnev <glassez@yandex.ru>
* Copyright (C) 2011 Christophe Dumez <chris@qbittorrent.org>
*
* This program is free software; you can redistribute it and/or
@ -33,8 +34,6 @@
#include "base/path.h"
#include "base/settingvalue.h"
class QStandardItemModel;
namespace BitTorrent
{
class Torrent;
@ -44,7 +43,6 @@ namespace Ui
{
class PreviewSelectDialog;
}
class PreviewListDelegate;
class PreviewSelectDialog final : public QDialog
{
@ -79,8 +77,6 @@ private:
void saveWindowState();
Ui::PreviewSelectDialog *m_ui = nullptr;
QStandardItemModel *m_previewListModel = nullptr;
PreviewListDelegate *m_listDelegate = nullptr;
const BitTorrent::Torrent *m_torrent = nullptr;
bool m_headerStateInitialized = false;