Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/controls/findbar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ FindBar::FindBar(QWidget *parent)
m_layout->setAlignment(Qt::AlignVCenter);
m_findLabel = new QLabel(tr("Find"));
m_editLine = new LineBar();
m_statusLabel = new QLabel(tr(""));
m_findPrevButton = new QPushButton(tr("Previous"));
m_findNextButton = new QPushButton(tr("Next"));
m_closeButton = new DIconButton(DStyle::SP_CloseButton);
Expand All @@ -49,6 +50,7 @@ FindBar::FindBar(QWidget *parent)
lineBarLayout->addItem(new QSpacerItem(1, 1, QSizePolicy::Minimum, QSizePolicy::MinimumExpanding));
m_layout->addLayout(lineBarLayout);

m_layout->addWidget(m_statusLabel);
m_layout->addWidget(m_findPrevButton);
m_layout->addWidget(m_findNextButton);
m_layout->addWidget(m_closeButton);
Expand Down Expand Up @@ -216,3 +218,8 @@ void FindBar::findPreClicked()
emit findPrev(m_editLine->lineEdit()->text());
}
}

void FindBar::setStatusText(QString statusStr)
{
m_statusLabel->setText(statusStr);
}
2 changes: 2 additions & 0 deletions src/controls/findbar.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class FindBar : public DFloatingWidget
void receiveText(QString t);
void setSearched(bool _);
void findPreClicked();
void setStatusText(QString str);

Q_SIGNALS:
void pressEsc();
Expand Down Expand Up @@ -74,6 +75,7 @@ public Q_SLOTS:
LineBar *m_editLine;
QHBoxLayout *m_layout;
QLabel *m_findLabel;
QLabel *m_statusLabel;
QString m_findFile;
int m_findFileColumn;
int m_findFileRow;
Expand Down
8 changes: 8 additions & 0 deletions src/controls/replacebar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ ReplaceBar::ReplaceBar(QWidget *parent)
m_replaceLine = new LineBar();
m_withLabel = new QLabel(tr("Replace With"));
m_withLine = new LineBar();
m_statusLabel = new QLabel(tr(""));
m_replaceButton = new QPushButton(tr("Replace"));
m_replaceSkipButton = new QPushButton(tr("Skip"));
m_replaceRestButton = new QPushButton(tr("Replace Rest"));
Expand All @@ -46,6 +47,8 @@ ReplaceBar::ReplaceBar(QWidget *parent)
m_layout->addLayout(createVerticalLine(m_replaceLine));
m_layout->addWidget(m_withLabel);
m_layout->addLayout(createVerticalLine(m_withLine));

m_layout->addWidget(m_statusLabel);
m_layout->addWidget(m_replaceButton);
m_layout->addWidget(m_replaceSkipButton);
m_layout->addWidget(m_replaceRestButton);
Expand Down Expand Up @@ -260,3 +263,8 @@ void ReplaceBar::change()
{
searched = false;
}

void ReplaceBar::setStatusText(QString str)
{
m_statusLabel->setText(str);
}
2 changes: 2 additions & 0 deletions src/controls/replacebar.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class ReplaceBar : public DFloatingWidget
void activeInput(QString text, QString file, int row, int column, int scrollOffset);
void setMismatchAlert(bool isAlert);
void setsearched(bool _);
void setStatusText(QString text);

