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 156 157 158 159 160 161 162
|
// SPDX-FileCopyrightText: 2019 - 2022 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later
#include "logviewitemdelegate.h"
#include <DApplication>
#include <DGuiApplicationHelper>
#include <DPalette>
#include <DStyle>
#include <DStyleHelper>
#include <QDebug>
#include <QModelIndex>
#include <QPainter>
#include <QStyleOptionViewItem>
#include <QPainterPath>
DWIDGET_USE_NAMESPACE
#define ICON_WIDTH 24
#define ICON_HEIGHT 24
#define ROW_HEIGHT 38
#define ROW_HEIGHT_COMPACT 26
LogViewItemDelegate::LogViewItemDelegate(QObject *parent)
: QStyledItemDelegate(parent)
{
}
/**
* @brief LogViewItemDelegate::paint 绘制内容数据和文字虚函数
* @param painter
* @param option
* @param index
*/
void LogViewItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
if (!index.isValid()) {
QStyledItemDelegate::paint(painter, option, index);
return;
}
painter->save();
painter->setRenderHint(QPainter::Antialiasing);
painter->setOpacity(1);
QStyleOptionViewItem opt = option;
initStyleOption(&opt, index);
QWidget *wnd = DApplication::activeWindow();
DPalette::ColorGroup cg;
if (!(opt.state & DStyle::State_Enabled)) {
cg = DPalette::Disabled;
} else {
if (!wnd) {
cg = DPalette::Inactive;
} else {
cg = DPalette::Active;
}
}
DStyle *style = dynamic_cast<DStyle *>(DApplication::style());
int margin = style->pixelMetric(DStyle::PM_ContentsMargins, &option);
//设置高亮文字色
DGuiApplicationHelper *dAppHelper = DGuiApplicationHelper::instance();
DPalette palette = dAppHelper->applicationPalette();
QPen forground;
forground.setColor(palette.color(cg, DPalette::Text));
if (opt.state & DStyle::State_Enabled) {
if (opt.state & DStyle::State_Selected) {
forground.setColor(palette.color(cg, DPalette::HighlightedText));
}
}
painter->setPen(forground);
QRect rect = opt.rect;
QFontMetrics fm(opt.font);
QPainterPath path, clipPath;
QRect textRect = rect;
switch (opt.viewItemPosition) {
case QStyleOptionViewItem::Beginning: {
// 左间距
rect.setX(rect.x() + margin);
} break;
case QStyleOptionViewItem::Middle: {
} break;
case QStyleOptionViewItem::End: {
// 右间距
rect.setWidth(rect.width() - margin);
} break;
case QStyleOptionViewItem::OnlyOne: {
// 左间距
rect.setX(rect.x() + margin);
// 右间距
rect.setWidth(rect.width() - margin);
} break;
default: {
painter->restore();
QStyledItemDelegate::paint(painter, option, index);
return;
}
}
//绘制图标
QRect iconRect = rect;
iconRect.setWidth(0);
if (opt.viewItemPosition == QStyleOptionViewItem::Beginning &&
index.data(Qt::DecorationRole).isValid()) {
iconRect.setX(rect.x() + margin);
iconRect.setY((rect.height() - ICON_HEIGHT) / 2 + rect.y());
iconRect.setWidth(ICON_WIDTH);
iconRect.setHeight(ICON_HEIGHT);
QIcon ic = index.data(Qt::DecorationRole).value<QIcon>();
ic.paint(painter, iconRect, Qt::AlignLeft | Qt::AlignVCenter);
}
//绘制文字
textRect = rect;
textRect.setX(iconRect.right() + margin - 2);
QString text = fm.elidedText(opt.text, opt.textElideMode, textRect.width());
painter->drawText(textRect, Qt::TextSingleLine | static_cast<int>(opt.displayAlignment), text);
painter->restore();
}
QSize LogViewItemDelegate::sizeHint(const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
QSize size = QStyledItemDelegate::sizeHint(option, index);
int nRowHeight = ROW_HEIGHT;
#ifdef DTKWIDGET_CLASS_DSizeMode
if (DGuiApplicationHelper::isCompactMode())
nRowHeight = ROW_HEIGHT_COMPACT;
else
nRowHeight = ROW_HEIGHT;
#else
nRowHeight = ROW_HEIGHT;
#endif
size.setHeight(nRowHeight);
return size;
}
/**
* @brief LogViewItemDelegate::initStyleOption 初始化样式,文字省略模式,内容居中居左等等
* @param option
* @param index
*/
void LogViewItemDelegate::initStyleOption(QStyleOptionViewItem *option,
const QModelIndex &index) const
{
option->showDecorationSelected = true;
bool ok = false;
if (index.data(Qt::TextAlignmentRole).isValid()) {
uint value = index.data(Qt::TextAlignmentRole).toUInt(&ok);
option->displayAlignment = static_cast<Qt::Alignment>(value);
}
if (!ok)
option->displayAlignment = Qt::AlignLeft | Qt::AlignVCenter;
option->textElideMode = Qt::ElideRight;
option->features = QStyleOptionViewItem::HasDisplay;
if (index.row() % 2 == 0)
option->features |= QStyleOptionViewItem::Alternate;
if (index.data(Qt::DisplayRole).isValid())
option->text = index.data().toString();
}
|