From 4e7e2e3a8a2cbcda1ac543b4fcd630613a29a221 Mon Sep 17 00:00:00 2001 From: JunjieBai Date: Fri, 27 Oct 2023 15:06:55 +0800 Subject: [PATCH] fix(app-search-plugin):add a method to get snippet of text and use it to wrap description of online application. --- libsearch/appsearch/app-search-plugin.cpp | 5 +- libsearch/file-utils.cpp | 67 +++++++++++++++++++++++ libsearch/file-utils.h | 1 + 3 files changed, 71 insertions(+), 2 deletions(-) diff --git a/libsearch/appsearch/app-search-plugin.cpp b/libsearch/appsearch/app-search-plugin.cpp index b10ba5d..3a0e907 100644 --- a/libsearch/appsearch/app-search-plugin.cpp +++ b/libsearch/appsearch/app-search-plugin.cpp @@ -156,7 +156,8 @@ QWidget *AppSearchPlugin::detailPage(const ResultInfo &ri) m_actionLabel2->hide(); m_actionLabel3->hide(); m_actionLabel4->show(); - QString showDesc = fontMetrics.elidedText(ri.description.at(0).key + " " + ri.description.at(0).value, Qt::ElideRight, 3114); //当字体长度超过215时显示为省略号 +// QString showDesc = fontMetrics.elidedText(ri.description.at(0).key + " " + ri.description.at(0).value, Qt::ElideRight, 3114); //当字体长度超过215时显示为省略号 + QString showDesc = FileUtils::getSnippetWithoutKeyword(ri.description.at(0).key + " " + ri.description.at(0).value, 10); m_descLabel->setText(FileUtils::escapeHtml(showDesc)); m_descFrame->show(); m_line_2->show(); @@ -235,7 +236,7 @@ void AppSearchPlugin::initDetailPage() m_descFrameLyt = new QVBoxLayout(m_descFrame); m_descLabel = new QLabel(m_descFrame); m_descLabel->setTextFormat(Qt::PlainText); - m_descLabel->setWordWrap(true); +// m_descLabel->setWordWrap(true); m_descFrameLyt->addWidget(m_descLabel); m_descFrame->setLayout(m_descFrameLyt); m_descFrameLyt->setContentsMargins(8, 0, 0, 0); diff --git a/libsearch/file-utils.cpp b/libsearch/file-utils.cpp index 4a9a45b..00f2a2e 100644 --- a/libsearch/file-utils.cpp +++ b/libsearch/file-utils.cpp @@ -1366,3 +1366,70 @@ void FileUtils::getOFDTextContent(const QString &path, QString &textContent) zipfile.close(); } + +QString FileUtils::getSnippetWithoutKeyword(const QString &content, int lineCount) { + QString snippet; + int numOfLine = 0; + QFont font(qApp->font().family()); + font.setPointSizeF(qApp->font().pointSizeF()); + QFontMetricsF fontMetricsF(font); + + qreal length = 0; + int wordCount = 0; + int boundaryStart = 0; + int boundaryEnd = 0; + QTextBoundaryFinder fm(QTextBoundaryFinder::Grapheme, content); + + for(;fm.position() != -1;fm.toNextBoundary()) { + boundaryEnd = fm.position(); + if (boundaryEnd == boundaryStart) { + continue; + } + if (numOfLine == lineCount) { + break; + } + QString word = content.mid(boundaryStart, boundaryEnd - boundaryStart); + wordCount += boundaryEnd - boundaryStart; + length = fontMetricsF.horizontalAdvance(content.mid(boundaryStart - wordCount, wordCount)); + + if (length >= LABEL_MAX_WIDTH || word == "\n") { + if (word == "\n") { + boundaryStart = boundaryEnd; + } else if (length > LABEL_MAX_WIDTH) { + fm.toPreviousBoundary(); + } else { + boundaryStart = boundaryEnd; + snippet.append(word); + } + snippet.append("\n"); + + numOfLine++; + if (numOfLine == lineCount) { + qreal distance = 1;//最后一位必然是\n + qreal wordSize = 0; + if (!(word == "\n" && length < LABEL_MAX_WIDTH)) { + if (length > LABEL_MAX_WIDTH) { + boundaryEnd = boundaryStart; + } + while (wordSize < fontMetricsF.horizontalAdvance("…")) { + boundaryStart = fm.position(); + + wordSize += fontMetricsF.horizontalAdvance(content.mid(boundaryStart, boundaryEnd - boundaryStart)); + distance += (boundaryEnd - boundaryStart); + boundaryEnd = boundaryStart; + fm.toPreviousBoundary(); + } + } + snippet = snippet.left(snippet.size() - distance); + snippet.append("…"); + break; + } + length = 0; + wordCount = 0; + continue; + } + snippet.append(word); + boundaryStart = boundaryEnd; + } + return snippet; +} diff --git a/libsearch/file-utils.h b/libsearch/file-utils.h index fdca51e..49d6460 100644 --- a/libsearch/file-utils.h +++ b/libsearch/file-utils.h @@ -62,6 +62,7 @@ public: static bool copyPath(QString &path); static QString escapeHtml(const QString &str); static QString getSnippet(const std::string &myStr, uint start, const QString &keyword); + static QString getSnippetWithoutKeyword(const QString &content, int lineCount); static bool isOpenXMLFileEncrypted(const QString &path); /** * @brief isEncrypedOrUnsupport