2015-06-10 21:27:11 +00:00
|
|
|
/*
|
|
|
|
* 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"
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
QSize FileTreeDelegate::sizeHint(QStyleOptionViewItem const& item, QModelIndex const& index) const
|
2015-06-10 21:27:11 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
QSize size;
|
2015-06-10 21:27:11 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
switch (index.column())
|
2015-06-10 21:27:11 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
case FileTreeModel::COL_PROGRESS:
|
|
|
|
case FileTreeModel::COL_WANTED:
|
2015-06-10 21:27:11 +00:00
|
|
|
size = QSize(20, 1);
|
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
default:
|
|
|
|
size = QItemDelegate::sizeHint(item, index);
|
2015-06-10 21:27:11 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
size.rheight() += 8; // make the spacing a little nicer
|
|
|
|
return size;
|
2015-06-10 21:27:11 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
void FileTreeDelegate::paint(QPainter* painter, QStyleOptionViewItem const& option, QModelIndex const& index) const
|
2015-06-10 21:27:11 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
int const column(index.column());
|
2015-06-10 21:27:11 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (column != FileTreeModel::COL_PROGRESS && column != FileTreeModel::COL_WANTED)
|
2015-06-10 21:27:11 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
QItemDelegate::paint(painter, option, index);
|
|
|
|
return;
|
2015-06-10 21:27:11 +00:00
|
|
|
}
|
|
|
|
|
2020-11-02 15:16:12 +00:00
|
|
|
QStyle const* style = QApplication::style();
|
2015-06-10 21:27:11 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
painter->save();
|
|
|
|
QItemDelegate::drawBackground(painter, option, index);
|
2015-06-10 21:27:11 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (column == FileTreeModel::COL_PROGRESS)
|
2015-06-10 21:27:11 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
QStyleOptionProgressBar p;
|
|
|
|
p.state = option.state | QStyle::State_Small;
|
2020-11-02 01:13:32 +00:00
|
|
|
p.direction = QApplication::layoutDirection();
|
2017-04-19 12:04:45 +00:00
|
|
|
p.rect = option.rect;
|
|
|
|
p.rect.setSize(QSize(option.rect.width() - 4, option.rect.height() - 8));
|
|
|
|
p.rect.moveCenter(option.rect.center());
|
2020-11-02 01:13:32 +00:00
|
|
|
p.fontMetrics = QApplication::fontMetrics();
|
2017-04-19 12:04:45 +00:00
|
|
|
p.minimum = 0;
|
|
|
|
p.maximum = 100;
|
|
|
|
p.textAlignment = Qt::AlignCenter;
|
|
|
|
p.textVisible = true;
|
|
|
|
p.progress = int(100.0 * index.data().toDouble());
|
2020-05-29 17:40:07 +00:00
|
|
|
p.text = QStringLiteral("%1%").arg(p.progress);
|
2017-04-19 12:04:45 +00:00
|
|
|
style->drawControl(QStyle::CE_ProgressBar, &p, painter);
|
2015-06-10 21:27:11 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else if (column == FileTreeModel::COL_WANTED)
|
2015-06-10 21:27:11 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
QStyleOptionViewItem vi(option);
|
|
|
|
vi.features |= QStyleOptionViewItem::HasCheckIndicator;
|
2020-06-05 19:02:11 +00:00
|
|
|
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()));
|
2015-06-10 21:27:11 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
QItemDelegate::drawFocus(painter, option, option.rect);
|
|
|
|
painter->restore();
|
2015-06-10 21:27:11 +00:00
|
|
|
}
|