80 lines
2.1 KiB
C++
80 lines
2.1 KiB
C++
/**
|
|
* @file PaiTableItemDelegate.cpp
|
|
* @date 2012-10-16
|
|
*/
|
|
#include <QPainter>
|
|
#include <QBrush>
|
|
#include <QLinearGradient>
|
|
#include <QApplication>
|
|
|
|
#include "PaiTableItemDelegate.h"
|
|
|
|
using namespace pai::gui;
|
|
|
|
PaiTableItemDelegate::PaiTableItemDelegate(QTableView *pTableView) :
|
|
m_GridPen(QColor("#C9D5DC"), 0, pTableView->gridStyle()),
|
|
m_HorizontalLine(true),
|
|
m_VerticallLine(false)
|
|
{
|
|
|
|
}
|
|
|
|
void PaiTableItemDelegate::setShowGrid(bool horizontal, bool verticalLine)
|
|
{
|
|
m_HorizontalLine = horizontal;
|
|
m_VerticallLine = verticalLine;
|
|
}
|
|
|
|
void PaiTableItemDelegate::paint(QPainter *pPainter, const QStyleOptionViewItem & option, const QModelIndex & index) const
|
|
{
|
|
QStyleOptionViewItemV4 opt = option;
|
|
initStyleOption(&opt, index);
|
|
bool bSelected = opt.state & QStyle::State_Selected;
|
|
|
|
QPointF startPoint(0, 0);
|
|
QPointF finalPoint(0, opt.rect.height());
|
|
QLinearGradient linearGrad(startPoint, finalPoint);
|
|
|
|
if(bSelected)
|
|
{
|
|
opt.state = opt.state & ~QStyle::State_Selected;
|
|
linearGrad.setColorAt(0, QColor("#F6FDFF"));
|
|
linearGrad.setColorAt(1, QColor("#DAF3FD"));
|
|
opt.backgroundBrush = QBrush(linearGrad);
|
|
}
|
|
else if(index.data(HOVERING_ROLE).toBool())
|
|
{
|
|
linearGrad.setColorAt(0, QColor("#F6FDFF"));
|
|
linearGrad.setColorAt(1, QColor("#DAF3FD"));
|
|
opt.backgroundBrush = QBrush(linearGrad);
|
|
}
|
|
else
|
|
{
|
|
opt.backgroundBrush = QBrush(Qt::white);
|
|
}
|
|
|
|
const QWidget *pWidget = opt.widget;
|
|
if(pWidget == NULL)
|
|
{
|
|
return;
|
|
}
|
|
QStyle *pStyle = pWidget ? pWidget->style() : QApplication::style();
|
|
|
|
pStyle->drawControl(QStyle::CE_ItemViewItem, &opt, pPainter, pWidget);
|
|
|
|
QPen oldPen(pPainter->pen());
|
|
pPainter->setPen(m_GridPen);
|
|
|
|
if(m_VerticallLine) // paint vertical lines
|
|
{
|
|
pPainter->drawLine(option.rect.topRight(), option.rect.bottomRight());
|
|
}
|
|
|
|
if(m_HorizontalLine) // paint horizontal lines
|
|
{
|
|
pPainter->drawLine(option.rect.bottomLeft(), option.rect.bottomRight());
|
|
}
|
|
|
|
pPainter->setPen(oldPen);
|
|
}
|