ukui-search/frontend/view/result-view-delegate.cpp

154 lines
5.3 KiB
C++
Raw Normal View History

2023-04-11 10:19:35 +08:00
/*
*
* Copyright (C) 2023, KylinSoft Co., Ltd.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*
*/
2021-05-25 19:42:40 +08:00
#include "result-view-delegate.h"
#include <QPainterPath>
#include <QApplication>
#include <QPen>
#include <QStyleFactory>
#include <QTreeView>
#include "search-result-model.h"
2021-12-14 14:43:35 +08:00
using namespace UkuiSearch;
static ResultItemStyle *global_instance_of_item_style = nullptr;
2021-05-25 19:42:40 +08:00
ResultViewDelegate::ResultViewDelegate(QObject *parent) : QStyledItemDelegate(parent),
m_textDoc(new QTextDocument(this)),
m_hightLightEffectHelper(new HightLightEffectHelper(this))
2021-05-25 19:42:40 +08:00
{
m_textDoc->setDefaultFont(QApplication::font());
connect(qApp, &QApplication::fontChanged, m_textDoc, &QTextDocument::setDefaultFont);
2021-05-25 19:42:40 +08:00
}
void ResultViewDelegate::setSearchKeyword(const QString &regFindKeyWords)
{
m_hightLightEffectHelper->setExpression(regFindKeyWords);
2021-05-25 19:42:40 +08:00
}
void ResultViewDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QStyleOptionViewItem opt = option;
2022-06-15 15:54:50 +08:00
initStyleOption(&opt, index);
opt.displayAlignment = QApplication::isLeftToRight() ?
Qt::Alignment(Qt::AlignLeft|Qt::AlignVCenter) : Qt::Alignment(Qt::AlignRight|Qt::AlignVCenter);
QString originalText = opt.text;
2022-06-15 15:54:50 +08:00
opt.text = QString();
QStyle *style = opt.widget->style();
style->drawControl(QStyle::CE_ItemViewItem, &opt, painter, opt.widget); //绘制非文本区域内容
2022-06-15 15:54:50 +08:00
QRect textRect = style->subElementRect(QStyle::SE_ItemViewItemText, &opt, opt.widget);
2022-07-05 10:26:00 +08:00
QFontMetrics fontMetrics(opt.font);
QString text = fontMetrics.elidedText(originalText, Qt::ElideRight, textRect.width() - m_textDoc->documentMargin() - 1); //富余5px的宽度
//无需显示tooltip时将状态位写回model
auto view = qobject_cast<QTreeView *>(this->parent());
if(text == originalText && originalText == index.data(Qt::ToolTipRole).toString()) {
if(view) {
view->model()->setData(index, false, AdditionalRoles::ShowToolTip);
}
} else {
if(view) {
view->model()->setData(index, true, AdditionalRoles::ShowToolTip);
}
}
2022-07-05 10:26:00 +08:00
opt.text = text;
2022-06-15 15:54:50 +08:00
painter->save();
if(opt.state & QStyle::State_Selected) {
QVariant selectPen = opt.widget->property("textSelectPen");
if(selectPen.isValid() && selectPen.canConvert<QPen>()) {
m_hightLightEffectHelper->setTextColor(selectPen.value<QPen>().brush());
} else {
m_hightLightEffectHelper->setTextColor(QBrush(opt.palette.highlightedText().color()));
}
2021-05-25 19:42:40 +08:00
} else {
m_hightLightEffectHelper->setTextColor(QBrush(opt.palette.text().color()));
2021-05-25 19:42:40 +08:00
}
2022-07-05 10:26:00 +08:00
m_textDoc->setPlainText(text);
m_hightLightEffectHelper->setDocument(m_textDoc);
m_hightLightEffectHelper->rehighlight();
int textRectX = QApplication::isLeftToRight() ? textRect.x() : textRect.width() - fontMetrics.horizontalAdvance(text) - m_textDoc->documentMargin();
painter->translate(textRectX, textRect.y() + (textRect.height() - fontMetrics.height()) / 2 - m_textDoc->documentMargin());
m_textDoc->drawContents(painter);
painter->restore();
2021-05-25 19:42:40 +08:00
}
ResultItemStyle *ResultItemStyle::getStyle()
{
if (!global_instance_of_item_style) {
global_instance_of_item_style = new ResultItemStyle;
}
return global_instance_of_item_style;
}
ResultItemStyle::ResultItemStyle()
{
connect(qApp, &QApplication::paletteChanged, this, [&](){
auto style = QStyleFactory::create("ukui");
if(style) {
setBaseStyle(style);
Q_EMIT baseStyleChanged();
}
});
}
int ResultItemStyle::styleHint(QStyle::StyleHint hint, const QStyleOption *option, const QWidget *widget, QStyleHintReturn *returnData) const
{
switch (hint) {
case SH_ItemView_ActivateItemOnSingleClick:
return false;
default:
return qApp->style()->styleHint(hint, option, widget, returnData);
}
}
HightLightEffectHelper::HightLightEffectHelper(QObject *parent) : QSyntaxHighlighter(parent)
{
m_expression.setCaseSensitivity(Qt::CaseInsensitive);
m_expression.setPatternSyntax(QRegExp::FixedString);
}
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);
}