From 825062aa75b44724bfa7cc8e78fe5b7bd237fa65 Mon Sep 17 00:00:00 2001 From: zhangjiaping Date: Fri, 29 Jan 2021 10:33:33 +0800 Subject: [PATCH] fix(frontend): Escape rice text & recalculate position for mainwindow. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Description: 转义富文本并重新计算主界面位置 Log: 转义富文本并重新计算主界面位置 Bug: - --- src/control/highlight-item-delegate.cpp | 19 +++++++-- src/control/highlight-item-delegate.h | 1 + src/control/search-detail-view.cpp | 17 +++++++- src/control/search-detail-view.h | 1 + src/input-box.cpp | 2 +- src/mainwindow.cpp | 56 +++++++++---------------- 6 files changed, 53 insertions(+), 43 deletions(-) diff --git a/src/control/highlight-item-delegate.cpp b/src/control/highlight-item-delegate.cpp index c2b679b..c8a00f3 100644 --- a/src/control/highlight-item-delegate.cpp +++ b/src/control/highlight-item-delegate.cpp @@ -69,7 +69,7 @@ QString HighlightItemDelegate::getHtmlText(QPainter *painter, const QStyleOption if ((indexColString.toUpper()).contains((m_regFindKeyWords.toUpper()))) { indexFindLeft = indexColString.toUpper().indexOf(m_regFindKeyWords.toUpper()); //得到查找字体在当前整个Item字体中的位置 // paintKeywordHighlight(painter, itemOption, indexColString, indexFindLeft, m_regFindKeyWords.length()); - htmlString = indexColString.left(indexFindLeft) + "" + indexColString.mid(indexFindLeft, m_regFindKeyWords.length())+ "" + indexColString.right(indexColString.length() - indexFindLeft - m_regFindKeyWords.length()); + htmlString = escapeHtml(indexColString.left(indexFindLeft)) + "" + escapeHtml(indexColString.mid(indexFindLeft, m_regFindKeyWords.length())) + "" + escapeHtml(indexColString.right(indexColString.length() - indexFindLeft - m_regFindKeyWords.length())); } else { bool boldOpenned = false; for (int i = 0; i < indexColString.length(); i++) { @@ -79,13 +79,13 @@ QString HighlightItemDelegate::getHtmlText(QPainter *painter, const QStyleOption boldOpenned = true; htmlString.append(QString("")); } - htmlString.append(QString(indexColString.at(i))); + htmlString.append(escapeHtml(QString(indexColString.at(i)))); } else { if (boldOpenned) { boldOpenned = false; htmlString.append(QString("")); } - htmlString.append(QString(indexColString.at(i))); + htmlString.append(escapeHtml(QString(indexColString.at(i)))); } } @@ -94,6 +94,19 @@ QString HighlightItemDelegate::getHtmlText(QPainter *painter, const QStyleOption return htmlString; } +/** + * @brief HighlightItemDelegate::escapeHtml 将文件名原本带的尖括号转义,以防被识别为富文本 + * @param str + * @return + */ +QString HighlightItemDelegate::escapeHtml(const QString & str) const +{ + QString temp = str; + temp.replace("<", "<"); + temp.replace(">", ">"); + return temp; +} + /** * @brief HighlightItemDelegate::paintKeywordHighlight drawitemtext方法绘制文字,印加粗字体与原字体不等宽导致像素不对齐,视觉效果较差,已暂时摒弃,保留以做底色高亮等备用 * @param painter diff --git a/src/control/highlight-item-delegate.h b/src/control/highlight-item-delegate.h index 5a23371..c4ea5f6 100644 --- a/src/control/highlight-item-delegate.h +++ b/src/control/highlight-item-delegate.h @@ -13,6 +13,7 @@ private: QString m_regFindKeyWords = 0; void paint(QPainter *,const QStyleOptionViewItem &, const QModelIndex &) const override; QString getHtmlText(QPainter *, const QStyleOptionViewItem &, const QModelIndex &) const; + QString escapeHtml(const QString&) const; void paintKeywordHighlight(QPainter *, const QStyleOptionViewItem &, const QString &, const int &, const int &) const; }; diff --git a/src/control/search-detail-view.cpp b/src/control/search-detail-view.cpp index ec1ead6..dabeeae 100644 --- a/src/control/search-detail-view.cpp +++ b/src/control/search-detail-view.cpp @@ -97,19 +97,32 @@ QString SearchDetailView::getHtmlText(const QString & text, const QString & keyw boldOpenned = true; htmlString.append(QString("")); } - htmlString.append(QString(text.at(i))); + htmlString.append(escapeHtml(QString(text.at(i)))); } else { if (boldOpenned) { boldOpenned = false; htmlString.append(QString("")); } - htmlString.append(QString(text.at(i))); + htmlString.append(escapeHtml(QString(text.at(i)))); } } htmlString.replace("\n", "
");//替换换行符 return htmlString; } +/** + * @brief SearchDetailView::escapeHtml 将搜索结果内容中的标签括号转义,来防止文件内容中的标记语言被识别为富文本 + * @param str 需要转义的字段 + * @return + */ +QString SearchDetailView::escapeHtml(const QString & str) +{ + QString temp = str; + temp.replace("<", "<"); + temp.replace(">", ">"); + return temp; +} + /** * @brief SearchDetailView::setupWidget 构建右侧搜索结果详情区域 * @param type 搜索类型 diff --git a/src/control/search-detail-view.h b/src/control/search-detail-view.h index 7f45119..f5cfaf8 100644 --- a/src/control/search-detail-view.h +++ b/src/control/search-detail-view.h @@ -30,6 +30,7 @@ private: bool openPathAction(const QString&); bool copyPathAction(const QString&); QString getHtmlText(const QString&, const QString&); + QString escapeHtml(const QString&); bool writeConfigFile(const QString&); bool m_isEmpty = true; int m_type = 0; diff --git a/src/input-box.cpp b/src/input-box.cpp index 9c232b1..8f4b02f 100644 --- a/src/input-box.cpp +++ b/src/input-box.cpp @@ -178,7 +178,7 @@ SearchLineEdit::SearchLineEdit() { this->setFocusPolicy(Qt::ClickFocus); this->installEventFilter(this); - this->setContextMenuPolicy(Qt::NoContextMenu); +// this->setContextMenuPolicy(Qt::NoContextMenu); this->setMaxLength(100); diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 3618513..f48ce57 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -20,7 +20,6 @@ #include #include #include -#include #include #include #include @@ -29,6 +28,7 @@ #include #include #include "kwindowsystem.h" +#include "qt-single-application.h" //#include "inotify-manager.h" #include "settings-widget.h" @@ -328,43 +328,25 @@ void MainWindow::moveToPanel() int height = QDBusReply(interface.call("GetPanelPosition", "height")); int d = 2; //窗口边沿到任务栏距离 - if (screenGeometry.width() == availableGeometry.width() && screenGeometry.height() == availableGeometry.height()) { - if (position == 0) { - //任务栏在下侧 - this->move(availableGeometry.x() + availableGeometry.width() - this->width(), screenMainRect.y() + availableGeometry.height() - this->height() - height - d); - } else if(position == 1) { - //任务栏在上侧 - this->move(availableGeometry.x() + availableGeometry.width() - this->width(), screenMainRect.y() + screenGeometry.height() - availableGeometry.height() + height + d); - } else if (position == 2) { - //任务栏在左侧 - if (screenGeometry.x() == 0) {//主屏在左侧 - this->move(height + d, screenMainRect.y() + screenMainRect.height() - this->height()); - } else {//主屏在右侧 - this->move(screenMainRect.x() + height + d, screenMainRect.y() + screenMainRect.height() - this->height()); - } - } else if (position == 3) { - //任务栏在右侧 - if (screenGeometry.x() == 0) {//主屏在左侧 - this->move(screenMainRect.width() - this->width() - height - d, screenMainRect.y() + screenMainRect.height() - this->height()); - } else {//主屏在右侧 - this->move(screenMainRect.x() + screenMainRect.width() - this->width() - height - d, screenMainRect.y() + screenMainRect.height() - this->height()); - } + if (position == 0) { + //任务栏在下侧 + this->move(availableGeometry.x() + availableGeometry.width() - this->width(), screenMainRect.y() + availableGeometry.height() - this->height() - height - d); + } else if(position == 1) { + //任务栏在上侧 + this->move(availableGeometry.x() + availableGeometry.width() - this->width(), screenMainRect.y() + screenGeometry.height() - availableGeometry.height() + height + d); + } else if (position == 2) { + //任务栏在左侧 + if (screenGeometry.x() == 0) {//主屏在左侧 + this->move(height + d, screenMainRect.y() + screenMainRect.height() - this->height()); + } else {//主屏在右侧 + this->move(screenMainRect.x() + height + d, screenMainRect.y() + screenMainRect.height() - this->height()); } - } else if(screenGeometry.width() == availableGeometry.width() ) { - if (m_sys_tray_icon->geometry().y() > availableGeometry.height()/2) { - //任务栏在下侧 - this->move(availableGeometry.x() + availableGeometry.width() - this->width(), screenMainRect.y() + availableGeometry.height() - this->height() - d); - } else { - //任务栏在上侧 - this->move(availableGeometry.x() + availableGeometry.width() - this->width(), screenMainRect.y() + screenGeometry.height() - availableGeometry.height() + d); - } - } else if (screenGeometry.height() == availableGeometry.height()) { - if (m_sys_tray_icon->geometry().x() > availableGeometry.width()/2) { - //任务栏在右侧 - this->move(availableGeometry.x() + availableGeometry.width() - this->width() - d, screenMainRect.y() + screenGeometry.height() - this->height()); - } else { - //任务栏在左侧 - this->move(screenGeometry.width() - availableGeometry.width() + d, screenMainRect.y() + screenGeometry.height() - this->height()); + } else if (position == 3) { + //任务栏在右侧 + if (screenGeometry.x() == 0) {//主屏在左侧 + this->move(screenMainRect.width() - this->width() - height - d, screenMainRect.y() + screenMainRect.height() - this->height()); + } else {//主屏在右侧 + this->move(screenMainRect.x() + screenMainRect.width() - this->width() - height - d, screenMainRect.y() + screenMainRect.height() - this->height()); } } }