From 95955e5d921c95a80ae3edd483620d2b389a5dba Mon Sep 17 00:00:00 2001 From: jixiaoxu Date: Thu, 23 Sep 2021 20:49:21 +0800 Subject: [PATCH] Fix detail page content display bug; --- libsearch/file-utils.cpp | 40 +++++++++++++++++--------- libsearch/index/file-search-plugin.cpp | 13 ++++----- 2 files changed, 33 insertions(+), 20 deletions(-) diff --git a/libsearch/file-utils.cpp b/libsearch/file-utils.cpp index 9958ed8..ecffc8f 100644 --- a/libsearch/file-utils.cpp +++ b/libsearch/file-utils.cpp @@ -842,33 +842,47 @@ QString FileUtils::chineseSubString(const std::string &myStr, int start, int len if(start < 0 || length < 0){ return " "; } - if (length >= myStr.length()) { - return QString::fromStdString(myStr); - } - QString sub = ""; + QString sub = QString::fromStdString(myStr); QFont ft(QApplication::font().family(),QApplication::font().pointSize()); QFontMetrics fm (ft); + if (length >= myStr.length()) { + afterSub = myStr.substr(start,length); //截取; + if (fm.width(QString::fromStdString(afterSub)) >= 2*LABEL_MAX_WIDTH) { + sub = fm.elidedText(sub, Qt::ElideRight, 2*LABEL_MAX_WIDTH); //超过两行则省略 + } else { + sub = fm.elidedText(sub, Qt::ElideLeft, 2*LABEL_MAX_WIDTH); //超过两行则省略 + } + return sub; + } if (start + length <= myStr.length()) { afterSub = myStr.substr(start,length); //截取 sub = QString::fromStdString(afterSub); //转QString if(start + length < myStr.length()){ - sub.replace(sub.length()-3,3,"..."); //替换后三位 - } - else{ - sub.append("..."); //直接加 + sub.replace(sub.length()-3,3,"…"); //替换后三位 + } else{ + sub.append("…"); //直接加 } sub = fm.elidedText(sub, Qt::ElideRight, 2*LABEL_MAX_WIDTH); //超过两行则省略 - } - else { + } else { int newStart = myStr.length()-length; //更新截取位置 + afterSub = myStr.substr(newStart, length); sub=QString::fromStdString(afterSub); - - sub.replace(0,3,"...").append("..."); - sub = fm.elidedText(sub, Qt::ElideLeft, 2*LABEL_MAX_WIDTH); + if (fm.width(QString::fromStdString(myStr.substr(newStart, start))) >= 2*LABEL_MAX_WIDTH) { + sub = fm.elidedText(sub, Qt::ElideLeft, 2*LABEL_MAX_WIDTH); + } else { + if (newStart + 3 < start) { + sub.replace(0,3,"…").append("…"); + } else { + afterSub = myStr.substr(start, length); + sub = "…" + QString::fromStdString(afterSub); + sub.append("…"); + } + sub = fm.elidedText(sub, Qt::ElideRight, 2*LABEL_MAX_WIDTH); + } } return sub; } diff --git a/libsearch/index/file-search-plugin.cpp b/libsearch/index/file-search-plugin.cpp index bf1a4db..785044b 100644 --- a/libsearch/index/file-search-plugin.cpp +++ b/libsearch/index/file-search-plugin.cpp @@ -87,9 +87,9 @@ QWidget *FileSearchPlugin::detailPage(const ResultInfo &ri) QFontMetrics fontMetrics = m_nameLabel->fontMetrics(); QString showname = fontMetrics.elidedText(ri.name, Qt::ElideRight, 215); //当字体长度超过215时显示为省略号 m_nameLabel->setText(QString("

%1

").arg(FileUtils::escapeHtml(showname))); - if(QString::compare(showname, ri.name)) { + //if(QString::compare(showname, ri.name)) { m_nameLabel->setToolTip(ri.name); - } + //} m_pluginLabel->setText(tr("File")); m_pathLabel2->setText(m_pathLabel2->fontMetrics().elidedText(m_currentActionKey, Qt::ElideRight, m_pathLabel2->width())); @@ -284,9 +284,9 @@ QWidget *DirSearchPlugin::detailPage(const ResultInfo &ri) QFontMetrics fontMetrics = m_nameLabel->fontMetrics(); QString showname = fontMetrics.elidedText(ri.name, Qt::ElideRight, 215); //当字体长度超过215时显示为省略号 m_nameLabel->setText(QString("

%1

").arg(FileUtils::escapeHtml(showname))); - if(QString::compare(showname, ri.name)) { + //if(QString::compare(showname, ri.name)) { m_nameLabel->setToolTip(ri.name); - } + //} m_pluginLabel->setText(tr("directory")); m_pathLabel2->setText(m_pathLabel2->fontMetrics().elidedText(m_currentActionKey, Qt::ElideRight, m_pathLabel2->width())); @@ -465,9 +465,9 @@ QWidget *FileContengSearchPlugin::detailPage(const ResultInfo &ri) QFontMetrics fontMetrics = m_nameLabel->fontMetrics(); QString showname = fontMetrics.elidedText(ri.name, Qt::ElideRight, 215); //当字体长度超过215时显示为省略号 m_nameLabel->setText(QString("

%1

").arg(FileUtils::escapeHtml(showname))); - if(QString::compare(showname, ri.name)) { + //if(QString::compare(showname, ri.name)) { m_nameLabel->setToolTip(ri.name); - } + //} m_snippetLabel->setText(getHtmlText(wrapData(m_snippetLabel,ri.description.at(0).value), m_keyWord)); m_pathLabel2->setText(m_pathLabel2->fontMetrics().elidedText(m_currentActionKey, Qt::ElideRight, m_pathLabel2->width())); @@ -505,7 +505,6 @@ QString FileContengSearchPlugin::wrapData(QLabel *p_label, const QString &text) QFontMetrics fontMetrics = p_label->fontMetrics(); int textSize = fontMetrics.width(wrapText); - if(textSize > LABEL_MAX_WIDTH){ int lastIndex = 0; int count = 0;