Q_SIGNALS:
void pressEsc();
Expand Down Expand Up @@ -77,6 +78,7 @@ public Q_SLOTS:
QHBoxLayout *m_layout;
QLabel *m_replaceLabel;
QLabel *m_withLabel;
QLabel *m_statusLabel;
QString m_replaceFile;
int m_replaceFileColumn;
int m_replaceFileRow;
Expand Down
145 changes: 91 additions & 54 deletions src/editor/dtextedit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2014,72 +2014,76 @@ bool TextEdit::updateKeywordSelectionsInView(QString keyword, QTextCharFormat ch
// Clear keyword selections first.
listSelection->clear();

if (keyword.isEmpty()) {
return false;
}

this->updateSearchStatus(keyword);

// Update selections with keyword.
if (!keyword.isEmpty()) {
QTextCursor cursor(document());
QTextEdit::ExtraSelection extra;
extra.format = charFormat;
QTextCursor cursor(document());
QTextEdit::ExtraSelection extra;
extra.format = charFormat;

QScrollBar *pScrollBar = verticalScrollBar();
QPoint startPoint = QPointF(0, 0).toPoint();
QTextBlock beginBlock = cursorForPosition(startPoint).block();
int beginPos = beginBlock.position();
QTextBlock endBlock;

if (pScrollBar->maximum() > 0) {
QPoint endPoint = QPointF(0, 1.5 * height()).toPoint();
endBlock = cursorForPosition(endPoint).block();
} else {
endBlock = document()->lastBlock();
}
int endPos = endBlock.position() + endBlock.length() - 1;

QScrollBar *pScrollBar = verticalScrollBar();
QPoint startPoint = QPointF(0, 0).toPoint();
QTextBlock beginBlock = cursorForPosition(startPoint).block();
int beginPos = beginBlock.position();
QTextBlock endBlock;
// 内部计算时,均视为 \n 结尾
QLatin1Char endLine('\n');
QString multiLineText;
QTextDocument::FindFlags flags;
flags |= QTextDocument::FindCaseSensitively;
if (keyword.contains(endLine)) {
auto temp = this->textCursor();
temp.setPosition(beginPos);
while (temp.position() < endPos) {
temp.movePosition(QTextCursor::EndOfLine, QTextCursor::KeepAnchor);
multiLineText += temp.selectedText();
multiLineText += endLine;
temp.setPosition(temp.position() + 1);
}
cursor = findCursor(keyword, multiLineText, 0, false, beginPos);
} else {
cursor = document()->find(keyword, beginPos, flags);
}

if (pScrollBar->maximum() > 0) {
QPoint endPoint = QPointF(0, 1.5 * height()).toPoint();
endBlock = cursorForPosition(endPoint).block();
} else {
endBlock = document()->lastBlock();
if (cursor.isNull()) {
return false;
}



while (!cursor.isNull()) {
extra.cursor = cursor;
/* 查找字符时,查找到完全相等的时候才高亮,如查找小写f时,大写的F不高亮 */
if (!extra.cursor.selectedText().compare(keyword) || keyword.contains(endLine)) {
listSelection->append(extra);
}
int endPos = endBlock.position() + endBlock.length() - 1;

// 内部计算时,均视为 \n 结尾
QLatin1Char endLine('\n');
QString multiLineText;
QTextDocument::FindFlags flags;
flags |= QTextDocument::FindCaseSensitively;
if (keyword.contains(endLine)) {
auto temp = this->textCursor();
temp.setPosition(beginPos);
while (temp.position() < endPos) {
temp.movePosition(QTextCursor::EndOfLine, QTextCursor::KeepAnchor);
multiLineText += temp.selectedText();
multiLineText += endLine;
temp.setPosition(temp.position() + 1);
}
cursor = findCursor(keyword, multiLineText, 0, false, beginPos);
int pos = std::max(extra.cursor.position(), extra.cursor.anchor());
cursor = findCursor(keyword, multiLineText, pos - beginPos, false, beginPos);
} else {
cursor = document()->find(keyword, beginPos, flags);
}

if (cursor.isNull()) {
return false;
cursor = document()->find(keyword, cursor, flags);
}

while (!cursor.isNull()) {
extra.cursor = cursor;
/* 查找字符时,查找到完全相等的时候才高亮,如查找小写f时,大写的F不高亮 */
if (!extra.cursor.selectedText().compare(keyword) || keyword.contains(endLine)) {
listSelection->append(extra);
}

if (keyword.contains(endLine)) {
int pos = std::max(extra.cursor.position(), extra.cursor.anchor());
cursor = findCursor(keyword, multiLineText, pos - beginPos, false, beginPos);
} else {
cursor = document()->find(keyword, cursor, flags);
}

if (cursor.position() > endPos) {
break;
}
if (cursor.position() > endPos) {
break;
}

return true;
}

return false;
return true;
}

bool TextEdit::searchKeywordSeletion(QString keyword, QTextCursor cursor, bool findNext)
Expand Down Expand Up @@ -2195,6 +2199,10 @@ void TextEdit::renderAllSelections()

// 设置到 QPlainText 中进行渲染
setExtraSelections(finalSelections);

QString status = m_searchCount == 0 ? QString("No Reuslt") : QString("%1/%2").arg(m_searchStep).arg(m_searchCount);
// 发射信号,通知window更新状态文字
Q_EMIT signal_renderAllFinish(this, status);
}

void TextEdit::updateMarkAllSelectColor()
Expand Down Expand Up @@ -8071,3 +8079,32 @@ void TextEdit::onTextContentChanged(int from, int charsRemoved, int charsAdded)
m_MidButtonPatse = false;
}
}

