diff --git a/libsearch/common.h b/libsearch/common.h index f3e2c41..4b2296e 100644 --- a/libsearch/common.h +++ b/libsearch/common.h @@ -4,6 +4,7 @@ #define UKUI_SEARCH_PIPE_PATH (QDir::homePath()+"/.config/org.ukui/ukui-search/ukuisearch").toLocal8Bit().constData() #define FILE_SEARCH_VALUE "0" #define DIR_SEARCH_VALUE "1" +#define LABEL_MAX_WIDTH 300 #define HOME_PATH QDir::homePath() static const QMap targetFileTypeMap = { diff --git a/libsearch/file-utils.cpp b/libsearch/file-utils.cpp index 9159a08..2913354 100644 --- a/libsearch/file-utils.cpp +++ b/libsearch/file-utils.cpp @@ -796,3 +796,41 @@ QString FileUtils::escapeHtml(const QString &str) temp.replace(">", ">"); return temp; } + +QString FileUtils::chineseSubString(const std::string &myStr, int start, int length) +{ + std::string afterSub = ""; + //越界保护 + if(start < 0 || length < 0){ + return " "; + } + if (length >= myStr.length()) { + return QString::fromStdString(myStr); + } + + QString sub = ""; + QFont ft(QApplication::font().family(),QApplication::font().pointSize()); + QFontMetrics fm (ft); + + 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 = fm.elidedText(sub, Qt::ElideRight, 2*LABEL_MAX_WIDTH); //超过两行则省略 + } + 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); + } + return sub; +} diff --git a/libsearch/file-utils.h b/libsearch/file-utils.h index e3319eb..856acd2 100644 --- a/libsearch/file-utils.h +++ b/libsearch/file-utils.h @@ -38,6 +38,7 @@ #include #include #include +#include #include #include @@ -53,12 +54,11 @@ #include "libsearch_global.h" #include "gobject-template.h" - +#include "common.h" //#define INITIAL_STATE 0 //#define CREATING_INDEX 1 //#define FINISH_CREATING_INDEX 2 #define MAX_CONTENT_LENGTH 20480000 - namespace Zeeker { class LIBSEARCH_EXPORT FileUtils { public: @@ -90,6 +90,7 @@ public: static bool openFile(QString &path, bool openInDir = false); static bool copyPath(QString &path); static QString escapeHtml(const QString & str); + static QString chineseSubString(const std::string &myStr,int start,int length); 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/libsearch/index/file-search-plugin.cpp b/libsearch/index/file-search-plugin.cpp index 63d7c59..5b25f28 100644 --- a/libsearch/index/file-search-plugin.cpp +++ b/libsearch/index/file-search-plugin.cpp @@ -450,7 +450,7 @@ QWidget *FileContengSearchPlugin::detailPage(const ResultInfo &ri) m_nameLabel->setToolTip(ri.name); } - m_snippetLabel->setText(getHtmlText(ri.description.at(0).value, m_keyWord)); + 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())); m_pathLabel2->setToolTip(m_currentActionKey); m_timeLabel2->setText(ri.description.at(2).value); @@ -480,6 +480,40 @@ QString FileContengSearchPlugin::getHtmlText(const QString &text, const QString return htmlString; } +QString FileContengSearchPlugin::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; + } + + if(count == 2){ + break; + } + } + } + p_label->setText(wrapText); + return wrapText; +} + void FileContengSearchPlugin::initDetailPage() { m_detailPage = new QWidget(); @@ -509,7 +543,7 @@ void FileContengSearchPlugin::initDetailPage() m_line_1->setStyleSheet("QFrame{background: rgba(0,0,0,0.2);}"); m_snippetLabel = new QLabel(m_detailPage); - m_snippetLabel->setWordWrap(true); +// m_snippetLabel->setWordWrap(true); m_snippetLabel->setContentsMargins(8, 0, 8, 0); m_pathFrame = new QFrame(m_detailPage); diff --git a/libsearch/index/file-search-plugin.h b/libsearch/index/file-search-plugin.h index 82a7bc4..229d13b 100644 --- a/libsearch/index/file-search-plugin.h +++ b/libsearch/index/file-search-plugin.h @@ -143,6 +143,7 @@ public: // QWidget *previewPage(QString key, int type, QWidget *parent = nullptr); QWidget *detailPage(const ResultInfo &ri); QString getHtmlText(const QString & text, const QString & keyword); + QString wrapData(QLabel *p_label, const QString &text); private: void initDetailPage(); QWidget *m_detailPage = nullptr; diff --git a/libsearch/index/search-manager.cpp b/libsearch/index/search-manager.cpp index f59a600..052e877 100644 --- a/libsearch/index/search-manager.cpp +++ b/libsearch/index/search-manager.cpp @@ -357,21 +357,22 @@ int FileContentSearch::getResult(Xapian::MSet &result, std::string &keyWord) { // snippet.append("...").prepend("..."); // } // ri.description.prepend(SearchPluginIface::DescriptionInfo{"",snippet}); -//// snippets.append(snippet); +//// snippets.append(snippet); // QString().swap(snippet); // std::string().swap(s); // ++count; // } //fix me: make a snippet without cut cjk char. auto pos = term.positionlist_begin(); - QString snippet; - if(data.length() - *pos < 120) { - std::string s = data.substr((data.length() < 120) ? 0 : (data.length() - 120), 120); - snippet = QString::fromStdString(s); - } else { - std::string s = data.substr(*pos, 120); - snippet = QString::fromStdString(s); - } + QString snippet = FileUtils::chineseSubString(data,*pos,120); + +// if(data.length() - *pos < 120) { +// std::string s = data.substr((data.length() < 120) ? 0 : (data.length() - 120), 120); +// snippet = QString::fromStdString(s); +// } else { +// std::string s = data.substr(*pos, 120); +// snippet = QString::fromStdString(s); +// } ri.description.prepend(SearchPluginIface::DescriptionInfo{"",snippet}); QString().swap(snippet);