new variation on the search-as-you-type code
This commit is contained in:
parent
48deb73c43
commit
7c6f05bb6b
@ -63,10 +63,24 @@ void SSearch::init()
|
|||||||
m_displayingCompletions = false;
|
m_displayingCompletions = false;
|
||||||
m_escape = false;
|
m_escape = false;
|
||||||
m_disableAutosearch = true;
|
m_disableAutosearch = true;
|
||||||
|
m_stroketimeout = new QTimer(this);
|
||||||
|
m_stroketimeout->setSingleShot(true);
|
||||||
|
connect(m_stroketimeout, SIGNAL(timeout()), this, SLOT(timerDone()));
|
||||||
|
m_keystroke = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
void SSearch::timerDone()
|
||||||
|
{
|
||||||
|
QString qs = queryText->currentText();
|
||||||
|
LOGINFO(("TIMER DONE. qstring [%s]\n", qs2utf8s(qs).c_str()));
|
||||||
|
searchTextChanged(qs);
|
||||||
}
|
}
|
||||||
|
|
||||||
void SSearch::searchTextChanged(const QString& text)
|
void SSearch::searchTextChanged(const QString& text)
|
||||||
{
|
{
|
||||||
|
QString qs = queryText->currentText();
|
||||||
|
LOGINFO(("SEARCHTEXTCHANGED. ks %d text [%s]\n",
|
||||||
|
m_keystroke, qs2utf8s(text).c_str()));
|
||||||
if (text.isEmpty()) {
|
if (text.isEmpty()) {
|
||||||
searchPB->setEnabled(false);
|
searchPB->setEnabled(false);
|
||||||
clearqPB->setEnabled(false);
|
clearqPB->setEnabled(false);
|
||||||
@ -75,35 +89,25 @@ void SSearch::searchTextChanged(const QString& text)
|
|||||||
} else {
|
} else {
|
||||||
searchPB->setEnabled(true);
|
searchPB->setEnabled(true);
|
||||||
clearqPB->setEnabled(true);
|
clearqPB->setEnabled(true);
|
||||||
if (prefs.autoSearchOnWS && !m_disableAutosearch) {
|
if (m_keystroke) {
|
||||||
|
m_tstartqs = qs;
|
||||||
|
}
|
||||||
|
if (prefs.autoSearchOnWS && !m_disableAutosearch &&
|
||||||
|
!m_keystroke && m_tstartqs == qs) {
|
||||||
m_disableAutosearch = true;
|
m_disableAutosearch = true;
|
||||||
LOGDEB(("Autosearch: current: [%s]\n",
|
LOGINFO(("Autosearch: current: [%s]\n", qs2utf8s(qs).c_str()));
|
||||||
qs2utf8s(queryText->currentText()).c_str()));
|
|
||||||
#if 1
|
|
||||||
string s;
|
string s;
|
||||||
int cs = partialWord(s);
|
int cs = partialWord(s);
|
||||||
if (cs < 0) {
|
if (cs < 0) {
|
||||||
startSimpleSearch();
|
startSimpleSearch();
|
||||||
return;
|
} else if (!m_stroketimeout->isActive()) {
|
||||||
|
s = qs2utf8s(queryText->currentText());
|
||||||
|
s += "*";
|
||||||
|
startSimpleSearch(s, 20);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Query database for completions
|
|
||||||
QStringList lst;
|
|
||||||
const int maxcompsize = 40;
|
|
||||||
completionList(s, lst, maxcompsize);
|
|
||||||
if (lst.size() >= maxcompsize) {
|
|
||||||
LOGDEB(("Autosearch: completion list too big: %d\n",
|
|
||||||
lst.size()));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
s = qs2utf8s(queryText->currentText());
|
|
||||||
s += "*";
|
|
||||||
startSimpleSearch(s);
|
|
||||||
#else
|
|
||||||
startSimpleSearch();
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
m_keystroke = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void SSearch::searchTypeChanged(int typ)
|
void SSearch::searchTypeChanged(int typ)
|
||||||
@ -149,7 +153,9 @@ void SSearch::searchTypeChanged(int typ)
|
|||||||
|
|
||||||
void SSearch::startSimpleSearch()
|
void SSearch::startSimpleSearch()
|
||||||
{
|
{
|
||||||
if (queryText->currentText().length() == 0)
|
QString qs = queryText->currentText();
|
||||||
|
LOGINFO(("startSimpleSearch qs [%s]\n", qs2utf8s(qs).c_str()));
|
||||||
|
if (qs.length() == 0)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
string u8 = (const char *)queryText->currentText().toUtf8();
|
string u8 = (const char *)queryText->currentText().toUtf8();
|
||||||
@ -168,11 +174,13 @@ void SSearch::startSimpleSearch()
|
|||||||
// So do it by hand.
|
// So do it by hand.
|
||||||
QString txt = queryText->currentText();
|
QString txt = queryText->currentText();
|
||||||
int index = queryText->findText(txt);
|
int index = queryText->findText(txt);
|
||||||
if (index >= 0)
|
if (index > 0) {
|
||||||
queryText->removeItem(index);
|
queryText->removeItem(index);
|
||||||
|
}
|
||||||
queryText->insertItem(0, txt);
|
queryText->insertItem(0, txt);
|
||||||
queryText->setCurrentIndex(0);
|
queryText->setCurrentIndex(0);
|
||||||
m_disableAutosearch = true;
|
m_disableAutosearch = true;
|
||||||
|
m_stroketimeout->stop();
|
||||||
|
|
||||||
// Save the current state of the listbox list to the prefs (will
|
// Save the current state of the listbox list to the prefs (will
|
||||||
// go to disk)
|
// go to disk)
|
||||||
@ -182,9 +190,9 @@ void SSearch::startSimpleSearch()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool SSearch::startSimpleSearch(const string& u8)
|
bool SSearch::startSimpleSearch(const string& u8, int maxexp)
|
||||||
{
|
{
|
||||||
LOGDEB(("SSearch::startSimpleSearch(%s)\n", u8.c_str()));
|
LOGINFO(("SSearch::startSimpleSearch(%s)\n", u8.c_str()));
|
||||||
string stemlang = prefs.stemlang();
|
string stemlang = prefs.stemlang();
|
||||||
|
|
||||||
SSearchType tp = (SSearchType)searchTypCMB->currentIndex();
|
SSearchType tp = (SSearchType)searchTypCMB->currentIndex();
|
||||||
@ -230,7 +238,9 @@ bool SSearch::startSimpleSearch(const string& u8)
|
|||||||
sdata->maybeAddAutoPhrase(*rcldb,
|
sdata->maybeAddAutoPhrase(*rcldb,
|
||||||
prefs.ssearchAutoPhraseThreshPC / 100.0);
|
prefs.ssearchAutoPhraseThreshPC / 100.0);
|
||||||
}
|
}
|
||||||
|
if (maxexp != -1) {
|
||||||
|
sdata->setMaxExpand(maxexp);
|
||||||
|
}
|
||||||
RefCntr<Rcl::SearchData> rsdata(sdata);
|
RefCntr<Rcl::SearchData> rsdata(sdata);
|
||||||
emit startSearch(rsdata);
|
emit startSearch(rsdata);
|
||||||
return true;
|
return true;
|
||||||
@ -239,6 +249,7 @@ bool SSearch::startSimpleSearch(const string& u8)
|
|||||||
void SSearch::setSearchString(const QString& txt)
|
void SSearch::setSearchString(const QString& txt)
|
||||||
{
|
{
|
||||||
m_disableAutosearch = true;
|
m_disableAutosearch = true;
|
||||||
|
m_stroketimeout->stop();
|
||||||
queryText->setEditText(txt);
|
queryText->setEditText(txt);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -274,6 +285,7 @@ void SSearch::addTerm(QString term)
|
|||||||
text += QString::fromLatin1(" ") + term;
|
text += QString::fromLatin1(" ") + term;
|
||||||
queryText->setEditText(text);
|
queryText->setEditText(text);
|
||||||
m_disableAutosearch = true;
|
m_disableAutosearch = true;
|
||||||
|
m_stroketimeout->stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
void SSearch::onWordReplace(const QString& o, const QString& n)
|
void SSearch::onWordReplace(const QString& o, const QString& n)
|
||||||
@ -286,6 +298,7 @@ void SSearch::onWordReplace(const QString& o, const QString& n)
|
|||||||
txt.replace(exp, n);
|
txt.replace(exp, n);
|
||||||
queryText->setEditText(txt);
|
queryText->setEditText(txt);
|
||||||
m_disableAutosearch = true;
|
m_disableAutosearch = true;
|
||||||
|
m_stroketimeout->stop();
|
||||||
Qt::KeyboardModifiers mods = QApplication::keyboardModifiers ();
|
Qt::KeyboardModifiers mods = QApplication::keyboardModifiers ();
|
||||||
if (mods == Qt::NoModifier)
|
if (mods == Qt::NoModifier)
|
||||||
startSimpleSearch();
|
startSimpleSearch();
|
||||||
@ -334,6 +347,10 @@ int SSearch::completionList(string s, QStringList& lst, int max)
|
|||||||
void SSearch::completion()
|
void SSearch::completion()
|
||||||
{
|
{
|
||||||
LOGDEB(("SSearch::completion\n"));
|
LOGDEB(("SSearch::completion\n"));
|
||||||
|
|
||||||
|
m_disableAutosearch = true;
|
||||||
|
m_stroketimeout->stop();
|
||||||
|
|
||||||
if (!rcldb)
|
if (!rcldb)
|
||||||
return;
|
return;
|
||||||
if (searchTypCMB->currentIndex() == SST_FNM) {
|
if (searchTypCMB->currentIndex() == SST_FNM) {
|
||||||
@ -352,21 +369,19 @@ void SSearch::completion()
|
|||||||
|
|
||||||
// Query database for completions
|
// Query database for completions
|
||||||
QStringList lst;
|
QStringList lst;
|
||||||
if (completionList(s, lst, 100) <= 0) {
|
const int maxdpy = 80;
|
||||||
|
const int maxwalked = 10000;
|
||||||
|
if (completionList(s, lst, maxwalked) <= 0) {
|
||||||
QApplication::beep();
|
QApplication::beep();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (lst.size() == 100) {
|
if (lst.size() >= maxdpy) {
|
||||||
QMessageBox *warning = new QMessageBox;
|
LOGINFO(("TRUNCATING COMPLETION\n"));
|
||||||
warning->setWindowTitle(tr("Recoll"));
|
lst = lst.mid(0, maxdpy);
|
||||||
warning->setText(tr("Too many completions"));
|
lst.append("[...]");
|
||||||
warning->show();
|
|
||||||
QTimer::singleShot(500, warning, SLOT(close()));
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// If list from db is single word, insert it, else popup the listview
|
// If list from db is single word, insert it, else popup the listview
|
||||||
m_disableAutosearch = true;
|
|
||||||
if (lst.size() == 1) {
|
if (lst.size() == 1) {
|
||||||
QString txt = queryText->currentText();
|
QString txt = queryText->currentText();
|
||||||
txt.truncate(cs);
|
txt.truncate(cs);
|
||||||
@ -389,12 +404,16 @@ void SSearch::completion()
|
|||||||
|
|
||||||
void SSearch::completionTermChosen(const QString& text)
|
void SSearch::completionTermChosen(const QString& text)
|
||||||
{
|
{
|
||||||
m_chosenCompletion = text;
|
if (text != "[...]")
|
||||||
|
m_chosenCompletion = text;
|
||||||
|
else
|
||||||
|
m_chosenCompletion.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
void SSearch::wrapupCompletion()
|
void SSearch::wrapupCompletion()
|
||||||
{
|
{
|
||||||
LOGDEB(("SSearch::wrapupCompletion\n"));
|
LOGDEB(("SSearch::wrapupCompletion\n"));
|
||||||
|
|
||||||
queryText->clear();
|
queryText->clear();
|
||||||
queryText->addItems(prefs.ssearchHistory);
|
queryText->addItems(prefs.ssearchHistory);
|
||||||
if (!m_chosenCompletion.isEmpty()) {
|
if (!m_chosenCompletion.isEmpty()) {
|
||||||
@ -581,19 +600,29 @@ bool SSearch::eventFilter(QObject *target, QEvent *event)
|
|||||||
if (ke->key() == Qt::Key_Escape) {
|
if (ke->key() == Qt::Key_Escape) {
|
||||||
LOGDEB(("Escape\n"));
|
LOGDEB(("Escape\n"));
|
||||||
m_escape = true;
|
m_escape = true;
|
||||||
|
m_disableAutosearch = true;
|
||||||
|
m_stroketimeout->stop();
|
||||||
return true;
|
return true;
|
||||||
} else if (m_escape && ke->key() == Qt::Key_Space) {
|
} else if (m_escape && ke->key() == Qt::Key_Space) {
|
||||||
LOGDEB(("Escape space\n"));
|
LOGDEB(("Escape space\n"));
|
||||||
ke->accept();
|
ke->accept();
|
||||||
completion();
|
completion();
|
||||||
m_escape = false;
|
m_escape = false;
|
||||||
|
m_disableAutosearch = true;
|
||||||
|
m_stroketimeout->stop();
|
||||||
return true;
|
return true;
|
||||||
} else if (ke->key() == Qt::Key_Space) {
|
} else if (ke->key() == Qt::Key_Space) {
|
||||||
// if (prefs.autoSearchOnWS)
|
// if (prefs.autoSearchOnWS)
|
||||||
// startSimpleSearch();
|
// startSimpleSearch();
|
||||||
}
|
}
|
||||||
m_escape = false;
|
m_escape = false;
|
||||||
m_disableAutosearch = false;
|
m_keystroke = true;
|
||||||
|
if (prefs.autoSearchOnWS) {
|
||||||
|
m_disableAutosearch = false;
|
||||||
|
QString qs = queryText->currentText();
|
||||||
|
LOGINFO(("STARTING TIMER, qs [%s]\n", qs2utf8s(qs).c_str()));
|
||||||
|
m_stroketimeout->start(500);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -17,8 +17,11 @@
|
|||||||
#ifndef _SSEARCH_W_H_INCLUDED_
|
#ifndef _SSEARCH_W_H_INCLUDED_
|
||||||
#define _SSEARCH_W_H_INCLUDED_
|
#define _SSEARCH_W_H_INCLUDED_
|
||||||
|
|
||||||
#include <qvariant.h>
|
#include <QVariant>
|
||||||
#include <qwidget.h>
|
#include <QWidget>
|
||||||
|
|
||||||
|
class QTimer;
|
||||||
|
|
||||||
#include "recoll.h"
|
#include "recoll.h"
|
||||||
#include "searchdata.h"
|
#include "searchdata.h"
|
||||||
#include "refcntr.h"
|
#include "refcntr.h"
|
||||||
@ -53,20 +56,26 @@ public slots:
|
|||||||
virtual void onWordReplace(const QString&, const QString&);
|
virtual void onWordReplace(const QString&, const QString&);
|
||||||
virtual void completionTermChosen(const QString& text);
|
virtual void completionTermChosen(const QString& text);
|
||||||
virtual void wrapupCompletion();
|
virtual void wrapupCompletion();
|
||||||
|
virtual void timerDone();
|
||||||
signals:
|
signals:
|
||||||
void startSearch(RefCntr<Rcl::SearchData>);
|
void startSearch(RefCntr<Rcl::SearchData>);
|
||||||
void clearSearch();
|
void clearSearch();
|
||||||
private:
|
private:
|
||||||
bool m_escape;
|
bool m_escape;
|
||||||
|
|
||||||
bool m_displayingCompletions;
|
bool m_displayingCompletions;
|
||||||
QString m_chosenCompletion;
|
QString m_chosenCompletion;
|
||||||
QString m_savedEditText;
|
QString m_savedEditText;
|
||||||
unsigned int m_completedWordStart;
|
unsigned int m_completedWordStart;
|
||||||
|
|
||||||
bool m_disableAutosearch;
|
bool m_disableAutosearch;
|
||||||
|
QTimer *m_stroketimeout;
|
||||||
|
bool m_keystroke;
|
||||||
|
QString m_tstartqs;
|
||||||
|
|
||||||
int partialWord(string& s);
|
int partialWord(string& s);
|
||||||
int completionList(string s, QStringList& lst, int max = 100);
|
int completionList(string s, QStringList& lst, int max = 100);
|
||||||
bool startSimpleSearch(const string& q);
|
bool startSimpleSearch(const string& q, int maxexp = -1);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -85,7 +85,7 @@
|
|||||||
<bool>true</bool>
|
<bool>true</bool>
|
||||||
</property>
|
</property>
|
||||||
<property name="maxCount">
|
<property name="maxCount">
|
||||||
<number>30</number>
|
<number>200</number>
|
||||||
</property>
|
</property>
|
||||||
<property name="insertPolicy">
|
<property name="insertPolicy">
|
||||||
<enum>QComboBox::NoInsert</enum>
|
<enum>QComboBox::NoInsert</enum>
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user