void TextEdit::updateSearchStatus(const QString &keyword)
{
QTextCursor currentCursor = textCursor();

if (currentCursor.isNull())
{
return;
}

// 记录当前cursor在结果列表中的位置
int anchorPosition = currentCursor.anchor();
m_searchCount = 0;

QString plainText = this->toPlainText();
m_searchCount = plainText.count(keyword);

int searchStart = -1;
for (size_t i = 0; i < m_searchCount; i++)
{
int appearPosition = plainText.indexOf(keyword, searchStart + 1);
if (appearPosition >= anchorPosition)
{
m_searchStep = i + 1;
break;
}
searchStart = appearPosition;
}
}
5 changes: 5 additions & 0 deletions src/editor/dtextedit.h
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,7 @@ class TextEdit : public DPlainTextEdit
void popupNotify(QString notify);
void signal_readingPath();
void signal_setTitleFocus();
void signal_renderAllFinish(const TextEdit *editor, const QString &statusText);
public slots:
/**
* @author liumaochuan ut000616
Expand Down Expand Up @@ -606,6 +607,8 @@ public slots:
const QString &replaceText, const QString &withText, int offset = 0) const;
// 查找行号line起始的折叠区域
bool findFoldBlock(int line, QTextBlock &beginBlock, QTextBlock &endBlock, QTextBlock &curBlock);
// 更新 查找/替换 时位置信息
void updateSearchStatus(const QString &keyword);

private slots:
// 文档内容变更时触发
Expand Down Expand Up @@ -861,5 +864,7 @@ private slots:
bool m_MidButtonPatse = false; // 鼠标中键黏贴处理
bool m_isPreeditBefore = false; // 上一个输入法时间是否是 preedit
int m_preeditLengthBefore = 0;
size_t m_searchStep = 0;
size_t m_searchCount = 0;
};
#endif
29 changes: 29 additions & 0 deletions src/widgets/window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -940,6 +940,9 @@ EditWrapper *Window::createEditor()
updateJumpLineBar(wrapper->textEditor());
}, Qt::QueuedConnection);

// Editor renderAllSelections执行完毕后,调用onEditorRenderAllFinish更新状态文字
connect(wrapper->textEditor(),&TextEdit::signal_renderAllFinish,this,&Window::onEditorRenderAllFinish,Qt::QueuedConnection);

bool wordWrap = m_settings->settings->option("base.font.wordwrap")->value().toBool();
wrapper->textEditor()->m_pIsShowCodeFoldArea = m_settings->settings->option("base.font.codeflod")->value().toBool();
wrapper->OnThemeChangeSlot(m_themePath);
Expand Down Expand Up @@ -1547,6 +1550,10 @@ void Window::popupReplaceBar()

m_replaceBar->activeInput(text, tabPath, row, column, scrollOffset);

wrapper->textEditor()->highlightKeywordInView(text);
// set keywords
m_keywordForSearchAll = m_keywordForSearch = text;

QTimer::singleShot(10, this, [ = ] { m_replaceBar->focus(); });
}

Expand Down Expand Up @@ -3595,6 +3602,28 @@ void Window::setPrintEnabled(bool enabled)
}
}

void Window::onEditorRenderAllFinish(const TextEdit *editor, const QString &status)
{
EditWrapper *wrapper = currentWrapper();

if (wrapper == nullptr) {
return;
}
// 如果当前TextEdit和发射信号的TextEdit不是同一个,则不处理
// 实际上,只有切换tab时才会发生这种情况
if (wrapper->textEditor() != editor) {
return;
}

if (m_findBar->isVisible()) {
m_findBar->setStatusText(status);
}

if (m_replaceBar->isVisible()) {
m_replaceBar->setStatusText(status);
}
}

QStackedWidget *Window::getStackedWgt()
{
return m_editorWidget;
Expand Down
2 changes: 2 additions & 0 deletions src/widgets/window.h
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,8 @@ public Q_SLOTS:
// 接收布局模式变更信号,更新界面布局
Q_SLOT void updateSizeMode();

Q_SLOT void onEditorRenderAllFinish(const TextEdit *editor, const QString &text);

protected:
void resizeEvent(QResizeEvent *event) override;
void closeEvent(QCloseEvent *event) override;
Expand Down