mirror of https://github.com/vitalif/openscad
#879 Fix forward/backward search in the scintilla editor
parent
cfb2f350c8
commit
a25f02780e
|
@ -140,7 +140,7 @@ private slots:
|
|||
private slots:
|
||||
void selectFindType(int);
|
||||
void find();
|
||||
void FindString(QString);
|
||||
void findString(QString);
|
||||
void findAndReplace();
|
||||
void findNext();
|
||||
void findPrev();
|
||||
|
@ -148,7 +148,7 @@ private slots:
|
|||
void replace();
|
||||
void replaceAll();
|
||||
protected:
|
||||
bool findOperation(QTextDocument::FindFlags options = 0);
|
||||
bool findOperation(bool findBackwards = false);
|
||||
virtual bool eventFilter(QObject* obj, QEvent *event);
|
||||
|
||||
private slots:
|
||||
|
|
|
@ -22,8 +22,7 @@ public:
|
|||
virtual QTextCursor textCursor() { QTextCursor c; return c;}
|
||||
virtual void setTextCursor (const QTextCursor &) { }
|
||||
virtual QTextDocument *document(){QTextDocument *t = new QTextDocument; return t;}
|
||||
virtual bool find(const QString &, QTextDocument::FindFlags options = 0){ return options;}
|
||||
virtual bool findNext(QTextDocument::FindFlags, QString&){return 0;}
|
||||
virtual bool find(const QString &, bool findNext = false, bool findBackwards = false) = 0;
|
||||
virtual void replaceSelectedText(QString&){ }
|
||||
|
||||
public slots:
|
||||
|
|
|
@ -191,10 +191,6 @@ void LegacyEditor::setTextCursor (const QTextCursor &cursor)
|
|||
{
|
||||
textedit->setTextCursor(cursor);
|
||||
}
|
||||
bool LegacyEditor::find(const QString & exp, QTextDocument::FindFlags options)
|
||||
{
|
||||
return textedit->find(exp, options);
|
||||
}
|
||||
void LegacyEditor::insertPlainText(const QString &text)
|
||||
{
|
||||
textedit->insertPlainText(text);
|
||||
|
@ -233,15 +229,20 @@ void LegacyEditor::replaceSelectedText(QString& newText)
|
|||
}
|
||||
}
|
||||
|
||||
bool LegacyEditor::findNext(QTextDocument::FindFlags options, QString& newText)
|
||||
bool LegacyEditor::findString(const QString & exp, bool findBackwards) const
|
||||
{
|
||||
bool success = this->find(newText, options);
|
||||
return textedit->find(exp, findBackwards ? QTextDocument::FindBackward : QTextDocument::FindFlags(0));
|
||||
}
|
||||
|
||||
bool LegacyEditor::find(const QString &newText, bool findNext, bool findBackwards)
|
||||
{
|
||||
bool success = this->findString(newText, findBackwards);
|
||||
if (!success) { // Implement wrap-around search behavior
|
||||
QTextCursor old_cursor = this->textCursor();
|
||||
QTextCursor tmp_cursor = old_cursor;
|
||||
tmp_cursor.movePosition((options & QTextDocument::FindBackward) ? QTextCursor::End : QTextCursor::Start);
|
||||
tmp_cursor.movePosition(findBackwards ? QTextCursor::End : QTextCursor::Start);
|
||||
this->setTextCursor(tmp_cursor);
|
||||
bool success = this->find(newText, options);
|
||||
bool success = this->findString(newText, findBackwards);
|
||||
if (!success) {
|
||||
this->setTextCursor(old_cursor);
|
||||
}
|
||||
|
|
|
@ -25,10 +25,10 @@ public:
|
|||
QTextCursor textCursor() const;
|
||||
void setTextCursor (const QTextCursor&);
|
||||
QTextDocument *document() { return textedit->document(); }
|
||||
bool find(const QString&, QTextDocument::FindFlags options = 0);
|
||||
bool find(const QString &, bool findNext = false, bool findBackwards = false);
|
||||
void replaceSelectedText(QString& newText);
|
||||
bool findNext(QTextDocument::FindFlags, QString&);
|
||||
|
||||
bool findString(const QString & exp, bool findBackwards) const;
|
||||
public slots:
|
||||
void zoomIn();
|
||||
void zoomOut();
|
||||
|
|
|
@ -23,7 +23,6 @@
|
|||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*
|
||||
*/
|
||||
#include <iostream>
|
||||
#include "GeometryCache.h"
|
||||
#include "ModuleCache.h"
|
||||
#include "MainWindow.h"
|
||||
|
@ -40,7 +39,9 @@
|
|||
#include "progress.h"
|
||||
#include "dxfdim.h"
|
||||
#include "legacyeditor.h"
|
||||
#ifdef USE_SCINTILLA_EDITOR
|
||||
#include "scintillaeditor.h"
|
||||
#endif
|
||||
#include "AboutDialog.h"
|
||||
#include "FontListDialog.h"
|
||||
#ifdef ENABLE_OPENCSG
|
||||
|
@ -172,14 +173,14 @@ MainWindow::MainWindow(const QString &filename)
|
|||
|
||||
useScintilla = (editortype == "QScintilla Editor");
|
||||
#ifdef USE_SCINTILLA_EDITOR
|
||||
if (useScintilla)
|
||||
{ editor = new ScintillaEditor(editorDockContents);
|
||||
this->editActionIndent->setVisible(false);
|
||||
this->editActionUnindent->setVisible(false);
|
||||
this->editActionComment->setVisible(false);
|
||||
this->editActionUncomment->setVisible(false);
|
||||
}
|
||||
else
|
||||
if (useScintilla) {
|
||||
editor = new ScintillaEditor(editorDockContents);
|
||||
this->editActionIndent->setVisible(false);
|
||||
this->editActionUnindent->setVisible(false);
|
||||
this->editActionComment->setVisible(false);
|
||||
this->editActionUncomment->setVisible(false);
|
||||
}
|
||||
else
|
||||
#endif
|
||||
editor = new LegacyEditor(editorDockContents);
|
||||
|
||||
|
@ -432,6 +433,7 @@ MainWindow::MainWindow(const QString &filename)
|
|||
Preferences::inst()->apply();
|
||||
|
||||
connect(this->findTypeComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(selectFindType(int)));
|
||||
connect(this->findInputField, SIGNAL(textChanged(QString)), this, SLOT(findString(QString)));
|
||||
connect(this->findInputField, SIGNAL(returnPressed()), this->nextButton, SLOT(animateClick()));
|
||||
find_panel->installEventFilter(this);
|
||||
|
||||
|
@ -441,7 +443,6 @@ MainWindow::MainWindow(const QString &filename)
|
|||
connect(this->replaceButton, SIGNAL(clicked()), this, SLOT(replace()));
|
||||
connect(this->replaceAllButton, SIGNAL(clicked()), this, SLOT(replaceAll()));
|
||||
connect(this->replaceInputField, SIGNAL(returnPressed()), this->replaceButton, SLOT(animateClick()));
|
||||
connect(this->findInputField, SIGNAL(textChanged(QString)), this, SLOT(FindString(QString)));
|
||||
|
||||
// make sure it looks nice..
|
||||
QSettings settings;
|
||||
|
@ -1284,10 +1285,9 @@ void MainWindow::find()
|
|||
findInputField->selectAll();
|
||||
}
|
||||
|
||||
void MainWindow::FindString(QString textToFind)
|
||||
void MainWindow::findString(QString textToFind)
|
||||
{
|
||||
QTextDocument::FindFlags options;
|
||||
editor->find(textToFind, options);
|
||||
editor->find(textToFind, false, false);
|
||||
}
|
||||
|
||||
void MainWindow::findAndReplace()
|
||||
|
@ -1306,20 +1306,19 @@ void MainWindow::selectFindType(int type) {
|
|||
if (type == 1) findAndReplace();
|
||||
}
|
||||
|
||||
bool MainWindow::findOperation(QTextDocument::FindFlags options) {
|
||||
bool success = editor->find(findInputField->text(), options);
|
||||
bool MainWindow::findOperation(bool findBackwards) {
|
||||
bool success = editor->find(findInputField->text(), findBackwards);
|
||||
if (!success) { // Implement wrap-around search behavior
|
||||
QTextCursor old_cursor = editor->textCursor();
|
||||
QTextCursor tmp_cursor = old_cursor;
|
||||
tmp_cursor.movePosition((options & QTextDocument::FindBackward) ? QTextCursor::End : QTextCursor::Start);
|
||||
tmp_cursor.movePosition(findBackwards ? QTextCursor::End : QTextCursor::Start);
|
||||
editor->setTextCursor(tmp_cursor);
|
||||
bool success = editor->find(findInputField->text(), options);
|
||||
success = editor->find(findInputField->text(), findBackwards);
|
||||
if (!success) {
|
||||
editor->setTextCursor(old_cursor);
|
||||
}
|
||||
return success;
|
||||
}
|
||||
return true;
|
||||
return success;
|
||||
}
|
||||
|
||||
void MainWindow::replace() {
|
||||
|
@ -1340,14 +1339,12 @@ void MainWindow::replaceAll() {
|
|||
|
||||
void MainWindow::findNext()
|
||||
{
|
||||
QTextDocument::FindFlags options;
|
||||
QString newText = this->replaceInputField->text();
|
||||
editor->findNext(options, newText);
|
||||
editor->find(this->findInputField->text(), true);
|
||||
}
|
||||
|
||||
void MainWindow::findPrev()
|
||||
{
|
||||
findOperation(QTextDocument::FindBackward);
|
||||
editor->find(this->findInputField->text(), true, true);
|
||||
}
|
||||
|
||||
void MainWindow::useSelectionForFind()
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
#include <algorithm>
|
||||
#include <QString>
|
||||
#include <QChar>
|
||||
#include "scintillaeditor.h"
|
||||
|
@ -250,15 +251,23 @@ void ScintillaEditor::onTextChanged()
|
|||
qsci->setMarginWidth(0, fontmetrics.width(QString::number(qsci->lines())) + 6);
|
||||
}
|
||||
|
||||
bool ScintillaEditor::find(const QString &expr, QTextDocument::FindFlags options)
|
||||
bool ScintillaEditor::find(const QString &expr, bool findNext, bool findBackwards)
|
||||
{
|
||||
qsci->findFirst(expr, false, false, false, false, true, 1, 1, true, false);
|
||||
}
|
||||
int startline = -1, startindex = -1;
|
||||
|
||||
bool ScintillaEditor::findNext(QTextDocument::FindFlags options, QString& newText)
|
||||
{
|
||||
qsci->findNext();
|
||||
return 0;
|
||||
// If findNext, start from the end of the current selection
|
||||
if (qsci->hasSelectedText()) {
|
||||
int lineFrom, indexFrom, lineTo, indexTo;
|
||||
qsci->getSelection(&lineFrom, &indexFrom, &lineTo, &indexTo);
|
||||
|
||||
if (findNext) {
|
||||
startline = findBackwards ? std::min(lineFrom, lineTo) : std::max(lineFrom, lineTo);
|
||||
startindex = findBackwards ? std::min(indexFrom, indexTo) : std::max(indexFrom, indexTo);
|
||||
}
|
||||
}
|
||||
|
||||
return qsci->findFirst(expr, false, false, false, true,
|
||||
!findBackwards, startline, startindex);
|
||||
}
|
||||
|
||||
void ScintillaEditor::replaceSelectedText(QString& newText)
|
||||
|
|
|
@ -23,8 +23,7 @@ public:
|
|||
void Monokai();
|
||||
void Solarized_light();
|
||||
void noColor();
|
||||
bool find(const QString&, QTextDocument::FindFlags options);
|
||||
bool findNext(QTextDocument::FindFlags, QString&);
|
||||
bool find(const QString &, bool findNext = false, bool findBackwards = false);
|
||||
void replaceSelectedText(QString&);
|
||||
|
||||
public slots:
|
||||
|
|
Loading…
Reference in New Issue