Skip to content

Commit

Permalink
feat: Add status label on findbar and replacebar
Browse files Browse the repository at this point in the history
Description: It is convent to know how many results I have and which result now is focused on.
  • Loading branch information
Fozei authored and Fozei committed Apr 29, 2024
1 parent 03bc8a0 commit abaf470
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 54 deletions.
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
125 changes: 71 additions & 54 deletions src/editor/dtextedit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2014,72 +2014,85 @@ bool TextEdit::updateKeywordSelectionsInView(QString keyword, QTextCharFormat ch
// Clear keyword selections first.
listSelection->clear();

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

// 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();
}
int endPos = endBlock.position() + endBlock.length() - 1;
if (cursor.isNull()) {
return false;
}

// 内部计算时,均视为 \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);
QTextCursor currentCursor = textCursor();
m_headStatusStr = "";
int start = 0;

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

// 记录当前cursor在结果列表中的位置
if(cursor.anchor() == currentCursor.anchor())
{
m_headStatusStr = QString("%1").arg(start);
}
cursor = findCursor(keyword, multiLineText, 0, false, beginPos);
} else {
cursor = document()->find(keyword, beginPos, flags);
}

if (cursor.isNull()) {
return false;
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);
}

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;
m_tailStatusStr = QString("%1").arg(listSelection->size());

return true;
}

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

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

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

void TextEdit::updateMarkAllSelectColor()
Expand Down
3 changes: 3 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(QString text);
public slots:
/**
* @author liumaochuan ut000616
Expand Down Expand Up @@ -861,5 +862,7 @@ private slots:
bool m_MidButtonPatse = false; // 鼠标中键黏贴处理
bool m_isPreeditBefore = false; // 上一个输入法时间是否是 preedit
int m_preeditLengthBefore = 0;
QString m_headStatusStr;
QString m_tailStatusStr;
};
#endif
22 changes: 22 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,21 @@ void Window::setPrintEnabled(bool enabled)
}
}

void Window::onEditorRenderAllFinish(QString status)

Check warning on line 3605 in src/widgets/window.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Parameter 'status' is passed by value. It could be passed as a const reference which is usually faster and recommended in C++.
{
EditWrapper *wrapper = currentWrapper();

if (currentWrapper() == nullptr) {
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(QString text);

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

0 comments on commit abaf470

Please sign in to comment.