Commit 00bd2143 authored by jhammen's avatar jhammen

text search show results in tree

parent c75ae84d
......@@ -37,10 +37,12 @@ SOURCES += \
synhighlighter.cpp
HEADERS += \
editorlink.h \
mainwindow.h \
editorwidget.h \
scriptrun.h \
outputwidget.h \
searchtreeitem.h \
searchwidget.h \
style.h \
projectlist.h \
......
/*
* This file is part of Bipscript-IDE.
*
* Bipscript-IDE is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Bipscript-IDE is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Bipscript-IDE. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef EDITORLINK_H
#define EDITORLINK_H
#include <QString>
#include <namedfile.h>
class EditorLocation {
NamedFile mfile;
int manchor;
int mposition;
public:
EditorLocation(NamedFile &file, int anchor, int position)
: mfile(file), manchor(anchor), mposition(position) {}
NamedFile &file() { return mfile; }
int anchor() { return manchor; }
int position() { return mposition; }
};
class EditorLink {
EditorLocation mloc;
QString mtext;
public:
EditorLink(const QString &text, NamedFile &file, int anchor, int position)
: mloc(file, anchor, position), mtext(text) {}
EditorLocation &location() { return mloc; }
QString &text() { return mtext; }
};
#endif // EDITORLINK_H
......@@ -81,21 +81,44 @@ bool EditorWidget::save() {
return true;
}
uint32_t EditorWidget::searchText(const QString &term) {
QList<EditorLink> EditorWidget::findAll(const QString &term) {
QList<EditorLink> ret;
// grab current cursor location
QTextCursor cursor = textCursor();
// find and hilite all results
moveCursor(QTextCursor::Start);
QList<QTextEdit::ExtraSelection> selections;
uint32_t count = 0;
while (find(term)) {
QTextCursor cursor = textCursor();
QTextEdit::ExtraSelection extra;
extra.cursor = textCursor();
extra.cursor = cursor;
extra.format.fontUnderline();
QColor color(210, 210, 210);
QColor color(222, 247, 177);
extra.format.setBackground(color);
selections.append(extra);
count++;
int position = cursor.position();
int anchor = cursor.anchor();
// expand selection to grab more text
cursor.movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor, 12);
QString text = cursor.selectedText();
EditorLink link(text, mfile, anchor, position);
ret.append(link);
}
setExtraSelections(selections);
return count;
// return to original cursor
setTextCursor(cursor);
findNext(term);
return ret;
}
void EditorWidget::findNext(const QString &term) {
if (!find(term)) {
moveCursor(QTextCursor::Start);
if (!find(term)) {
QMessageBox::warning(this, "No results", "'" + term + "' not found in current file");
}
}
setFocus();
}
void EditorWidget::focusLine(int line, int column) {
......@@ -111,6 +134,14 @@ void EditorWidget::focusLine(int line, int column) {
setFocus();
}
void EditorWidget::focusLocation(EditorLocation &loc) {
QTextCursor cursor = textCursor();
cursor.setPosition(loc.position());
cursor.setPosition(loc.anchor(), QTextCursor::KeepAnchor);
setTextCursor(cursor);
setFocus();
}
void EditorWidget::commentSelection() {
Selection sel = currentSelection();
// uncomment if all lines currently commented
......
......@@ -17,6 +17,7 @@
#ifndef EDITORWIDGET_H
#define EDITORWIDGET_H
#include "editorlink.h"
#include "namedfile.h"
#include <QPlainTextEdit>
......@@ -55,8 +56,10 @@ class EditorWidget : public QPlainTextEdit {
bool hasSelection() { return textCursor().hasSelection(); }
bool load();
bool save();
uint32_t searchText(const QString &term);
QList<EditorLink> findAll(const QString &term);
void findNext(const QString &term);
void focusLine(int line, int column = 0);
void focusLocation(EditorLocation &loc);
void commentSelection();
void moveFile(NamedFile &file) { mfile = file; }
// error markers
......
......@@ -59,6 +59,9 @@ MainWindow::MainWindow(QWidget *parent)
searchWidget = new SearchWidget();
ui->leftTabWidget->addTab(searchWidget, "Search");
connect(searchWidget, SIGNAL(searchCurrent(QString)), this, SLOT(searchCurrent(QString)));
connect(searchWidget, SIGNAL(searchNext(QString)), this, SLOT(searchNext(QString)));
connect(searchWidget, SIGNAL(navigateResult(EditorLocation &)), this,
SLOT(handleLink(EditorLocation &)));
projectList.load();
if (projectList.currentExists()) {
......@@ -197,6 +200,11 @@ void MainWindow::handleLink(const QUrl &url) {
}
}
void MainWindow::handleLink(EditorLocation &loc) {
createOrOpenEditor(loc.file().path);
currentEditor()->focusLocation(loc);
}
void MainWindow::markDirty(bool value) {
QString tabText(currentEditor()->name());
if (value) {
......@@ -211,11 +219,12 @@ void MainWindow::markDirty(bool value) {
void MainWindow::searchCurrent(const QString &term) {
EditorWidget *editor = currentEditor();
if (editor) {
uint32_t results = editor->searchText(term);
searchWidget->results(results);
searchWidget->results(editor->findAll(term));
}
}
void MainWindow::searchNext(const QString &term) { currentEditor()->findNext(term); }
void MainWindow::showTime(Position pos) {
QTime qtime(0, 0, 0);
qtime = qtime.addMSecs(pos.msec);
......
......@@ -48,8 +48,10 @@ class MainWindow : public QMainWindow {
void engineDisconnect();
void folderTreeClicked(QTreeWidgetItem *item, int col);
void handleLink(const QUrl &);
void handleLink(EditorLocation &);
void markDirty(bool unsaved);
void searchCurrent(const QString &);
void searchNext(const QString &);
void showTime(Position pos);
void tabChanged(int index);
void closeOtherEditors();
......
/*
* This file is part of Bipscript-IDE.
*
* Bipscript-IDE is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Bipscript-IDE is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Bipscript-IDE. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef SEARCHTREEITEM_H
#define SEARCHTREEITEM_H
#include <QTreeWidgetItem>
#include "editorlink.h"
class SearchTreeItem : public QTreeWidgetItem {
EditorLocation mloc;
public:
SearchTreeItem(QTreeWidgetItem *parent, EditorLink &link)
: QTreeWidgetItem(parent, QStringList(link.text()), QTreeWidgetItem::UserType),
mloc(link.location()) {}
EditorLocation &location() { return mloc; }
};
#endif // SEARCHTREEITEM_H
......@@ -18,8 +18,11 @@
#include "searchwidget.h"
#include "ui_searchwidget.h"
SearchWidget::SearchWidget(QWidget *parent) : QWidget(parent), ui(new Ui::SearchWidget) {
SearchWidget::SearchWidget(QWidget *parent)
: QWidget(parent), ui(new Ui::SearchWidget), newSearch(true) {
ui->setupUi(this);
connect(ui->treeWidget, SIGNAL(itemClicked(QTreeWidgetItem *, int)), this,
SLOT(resultTreeClicked(QTreeWidgetItem *, int)));
}
void SearchWidget::newFileSearch() {
......@@ -27,19 +30,51 @@ void SearchWidget::newFileSearch() {
ui->searchLineEdit->selectAll();
}
void SearchWidget::results(uint32_t results) {
QString mesg = QString("%1 results").arg(results);
void SearchWidget::results(QList<EditorLink> results) {
int count = results.size();
QString label(count == 1 ? "result" : "results");
QString mesg = QString("%1 %2").arg(count).arg(label);
ui->countLabel->setText(mesg);
QTreeWidgetItem *top = nullptr;
QTreeWidgetItem *parent = nullptr;
QString path;
foreach (EditorLink link, results) {
EditorLocation &loc = link.location();
QString label = QString("%1").arg(loc.position());
QTreeWidgetItem *item = new SearchTreeItem(top, link);
if (loc.file().path != path) {
path = loc.file().path;
parent =
new QTreeWidgetItem(top, QStringList(loc.file().name), QTreeWidgetItem::UserType);
ui->treeWidget->addTopLevelItem(parent);
parent->setExpanded(true);
}
parent->addChild(item);
}
}
SearchWidget::~SearchWidget() { delete ui; }
void SearchWidget::on_searchButton_clicked() {
QLineEdit *lineEdit = this->ui->searchLineEdit;
emit searchCurrent(lineEdit->text());
void SearchWidget::resultTreeClicked(QTreeWidgetItem *treeItem, int) {
SearchTreeItem *item = dynamic_cast<SearchTreeItem *>(treeItem);
if (item) {
emit navigateResult(item->location());
}
}
void SearchWidget::on_searchLineEdit_returnPressed() {
void SearchWidget::on_searchButton_clicked() { doSearch(); }
void SearchWidget::on_searchLineEdit_returnPressed() { doSearch(); }
void SearchWidget::doSearch() {
QLineEdit *lineEdit = this->ui->searchLineEdit;
emit searchCurrent(lineEdit->text());
if (newSearch) {
ui->treeWidget->clear();
emit searchCurrent(lineEdit->text());
newSearch = false;
} else {
emit searchNext(lineEdit->text());
}
}
void SearchWidget::on_searchLineEdit_textEdited(const QString &) { newSearch = true; }
......@@ -17,6 +17,7 @@
#ifndef SEARCHWIDGET_H
#define SEARCHWIDGET_H
#include "searchtreeitem.h"
#include <QWidget>
namespace Ui {
......@@ -29,18 +30,25 @@ class SearchWidget : public QWidget {
public:
explicit SearchWidget(QWidget *parent = nullptr);
void newFileSearch();
void results(uint32_t results);
void results(QList<EditorLink> results);
~SearchWidget();
signals:
void searchCurrent(QString);
void searchNext(QString);
void navigateResult(EditorLocation &);
private slots:
void resultTreeClicked(QTreeWidgetItem *, int);
void on_searchButton_clicked();
void on_searchLineEdit_returnPressed();
void on_searchLineEdit_textEdited(const QString &arg1);
private:
Ui::SearchWidget *ui;
// QList<Editor
bool newSearch;
void doSearch();
};
#endif // SEARCHWIDGET_H
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment