Commit 5721c52f authored by jhammen's avatar jhammen

update search results after text replace

parent a767358c
......@@ -228,7 +228,7 @@ void MainWindow::markDirty(bool value) {
void MainWindow::searchCurrent(const QString &term) {
EditorWidget *editor = currentEditor();
if (editor) {
searchWidget->addResults(editor->name(), editor->findAll(term, true), true);
searchWidget->addResults(editor->path(), editor->name(), editor->findAll(term, true), true);
}
}
......@@ -239,8 +239,10 @@ void MainWindow::searchNext(const QString &term) {
}
void MainWindow::replaceNext(const QString &term, const QString &replace) {
if (currentEditor()) {
currentEditor()->replaceNext(term, replace);
EditorWidget *editor = currentEditor();
if (editor) {
editor->replaceNext(term, replace);
searchWidget->updateResults(editor->path(), editor->findAll(term, true));
}
}
......@@ -254,12 +256,13 @@ void MainWindow::searchFolder(const QString &term, NamedFile &folder) {
if (editor) {
QList<EditorLink> results = editor->findAll(term, false);
if (results.size()) {
searchWidget->addResults(editor->name(), results, true);
searchWidget->addResults(editor->path(), editor->name(), results, true);
}
} else {
QList<EditorLink> results = searchFile(term, fileInfo.filePath());
if (results.size()) {
searchWidget->addResults(fileInfo.fileName(), results, false);
searchWidget->addResults(fileInfo.filePath(), fileInfo.fileName(), results,
false);
}
}
} else if (fileInfo.isDir()) {
......
......@@ -21,6 +21,31 @@
#include "editorlink.h"
class SearchTreeFileItem : public QTreeWidgetItem {
int mresults;
QString mpath;
QString mfilename;
public:
SearchTreeFileItem(const QString &path, const QString &filename, int results)
: QTreeWidgetItem(static_cast<QTreeWidgetItem *>(nullptr),
QStringList(header(filename, results)), QTreeWidgetItem::UserType),
mresults(results), mpath(path), mfilename(filename) {
setToolTip(0, path);
}
int results() { return mresults; }
void results(int results) {
mresults = results;
setText(0, header(mfilename, mresults));
}
QString &path() { return mpath; }
private:
static QString header(const QString &filename, int results) {
return QString("%1 (%2)").arg(filename).arg(results);
}
};
class SearchTreeItem : public QTreeWidgetItem {
EditorLocation mloc;
......
......@@ -36,12 +36,10 @@ void SearchWidget::newProjectSearch() {
ui->projectScopeButton->setChecked(true);
}
void SearchWidget::addResults(const QString &filename, QList<EditorLink> results, bool open) {
QTreeWidgetItem *top = nullptr;
void SearchWidget::addResults(const QString &path, const QString &filename,
QList<EditorLink> results, bool open) {
// header tree node for file name
QString header = QString("%1 (%2)").arg(filename).arg(results.size());
QTreeWidgetItem *parent =
new QTreeWidgetItem(top, QStringList(header), QTreeWidgetItem::UserType);
SearchTreeFileItem *parent = new SearchTreeFileItem(path, filename, results.size());
ui->treeWidget->addTopLevelItem(parent);
if (open) {
parent->setExpanded(true);
......@@ -57,6 +55,24 @@ void SearchWidget::addResults(const QString &filename, QList<EditorLink> results
showCount();
}
void SearchWidget::updateResults(const QString &path, QList<EditorLink> results) {
QTreeWidgetItem *root = ui->treeWidget->invisibleRootItem();
// child tree nodes for this path
for (int i = 0; i < root->childCount(); i++) {
SearchTreeFileItem *item = static_cast<SearchTreeFileItem *>(root->child(i));
if (item->path() == path) {
count += results.size() - item->results();
item->takeChildren();
foreach (EditorLink link, results) {
QTreeWidgetItem *child = new SearchTreeItem(item, link);
item->addChild(child);
}
item->results(results.size());
showCount();
}
}
}
void SearchWidget::clear() {
ui->searchLineEdit->clear();
ui->treeWidget->clear();
......
......@@ -31,7 +31,9 @@ class SearchWidget : public QWidget {
explicit SearchWidget(QWidget *parent = nullptr);
void newFileSearch();
void newProjectSearch();
void addResults(const QString &filename, QList<EditorLink> addResults, bool open);
void addResults(const QString &path, const QString &filename, QList<EditorLink> results,
bool open);
void updateResults(const QString &path, QList<EditorLink> results);
void clear();
~SearchWidget();
......
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