| 18 | } |
| 19 | |
| 20 | void T_IconDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const |
| 21 | { |
| 22 | QStyleOptionViewItem viewOption(option); |
| 23 | initStyleOption(&viewOption, index); |
| 24 | |
| 25 | if (option.state.testFlag(QStyle::State_HasFocus)) |
| 26 | { |
| 27 | viewOption.state &= ~QStyle::State_HasFocus; |
| 28 | } |
| 29 | QStyledItemDelegate::paint(painter, viewOption, index); |
| 30 | QStringList iconList = index.data(Qt::UserRole).toStringList(); |
| 31 | if (iconList.count() != 2) |
| 32 | { |
| 33 | return; |
| 34 | } |
| 35 | QString iconName = iconList.at(0); |
| 36 | QString iconValue = iconList.at(1); |
| 37 | painter->save(); |
| 38 | painter->setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform); |
| 39 | painter->save(); |
| 40 | QFont iconFont = QFont("ElaAwesome"); |
| 41 | iconFont.setPixelSize(22); |
| 42 | painter->setFont(iconFont); |
| 43 | painter->setPen(ElaThemeColor(_themeMode, BasicText)); |
| 44 | painter->drawText(option.rect.x() + option.rect.width() / 2 - 11, option.rect.y() + option.rect.height() / 2 - 11, iconValue); |
| 45 | painter->restore(); |
| 46 | // 文字绘制 |
| 47 | painter->setPen(ElaThemeColor(_themeMode, BasicText)); |
| 48 | QFont titlefont = painter->font(); |
| 49 | titlefont.setPixelSize(13); |
| 50 | painter->setFont(titlefont); |
| 51 | qreal rowTextWidth = option.rect.width() * 0.8; |
| 52 | int subTitleRow = painter->fontMetrics().horizontalAdvance(iconName) / rowTextWidth; |
| 53 | if (subTitleRow > 0) |
| 54 | { |
| 55 | QString subTitleText = iconName; |
| 56 | for (int i = 0; i < subTitleRow + 1; i++) |
| 57 | { |
| 58 | QString text = painter->fontMetrics().elidedText(subTitleText, Qt::ElideRight, rowTextWidth); |
| 59 | if (text.right(3).contains("…")) |
| 60 | { |
| 61 | text = text.replace("…", subTitleText.mid(text.length() - 1, 1)); |
| 62 | } |
| 63 | subTitleText.remove(0, text.length()); |
| 64 | painter->drawText(option.rect.x() + option.rect.width() / 2 - painter->fontMetrics().horizontalAdvance(text) / 2, option.rect.y() + option.rect.height() - 10 * (subTitleRow + 1 - i), text); |
| 65 | } |
| 66 | } |
| 67 | else |
| 68 | { |
| 69 | painter->drawText(option.rect.x() + option.rect.width() / 2 - painter->fontMetrics().horizontalAdvance(iconName) / 2, option.rect.y() + option.rect.height() - 20, iconName); |
| 70 | } |
| 71 | painter->restore(); |
| 72 | } |
| 73 | |
| 74 | QSize T_IconDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const |
| 75 | { |