优化结果列表字体绘制方法,避免文字位置显示错误;

增加treeview圆角效果;
调整UI圆角大小。
This commit is contained in:
iaom 2022-05-19 13:47:54 +08:00
parent 7b9dbaaf92
commit 9c9161d9f3
5 changed files with 131 additions and 28 deletions

View File

@ -49,7 +49,7 @@ SearchLineEdit::SearchLineEdit(QWidget *parent) : QLineEdit(parent) {
m_queryIcon->setPixmap(pixmap); m_queryIcon->setPixmap(pixmap);
m_ly = new QHBoxLayout(this); m_ly = new QHBoxLayout(this);
m_ly->addSpacing(4); m_ly->addSpacing(8);
m_ly->addWidget(m_queryIcon); m_ly->addWidget(m_queryIcon);
m_ly->addStretch(); m_ly->addStretch();
@ -88,7 +88,7 @@ void SearchLineEdit::paintEvent(QPaintEvent *e)
p.setBrush(palette().base()); p.setBrush(palette().base());
p.setOpacity(GlobalSettings::getInstance()->getValue(TRANSPARENCY_KEY).toDouble()); p.setOpacity(GlobalSettings::getInstance()->getValue(TRANSPARENCY_KEY).toDouble());
p.setPen(Qt::NoPen); p.setPen(Qt::NoPen);
p.drawRoundedRect(this->rect(), 6, 6); p.drawRoundedRect(this->rect(), 12, 12);
return QLineEdit::paintEvent(e); return QLineEdit::paintEvent(e);
} }
@ -132,7 +132,7 @@ void SeachBarWidget::paintEvent(QPaintEvent *e)
QPainter p(this); QPainter p(this);
p.setRenderHint(QPainter::Antialiasing); p.setRenderHint(QPainter::Antialiasing);
QPainterPath rectPath; QPainterPath rectPath;
rectPath.addRoundedRect(this->rect().adjusted(10, 10, -10, -10), 6, 6); rectPath.addRoundedRect(this->rect().adjusted(10, 10, -10, -10), 12, 12);
// 画一个黑底 // 画一个黑底
@ -191,7 +191,7 @@ void LineEditStyle::drawPrimitive(QStyle::PrimitiveElement element, const QStyle
painter->setPen(Qt::NoPen); painter->setPen(Qt::NoPen);
painter->setBrush(f->palette.brush(QPalette::Active, QPalette::Button)); painter->setBrush(f->palette.brush(QPalette::Active, QPalette::Button));
painter->setRenderHint(QPainter::Antialiasing, true); painter->setRenderHint(QPainter::Antialiasing, true);
painter->drawRoundedRect(option->rect, 4, 4); painter->drawRoundedRect(option->rect, 12, 12);
painter->restore(); painter->restore();
return; return;
} }

View File

@ -88,10 +88,10 @@ void SearchResultPage::paintEvent(QPaintEvent *event)
p.setBrush(palette().base()); p.setBrush(palette().base());
p.setOpacity(GlobalSettings::getInstance()->getValue(TRANSPARENCY_KEY).toDouble()); p.setOpacity(GlobalSettings::getInstance()->getValue(TRANSPARENCY_KEY).toDouble());
p.setPen(Qt::NoPen); p.setPen(Qt::NoPen);
p.drawRoundedRect(this->rect().adjusted(10,10,-10,-10), 6, 6); p.drawRoundedRect(this->rect().adjusted(10,10,-10,-10), 12, 12);
QPainterPath rectPath; QPainterPath rectPath;
rectPath.addRoundedRect(this->rect().adjusted(10, 10, -10, -10), 6, 6); rectPath.addRoundedRect(this->rect().adjusted(10, 10, -10, -10), 12, 12);
// 画一个黑底 // 画一个黑底
QPixmap pixmap(this->rect().size()); QPixmap pixmap(this->rect().size());

View File

