From 23ef4b3d50ac714f1b57ea7aec1e3f30eab7e295 Mon Sep 17 00:00:00 2001
From: keke <243768648@qq.com>
Date: Fri, 7 Oct 2022 23:14:30 +0800
Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=87=E6=9C=AC=E5=86=85=E5=AE=B9?=
=?UTF-8?q?=E6=9F=A5=E6=89=BE=E4=B8=8B=E4=B8=80=E9=A1=B9=E5=8A=9F=E8=83=BD?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
resources/icons.qrc | 2 +
resources/icons/chevron-down_48px.svg | 1 +
resources/icons/chevron-up_48px.svg | 1 +
src/appmanagerwidget.cpp | 93 ++++++++++++++++++++++++++-
src/appmanagerwidget.h | 5 ++
5 files changed, 99 insertions(+), 3 deletions(-)
create mode 100644 resources/icons/chevron-down_48px.svg
create mode 100644 resources/icons/chevron-up_48px.svg
diff --git a/resources/icons.qrc b/resources/icons.qrc
index 9665e3a..d41ed2c 100644
--- a/resources/icons.qrc
+++ b/resources/icons.qrc
@@ -5,5 +5,7 @@
icons/ccc-app-manager_56px.svg
icons/app-manager_bak_64px.svg
icons/rotate_48px.svg
+ icons/chevron-down_48px.svg
+ icons/chevron-up_48px.svg
diff --git a/resources/icons/chevron-down_48px.svg b/resources/icons/chevron-down_48px.svg
new file mode 100644
index 0000000..fa3c294
--- /dev/null
+++ b/resources/icons/chevron-down_48px.svg
@@ -0,0 +1 @@
+
\ No newline at end of file
diff --git a/resources/icons/chevron-up_48px.svg b/resources/icons/chevron-up_48px.svg
new file mode 100644
index 0000000..8f222fb
--- /dev/null
+++ b/resources/icons/chevron-up_48px.svg
@@ -0,0 +1 @@
+
\ No newline at end of file
diff --git a/src/appmanagerwidget.cpp b/src/appmanagerwidget.cpp
index 54eb94a..142d054 100644
--- a/src/appmanagerwidget.cpp
+++ b/src/appmanagerwidget.cpp
@@ -33,6 +33,11 @@ Q_DECLARE_METATYPE(QMargins)
const QMargins ListViewItemMargin(5, 3, 5, 3);
const QVariant ListViewItemMarginVar = QVariant::fromValue(ListViewItemMargin);
+// 高亮文字背景颜色
+const QColor HighlightTextBgColor(255, 255, 0, 190);
+// 当前定位到的高亮文字背景颜色
+const QColor LocatedHighlightTextBgColor(0, 0, 255, 120);
+
AppManagerWidget::AppManagerWidget(AppManagerModel *model, QWidget *parent)
: QWidget(parent)
, m_model(model)
@@ -238,11 +243,21 @@ AppManagerWidget::AppManagerWidget(AppManagerModel *model, QWidget *parent)
m_findLineEdit = new QLineEdit(this);
m_findLineEdit->setPlaceholderText("");
findContentFrameLayout->addWidget(m_findLineEdit);
+ // 查找下一个按钮
+ DIconButton *findNextContentBtn = new DIconButton(this);
+ findNextContentBtn->setToolTip("查找下一个");
+ findNextContentBtn->setIcon(QIcon::fromTheme("chevron-down"));
+ findNextContentBtn->setFixedSize(30, 30);
+ findNextContentBtn->setIconSize(QSize(30, 30));
+ findNextContentBtn->setEnabledCircle(true);
+ findContentFrameLayout->addWidget(findNextContentBtn);
// 取消搜索按钮
- DFloatingButton *cancelSearchBtn = new DFloatingButton(this);
+ DIconButton *cancelSearchBtn = new DIconButton(this);
+ cancelSearchBtn->setToolTip("取消查找");
cancelSearchBtn->setIcon(DStyle::StandardPixmap::SP_CloseButton);
cancelSearchBtn->setFixedSize(30, 30);
cancelSearchBtn->setIconSize(QSize(30, 30));
+ cancelSearchBtn->setEnabledCircle(true);
findContentFrameLayout->addWidget(cancelSearchBtn);
m_appInfoTextEdit = new QTextEdit(this);
@@ -371,7 +386,8 @@ AppManagerWidget::AppManagerWidget(AppManagerModel *model, QWidget *parent)
});
connect(m_findLineEdit, &QLineEdit::editingFinished, this, &AppManagerWidget::updateHighlightText);
- connect(cancelSearchBtn, &DFloatingButton::clicked, this, [findContentFrame, openFindToolBtn, this] {
+ connect(findNextContentBtn, &DIconButton::clicked, this, &AppManagerWidget::moveToNextHighlightText);
+ connect(cancelSearchBtn, &DIconButton::clicked, this, [findContentFrame, openFindToolBtn, this] {
findContentFrame->setVisible(false);
openFindToolBtn->setDown(false);
// 清空文本搜索内容
@@ -1009,6 +1025,10 @@ void AppManagerWidget::updateAppCountLabel()
void AppManagerWidget::updateHighlightText()
{
+ // 清空上次高亮指针列表
+ m_highlightCursorList.clear();
+ m_currentMovedCursor.clearSelection();
+ // 判断待搜索的文档
QTextDocument *doc;
if (m_infoBtn->isChecked()) {
doc = m_appInfoTextEdit->document();
@@ -1033,14 +1053,81 @@ void AppManagerWidget::updateHighlightText()
// 开始查找
cursor.beginEditBlock();
QTextCharFormat colorFormat(highlightCursor.charFormat());
- colorFormat.setBackground(Qt::GlobalColor::red);
+ colorFormat.setBackground(HighlightTextBgColor);
while (!highlightCursor.isNull() && !highlightCursor.atEnd()) {
highlightCursor = doc->find(findtext, highlightCursor);
if (!highlightCursor.isNull()) {
highlightCursor.mergeCharFormat(colorFormat);
+ m_highlightCursorList.append(highlightCursor);
}
qApp->processEvents();
}
cursor.endEditBlock();
}
+
+void AppManagerWidget::moveToNextHighlightText()
+{
+ if (m_highlightCursorList.isEmpty()) {
+ qInfo() << Q_FUNC_INFO << "highlight text is empty";
+ return;
+ }
+
+ // 判断待搜索的文档编辑器
+ QTextEdit *edit;
+ if (m_infoBtn->isChecked()) {
+ edit = m_appInfoTextEdit;
+ } else if (m_filesBtn->isChecked()) {
+ edit = m_appFileListTextEdit;
+ } else {
+ qWarning() << Q_FUNC_INFO << "no info content need find";
+ return;
+ }
+
+ QTextCursor nextHighlightCursor;
+ int currentCursorAnchor = edit->textCursor().anchor();
+// qInfo() << Q_FUNC_INFO << "currentCursorAnchor" << currentCursorAnchor;
+ for (QList::const_iterator cIter = m_highlightCursorList.cbegin();
+ cIter != m_highlightCursorList.cend(); ++cIter) {
+ if (currentCursorAnchor < cIter->anchor()) {
+ nextHighlightCursor = *cIter;
+ break;
+ }
+ }
+ if (nextHighlightCursor.isNull()) {
+ nextHighlightCursor = m_highlightCursorList.first();
+ }
+ int nextHighlightCursorAnchor = nextHighlightCursor.anchor();
+// qInfo() << Q_FUNC_INFO << "nextHighlightCursorAnchor" << nextHighlightCursorAnchor;
+
+ bool finded = true; // 是否找到
+ bool haveLoopFinded = false; // 只循环查找一次
+ edit->moveCursor(QTextCursor::MoveOperation::PreviousCharacter, QTextCursor::MoveMode::MoveAnchor);
+ while (edit->textCursor().anchor() != nextHighlightCursorAnchor) {
+ edit->moveCursor(QTextCursor::MoveOperation::NextCharacter, QTextCursor::MoveMode::MoveAnchor);
+ // 当移到文档末尾时还没找到高亮文字指针
+ if (edit->textCursor().atEnd()) {
+ if (!haveLoopFinded) {
+ QTextCursor currentCursor = edit->textCursor();
+ currentCursor.movePosition(QTextCursor::MoveOperation::Start, QTextCursor::MoveMode::MoveAnchor);
+ edit->setTextCursor(currentCursor);
+ haveLoopFinded = true;
+ qInfo() << Q_FUNC_INFO << "move to start" << edit->textCursor().anchor();
+ } else {
+ finded = false;
+ break;
+ }
+ }
+ }
+
+ // 还原上次移到到的高亮文字背景颜色
+ QTextCharFormat colorFormat(m_currentMovedCursor.charFormat());
+ colorFormat.setBackground(HighlightTextBgColor);
+ m_currentMovedCursor.setCharFormat(colorFormat);
+
+ // 设置当前移到到的高亮文字背景颜色
+ m_currentMovedCursor = nextHighlightCursor;
+ colorFormat = m_currentMovedCursor.charFormat();
+ colorFormat.setBackground(LocatedHighlightTextBgColor);
+ m_currentMovedCursor.mergeCharFormat(colorFormat);
+}
diff --git a/src/appmanagerwidget.h b/src/appmanagerwidget.h
index 0b4ab1f..ef0f9d3 100644
--- a/src/appmanagerwidget.h
+++ b/src/appmanagerwidget.h
@@ -7,6 +7,7 @@
#include
#include
+#include
class AppManagerModel;
@@ -70,6 +71,8 @@ private:
void updateAppCountLabel();
// 更新高亮显示文字
void updateHighlightText();
+ // 移动到下一个高亮显示文字
+ void moveToNextHighlightText();
private:
QList m_appInfoList;
@@ -101,6 +104,8 @@ private:
DButtonBoxButton *m_filesBtn;
DButtonBox *m_infoSwitchBtn;
QLineEdit *m_findLineEdit;
+ QList m_highlightCursorList;
+ QTextCursor m_currentMovedCursor;
QTextEdit *m_appInfoTextEdit;
QTextEdit *m_appFileListTextEdit;
};
--
Gitee