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
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
QStyle* style(qApp->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;
|
|
|
|
p.direction = qApp->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 = qApp->fontMetrics();
|
|
|
|
p.minimum = 0;
|
|
|
|
p.maximum = 100;
|
|
|
|
p.textAlignment = Qt::AlignCenter;
|
|
|
|
p.textVisible = true;
|
|
|
|
p.progress = int(100.0 * index.data().toDouble());
|
|
|
|
p.text = QString::fromLatin1("%1%").arg(p.progress);
|
|
|
|
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;
|
|
|
|
QRect checkRect = style->subElementRect(QStyle::SE_ItemViewItemCheckIndicator, &vi, nullptr);
|
|
|
|
checkRect.moveCenter(option.rect.center());
|
|
|
|
drawCheck(painter, vi, checkRect, 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
|
|
|
}
|