@ -549,8 +549,8 @@ void MainWindow::paintEvent(QPaintEvent *event) {
QPainterPath path; QPainterPath path;
path.addRoundedRect(m_searchBarWidget->x()+10, m_searchBarWidget->y()+10, m_searchBarWidget->width()-20, m_searchBarWidget->height()-20, 6, 6); path.addRoundedRect(m_searchBarWidget->x()+10, m_searchBarWidget->y()+10, m_searchBarWidget->width()-20, m_searchBarWidget->height()-20, 12, 12);
path.addRoundedRect(m_searchResultPage->x()+10, m_searchResultPage->y()+10, m_searchResultPage->width()-20, m_searchResultPage->height()-20, 6, 6); path.addRoundedRect(m_searchResultPage->x()+10, m_searchResultPage->y()+10, m_searchResultPage->width()-20, m_searchResultPage->height()-20, 12, 12);
KWindowEffects::enableBlurBehind(this->winId(), true, QRegion(path.toFillPolygon().toPolygon())); KWindowEffects::enableBlurBehind(this->winId(), true, QRegion(path.toFillPolygon().toPolygon()));

View File

@ -22,31 +22,49 @@ QSize ResultViewDelegate::sizeHint(const QStyleOptionViewItem &option, const QMo
} }
void ResultViewDelegate::paint(QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index) const { void ResultViewDelegate::paint(QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index) const {
QStyleOptionViewItemV4 optionV4 = option; QStyleOptionViewItem opt = option;
initStyleOption(&optionV4, index); initStyleOption(&opt, index);
QStyle *style = optionV4.widget ? optionV4.widget->style() : QApplication::style(); QStyle *style = opt.widget ? opt.widget->style() : QApplication::style();
optionV4.text = QString(); QString text = opt.text;
style->drawControl(QStyle::CE_ItemViewItem, &optionV4, painter); //绘制非文本区域内容 if(text.isEmpty()) {
if(index.model()->data(index, Qt::DisplayRole).toString().isEmpty()) return; return;
}
opt.text = QString();
style->drawControl(QStyle::CE_ItemViewItem, &opt, painter); //绘制非文本区域内容
// style->drawPrimitive(QStyle::PE_PanelItemViewItem, qstyleoption_cast<QStyleOption *>(&opt), painter, opt.widget);
//fix me: for files which name begin with some ' ' , space will be hide... opt.text = text;
QTextDocument doc; QTextDocument doc;
doc.setHtml(getHtmlText(painter, option, index)); //提取富文本 doc.setHtml(getHtmlText(painter, opt, index)); //提取富文本
QAbstractTextDocumentLayout::PaintContext ctx; QAbstractTextDocumentLayout* layout = doc.documentLayout();
const double height = layout->documentSize().height();
if(optionV4.state & QStyle::State_Selected)
ctx.palette.setColor(QPalette::Text, optionV4.palette.color(QPalette::Active, QPalette::HighlightedText));
QRect textRect = style->subElementRect(QStyle::SE_ItemViewItemText, &optionV4); QRect textRect = style->subElementRect(QStyle::SE_ItemViewItemText, &opt, opt.widget);
//使图标和文本间隔与原来保持一致故文本区域右移4 //使图标和文本间隔与原来保持一致故文本区域右移4
textRect.adjust(4, 0, 0, 0); textRect.adjust(4, 0, 0, 0);
painter->save(); double y = textRect.y();
painter->translate(textRect.topLeft()); y += (textRect.height() - height) / 2;
painter->setClipRect(textRect.translated(-textRect.topLeft()));
doc.documentLayout()->draw(painter, ctx); //绘制文本区域内容 QAbstractTextDocumentLayout::PaintContext context;
painter->restore();
QPalette::ColorGroup cg = opt.state & QStyle::State_Enabled
? QPalette::Normal : QPalette::Disabled;
if (cg == QPalette::Normal && !(opt.state & QStyle::State_Active))
cg = QPalette::Inactive;
if(opt.state & QStyle::State_Selected) {
painter->setPen(opt.palette.color(cg, QPalette::HighlightedText));
} else {
painter->setPen(opt.palette.color(cg, QPalette::Text));
}
painter->save();
painter->translate(QPointF(textRect.x(), y));
layout->draw(painter, context); //绘制文本区域内容
painter->restore();
} }
QString ResultViewDelegate::getHtmlText(QPainter *painter, const QStyleOptionViewItem &itemOption, const QModelIndex &index) const QString ResultViewDelegate::getHtmlText(QPainter *painter, const QStyleOptionViewItem &itemOption, const QModelIndex &index) const
@ -99,6 +117,90 @@ ResultItemStyle *ResultItemStyle::getStyle()
return global_instance_of_item_style; return global_instance_of_item_style;
} }
void ResultItemStyle::drawPrimitive(QStyle::PrimitiveElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget) const
{
switch (element) {
case PE_PanelItemViewItem: {
bool isIconView = false;
auto opt = qstyleoption_cast<const QStyleOptionViewItem *>(option);
if (!opt)
return;
if (opt) {
isIconView = (opt->decorationPosition & QStyleOptionViewItem::Top);
}
bool isHover = (option->state & State_MouseOver) && (option->state & ~State_Selected);
bool isSelected = option->state & State_Selected;
bool enable = option->state & State_Enabled;
QColor color = option->palette.color(enable? QPalette::Active: QPalette::Disabled, QPalette::Highlight);
if (isSelected) {
color.setAlpha(255);
} else if (isHover) {
// color = opt->palette.color(QPalette::Active, QPalette::BrightText);
// color.setAlpha(0.05);
color = QColor(241, 241, 241);
} else {
color.setAlpha(0);
}
QPainterPath path;
if(opt->viewItemPosition == QStyleOptionViewItem::OnlyOne) {
path.addRoundedRect(option->rect, 6, 6);
} else if(opt->viewItemPosition == QStyleOptionViewItem::Beginning) {
//一个左侧有两个圆角的矩形
path.moveTo(option->rect.topLeft() + QPoint(6, 0));
path.cubicTo(option->rect.topLeft() + QPoint(6, 0),
option->rect.topLeft(), option->rect.topLeft() +
QPoint(0, 6));
path.lineTo(option->rect.bottomLeft() - QPoint(0, 6));
path.cubicTo(option->rect.bottomLeft() - QPoint(0, 6),
option->rect.bottomLeft() + QPoint(0, 1),
option->rect.bottomLeft() + QPoint(6, 1));
path.lineTo(option->rect.bottomRight() + QPoint(1, 1));
path.lineTo(option->rect.topRight()+ QPoint(1, 0));
path.lineTo(option->rect.topLeft() + QPoint(6, 0));
} else if(opt->viewItemPosition == QStyleOptionViewItem::Middle) {
path.addRect(option->rect.adjusted(-1, 0, 1, 0));
} else if(opt->viewItemPosition == QStyleOptionViewItem::End) {
//一个右侧有两个圆角的矩形
path.moveTo(option->rect.topRight() + QPoint(-6, 0));
path.cubicTo(option->rect.topRight() + QPoint(-6, 0),
option->rect.topRight(),
option->rect.topRight() +
QPoint(0, 6));
path.lineTo(option->rect.bottomRight() - QPoint(0, 6));
path.cubicTo(option->rect.bottomRight() - QPoint(0, 6),
option->rect.bottomRight() + QPoint(0, 1),
option->rect.bottomRight() + QPoint(-6, 1));
path.lineTo(option->rect.bottomLeft() + QPoint(0, 1));
path.lineTo(option->rect.topLeft());
path.lineTo(option->rect.topRight() + QPoint(-6, 0));
} else {
// path.addRoundedRect(option->rect, 8, 8);
}
painter->save();
painter->setRenderHint(QPainter::Antialiasing);
painter->setPen(Qt::NoPen);
painter->setBrush(color);
painter->drawPath(path);
// painter->fillPath(path, painter->brush());
painter->restore();
break;
}
default:
return QProxyStyle::drawPrimitive(element, option, painter, widget);
}
}
void ResultItemStyle::drawControl(QStyle::ControlElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget) const void ResultItemStyle::drawControl(QStyle::ControlElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget) const
{ {
switch (element) { switch (element) {

View File

@ -49,6 +49,7 @@ class ResultItemStyle : public QProxyStyle
{ {
public: public:
static ResultItemStyle *getStyle(); static ResultItemStyle *getStyle();
void drawPrimitive(QStyle::PrimitiveElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget = nullptr) const;
void drawControl(ControlElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget = nullptr) const override; void drawControl(ControlElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget = nullptr) const override;
private: private:
explicit ResultItemStyle() {} explicit ResultItemStyle() {}