From 2ac52279a091b1e4a81df199d302e12c8e2cad3e Mon Sep 17 00:00:00 2001 From: Ken Moore Date: Tue, 19 Apr 2016 13:53:17 -0400 Subject: Get find/replace support fully implemented. --- desktop-utilities/lumina-textedit/MainUI.cpp | 110 +++++++++++++- desktop-utilities/lumina-textedit/MainUI.h | 12 ++ desktop-utilities/lumina-textedit/MainUI.ui | 169 +++++++++++++++++++++ .../lumina-textedit/syntaxSupport.cpp | 4 +- 4 files changed, 292 insertions(+), 3 deletions(-) (limited to 'desktop-utilities') diff --git a/desktop-utilities/lumina-textedit/MainUI.cpp b/desktop-utilities/lumina-textedit/MainUI.cpp index d477071d..86f204bc 100644 --- a/desktop-utilities/lumina-textedit/MainUI.cpp +++ b/desktop-utilities/lumina-textedit/MainUI.cpp @@ -14,6 +14,7 @@ #include #include +#include MainUI::MainUI() : QMainWindow(), ui(new Ui::MainUI){ ui->setupUi(this); @@ -21,6 +22,9 @@ MainUI::MainUI() : QMainWindow(), ui(new Ui::MainUI){ Custom_Syntax::SetupDefaultColors(settings); //pre-load any color settings as needed this->setWindowTitle(tr("Text Editor")); ui->tabWidget->clear(); + closeFindS = new QShortcut(QKeySequence(Qt::Key_Escape), this); + connect(closeFindS, SIGNAL(activated()), this, SLOT(closeFindReplace()) ); + ui->groupReplace->setVisible(false); //Update the menu of available syntax highlighting modes QStringList smodes = Custom_Syntax::availableRules(); for(int i=0; iactionLine_Numbers, SIGNAL(toggled(bool)), this, SLOT(showLineNumbers(bool)) ); connect(ui->actionWrap_Lines, SIGNAL(toggled(bool)), this, SLOT(wrapLines(bool)) ); connect(ui->actionCustomize_Colors, SIGNAL(triggered()), this, SLOT(ModifyColors()) ); + connect(ui->actionFind, SIGNAL(triggered()), this, SLOT(openFind()) ); + connect(ui->actionReplace, SIGNAL(triggered()), this, SLOT(openReplace()) ); + connect(ui->tool_find_next, SIGNAL(clicked()), this, SLOT(findNext()) ); + connect(ui->tool_find_prev, SIGNAL(clicked()), this, SLOT(findPrev()) ); + connect(ui->tool_replace, SIGNAL(clicked()), this, SLOT(replaceOne()) ); + connect(ui->tool_replace_all, SIGNAL(clicked()), this, SLOT(replaceAll()) ); + connect(ui->line_find, SIGNAL(returnPressed()), this, SLOT(findNext()) ); + connect(ui->line_replace, SIGNAL(returnPressed()), this, SLOT(replaceOne()) ); updateIcons(); //Now load the initial size of the window QSize lastSize = settings->value("lastSize",QSize()).toSize(); @@ -56,6 +68,7 @@ MainUI::~MainUI(){ void MainUI::LoadArguments(QStringList args){ //CLI arguments for(int i=0; iline_find->setFocus(); } } @@ -70,9 +83,17 @@ void MainUI::updateIcons(){ ui->actionClose_File->setIcon(LXDG::findIcon("document-close") ); ui->actionSave_File->setIcon(LXDG::findIcon("document-save") ); ui->actionSave_File_As->setIcon(LXDG::findIcon("document-save-as") ); + ui->actionFind->setIcon(LXDG::findIcon("edit-find") ); + ui->actionReplace->setIcon(LXDG::findIcon("edit-find-replace") ); ui->menuSyntax_Highlighting->setIcon( LXDG::findIcon("format-text-color") ); ui->actionCustomize_Colors->setIcon( LXDG::findIcon("format-fill-color") ); - + //icons for the special find/replace groupbox + ui->tool_find_next->setIcon(LXDG::findIcon("go-down-search")); + ui->tool_find_prev->setIcon(LXDG::findIcon("go-up-search")); + ui->tool_find_casesensitive->setIcon(LXDG::findIcon("format-text-italic")); + ui->tool_replace->setIcon(LXDG::findIcon("arrow-down")); + ui->tool_replace_all->setIcon(LXDG::findIcon("arrow-down-double")); + //ui->tool_find_next->setIcon(LXDG::findIcon("")); } // ================= @@ -118,6 +139,7 @@ void MainUI::OpenFile(QString file){ ui->tabWidget->addTab(edit, files[i].section("/",-1)); edit->showLineNumbers(ui->actionLine_Numbers->isChecked()); edit->setLineWrapMode( ui->actionWrap_Lines->isChecked() ? QPlainTextEdit::WidgetWidth : QPlainTextEdit::NoWrap); + edit->setFocusPolicy(Qt::ClickFocus); //no "tabbing" into this widget ui->tabWidget->setCurrentWidget(edit); edit->LoadFile(files[i]); edit->setFocus(); @@ -206,3 +228,89 @@ void MainUI::tabClosed(int tab){ ui->tabWidget->removeTab(tab); edit->deleteLater(); } + +//Find/Replace functions +void MainUI::closeFindReplace(){ + ui->groupReplace->setVisible(false); + PlainTextEditor *cur = currentEditor(); + if(cur!=0){ cur->setFocus(); } +} + +void MainUI::openFind(){ + PlainTextEditor *cur = currentEditor(); + if(cur==0){ return; } + ui->groupReplace->setVisible(true); + ui->line_find->setText( cur->textCursor().selectedText() ); + ui->line_replace->setText(""); + ui->line_find->setFocus(); +} + +void MainUI::openReplace(){ + PlainTextEditor *cur = currentEditor(); + if(cur==0){ return; } + ui->groupReplace->setVisible(true); + ui->line_find->setText( cur->textCursor().selectedText() ); + ui->line_replace->setText(""); + ui->line_replace->setFocus(); +} + +void MainUI::findNext(){ + PlainTextEditor *cur = currentEditor(); + if(cur==0){ return; } + bool found = cur->find( ui->line_find->text(), ui->tool_find_casesensitive->isChecked() ? QTextDocument::FindCaseSensitively : QTextDocument::FindFlags() ); + if(!found){ + //Try starting back at the top of the file + cur->moveCursor(QTextCursor::Start); + cur->find( ui->line_find->text(), ui->tool_find_casesensitive->isChecked() ? QTextDocument::FindCaseSensitively : QTextDocument::FindFlags() ); + } +} + +void MainUI::findPrev(){ + PlainTextEditor *cur = currentEditor(); + if(cur==0){ return; } + bool found = cur->find( ui->line_find->text(), ui->tool_find_casesensitive->isChecked() ? QTextDocument::FindCaseSensitively | QTextDocument::FindBackward : QTextDocument::FindBackward ); + if(!found){ + //Try starting back at the bottom of the file + cur->moveCursor(QTextCursor::End); + cur->find( ui->line_find->text(), ui->tool_find_casesensitive->isChecked() ? QTextDocument::FindCaseSensitively | QTextDocument::FindBackward : QTextDocument::FindBackward ); + } +} + +void MainUI::replaceOne(){ + PlainTextEditor *cur = currentEditor(); + if(cur==0){ return; } + //See if the current selection matches the find field first + bool done = false; + if(cur->textCursor().selectedText()==ui->line_find->text()){ + cur->insertPlainText(ui->line_replace->text()); + done = true; + }else{ + //Find/replace the next occurance of the string + bool found = cur->find( ui->line_find->text(), ui->tool_find_casesensitive->isChecked() ? QTextDocument::FindCaseSensitively : QTextDocument::FindFlags() ); + if(found){ cur->insertPlainText(ui->line_replace->text()); done = true;} + } + if(done){ + //Re-highlight the newly-inserted text + cur->find( ui->line_replace->text(), QTextDocument::FindCaseSensitively | QTextDocument::FindBackward); + } +} + +void MainUI::replaceAll(){ +PlainTextEditor *cur = currentEditor(); + if(cur==0){ return; } + //See if the current selection matches the find field first + bool done = false; + if(cur->textCursor().selectedText()==ui->line_find->text()){ + cur->insertPlainText(ui->line_replace->text()); + done = true; + } + while( cur->find( ui->line_find->text(), ui->tool_find_casesensitive->isChecked() ? QTextDocument::FindCaseSensitively : QTextDocument::FindFlags() ) ){ + //Find/replace every occurance of the string + cur->insertPlainText(ui->line_replace->text()); + done = true; + } + if(done){ + //Re-highlight the newly-inserted text + cur->find( ui->line_replace->text(), QTextDocument::FindCaseSensitively | QTextDocument::FindBackward); + } +} diff --git a/desktop-utilities/lumina-textedit/MainUI.h b/desktop-utilities/lumina-textedit/MainUI.h index 64b4de3d..e040591a 100644 --- a/desktop-utilities/lumina-textedit/MainUI.h +++ b/desktop-utilities/lumina-textedit/MainUI.h @@ -10,6 +10,7 @@ #include #include #include +#include #include "PlainTextEditor.h" @@ -31,6 +32,7 @@ public slots: private: Ui::MainUI *ui; QSettings *settings; + QShortcut *closeFindS; //Simplification functions PlainTextEditor* currentEditor(); @@ -55,6 +57,16 @@ private slots: void tabChanged(); void tabClosed(int); + //Find/Replace functions + void closeFindReplace(); + void openFind(); + void openReplace(); + + void findNext(); + void findPrev(); + void replaceOne(); + void replaceAll(); + protected: void resizeEvent(QResizeEvent *ev){ settings->setValue("lastSize", ev->size()); diff --git a/desktop-utilities/lumina-textedit/MainUI.ui b/desktop-utilities/lumina-textedit/MainUI.ui index 264646d7..0bd17b99 100644 --- a/desktop-utilities/lumina-textedit/MainUI.ui +++ b/desktop-utilities/lumina-textedit/MainUI.ui @@ -15,6 +15,9 @@ + + 2 + 0 @@ -29,6 +32,9 @@ + + Qt::ClickFocus + true @@ -45,6 +51,136 @@ + + + + Find/Replace + + + false + + + + 1 + + + 3 + + + 1 + + + 1 + + + 2 + + + + + + + + Qt::NoFocus + + + Find the previous match + + + ... + + + + + + + Find: + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + + + + + Qt::NoFocus + + + Find the next match + + + ... + + + + + + + Replace: + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + + + + + + + + Qt::NoFocus + + + + + + Match case + + + + + + true + + + false + + + true + + + + + + + Qt::NoFocus + + + Replace next match + + + ... + + + + + + + Qt::NoFocus + + + Replace all matches (to end of document) + + + ... + + + + + + @@ -86,11 +222,22 @@ + + + Edit + + + + + + + Qt::CustomContextMenu + toolBar @@ -202,6 +349,28 @@ Wrap Lines + + + Find + + + Ctrl+F + + + Qt::ApplicationShortcut + + + + + Replace + + + Ctrl+R + + + Qt::ApplicationShortcut + + diff --git a/desktop-utilities/lumina-textedit/syntaxSupport.cpp b/desktop-utilities/lumina-textedit/syntaxSupport.cpp index 2d00201f..1e3f796b 100644 --- a/desktop-utilities/lumina-textedit/syntaxSupport.cpp +++ b/desktop-utilities/lumina-textedit/syntaxSupport.cpp @@ -152,12 +152,12 @@ void Custom_Syntax::loadRules(QString type){ // Strong Emphasis rule.format.setFontItalic(false); rule.format.setFontWeight(QFont::Bold); - rule.pattern = QRegExp("\\b[*]{2}.*[*]{2}\\b"); + rule.pattern = QRegExp("\\b[*]{2}[^*\n]+[*]{2}\\b"); rules << rule; // Emphasis rule.format.setFontItalic(true); rule.format.setFontWeight(QFont::Normal); - rule.pattern = QRegExp("\\b[*].+[*]\\b"); + rule.pattern = QRegExp("\\b[*][^*\n]+[*]\\b"); rules << rule; // Code Sample rule.format.setFontItalic(false); -- cgit