QSize
QTorrentItemDelegat::sizeHint(const QStyleOptionViewItem& option, const Torrent& tor) const
{
	try
	{
		const QStyle* style(QApplication::style());
		const int iconSize(style->pixelMetric(QStyle::PM_MessageBoxIconSize));
		QFont nameFont(option.font);
		nameFont.setWeight(QFont::Bold);
		const QFontMetrics nameFM(nameFont);
		const QString nameStr(tor.GetName());
		int nameWidth = nameFM.width(nameStr);
		QFont statusFont(option.font);
		statusFont.setPointSize(int (option.font.pointSize() * 0.9));
		const QFontMetrics statusFM(statusFont);
		const QString statusStr(GetStatusString(tor));
		int statusWidth = statusFM.width(statusStr);
		QFont progressFont(statusFont);
		const QFontMetrics progressFM(progressFont);
		const QString progressStr(GetProgressString(tor));
		const int progressWidth = progressFM.width(progressStr);
		const QSize m(margin(*style));
		int width = m.width() + iconSize  + std::max(nameWidth, statusWidth + progressWidth);
		int height = m.height() * 3 + nameFM.lineSpacing() + 2 * GUI_PAD + progressFM.lineSpacing();
		return QSize(width, height);
	}
	catch(...)
	{
		qDebug() << "exception in size hint";
	}

	return QSize(0, 0);
}
QSize
QTorrentItemDelegat::sizeHint(const QStyleOptionViewItem& option, const Torrent& tor) const
{
	const QStyle* style(QApplication::style());
	const int iconSize(style->pixelMetric(QStyle::PM_MessageBoxIconSize));
	QFont nameFont(option.font);
	nameFont.setWeight(QFont::Bold);
	const QFontMetrics nameFM(nameFont);
	QString nameStr;
	int queuePosition = tor.GetQueuePosition();

	if (queuePosition > -1)
	{
		nameStr = QString("#%1 %2").arg(QString::number(queuePosition + 1), tor.GetName());
	}
	else
	{
		nameStr = tor.GetName();
	}

	int nameWidth = nameFM.width(nameStr);
	QFont statusFont(option.font);
	statusFont.setPointSize(int(option.font.pointSize() * 0.9));
	const QFontMetrics statusFM(statusFont);
	const QString statusStr(GetStatusString(tor));
	int statusWidth = statusFM.width(statusStr);
	QFont progressFont(statusFont);
	const QFontMetrics progressFM(progressFont);
	const QString progressStr(GetProgressString(tor));
	const int progressWidth = progressFM.width(progressStr);
	const QSize m(margin(*style));
	int width = m.width() + iconSize + std::max(nameWidth, statusWidth + progressWidth);
	int height = m.height() * 3 + nameFM.lineSpacing() + 2 * GUI_PAD + progressFM.lineSpacing();
	return QSize(width, height);
}
void QTorrentItemDelegat::drawTorrent(QPainter* painter, const QStyleOptionViewItem& option, const Torrent& tor, const QModelIndex& index) const
{
	QStyleOptionViewItemV4 opt = option;
	initStyleOption(&opt, index);
	QStyle* style;

	if(opt.widget != NULL)
	{
		style = opt.widget->style();
	}
	else
	{
		style = QApplication::style();
	}

	const int iconSize(style->pixelMetric(QStyle::PM_LargeIconSize));
	QFont nameFont(option.font);
	nameFont.setWeight(QFont::Bold);
	const QFontMetrics nameFM(nameFont);
	const QIcon mimeIcon(tor.GetMimeTypeIcon());
	QString nameStr(tor.GetName());
	QSize nameSize(nameFM.size(0, nameStr));
	QFont statusFont(option.font);
	statusFont.setPointSize(int (option.font.pointSize() * 0.9));
	const QFontMetrics statusFM(statusFont);
	QString statusStr(GetStatusString(tor));
	int statusWidth = statusFM.width(statusStr);
	QFont progressFont(statusFont);
	const QFontMetrics progressFM(progressFont);
	QString progressStr(GetProgressString(tor));
	bool isPaused(tor.isPaused());
	painter->save();
	painter->setRenderHint(QPainter::Antialiasing);
	style->drawPrimitive(QStyle::PE_PanelItemViewItem, &opt, painter, opt.widget);
	/* if (option.state & QStyle::State_Selected) {
	     QPalette::ColorGroup cg = option.state & QStyle::State_Enabled
	             ? QPalette::Active : QPalette::Disabled;
	     if (cg == QPalette::Normal && !(option.state & QStyle::State_Active))
	         cg = QPalette::Inactive;


	    painter->fillRect(option.rect, opt.backgroundBrush);
	 }*/
	//style->drawControl(QStyle::CE_ItemViewItem,&option,painter);
	QIcon::Mode im;

	if(isPaused || !(option.state & QStyle::State_Enabled))
	{
		im = QIcon::Disabled;
	}
	else if(option.state & QStyle::State_Selected)
	{
		im = QIcon::Selected;
	}
	else
	{
		im = QIcon::Normal;
	}

	QIcon::State qs;

	if(isPaused)
	{
		qs = QIcon::Off;
	}
	else
	{
		qs = QIcon::On;
	}

	QPalette::ColorGroup cg = QPalette::Normal;

	if(isPaused || !(option.state & QStyle::State_Enabled))
	{
		cg = QPalette::Disabled;
	}

	if(cg == QPalette::Normal && !(option.state & QStyle::State_Active))
	{
		cg = QPalette::Inactive;
	}

	QPalette::ColorRole cr;

	if(option.state & QStyle::State_Selected)
	{
		cr = QPalette::HighlightedText;
	}

	cr = QPalette::Text;
	// layout
	const QSize m(margin(*style));
	QRect fillArea(option.rect);
	fillArea.adjust(m.width(), m.height(), -m.width(), -m.height());
	QRect iconArea(fillArea.x(), fillArea.y() + (fillArea.height() - iconSize) / 2, iconSize, iconSize);
	QRect nameArea(iconArea.x() + iconArea.width() + GUI_PAD, fillArea.y(),
	               fillArea.width() - GUI_PAD - iconArea.width(), nameSize.height());

	if (nameArea.width() + nameArea.x() > opt.rect.width())
	{
		nameArea.setWidth(opt.rect.width() - nameArea.x());
	}

	QRect barArea(nameArea.x(), nameArea.y() + statusFM.lineSpacing() + GUI_PAD / 2, nameArea.width(), BAR_HEIGHT);
	QRect progArea(nameArea.x(), barArea.y() + barArea.height() + GUI_PAD / 2, barArea.width() - statusWidth, nameArea.height());
	QRect statusArea(progArea.x() + progArea.width(), progArea.y(), statusWidth, progArea.height());

	if(tor.hasError())
	{
		painter->setPen(QColor("red"));
	}
	else
	{
		painter->setPen(opt.palette.color(cg, cr));
	}

	mimeIcon.paint(painter, iconArea, Qt::AlignCenter, im, qs);
	painter->setFont(nameFont);
	nameStr = nameFM.elidedText(nameStr, Qt::ElideRight, nameArea.width());
	style->drawItemText(painter, nameArea, Qt::AlignLeft, opt.palette, option.state & QStyle::State_Enabled, nameStr);
	painter->setFont(statusFont);
	statusStr = statusFM.elidedText(statusStr, Qt::ElideRight, statusArea.width());
	style->drawItemText(painter,  statusArea, Qt::AlignRight, opt.palette, option.state & QStyle::State_Enabled, statusStr);
	painter->setFont(progressFont);
	progressStr = statusFM.elidedText(progressStr, Qt::ElideRight, progArea.width());
	style->drawItemText(painter, progArea, Qt::AlignLeft, opt.palette, option.state & QStyle::State_Enabled, progressStr);
	int progressPercentage = tor.GetProgress();
	myProgressBarStyle->resize(barArea.size());
	myProgressBarStyle->setValue(progressPercentage);
	//painter->translate(barArea.topLeft());
	//myProgressBarStyle->render(painter);
	QStyleOptionProgressBarV2 pbStyleOpt;
	myProgressBarStyle->initilizeStyleOption(&pbStyleOpt);
	pbStyleOpt.rect = barArea;
	/*myProgressBarStyle->rect = barArea;
	if ( tor.isDownloading() ) {
	    myProgressBarStyle->palette.setBrush( QPalette::Highlight, blueBrush );
	    myProgressBarStyle->palette.setColor( QPalette::Base, blueBack );
	    myProgressBarStyle->palette.setColor( QPalette::Background, blueBack );
	}
	else if ( tor.isSeeding() ) {
	    myProgressBarStyle->palette.setBrush( QPalette::Highlight, greenBrush );
	    myProgressBarStyle->palette.setColor( QPalette::Base, greenBack );
	    myProgressBarStyle->palette.setColor( QPalette::Background, greenBack );
	}

	myProgressBarStyle->state = progressBarState;
	setProgressBarPercentDone( option, tor );
	*/
	style->drawControl(QStyle::CE_ProgressBar, &pbStyleOpt, painter, myProgressBarStyle);
	painter->restore();
}