#include "groupscreendelegate.h" #include "Common/External/Core/PaintHelper/painthelper.h" #include "Common/DataBase/entities.h" #include GroupScreenDelegate::GroupScreenDelegate(QObject *parent) : ButtonDelegate(parent) , m_nTargetRow(-1) , m_bIsUpOn(false) , m_nRadius(8) { } void GroupScreenDelegate::SetDragTarget(int nTargetRow, bool bIsUpOn) { m_nTargetRow = nTargetRow; m_bIsUpOn = bIsUpOn; } QString GroupScreenDelegate::GetCheckboxImage(Qt::CheckState state) const { if(state == Qt::Unchecked) { return ":/checkbox_unchecked.png"; } else if(state == Qt::Checked) { return ":/checkbox_checked.png"; } return ""; } void GroupScreenDelegate::PaintText(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { PainterEx *painterEx = static_cast(painter); QString text = index.data().toString(); painterEx->setFont(GetFont(option, index)); QRect rectText = GetFontRect(option, index); if(index.column() == 0) { rectText = QRect(QPoint(rectText.left() + 12 + 16 + 16, rectText.top()), rectText.size()); } painterEx->DrawText(rectText, text, GetTextColor(option, index), Qt::AlignLeft|Qt::AlignVCenter); QRect rect = option.rect; // 绘制复选框 if(painterEx && index.column() == 0) { if(!index.model()->data(index, Qt::UserRole).canConvert()) { return; } stGroupScreen data = index.model()->data(index, Qt::UserRole).value(); painterEx->save(); QRect rectTarget(rect.left() + 12, rect.top() + (rect.height() - 18) / 2, 18, 18); painterEx->DrawPixmap(rectTarget, GetCheckboxImage(data.checkState)); painterEx->restore(); } } void GroupScreenDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { ButtonDelegate::paint(painter,option,index); if(m_nTargetRow == -1) return; PainterEx *painterEx = static_cast(painter); // 绘制第一列单元格圆圈和线 if(index.row() == m_nTargetRow && index.column() == 0) { int y = m_bIsUpOn ? option.rect.top() : option.rect.bottom(); painterEx->DrawCircle(QPoint(m_nRadius + 1, y), m_nRadius, Qt::transparent, QPen(QColor(86,135,228), 2)); painterEx->setPen(QPen(QColor(86,135,228), 2)); painterEx->drawLine(option.rect.left() + m_nRadius * 2 + 1, y, option.rect.right(), y); } // 如果指示器绘制在当前行的下方, 因为头部圆圈会跨到下一行, 所以在绘制下一行的时候, 又绘制一次 if(!m_bIsUpOn && (index.row() == m_nTargetRow + 1) && index.column() == 0) { int y = option.rect.top() - 1; painterEx->DrawCircle(QPoint(m_nRadius + 1, y), m_nRadius, Qt::transparent, QPen(QColor(86,135,228), 2)); painterEx->setPen(QPen(QColor(86,135,228), 2)); painterEx->drawLine(option.rect.left() + m_nRadius * 2 + 1, y, option.rect.right(), y); } // 绘制后面单元格的线 if(index.row() == m_nTargetRow && index.column() != 0) { painterEx->DrawBorder(option.rect, QPen(QColor(86,135,228), 2), m_bIsUpOn ? PainterEx::RectBorderTop : PainterEx::RectBorderBottom); } }