make a snippet without cut cjk char

This commit is contained in:
baijunjie@kylinos.cn 2021-08-06 17:45:28 +08:00
parent 6b5a3fc18d
commit cc3cd423f9
6 changed files with 89 additions and 13 deletions

View File

@ -4,6 +4,7 @@
#define UKUI_SEARCH_PIPE_PATH (QDir::homePath()+"/.config/org.ukui/ukui-search/ukuisearch").toLocal8Bit().constData() #define UKUI_SEARCH_PIPE_PATH (QDir::homePath()+"/.config/org.ukui/ukui-search/ukuisearch").toLocal8Bit().constData()
#define FILE_SEARCH_VALUE "0" #define FILE_SEARCH_VALUE "0"
#define DIR_SEARCH_VALUE "1" #define DIR_SEARCH_VALUE "1"
#define LABEL_MAX_WIDTH 300
#define HOME_PATH QDir::homePath() #define HOME_PATH QDir::homePath()
static const QMap<QString, bool> targetFileTypeMap = { static const QMap<QString, bool> targetFileTypeMap = {

View File

@ -796,3 +796,41 @@ QString FileUtils::escapeHtml(const QString &str)
temp.replace(">", "&gt;"); temp.replace(">", "&gt;");
return temp; 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;
}

View File

@ -38,6 +38,7 @@
#include <QDesktopServices> #include <QDesktopServices>
#include <QApplication> #include <QApplication>
#include <QClipboard> #include <QClipboard>
#include <QFontMetrics>
#include <quazip/quazipfile.h> #include <quazip/quazipfile.h>
#include <stdio.h> #include <stdio.h>
@ -53,12 +54,11 @@
#include "libsearch_global.h" #include "libsearch_global.h"
#include "gobject-template.h" #include "gobject-template.h"
#include "common.h"
//#define INITIAL_STATE 0 //#define INITIAL_STATE 0
//#define CREATING_INDEX 1 //#define CREATING_INDEX 1
//#define FINISH_CREATING_INDEX 2 //#define FINISH_CREATING_INDEX 2
#define MAX_CONTENT_LENGTH 20480000 #define MAX_CONTENT_LENGTH 20480000
namespace Zeeker { namespace Zeeker {
class LIBSEARCH_EXPORT FileUtils { class LIBSEARCH_EXPORT FileUtils {
public: public:
@ -90,6 +90,7 @@ public:
static bool openFile(QString &path, bool openInDir = false); static bool openFile(QString &path, bool openInDir = false);
static bool copyPath(QString &path); static bool copyPath(QString &path);
static QString escapeHtml(const QString & str); 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 _max_index_count;
static size_t _current_index_count; //this one has been Abandoned,do not use it. static size_t _current_index_count; //this one has been Abandoned,do not use it.
static unsigned short _index_status; static unsigned short _index_status;

View File

@ -450,7 +450,7 @@ QWidget *FileContengSearchPlugin::detailPage(const ResultInfo &ri)
m_nameLabel->setToolTip(ri.name); 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->setText(m_pathLabel2->fontMetrics().elidedText(m_currentActionKey, Qt::ElideRight, m_pathLabel2->width()));
m_pathLabel2->setToolTip(m_currentActionKey); m_pathLabel2->setToolTip(m_currentActionKey);
m_timeLabel2->setText(ri.description.at(2).value); m_timeLabel2->setText(ri.description.at(2).value);
@ -480,6 +480,40 @@ QString FileContengSearchPlugin::getHtmlText(const QString &text, const QString
return htmlString; 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() void FileContengSearchPlugin::initDetailPage()
{ {
m_detailPage = new QWidget(); m_detailPage = new QWidget();
@ -509,7 +543,7 @@ void FileContengSearchPlugin::initDetailPage()
m_line_1->setStyleSheet("QFrame{background: rgba(0,0,0,0.2);}"); m_line_1->setStyleSheet("QFrame{background: rgba(0,0,0,0.2);}");
m_snippetLabel = new QLabel(m_detailPage); m_snippetLabel = new QLabel(m_detailPage);
m_snippetLabel->setWordWrap(true); // m_snippetLabel->setWordWrap(true);
m_snippetLabel->setContentsMargins(8, 0, 8, 0); m_snippetLabel->setContentsMargins(8, 0, 8, 0);
m_pathFrame = new QFrame(m_detailPage); m_pathFrame = new QFrame(m_detailPage);

View File

@ -143,6 +143,7 @@ public:
// QWidget *previewPage(QString key, int type, QWidget *parent = nullptr); // QWidget *previewPage(QString key, int type, QWidget *parent = nullptr);
QWidget *detailPage(const ResultInfo &ri); QWidget *detailPage(const ResultInfo &ri);
QString getHtmlText(const QString & text, const QString & keyword); QString getHtmlText(const QString & text, const QString & keyword);
QString wrapData(QLabel *p_label, const QString &text);
private: private:
void initDetailPage(); void initDetailPage();
QWidget *m_detailPage = nullptr; QWidget *m_detailPage = nullptr;

View File

@ -364,14 +364,15 @@ int FileContentSearch::getResult(Xapian::MSet &result, std::string &keyWord) {
// } // }
//fix me: make a snippet without cut cjk char. //fix me: make a snippet without cut cjk char.
auto pos = term.positionlist_begin(); auto pos = term.positionlist_begin();
QString snippet; 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); // if(data.length() - *pos < 120) {
snippet = QString::fromStdString(s); // std::string s = data.substr((data.length() < 120) ? 0 : (data.length() - 120), 120);
} else { // snippet = QString::fromStdString(s);
std::string s = data.substr(*pos, 120); // } else {
snippet = QString::fromStdString(s); // std::string s = data.substr(*pos, 120);
} // snippet = QString::fromStdString(s);
// }
ri.description.prepend(SearchPluginIface::DescriptionInfo{"",snippet}); ri.description.prepend(SearchPluginIface::DescriptionInfo{"",snippet});
QString().swap(snippet); QString().swap(snippet);