RedPanda-CPP/RedPandaIDE/widgets/searchdialog.cpp

153 lines
4.2 KiB
C++
Raw Normal View History

2021-08-02 21:58:39 +08:00
#include "searchdialog.h"
#include "ui_searchdialog.h"
#include <QMessageBox>
#include <memory>
2023-01-11 16:22:26 +08:00
#include <qsynedit/searcher/basicsearcher.h>
#include <qsynedit/searcher/regexsearcher.h>
#include "../utils.h"
#include "../editor.h"
#include "../editorlist.h"
#include "../mainwindow.h"
2021-08-02 21:58:39 +08:00
SearchDialog::SearchDialog(QWidget *parent) :
QDialog(parent),
2021-08-03 23:55:57 +08:00
ui(new Ui::SearchDialog),
mSearchOptions()
2021-08-02 21:58:39 +08:00
{
ui->setupUi(this);
mBasicSearchEngine= std::make_shared<QSynedit::BasicSearcher>();
mRegexSearchEngine= std::make_shared<QSynedit::RegexSearcher>();
2021-08-02 21:58:39 +08:00
}
SearchDialog::~SearchDialog()
{
delete ui;
}
2021-08-02 23:42:10 +08:00
void SearchDialog::find(const QString &text)
{
ui->cbFind->setCurrentText(text);
2022-03-11 20:51:33 +08:00
ui->cbFind->setFocus();
2021-08-02 23:42:10 +08:00
show();
}
void SearchDialog::findNext()
{
doSearch(false);
if (ui->chkCloseAfterSearch)
close();
}
void SearchDialog::findPrevious()
2021-08-02 23:42:10 +08:00
{
doSearch(true);
if (ui->chkCloseAfterSearch)
close();
2021-08-02 23:42:10 +08:00
}
void SearchDialog::doSearch(bool backward)
2021-08-02 23:42:10 +08:00
{
saveComboHistory(ui->cbFind,ui->cbFind->currentText());
2021-08-03 23:55:57 +08:00
mSearchOptions&=0;
// Apply options
if (backward) {
mSearchOptions.setFlag(QSynedit::ssoBackwards);
}
2021-08-03 23:55:57 +08:00
if (ui->chkRegExp->isChecked()) {
2022-09-25 09:55:18 +08:00
mSearchOptions.setFlag(QSynedit::ssoRegExp);
2021-08-03 23:55:57 +08:00
}
if (ui->chkCaseSensetive->isChecked()) {
2022-09-25 09:55:18 +08:00
mSearchOptions.setFlag(QSynedit::ssoMatchCase);
2021-08-03 23:55:57 +08:00
}
if (ui->chkWholeWord->isChecked()) {
2022-09-25 09:55:18 +08:00
mSearchOptions.setFlag(QSynedit::ssoWholeWord);
2021-08-03 23:55:57 +08:00
}
if (ui->chkWrapAround->isChecked()) {
2022-09-25 09:55:18 +08:00
mSearchOptions.setFlag(QSynedit::ssoWrapAround);
}
2021-08-03 23:55:57 +08:00
// Apply scope, when enabled
if (ui->grpScope->isEnabled()) {
2021-08-03 23:55:57 +08:00
if (ui->rbSelection->isChecked()) {
2022-09-25 09:55:18 +08:00
mSearchOptions.setFlag(QSynedit::ssoSelectedOnly);
2021-08-03 23:55:57 +08:00
}
}
// Apply origin, when enabled
if (ui->grpOrigin->isEnabled()) {
2021-08-03 23:55:57 +08:00
if (ui->rbEntireScope->isChecked()) {
2022-09-25 09:55:18 +08:00
mSearchOptions.setFlag(QSynedit::ssoEntireScope);
2021-08-03 23:55:57 +08:00
}
}
Editor *editor = pMainWindow->editorList()->getEditor();
if (editor) {
// Modify the caret when using 'from cursor' and when the selection is ignored
if (!mSearchOptions.testFlag(QSynedit::ssoEntireScope) && !mSearchOptions.testFlag(QSynedit::ssoSelectedOnly)
&& editor->selAvail()) {
// start at end of selection
if (mSearchOptions.testFlag(QSynedit::ssoBackwards)) {
editor->setCaretXY(editor->blockBegin());
} else {
editor->setCaretXY(editor->blockEnd());
}
2021-08-05 12:31:53 +08:00
}
QSynedit::PSynSearchBase searchEngine;
if (mSearchOptions.testFlag(QSynedit::ssoRegExp)) {
searchEngine = mRegexSearchEngine;
2021-08-03 23:55:57 +08:00
} else {
searchEngine = mBasicSearchEngine;
2021-08-03 23:55:57 +08:00
}
editor->searchReplace(
ui->cbFind->currentText(),
"",
mSearchOptions,
searchEngine, nullptr, [this,backward](){
QString msg;
if (backward) {
msg = tr("Beginning of file has been reached. ")
+tr("Do you want to continue from file's end?");
} else {
msg = tr("End of file has been reached. ")
+tr("Do you want to continue from file's beginning?");
}
QWidget *p;
if (isVisible()) {
p=this;
} else {
p=pMainWindow;
}
return QMessageBox::question(p,
tr("Continue Search"),
msg,
QMessageBox::Yes|QMessageBox::No,
QMessageBox::Yes) == QMessageBox::Yes;
});
2021-08-03 23:55:57 +08:00
}
2021-08-05 12:31:53 +08:00
}
2021-08-03 23:55:57 +08:00
void SearchDialog::on_cbFind_currentTextChanged(const QString &value)
2021-08-05 12:31:53 +08:00
{
ui->btnNext->setEnabled(!value.isEmpty());
ui->btnPrevious->setEnabled(!value.isEmpty());
2021-08-03 23:55:57 +08:00
}
void SearchDialog::on_btnClose_clicked()
{
close();
}
void SearchDialog::on_btnNext_clicked()
{
doSearch(false);
}
void SearchDialog::on_btnPrevious_clicked()
2021-08-03 23:55:57 +08:00
{
doSearch(true);
}