Skip to content

Commit

Permalink
add simple search facility
Browse files Browse the repository at this point in the history
  • Loading branch information
digikata committed Jun 14, 2013
1 parent 88c5e17 commit c7453af
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 1 deletion.
30 changes: 30 additions & 0 deletions mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,18 @@ MainWindow::MainWindow(QWidget *parent) :
connect( search, SIGNAL(search_status(QString)),
ui->statusBar, SLOT(showMessage(QString)));

connect( search, SIGNAL(search_results(QStringList)),
this, SLOT(elideNotes(QStringList)));

connect( ui->pb_opt, &QPushButton::clicked,
[=]()
{
ui->mainline->clear();
search->search_clear();
iterList( [](QListWidgetItem* it)
{
it->setHidden(false);
});
});

ui->mainToolBar->hide();
Expand All @@ -33,6 +40,8 @@ MainWindow::MainWindow(QWidget *parent) :
{
loadNote(item->text());
});


}

MainWindow::~MainWindow()
Expand All @@ -47,6 +56,27 @@ QString MainWindow::getNotesPath()
return QString("../corpus");
}

void MainWindow::elideNotes(const QStringList & notefiles)
{
ui->doclist->setUpdatesEnabled(false);
for( int irow=0; irow<ui->doclist->count(); ++irow)
{
auto it = ui->doclist->item(irow);
it->setHidden(!notefiles.contains( it->text() ));
}
ui->doclist->setUpdatesEnabled(true);
}


void MainWindow::iterList(nitFunc nitfunc)
{
ui->doclist->setUpdatesEnabled(false);
for( int irow=0; irow<ui->doclist->count(); ++irow)
{
nitfunc(ui->doclist->item(irow));
}
ui->doclist->setUpdatesEnabled(true);
}

void MainWindow::iterNotes(noteFunc proc)
{
Expand Down
5 changes: 4 additions & 1 deletion mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <QString>
#include <QStringList>
#include <QMainWindow>
#include <QListWidget>

#include "searcher.h"

Expand All @@ -23,7 +24,7 @@ class MainWindow : public QMainWindow
~MainWindow();

public slots:

void elideNotes(const QStringList&);

private:
Ui::MainWindow *ui;
Expand All @@ -34,8 +35,10 @@ public slots:

// title, path
typedef std::function<void (const QString&)> noteFunc;
typedef std::function<void (QListWidgetItem*)> nitFunc;

void iterNotes(noteFunc);
void iterList(nitFunc);
void loadNote( const QString& fname);
void populateList();
};
Expand Down
17 changes: 17 additions & 0 deletions searcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "searcher.h"

static QSqlDatabase* s_db = 0;
static QSqlQuery* s_qy = 0;

Searcher::Searcher(QObject *parent) :
QObject(parent)
Expand Down Expand Up @@ -46,5 +47,21 @@ void Searcher::search_clear()

void Searcher::search_update(QString str)
{
if( s_qy == 0 )
{
s_qy = new QSqlQuery(*s_db);
s_qy->prepare("SELECT fname FROM docs WHERE docs MATCH :sterm;");
}
emit search_status("Searching: " + str);

QString sstr = "*" + str + "*";
s_qy->bindValue( ":sterm", QVariant(sstr));
s_qy->exec();

QStringList results;
while(s_qy->next())
{
results << s_qy->value(0).toString();
}
emit search_results(results);
}
2 changes: 2 additions & 0 deletions searcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define SEARCHER_H

#include <QObject>
#include <QStringList>

class Searcher : public QObject
{
Expand All @@ -12,6 +13,7 @@ class Searcher : public QObject

signals:
void search_status(QString);
void search_results(const QStringList&);

public slots:
void load_entry(const QString& title, const QString& body);
Expand Down

0 comments on commit c7453af

Please sign in to comment.