1
0
Fork 0
mirror of https://github.com/transmission/transmission synced 2024-12-28 10:38:45 +00:00
transmission/qt/FileTreeDelegate.cc
Charles Kerr 0bfbc3eba7
Sonarcloud warnings 4 (#1499)
* chore: fix some sonarcloud html warnings (e.g. deprecated attribute use)

* chore: uppercase literal suffixes

* chore: remove redundant casts

* chore: remove commented-out code

* chore: use qInfo() instead of std::cerr
2020-11-02 09:16:12 -06:00

77 lines
2.3 KiB
C++

/*
* This file Copyright (C) 2009-2015 Mnemosyne LLC
*
* It may be used under the GNU GPL versions 2 or 3
* or any future license endorsed by Mnemosyne LLC.
*
*/
#include <QApplication>
#include <QPainter>
#include "FileTreeDelegate.h"
#include "FileTreeModel.h"
QSize FileTreeDelegate::sizeHint(QStyleOptionViewItem const& item, QModelIndex const& index) const
{
QSize size;
switch (index.column())
{
case FileTreeModel::COL_PROGRESS:
case FileTreeModel::COL_WANTED:
size = QSize(20, 1);
break;
default:
size = QItemDelegate::sizeHint(item, index);
}
size.rheight() += 8; // make the spacing a little nicer
return size;
}
void FileTreeDelegate::paint(QPainter* painter, QStyleOptionViewItem const& option, QModelIndex const& index) const
{
int const column(index.column());
if (column != FileTreeModel::COL_PROGRESS && column != FileTreeModel::COL_WANTED)
{
QItemDelegate::paint(painter, option, index);
return;
}
QStyle const* style = QApplication::style();
painter->save();
QItemDelegate::drawBackground(painter, option, index);
if (column == FileTreeModel::COL_PROGRESS)
{
QStyleOptionProgressBar p;
p.state = option.state | QStyle::State_Small;
p.direction = QApplication::layoutDirection();
p.rect = option.rect;
p.rect.setSize(QSize(option.rect.width() - 4, option.rect.height() - 8));
p.rect.moveCenter(option.rect.center());
p.fontMetrics = QApplication::fontMetrics();
p.minimum = 0;
p.maximum = 100;
p.textAlignment = Qt::AlignCenter;
p.textVisible = true;
p.progress = int(100.0 * index.data().toDouble());
p.text = QStringLiteral("%1%").arg(p.progress);
style->drawControl(QStyle::CE_ProgressBar, &p, painter);
}
else if (column == FileTreeModel::COL_WANTED)
{
QStyleOptionViewItem vi(option);
vi.features |= QStyleOptionViewItem::HasCheckIndicator;
QRect check_rect = style->subElementRect(QStyle::SE_ItemViewItemCheckIndicator, &vi, nullptr);
check_rect.moveCenter(option.rect.center());
drawCheck(painter, vi, check_rect, static_cast<Qt::CheckState>(index.data().toInt()));
}
QItemDelegate::drawFocus(painter, option, option.rect);
painter->restore();
}