Merge branch '0802sub' into 'new-frontend'

0802sub

See merge request kylin-desktop/ukui-search!80
This commit is contained in:
PengfeiZhang 2021-08-09 01:38:56 +00:00
commit 5f33686f1a
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 FILE_SEARCH_VALUE "0"
#define DIR_SEARCH_VALUE "1"
#define LABEL_MAX_WIDTH 300
#define HOME_PATH QDir::homePath()
static const QMap<QString, bool> targetFileTypeMap = {

View File

@ -796,3 +796,41 @@ QString FileUtils::escapeHtml(const QString &str)
temp.replace(">", "&gt;");
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 <QApplication>
#include <QClipboard>
#include <QFontMetrics>
#include <quazip/quazipfile.h>
#include <stdio.h>
@ -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;

View File

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

View File

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

View File

@ -364,14 +364,15 @@ int FileContentSearch::getResult(Xapian::MSet &result, std::string &keyWord) {
// }
//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);