Merge branch 'ukss-dev' into 'ukss-dev'

优化关键词高亮方案,结果项显示效果适配主题框架。

See merge request kylin-desktop/ukui-search!335
This commit is contained in:
PengfeiZhang 2022-06-18 01:14:27 +00:00
commit 7909eb3ba0
2 changed files with 72 additions and 80 deletions

View File

@ -3,15 +3,15 @@
using namespace UkuiSearch; using namespace UkuiSearch;
static ResultItemStyle *global_instance_of_item_style = nullptr; static ResultItemStyle *global_instance_of_item_style = nullptr;
ResultViewDelegate::ResultViewDelegate(QObject *parent) : QStyledItemDelegate(parent) ResultViewDelegate::ResultViewDelegate(QObject *parent) : QStyledItemDelegate(parent),
m_textDoc(new QTextDocument(this)),
m_hightLightEffectHelper(new HightLightEffectHelper(this))
{ {
} }
void ResultViewDelegate::setSearchKeyword(const QString &regFindKeyWords) void ResultViewDelegate::setSearchKeyword(const QString &regFindKeyWords)
{ {
m_regFindKeyWords.clear(); m_hightLightEffectHelper->setExpression(regFindKeyWords);
m_regFindKeyWords = regFindKeyWords;
} }
QSize ResultViewDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const QSize ResultViewDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
@ -21,90 +21,33 @@ QSize ResultViewDelegate::sizeHint(const QStyleOptionViewItem &option, const QMo
return size; return size;
} }
void ResultViewDelegate::paint(QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index) const { void ResultViewDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QStyleOptionViewItem opt = option; QStyleOptionViewItem opt = option;
initStyleOption(&opt, index); initStyleOption(&opt, index);
QStyle *style = opt.widget->style(); opt.displayAlignment = Qt::Alignment(Qt::AlignLeft|Qt::AlignVCenter);
QString text = opt.text; QString text = opt.text;
if(text.isEmpty()) {
return;
}
opt.text = QString(); opt.text = QString();
QStyle *style = opt.widget->style();
style->proxy()->drawControl(QStyle::CE_ItemViewItem, &opt, painter, opt.widget); //绘制非文本区域内容 style->proxy()->drawControl(QStyle::CE_ItemViewItem, &opt, painter, opt.widget); //绘制非文本区域内容
opt.text = text; opt.text = text;
QTextDocument doc;
doc.setHtml(getHtmlText(painter, opt, index)); //提取富文本
QAbstractTextDocumentLayout* layout = doc.documentLayout();
const double height = layout->documentSize().height();
QRect textRect = style->subElementRect(QStyle::SE_ItemViewItemText, &opt, opt.widget); QRect textRect = style->subElementRect(QStyle::SE_ItemViewItemText, &opt, opt.widget);
//使图标和文本间隔与原来保持一致故文本区域右移4
// textRect.adjust(4, 0, 0, 0);
double y = textRect.y();
y += (textRect.height() - height) / 2;
QAbstractTextDocumentLayout::PaintContext context;
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->save();
painter->translate(QPointF(textRect.x(), y)); if(opt.state & QStyle::State_Selected) {
layout->draw(painter, context); //绘制文本区域内容 m_hightLightEffectHelper->setTextColor(QBrush(opt.palette.highlightedText().color()));
painter->restore();
}
QString ResultViewDelegate::getHtmlText(QPainter *painter, const QStyleOptionViewItem &itemOption, const QModelIndex &index) const
{
int indexFindLeft = 0;
QString indexString = index.model()->data(index, Qt::DisplayRole).toString();
QFont ft(painter->font().family(), GlobalSettings::getInstance()->getValue(FONT_SIZE_KEY).toInt());
QFontMetrics fm(ft);
QString indexColString = fm.elidedText(indexString, Qt::ElideRight, itemOption.rect.width() - 30 - 10); //当字体超过Item的长度时显示为省略号
QString htmlString;
if((indexColString.toUpper()).contains((m_regFindKeyWords.toUpper()))) {
indexFindLeft = indexColString.toUpper().indexOf(m_regFindKeyWords.toUpper()); //得到查找字体在当前整个Item字体中的位置
htmlString = escapeHtml(indexColString.left(indexFindLeft)) + "<b>" + escapeHtml(indexColString.mid(indexFindLeft, m_regFindKeyWords.length())) + "</b>" + escapeHtml(indexColString.right(indexColString.length() - indexFindLeft - m_regFindKeyWords.length()));
} else { } else {
bool boldOpenned = false; m_hightLightEffectHelper->setTextColor(QBrush(opt.palette.text().color()));
for(int i = 0; i < indexColString.length(); i++) {
if((m_regFindKeyWords.toUpper()).contains(QString(indexColString.at(i)).toUpper())) {
if(! boldOpenned) {
boldOpenned = true;
htmlString.append(QString("<b>"));
}
htmlString.append(escapeHtml(QString(indexColString.at(i))));
} else {
if(boldOpenned) {
boldOpenned = false;
htmlString.append(QString("</b>"));
}
htmlString.append(escapeHtml(QString(indexColString.at(i))));
}
}
} }
// qDebug()<<indexColString<<"---->"<<htmlString; painter->translate(textRect.topLeft());
return "<pre>" + htmlString + "</pre>";
}
QString ResultViewDelegate::escapeHtml(const QString &str) const m_textDoc->setHtml("<pre>" + text + "</pre>");
{ m_hightLightEffectHelper->setDocument(m_textDoc);
QString temp = str; m_hightLightEffectHelper->rehighlight();
temp.replace("<", "&lt;"); m_textDoc->drawContents(painter);
temp.replace(">", "&gt;"); painter->restore();
return temp;
} }
ResultItemStyle *ResultItemStyle::getStyle() ResultItemStyle *ResultItemStyle::getStyle()
@ -259,3 +202,32 @@ void ResultItemStyle::drawControl(QStyle::ControlElement element, const QStyleOp
break; break;
} }
} }
HightLightEffectHelper::HightLightEffectHelper(QObject *parent) : QSyntaxHighlighter(parent)
{
m_expression.setCaseSensitivity(Qt::CaseInsensitive);
}
void HightLightEffectHelper::setExpression(const QString &text)
{
m_expression.setPattern(text);
}
void HightLightEffectHelper::setTextColor(const QBrush &brush)
{
m_textCharFormat.setForeground(brush);
}
void HightLightEffectHelper::highlightBlock(const QString &text)
{
setFormat(0, text.length(), m_textCharFormat);
m_textCharFormat.setFontWeight(QFont::Bold);
int index = text.indexOf(m_expression);
while(index >= 0){
int length = m_expression.matchedLength();
setFormat(index, length, m_textCharFormat);
index = text.indexOf(m_expression, index+length);
}
m_textCharFormat.setFontWeight(QFont::Normal);
}

