From 197e8955a4e78cc0eea94181ed0034f11320c0cc Mon Sep 17 00:00:00 2001 From: Oskar Linde Date: Sat, 1 Feb 2014 15:53:18 +0100 Subject: Editor: Implement Search & Replace Conflicts: src/MainWindow.h diff --git a/src/MainWindow.h b/src/MainWindow.h index a908be0..e16f5b4 100644 --- a/src/MainWindow.h +++ b/src/MainWindow.h @@ -117,12 +117,16 @@ private slots: private slots: void actionRenderCSG(); + void selectFindType(int); void find(); + void findAndReplace(); void findNext(); void findPrev(); void useSelectionForFind(); + void replace(); + void replaceAll(); protected: - void findOperation(QTextDocument::FindFlags options = 0); + bool findOperation(QTextDocument::FindFlags options = 0); virtual bool eventFilter(QObject* obj, QEvent *event); private slots: diff --git a/src/MainWindow.ui b/src/MainWindow.ui index 75635b7..ddd69ee 100644 --- a/src/MainWindow.ui +++ b/src/MainWindow.ui @@ -60,7 +60,7 @@ 0 - + 5 @@ -73,37 +73,72 @@ 0 - - + + 0 + + + - Find + Replace - - + + + + All + + - + + + + Search string + + + + + + + Replacement string + + + + < - + > - + Done + + + + + Find + + + + + Replace + + + + @@ -263,6 +298,7 @@ + @@ -763,6 +799,14 @@ Ctrl+F + + + Find and Replace... + + + Ctrl+Alt+F + + Find Next diff --git a/src/mainwin.cc b/src/mainwin.cc index 07dff73..2c70102 100644 --- a/src/mainwin.cc +++ b/src/mainwin.cc @@ -287,6 +287,7 @@ MainWindow::MainWindow(const QString &filename) connect(this->editActionPreferences, SIGNAL(triggered()), this, SLOT(preferences())); // Edit->Find connect(this->editActionFind, SIGNAL(triggered()), this, SLOT(find())); + connect(this->editActionFindAndReplace, SIGNAL(triggered()), this, SLOT(findAndReplace())); connect(this->editActionFindNext, SIGNAL(triggered()), this, SLOT(findNext())); connect(this->editActionFindPrevious, SIGNAL(triggered()), this, SLOT(findPrev())); connect(this->editActionUseSelectionForFind, SIGNAL(triggered()), this, SLOT(useSelectionForFind())); @@ -379,12 +380,16 @@ MainWindow::MainWindow(const QString &filename) this, SLOT(setSyntaxHighlight(const QString&))); Preferences::inst()->apply(); + connect(this->findTypeComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(selectFindType(int))); connect(this->findInputField, SIGNAL(returnPressed()), this, SLOT(findNext())); find_panel->installEventFilter(this); connect(this->prevButton, SIGNAL(clicked()), this, SLOT(findPrev())); connect(this->nextButton, SIGNAL(clicked()), this, SLOT(findNext())); connect(this->hideFindButton, SIGNAL(clicked()), find_panel, SLOT(hide())); + connect(this->replaceButton, SIGNAL(clicked()), this, SLOT(replace())); + connect(this->replaceAllButton, SIGNAL(clicked()), this, SLOT(replaceAll())); + connect(this->replaceInputField, SIGNAL(returnPressed()), this, SLOT(replace())); // make sure it looks nice.. QSettings settings; @@ -1106,12 +1111,32 @@ void MainWindow::pasteViewportRotation() void MainWindow::find() { + findTypeComboBox->setCurrentIndex(0); + replaceInputField->hide(); + replaceButton->hide(); + replaceAllButton->hide(); find_panel->show(); findInputField->setFocus(); findInputField->selectAll(); } -void MainWindow::findOperation(QTextDocument::FindFlags options) { +void MainWindow::findAndReplace() +{ + findTypeComboBox->setCurrentIndex(1); + replaceInputField->show(); + replaceButton->show(); + replaceAllButton->show(); + find_panel->show(); + findInputField->setFocus(); + findInputField->selectAll(); +} + +void MainWindow::selectFindType(int type) { + if (type == 0) find(); + if (type == 1) findAndReplace(); +} + +bool MainWindow::findOperation(QTextDocument::FindFlags options) { bool success = editor->find(findInputField->text(), options); if (!success) { // Implement wrap-around search behavior QTextCursor old_cursor = editor->textCursor(); @@ -1122,6 +1147,23 @@ void MainWindow::findOperation(QTextDocument::FindFlags options) { if (!success) { editor->setTextCursor(old_cursor); } + return success; + } + return true; +} + +void MainWindow::replace() { + QTextCursor cursor = editor->textCursor(); + QString selectedText = cursor.selectedText(); + if (selectedText == findInputField->text()) { + cursor.insertText(replaceInputField->text()); + } + findNext(); +} + +void MainWindow::replaceAll() { + while (findOperation()) { + editor->textCursor().insertText(replaceInputField->text()); } } -- cgit v0.10.1