1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
|
//
// C++ Implementation: TransferListItemDelegate
//
// Description:
//
//
// Author: Arsenij Vodjanov <arsenij@gmail.com>, (C) 2005
//
// Copyright: See COPYING file that comes with this distribution
//
//
#include "transferlistmodel.h"
#include "transferlistitemdelegate.h"
#include <QPainter>
#include <QFont>
#include <QRect>
#include <QStyleOptionViewItem>
#include <QModelIndex>
#include "log.h"
TransferListItemDelegate::TransferListItemDelegate( QObject *parent )
: QItemDelegate( parent )
{
}
TransferListItemDelegate::~TransferListItemDelegate()
{
}
/*! Reimplemented from QItemDelegate */
QSize TransferListItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
if ( index.column() != TransferListModel::COL_PROGRESS_BAR )
return QItemDelegate::sizeHint(option, index);
const QAbstractItemModel *model = index.model();
if (model==0) {
logger->warn("Warning: model==0 in TransferListItemDelegate::sizeHint()");
return QSize(0,0);
}
QString text = "BLA 00:00:00 BLA"; // just some string for a size hint
QVariant value = model->data(index, Qt::FontRole);
QFont fnt = value.isValid() ? qvariant_cast<QFont>(value) : option.font;
QFontMetrics fontMetrics(fnt);
return QSize( fontMetrics.width(text), fontMetrics.lineSpacing()+2 ); // extra pixel at top and bottom
}
/*! Reimplemented from QItemDelegate */
void TransferListItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
if ( index.column() != TransferListModel::COL_PROGRESS_BAR )
return QItemDelegate::paint(painter, option, index);
const QAbstractItemModel *model = index.model();
if (model==0) {
logger->warn("Warning: model==0 in TransferListItemDelegate::paint()");
return;
}
// This is stupid. Probably should have reimplemented some item view class instead.
QList<QVariant> pdata = model->data(index, Qt::DisplayRole).toList();
int secs = pdata.at( TransferListModel::PDATA_SECONDS_LEFT ).toInt();
bool isDownload = pdata.at(TransferListModel::PDATA_IS_DOWNLOAD).toBool();
qlonglong startPos = pdata.at( TransferListModel::PDATA_START_POS ).toLongLong();
qlonglong pos = pdata.at( TransferListModel::PDATA_POS ).toLongLong();
qlonglong actual = pdata.at( TransferListModel::PDATA_ACTUAL ).toLongLong();
qlonglong fsize = pdata.at( TransferListModel::PDATA_SIZE ).toLongLong();
QString text = QString("%1% %2:%3:%4").arg((qlonglong)(pos*100/fsize)).arg( (qlonglong)(secs / (60*60))).arg( (qlonglong)(secs / 60)).arg( (qlonglong)(secs % 60));
// Draw progressbar and text
QStyleOptionViewItem opt = option;
QRect rect = option.rect;
QFont font = painter->font();
QPen pen = painter->pen();
QBrush brush = painter->brush();
// set font
QVariant value = model->data(index, Qt::FontRole);
if (value.isValid())
opt.font = qvariant_cast<QFont>(value);
// set text alignment
opt.displayAlignment = Qt::AlignCenter;
// set text color
value = model->data(index, Qt::TextColorRole);
if (value.isValid() && qvariant_cast<QColor>(value).isValid())
opt.palette.setColor(QPalette::Text, qvariant_cast<QColor>(value));
// draw the background color
QPalette::ColorGroup cg = opt.state & QStyle::State_Enabled ? QPalette::Normal : QPalette::Disabled;
if (opt.showDecorationSelected && (option.state & QStyle::State_Selected)) {
painter->fillRect(rect, opt.palette.brush(cg, QPalette::Highlight));
} else {
value = model->data(index, Qt::BackgroundColorRole);
if (value.isValid() && qvariant_cast<QColor>(value).isValid())
painter->fillRect(rect, qvariant_cast<QColor>(value));
}
// draw bar borders
painter->setPen( opt.palette.color(cg, QPalette::Mid) );
painter->drawRoundRect(rect.adjusted(2,2,-3,-3), 5,5);
QRect barRect = rect.adjusted(4,4,-4,-4);
// figure out rectangle positions for progress bar
int x = barRect.x(), y = barRect.y(), height = barRect.height();
qlonglong width = barRect.width();
int x1 = x + startPos*width/fsize;
int x2 = x + (pos-actual)*width/fsize;
int x3 = x + pos*width/fsize;
// draw progress bar in different color for upload and download... ugly if-statement.
if (isDownload) {
painter->fillRect( x, y, x1-x, height, QColor( 16,140,16 ) );
painter->fillRect( x1, y, x2-x1, height, QColor( 16,180,16 ) );
painter->fillRect( x2, y, x3-x2, height, QColor( 20,220,20 ) );
} else {
painter->fillRect( x, y, x1-x, height, QColor( 160,0,0 ) );
painter->fillRect( x1, y, x2-x1, height, QColor( 190,10,10 ) );
painter->fillRect( x2, y, x3-x2, height, QColor( 255,20,20 ) );
}
painter->fillRect( x3, y, (width-x3)+x+1, height, QColor( 220,220,245 ) );
// draw text
QRect textRect = rect.adjusted(1, 0, -1, -1); // remove width padding ( const int textMargin = 1 in qitemdelegate.cpp )
QRect textShadowRect = textRect.adjusted(1,1,1,1);
QColor textColor = QColor(220,220,220);
painter->setFont(opt.font);
painter->setPen( QColor( 255-textColor.red(), 255-textColor.green(), 255-textColor.blue() ) );
if ((painter->fontMetrics().width(text)+1) > textRect.width()) {
// inverted text color for shadow
painter->drawText(textShadowRect, opt.displayAlignment,
elidedText(painter->fontMetrics(), textShadowRect.width(), // QAbstractItemDelegate::elidedText()
opt.textElideMode, text));
painter->setPen(textColor);
painter->drawText(textRect, opt.displayAlignment,
elidedText(painter->fontMetrics(), textRect.width(), // QAbstractItemDelegate::elidedText()
opt.textElideMode, text));
} else {
painter->drawText(textShadowRect, opt.displayAlignment, text);
painter->setPen(textColor);
painter->drawText(textRect, opt.displayAlignment, text);
}
painter->setFont(font);
painter->setPen(pen);
painter->setBrush(brush);
drawFocus(painter, opt, textRect);
}
|