View File

@ -27,10 +27,29 @@
#include <QTextDocument> #include <QTextDocument>
#include <QAbstractTextDocumentLayout> #include <QAbstractTextDocumentLayout>
#include <QProxyStyle> #include <QProxyStyle>
#include <QSyntaxHighlighter>
#include <QTextCharFormat>
#include <QRegExp>
#include "global-settings.h" #include "global-settings.h"
namespace UkuiSearch { namespace UkuiSearch {
class ResultViewDelegate : public QStyledItemDelegate { class HightLightEffectHelper : public QSyntaxHighlighter
{
public:
explicit HightLightEffectHelper(QObject *parent = nullptr);
void setExpression(const QString &text);
void setTextColor(const QBrush &brush);
protected:
void highlightBlock(const QString &text);
private:
QRegExp m_expression;
QTextCharFormat m_textCharFormat;
};
class ResultViewDelegate : public QStyledItemDelegate
{
Q_OBJECT Q_OBJECT
public: public:
explicit ResultViewDelegate(QObject *parent = nullptr); explicit ResultViewDelegate(QObject *parent = nullptr);
@ -38,11 +57,12 @@ public:
void setSearchKeyword(const QString &); void setSearchKeyword(const QString &);
protected: protected:
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const; QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const;
private:
QString m_regFindKeyWords = 0;
void paint(QPainter *, const QStyleOptionViewItem &, const QModelIndex &) const override; void paint(QPainter *, const QStyleOptionViewItem &, const QModelIndex &) const override;
QString getHtmlText(QPainter *, const QStyleOptionViewItem &, const QModelIndex &) const;
QString escapeHtml(const QString&) const; private:
QTextDocument *m_textDoc = nullptr;
HightLightEffectHelper *m_hightLightEffectHelper = nullptr;
}; };
class ResultItemStyle : public QProxyStyle class ResultItemStyle : public QProxyStyle