From 70d7135e54719853a82f228ac8883ade592042ce Mon Sep 17 00:00:00 2001 From: baijunjie Date: Mon, 13 Sep 2021 20:43:30 +0800 Subject: [PATCH] Merge branch '0908dev' into 'dev-unity' Fix: incomplete file name display;the contents of some files are not fully displayed. --- libsearch/common.h | 1 + libsearch/file-utils.cpp | 29 +++++++++++++++++++++++++++++ libsearch/file-utils.h | 3 +++ src/control/search-detail-view.cpp | 13 +++++++------ 4 files changed, 40 insertions(+), 6 deletions(-) diff --git a/libsearch/common.h b/libsearch/common.h index 59f0239..381657f 100644 --- a/libsearch/common.h +++ b/libsearch/common.h @@ -2,6 +2,7 @@ #define COMMON_H #include #define UKUI_SEARCH_PIPE_PATH (QDir::homePath()+"/.config/org.ukui/ukui-search/ukuisearch").toLocal8Bit().constData() +#define LABEL_MAX_WIDTH 320 #define HOME_PATH QDir::homePath() static const QMap targetFileTypeMap = { std::map::value_type("doc", true), diff --git a/libsearch/file-utils.cpp b/libsearch/file-utils.cpp index 98789a0..79905f0 100644 --- a/libsearch/file-utils.cpp +++ b/libsearch/file-utils.cpp @@ -839,3 +839,32 @@ QString FileUtils::chineseSubString(const std::string &data, int start, int leng } return snippet; } + +QString FileUtils::wrapData(QLabel *p_label, const QString &text) +{ + QString wrapText = text; + + QFontMetrics fontMetrics = p_label->fontMetrics(); + int textSize = fontMetrics.width(wrapText); + + if(textSize > LABEL_MAX_WIDTH){ + int lastIndex = 0; + int count = 0; + + for(int i = lastIndex; i < wrapText.length(); i++) { + + if(fontMetrics.width(wrapText.mid(lastIndex, i - lastIndex)) == LABEL_MAX_WIDTH) { + lastIndex = i; + wrapText.insert(i, '\n'); + count++; + } else if(fontMetrics.width(wrapText.mid(lastIndex, i - lastIndex)) > LABEL_MAX_WIDTH) { + lastIndex = i; + wrapText.insert(i - 1, '\n'); + count++; + } else { + continue; + } + } + } + return wrapText; +} diff --git a/libsearch/file-utils.h b/libsearch/file-utils.h index b3ef3ae..6e8a617 100644 --- a/libsearch/file-utils.h +++ b/libsearch/file-utils.h @@ -37,6 +37,7 @@ #include #include #include +#include #include #include @@ -49,6 +50,7 @@ #include //#include #include +#include #include "libsearch_global.h" #include "gobject-template.h" @@ -86,6 +88,7 @@ public: static void getPdfTextContent(QString &path, QString &textcontent); static void getTxtContent(QString &path, QString &textcontent); static QString chineseSubString(const std::string &data, int start, int length); + static QString wrapData(QLabel *p_label, const QString &text); static size_t _max_index_count; static size_t _current_index_count; //this one has been Abandoned,do not use it. static unsigned short _index_status; diff --git a/src/control/search-detail-view.cpp b/src/control/search-detail-view.cpp index 8865e81..386bc5a 100644 --- a/src/control/search-detail-view.cpp +++ b/src/control/search-detail-view.cpp @@ -206,6 +206,7 @@ void SearchDetailView::setAppWidget(const QString &appname, const QString &path, if(description != "" && !description.isEmpty()) { m_detailFrame->show(); m_contentLabel->show(); + m_contentLabel->setWordWrap(true); m_contentLabel->setText(QString(tr("Introduction: %1")).arg(description)); } setIcon(iconpath, false); @@ -216,7 +217,7 @@ void SearchDetailView::setAppWidget(const QString &appname, const QString &path, m_optionView->show(); QFontMetrics fontMetrics = m_nameLabel->fontMetrics(); - QString showname = fontMetrics.elidedText(m_name, Qt::ElideRight, 274); //当字体长度超过215时显示为省略号 + QString showname = fontMetrics.elidedText(m_name, Qt::ElideRight, 215); //当字体长度超过215时显示为省略号 // m_nameLabel->setText(showname); m_nameLabel->setText(QString("

%1

").arg(escapeHtml(showname))); if(QString::compare(showname, m_name)) { @@ -257,7 +258,7 @@ QString SearchDetailView::getHtmlText(const QString & text, const QString & keyw if((keyword.toUpper()).contains(QString(text.at(i)).toUpper())) { if(! boldOpenned) { boldOpenned = true; - htmlString.append(QString("")); + htmlString.append(QString("")); } htmlString.append(escapeHtml(QString(text.at(i)))); } else { @@ -306,7 +307,8 @@ void SearchDetailView::setupWidget(const int& type, const QString& path) { m_detailFrame->show(); if(isContent) { //文件内容区域 m_contentLabel->show(); - m_contentLabel->setText(QApplication::translate("", getHtmlText(m_contentText, m_keyword).toLocal8Bit(), nullptr)); + m_contentLabel->setWordWrap(false); + m_contentLabel->setText(QApplication::translate("", getHtmlText(FileUtils::wrapData(m_contentLabel,m_contentText), m_keyword).toLocal8Bit(),nullptr)); } m_pathFrame->show(); m_timeFrame->show(); @@ -351,7 +353,7 @@ void SearchDetailView::setupWidget(const int& type, const QString& path) { setIcon(path); QFontMetrics fontMetrics = m_nameLabel->fontMetrics(); QString wholeName = FileUtils::getFileName(path); - QString name = fontMetrics.elidedText(wholeName, Qt::ElideRight, 274); + QString name = fontMetrics.elidedText(wholeName, Qt::ElideRight, 215); // m_nameLabel->setText(name); m_nameLabel->setText(QString("

%1

").arg(escapeHtml(name))); if(QString::compare(name, wholeName)) { @@ -514,8 +516,7 @@ void SearchDetailView::initUI() { //文件内容区域 m_contentLabel = new QLabel(m_detailFrame); - m_contentLabel->setWordWrap(true); - m_contentLabel->setContentsMargins(9, 0, 9, 0); + m_contentLabel->setContentsMargins(0, 0, 0, 0); m_detailLyt->addWidget(m_contentLabel); //路径与修改时间区域