fix(app-search-plugin):add a method to get snippet of text and use it to wrap description of online application.

This commit is contained in:
JunjieBai 2023-10-27 15:06:55 +08:00
parent 26d363ab3f
commit 4e7e2e3a8a
3 changed files with 71 additions and 2 deletions

View File

@ -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);

View File

@ -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;
}

View File

@ -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