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 Vladimir Golovnev (Glassez)
parent 837d39dac7
commit 903173b8f1
No known key found for this signature in database
GPG key ID: 52A2C7DEE2DFA6F7
4 changed files with 29 additions and 35 deletions

View file

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

View file

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

View file

@ -1,5 +1,6 @@
/* /*
* Bittorrent Client using Qt and libtorrent. * Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2023 Vladimir Golovnev <glassez@yandex.ru>
* Copyright (C) 2011 Christophe Dumez <chris@qbittorrent.org> * Copyright (C) 2011 Christophe Dumez <chris@qbittorrent.org>
* *
* This program is free software; you can redistribute it and/or * This program is free software; you can redistribute it and/or
@ -70,16 +71,19 @@ PreviewSelectDialog::PreviewSelectDialog(QWidget *parent, const BitTorrent::Torr
const Preferences *pref = Preferences::instance(); const Preferences *pref = Preferences::instance();
// Preview list // Preview list
m_previewListModel = new QStandardItemModel(0, NB_COLUMNS, this); auto *previewListModel = new QStandardItemModel(0, NB_COLUMNS, this);
m_previewListModel->setHeaderData(NAME, Qt::Horizontal, tr("Name")); previewListModel->setHeaderData(NAME, Qt::Horizontal, tr("Name"));
m_previewListModel->setHeaderData(SIZE, Qt::Horizontal, tr("Size")); previewListModel->setHeaderData(SIZE, Qt::Horizontal, tr("Size"));
m_previewListModel->setHeaderData(PROGRESS, Qt::Horizontal, tr("Progress")); previewListModel->setHeaderData(PROGRESS, Qt::Horizontal, tr("Progress"));
m_ui->previewList->setAlternatingRowColors(pref->useAlternatingRowColors()); 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_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 // Fill list in
const QVector<qreal> fp = torrent->filesProgress(); const QVector<qreal> fp = torrent->filesProgress();
for (int i = 0; i < torrent->filesCount(); ++i) for (int i = 0; i < torrent->filesCount(); ++i)
@ -87,20 +91,20 @@ PreviewSelectDialog::PreviewSelectDialog(QWidget *parent, const BitTorrent::Torr
const Path filePath = torrent->filePath(i); const Path filePath = torrent->filePath(i);
if (Utils::Misc::isPreviewable(filePath)) if (Utils::Misc::isPreviewable(filePath))
{ {
int row = m_previewListModel->rowCount(); int row = previewListModel->rowCount();
m_previewListModel->insertRow(row); previewListModel->insertRow(row);
m_previewListModel->setData(m_previewListModel->index(row, NAME), filePath.filename()); previewListModel->setData(previewListModel->index(row, NAME), filePath.filename());
m_previewListModel->setData(m_previewListModel->index(row, SIZE), torrent->fileSize(i)); previewListModel->setData(previewListModel->index(row, SIZE), Utils::Misc::friendlyUnit(torrent->fileSize(i)));
m_previewListModel->setData(m_previewListModel->index(row, PROGRESS), fp[i]); previewListModel->setData(previewListModel->index(row, PROGRESS), fp[i]);
m_previewListModel->setData(m_previewListModel->index(row, FILE_INDEX), 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()->setContextMenuPolicy(Qt::CustomContextMenu);
m_ui->previewList->header()->setFirstSectionMovable(true); m_ui->previewList->header()->setFirstSectionMovable(true);
m_ui->previewList->header()->setSortIndicator(0, Qt::AscendingOrder); 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); connect(m_ui->previewList->header(), &QWidget::customContextMenuRequested, this, &PreviewSelectDialog::displayColumnHeaderMenu);
@ -129,7 +133,7 @@ void PreviewSelectDialog::previewButtonClicked()
// File // File
if (!path.exists()) 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; QWidget *parent = isSingleFile ? this->parentWidget() : this;
QMessageBox::critical(parent, tr("Preview impossible") QMessageBox::critical(parent, tr("Preview impossible")
, tr("Sorry, we can't preview this file: \"%1\".").arg(path.toString())); , tr("Sorry, we can't preview this file: \"%1\".").arg(path.toString()));
@ -199,6 +203,6 @@ void PreviewSelectDialog::showEvent(QShowEvent *event)
} }
// Only one file, no choice // Only one file, no choice
if (m_previewListModel->rowCount() <= 1) if (m_ui->previewList->model()->rowCount() <= 1)
previewButtonClicked(); previewButtonClicked();
} }

View file

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