2016-09-21 21:09:12 +02:00
|
|
|
#include "./downloaditemdelegate.h"
|
|
|
|
|
2021-01-25 19:48:11 +01:00
|
|
|
#include <syncthingmodel/syncthingdownloadmodel.h>
|
2021-10-15 00:23:22 +02:00
|
|
|
#include <syncthingmodel/syncthingicons.h>
|
|
|
|
|
|
|
|
#include <qtforkawesome/icon.h>
|
2021-10-15 22:18:54 +02:00
|
|
|
#include <qtforkawesome/renderer.h>
|
2016-09-21 21:09:12 +02:00
|
|
|
|
|
|
|
#include <QApplication>
|
|
|
|
#include <QBrush>
|
2016-09-28 00:06:21 +02:00
|
|
|
#include <QFontMetrics>
|
2017-05-01 03:34:43 +02:00
|
|
|
#include <QPainter>
|
|
|
|
#include <QPalette>
|
|
|
|
#include <QPixmap>
|
|
|
|
#include <QStyle>
|
|
|
|
#include <QStyleOptionViewItem>
|
|
|
|
#include <QTextOption>
|
2016-09-21 21:09:12 +02:00
|
|
|
|
2016-09-28 00:06:21 +02:00
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
using namespace std;
|
2016-09-21 21:09:12 +02:00
|
|
|
using namespace Data;
|
|
|
|
|
|
|
|
namespace QtGui {
|
|
|
|
|
|
|
|
inline int centerObj(int avail, int size)
|
|
|
|
{
|
|
|
|
return (avail - size) / 2;
|
|
|
|
}
|
|
|
|
|
2017-05-01 03:34:43 +02:00
|
|
|
DownloadItemDelegate::DownloadItemDelegate(QObject *parent)
|
|
|
|
: QStyledItemDelegate(parent)
|
|
|
|
{
|
|
|
|
}
|
2016-09-21 21:09:12 +02:00
|
|
|
|
|
|
|
void DownloadItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
|
|
|
|
{
|
2016-09-28 00:06:21 +02:00
|
|
|
// init style options to use drawControl(), except for the text
|
|
|
|
QStyleOptionViewItem opt = option;
|
|
|
|
initStyleOption(&opt, index);
|
|
|
|
opt.textElideMode = Qt::ElideNone; // elide manually
|
|
|
|
opt.features = QStyleOptionViewItem::None;
|
2017-05-01 03:34:43 +02:00
|
|
|
if (index.parent().isValid()) {
|
2016-09-28 00:06:21 +02:00
|
|
|
opt.displayAlignment = Qt::AlignTop | Qt::AlignLeft;
|
|
|
|
opt.decorationSize = QSize(option.rect.height(), option.rect.height());
|
|
|
|
opt.features |= QStyleOptionViewItem::HasDecoration;
|
|
|
|
opt.text = option.fontMetrics.elidedText(opt.text, Qt::ElideMiddle, opt.rect.width() - opt.rect.height() - 26);
|
|
|
|
} else {
|
|
|
|
opt.text = option.fontMetrics.elidedText(opt.text, Qt::ElideMiddle, opt.rect.width() / 2 - 4);
|
|
|
|
}
|
|
|
|
QApplication::style()->drawControl(QStyle::CE_ItemViewItem, &opt, painter);
|
|
|
|
|
|
|
|
// draw progress bar
|
|
|
|
const QAbstractItemModel *model = index.model();
|
|
|
|
QStyleOptionProgressBar progressBarOption;
|
|
|
|
progressBarOption.state = option.state;
|
|
|
|
progressBarOption.direction = option.direction;
|
|
|
|
progressBarOption.rect = option.rect;
|
2017-05-01 03:34:43 +02:00
|
|
|
if (index.parent().isValid()) {
|
2016-09-28 00:06:21 +02:00
|
|
|
progressBarOption.rect.setX(opt.rect.x() + opt.rect.height() + 4);
|
|
|
|
progressBarOption.rect.setY(opt.rect.y() + opt.rect.height() / 2);
|
|
|
|
} else {
|
2019-05-04 22:18:50 +02:00
|
|
|
#if (QT_VERSION >= QT_VERSION_CHECK(5, 11, 0))
|
|
|
|
progressBarOption.rect.setX(opt.rect.x() + opt.fontMetrics.horizontalAdvance(opt.text) + 6);
|
|
|
|
#else
|
2016-09-28 00:06:21 +02:00
|
|
|
progressBarOption.rect.setX(opt.rect.x() + opt.fontMetrics.width(opt.text) + 6);
|
2019-05-04 22:18:50 +02:00
|
|
|
#endif
|
2016-09-28 00:06:21 +02:00
|
|
|
progressBarOption.rect.setWidth(progressBarOption.rect.width() - 18);
|
|
|
|
}
|
|
|
|
progressBarOption.textAlignment = Qt::AlignCenter;
|
|
|
|
progressBarOption.textVisible = true;
|
2017-05-01 03:34:43 +02:00
|
|
|
if (option.state & QStyle::State_Selected) {
|
2019-06-25 13:49:53 +02:00
|
|
|
progressBarOption.palette.setBrush(QPalette::WindowText, option.palette.brush(QPalette::HighlightedText));
|
2016-09-28 00:06:21 +02:00
|
|
|
}
|
|
|
|
progressBarOption.progress = model->data(index, SyncthingDownloadModel::ItemPercentage).toInt();
|
|
|
|
progressBarOption.minimum = 0;
|
|
|
|
progressBarOption.maximum = 100;
|
|
|
|
progressBarOption.text = model->data(index, SyncthingDownloadModel::ItemProgressLabel).toString();
|
|
|
|
QApplication::style()->drawControl(QStyle::CE_ProgressBar, &progressBarOption, painter);
|
|
|
|
|
|
|
|
// draw buttons
|
|
|
|
int buttonY = option.rect.y();
|
2017-05-01 03:34:43 +02:00
|
|
|
if (!index.parent().isValid()) {
|
2016-09-28 00:06:21 +02:00
|
|
|
buttonY += centerObj(progressBarOption.rect.height(), 16);
|
|
|
|
}
|
2022-10-03 14:40:14 +02:00
|
|
|
QtForkAwesome::Renderer::global().render(
|
2021-10-15 22:18:54 +02:00
|
|
|
QtForkAwesome::Icon::Folder, painter, QRect(option.rect.right() - 16, buttonY, 16, 16), QGuiApplication::palette().color(QPalette::Text));
|
2016-09-21 21:09:12 +02:00
|
|
|
|
2016-09-28 00:06:21 +02:00
|
|
|
// draw file icon
|
2017-05-01 03:34:43 +02:00
|
|
|
if (index.parent().isValid()) {
|
2016-09-28 00:06:21 +02:00
|
|
|
const int fileIconHeight = option.rect.height() - 2;
|
2017-05-01 03:34:43 +02:00
|
|
|
painter->drawPixmap(option.rect.left(), option.rect.y() + 1, fileIconHeight, fileIconHeight,
|
|
|
|
model->data(index, Qt::DecorationRole).value<QIcon>().pixmap(fileIconHeight));
|
2016-09-28 00:06:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QSize DownloadItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
|
|
|
|
{
|
|
|
|
QSize defaultSize(QStyledItemDelegate::sizeHint(option, index));
|
2017-05-01 03:34:43 +02:00
|
|
|
if (index.parent().isValid()) {
|
2016-09-28 00:06:21 +02:00
|
|
|
defaultSize.setHeight(defaultSize.height() + defaultSize.height() - 12);
|
|
|
|
}
|
|
|
|
return defaultSize;
|
2016-09-21 21:09:12 +02:00
|
|
|
}
|
2017-09-17 21:48:15 +02:00
|
|
|
} // namespace QtGui
|