131 lines
3.3 KiB
C++
131 lines
3.3 KiB
C++
#include "ordertypedelegate.h"
|
|
|
|
/*
|
|
==================== Order Type Delegate ====================
|
|
订单类型代理:
|
|
根据不同订单类型,设置单元格格背景颜色
|
|
*/
|
|
OrderTypeDelegate::OrderTypeDelegate(QObject *parent)
|
|
: QStyledItemDelegate(parent)
|
|
{
|
|
}
|
|
|
|
void OrderTypeDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
|
|
const QModelIndex &index) const
|
|
{
|
|
if (!index.isValid()) return;
|
|
|
|
QStyleOptionViewItem opt = option;
|
|
initStyleOption(&opt, index);
|
|
|
|
|
|
opt.displayAlignment = Qt::AlignCenter; // 设置文字居中
|
|
|
|
// 获取订单类型
|
|
QString type = index.data(Qt::DisplayRole).toString();
|
|
// 设置背景色
|
|
if (type == "买入") {
|
|
painter->fillRect(option.rect, m_buyColor);
|
|
}
|
|
else if (type == "卖出") {
|
|
painter->fillRect(option.rect, m_sellColor);
|
|
}
|
|
|
|
// // 添加悬停效果
|
|
// if (option.state & QStyle::State_MouseOver) {
|
|
// QColor hoverColor = (type == "买入")
|
|
// ? m_buyColor.lighter(120)
|
|
// : m_sellColor.lighter(120);
|
|
// painter->fillRect(option.rect, hoverColor);
|
|
// }
|
|
|
|
painter->save();
|
|
painter->setFont(opt.font);
|
|
painter->setPen(opt.palette.color(QPalette::Text));
|
|
painter->drawText(opt.rect, Qt::AlignCenter, opt.text);
|
|
painter->restore();
|
|
}
|
|
|
|
|
|
void OrderTypeDelegate::setBuyColor(const QColor &color)
|
|
{
|
|
m_buyColor = color;
|
|
}
|
|
|
|
void OrderTypeDelegate::setSellColor(const QColor &color)
|
|
{
|
|
m_sellColor = color;
|
|
}
|
|
|
|
|
|
NumberFormatDelegate::NumberFormatDelegate(QObject *parent)
|
|
: QStyledItemDelegate(parent)
|
|
{
|
|
}
|
|
|
|
// 暂时不动,后面只在显示端进行格式化,方便统一处理数据
|
|
QString NumberFormatDelegate::displayText(const QVariant &value, const QLocale &locale) const
|
|
{
|
|
bool ok;
|
|
double num = value.toDouble(&ok);
|
|
|
|
if (ok) {
|
|
// 格式化为带千位分隔符的数字
|
|
return locale.toString(num, 'f', 0);
|
|
}
|
|
|
|
return QStyledItemDelegate::displayText(value, locale);
|
|
}
|
|
|
|
|
|
HighlightDelegate::HighlightDelegate(QObject *parent)
|
|
: QStyledItemDelegate(parent)
|
|
{
|
|
}
|
|
|
|
void HighlightDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
|
|
const QModelIndex &index) const
|
|
{
|
|
QStyleOptionViewItem opt = option;
|
|
initStyleOption(&opt, index);
|
|
|
|
// 设置文字居中
|
|
opt.displayAlignment = Qt::AlignCenter;
|
|
|
|
// 检查是否需要高亮 - 通过映射到源模型获取高亮状态
|
|
QAbstractItemModel *model = const_cast<QAbstractItemModel*>(index.model());
|
|
QSortFilterProxyModel *proxyModel = qobject_cast<QSortFilterProxyModel*>(model);
|
|
|
|
bool isHighlighted = false;
|
|
if (proxyModel) {
|
|
QModelIndex sourceIndex = proxyModel->mapToSource(index);
|
|
isHighlighted = sourceIndex.data(IsHighlightedRole).toBool();
|
|
}
|
|
else {
|
|
isHighlighted = index.data(IsHighlightedRole).toBool();
|
|
}
|
|
|
|
if (isHighlighted) {
|
|
// 设置高亮背景
|
|
painter->fillRect(opt.rect, QColor(255, 192, 203)); // 浅黄色背景
|
|
|
|
//QFont boldFont = opt.font;// 设置加粗字体
|
|
//boldFont.setBold(true);
|
|
//painter->setFont(boldFont);
|
|
|
|
// 绘制文本
|
|
painter->setPen(opt.palette.color(QPalette::Text));
|
|
painter->drawText(opt.rect, opt.displayAlignment, opt.text);
|
|
}
|
|
else {
|
|
// 正常绘制
|
|
QStyledItemDelegate::paint(painter, opt, index);
|
|
|
|
// 正常绘制(居中)
|
|
painter->save();
|
|
painter->setFont(opt.font);
|
|
painter->setPen(opt.palette.color(QPalette::Text));
|
|
painter->drawText(opt.rect, Qt::AlignCenter, opt.text);
|
|
painter->restore();
|
|
}
|
|
} |