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"
|
2022-02-24 19:58:47 +08:00
|
|
|
|
#include <QPainterPath>
|
2023-05-31 17:59:20 +08:00
|
|
|
|
#include <QApplication>
|
2023-12-14 14:00:03 +08:00
|
|
|
|
#include <QPen>
|
|
|
|
|
#include <QStyleFactory>
|
2024-03-04 17:55:42 +08:00
|
|
|
|
#include <QTreeView>
|
|
|
|
|
#include "search-result-model.h"
|
2021-12-14 14:43:35 +08:00
|
|
|
|
using namespace UkuiSearch;
|
2021-12-13 10:53:28 +08:00
|
|
|
|
static ResultItemStyle *global_instance_of_item_style = nullptr;
|
2021-05-25 19:42:40 +08:00
|
|
|
|
|
2022-06-17 10:45:21 +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
|
|
|
|
{
|
2022-12-30 13:52:33 +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 ®FindKeyWords)
|
|
|
|
|
{
|
2022-06-17 10:45:21 +08:00
|
|
|
|
m_hightLightEffectHelper->setExpression(regFindKeyWords);
|
2021-05-25 19:42:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-06-17 10:45:21 +08:00
|
|
|
|
void ResultViewDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
|
|
|
|
|
{
|
2022-05-19 13:47:54 +08:00
|
|
|
|
QStyleOptionViewItem opt = option;
|
2022-06-15 15:54:50 +08:00
|
|
|
|
initStyleOption(&opt, index);
|
2024-06-12 16:09:29 +08:00
|
|
|
|
opt.displayAlignment = QApplication::isLeftToRight() ?
|
|
|
|
|
Qt::Alignment(Qt::AlignLeft|Qt::AlignVCenter) : Qt::Alignment(Qt::AlignRight|Qt::AlignVCenter);
|
2022-05-19 13:47:54 +08:00
|
|
|
|
|
2024-03-04 17:55:42 +08:00
|
|
|
|
QString originalText = opt.text;
|
2022-06-15 15:54:50 +08:00
|
|
|
|
opt.text = QString();
|
2022-06-17 10:45:21 +08:00
|
|
|
|
|
|
|
|
|
QStyle *style = opt.widget->style();
|
2023-12-14 14:00:03 +08:00
|
|
|
|
style->drawControl(QStyle::CE_ItemViewItem, &opt, painter, opt.widget); //绘制非文本区域内容
|
2022-05-19 13:47:54 +08:00
|
|
|
|
|
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);
|
2024-03-04 17:55:42 +08:00
|
|
|
|
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;
|
|
|
|
|
|
2024-03-04 17:55:42 +08:00
|
|
|
|
|
2022-06-15 15:54:50 +08:00
|
|
|
|
painter->save();
|
2023-12-14 14:00:03 +08:00
|
|
|
|
|
2022-06-17 10:45:21 +08:00
|
|
|
|
if(opt.state & QStyle::State_Selected) {
|
2023-12-14 14:00:03 +08:00
|
|
|
|
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 {
|
2022-06-17 10:45:21 +08:00
|
|
|
|
m_hightLightEffectHelper->setTextColor(QBrush(opt.palette.text().color()));
|
2021-05-25 19:42:40 +08:00
|
|
|
|
}
|
2022-10-26 15:12:09 +08:00
|
|
|
|
|
2022-07-05 10:26:00 +08:00
|
|
|
|
m_textDoc->setPlainText(text);
|
2022-06-17 10:45:21 +08:00
|
|
|
|
m_hightLightEffectHelper->setDocument(m_textDoc);
|
|
|
|
|
m_hightLightEffectHelper->rehighlight();
|
2022-10-26 15:12:09 +08:00
|
|
|
|
|
2024-06-12 16:09:29 +08:00
|
|
|
|
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());
|
2022-06-17 10:45:21 +08:00
|
|
|
|
m_textDoc->drawContents(painter);
|
|
|
|
|
painter->restore();
|
2021-05-25 19:42:40 +08:00
|
|
|
|
}
|
2021-12-13 10:53:28 +08:00
|
|
|
|
|
|
|
|
|
ResultItemStyle *ResultItemStyle::getStyle()
|
|
|
|
|
{
|
|
|
|
|
if (!global_instance_of_item_style) {
|
|
|
|
|
global_instance_of_item_style = new ResultItemStyle;
|
|
|
|
|
}
|
|
|
|
|
return global_instance_of_item_style;
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-14 14:00:03 +08:00
|
|
|
|
ResultItemStyle::ResultItemStyle()
|
|
|
|
|
{
|
|
|
|
|
connect(qApp, &QApplication::paletteChanged, this, [&](){
|
|
|
|
|
auto style = QStyleFactory::create("ukui");
|
|
|
|
|
if(style) {
|
|
|
|
|
setBaseStyle(style);
|
|
|
|
|
Q_EMIT baseStyleChanged();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-04 17:35:40 +08:00
|
|
|
|
int ResultItemStyle::styleHint(QStyle::StyleHint hint, const QStyleOption *option, const QWidget *widget, QStyleHintReturn *returnData) const
|
2022-05-19 13:47:54 +08:00
|
|
|
|
{
|
2022-11-04 17:35:40 +08:00
|
|
|
|
switch (hint) {
|
|
|
|
|
case SH_ItemView_ActivateItemOnSingleClick:
|
|
|
|
|
return false;
|
2021-12-13 10:53:28 +08:00
|
|
|
|
default:
|
2022-11-04 17:35:40 +08:00
|
|
|
|
return qApp->style()->styleHint(hint, option, widget, returnData);
|
2021-12-13 10:53:28 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-06-17 10:45:21 +08:00
|
|
|
|
|
|
|
|
|
HightLightEffectHelper::HightLightEffectHelper(QObject *parent) : QSyntaxHighlighter(parent)
|
|
|
|
|
{
|
|
|
|
|
m_expression.setCaseSensitivity(Qt::CaseInsensitive);
|
2022-07-15 11:47:39 +08:00
|
|
|
|
m_expression.setPatternSyntax(QRegExp::FixedString);
|
2022-06-17 10:45:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|