core,registry,ui: Make fuzzy search optional

This commit is contained in:
Oleg Shparber 2017-04-26 00:34:26 -04:00
parent 42e79a66ef
commit 7b1223b03d
9 changed files with 117 additions and 10 deletions

View File

@ -192,6 +192,7 @@ void Application::checkForUpdates(bool quiet)
void Application::applySettings()
{
m_docsetRegistry->setStoragePath(m_settings->docsetPath);
m_docsetRegistry->setFuzzySearchEnabled(m_settings->fuzzySearchEnabled);
// HTTP Proxy Settings
switch (m_settings->proxyType) {

View File

@ -35,6 +35,7 @@ namespace {
const char GroupContent[] = "content";
const char GroupDocsets[] = "docsets";
const char GroupGlobalShortcuts[] = "global_shortcuts";
const char GroupSearch[] = "search";
const char GroupTabs[] = "tabs";
const char GroupInternal[] = "internal";
const char GroupState[] = "state";
@ -75,6 +76,10 @@ void Settings::load()
openNewTabAfterActive = settings->value(QStringLiteral("open_new_tab_after_active"), false).toBool();
settings->endGroup();
settings->beginGroup(GroupSearch);
fuzzySearchEnabled = settings->value(QStringLiteral("fuzzy_search_enabled"), false).toBool();
settings->endGroup();
settings->beginGroup(GroupContent);
minimumFontSize = settings->value(QStringLiteral("minimum_font_size"),
QWebSettings::globalSettings()->fontSize(QWebSettings::MinimumFontSize)).toInt();
@ -142,6 +147,10 @@ void Settings::save()
settings->setValue(QStringLiteral("open_new_tab_after_active"), openNewTabAfterActive);
settings->endGroup();
settings->beginGroup(GroupSearch);
settings->setValue(QStringLiteral("fuzzy_search_enabled"), fuzzySearchEnabled);
settings->endGroup();
settings->beginGroup(GroupContent);
settings->setValue(QStringLiteral("minimum_font_size"), minimumFontSize);
settings->setValue(QStringLiteral("dark_mode"), darkModeEnabled);

View File

@ -56,6 +56,9 @@ public:
// Tabs Behavior
bool openNewTabAfterActive;
// Search
bool fuzzySearchEnabled;
// Content
int minimumFontSize;
bool darkModeEnabled;

View File

@ -260,15 +260,29 @@ QList<SearchResult> Docset::search(const QString &query, const CancellationToken
{
QString sql;
if (m_type == Docset::Type::Dash) {
sql = QStringLiteral("SELECT name, type, path, '', zealScore('%1', name) as score"
" FROM searchIndex"
" WHERE score > 0"
" ORDER BY score DESC");
if (m_fuzzySearchEnabled) {
sql = QStringLiteral("SELECT name, type, path, '', zealScore('%1', name) as score"
" FROM searchIndex"
" WHERE score > 0"
" ORDER BY score DESC");
} else {
sql = QStringLiteral("SELECT name, type, path, ''"
" FROM searchIndex"
" WHERE (name LIKE '%%1%' ESCAPE '\\')"
" ORDER BY name COLLATE NOCASE");
}
} else {
sql = QStringLiteral("SELECT name, type, path, fragment, zealScore('%1', name) as score"
" FROM searchIndex"
" WHERE score > 0"
" ORDER BY score DESC");
if (m_fuzzySearchEnabled) {
sql = QStringLiteral("SELECT name, type, path, fragment, zealScore('%1', name) as score"
" FROM searchIndex"
" WHERE score > 0"
" ORDER BY score DESC");
} else {
sql = QStringLiteral("SELECT name, type, path, fragment"
" FROM searchIndex"
" WHERE (name LIKE '%%1%' ESCAPE '\\')"
" ORDER BY name COLLATE NOCASE");
}
}
// Limit for very short queries.
@ -643,6 +657,16 @@ QString Docset::parseSymbolType(const QString &str)
return aliases.value(str, str);
}
bool Docset::isFuzzySearchEnabled() const
{
return m_fuzzySearchEnabled;
}
void Docset::setFuzzySearchEnabled(bool enabled)
{
m_fuzzySearchEnabled = enabled;
}
/**
* \brief Returns score based on a substring position in a string.
* \param str Original string.

View File

@ -75,6 +75,9 @@ public:
// FIXME: This is an ugly workaround before we have a proper docset sources implementation
bool hasUpdate = false;
bool isFuzzySearchEnabled() const;
void setFuzzySearchEnabled(bool enabled);
private:
enum class Type {
Invalid,
@ -107,6 +110,7 @@ private:
QMap<QString, int> m_symbolCounts;
mutable QMap<QString, QMap<QString, QUrl>> m_symbols;
Util::SQLiteDatabase *m_db = nullptr;
bool m_fuzzySearchEnabled = false;
};
} // namespace Registry

View File

@ -80,6 +80,24 @@ void DocsetRegistry::setStoragePath(const QString &path)
addDocsetsFromFolder(path);
}
bool DocsetRegistry::isFuzzySearchEnabled() const
{
return m_fuzzySearchEnabled;
}
void DocsetRegistry::setFuzzySearchEnabled(bool enabled)
{
if (enabled == m_fuzzySearchEnabled) {
return;
}
m_fuzzySearchEnabled = enabled;
for (Docset *docset : m_docsets) {
docset->setFuzzySearchEnabled(enabled);
}
}
int DocsetRegistry::count() const
{
return m_docsets.count();
@ -134,10 +152,12 @@ void DocsetRegistry::addDocset(const QString &path)
return;
}
const QString name = docset->name();
docset->setFuzzySearchEnabled(m_fuzzySearchEnabled);
if (m_docsets.contains(name))
const QString name = docset->name();
if (m_docsets.contains(name)) {
remove(name);
}
m_docsets[name] = docset;
emit docsetAdded(name);

View File

@ -47,6 +47,9 @@ public:
QString storagePath() const;
void setStoragePath(const QString &path);
bool isFuzzySearchEnabled() const;
void setFuzzySearchEnabled(bool enabled);
int count() const;
bool contains(const QString &name) const;
QStringList names() const;
@ -75,6 +78,7 @@ private:
void addDocsetsFromFolder(const QString &path);
QString m_storagePath;
bool m_fuzzySearchEnabled = false;
QThread *m_thread = nullptr;
QMap<QString, Docset *> m_docsets;

View File

@ -102,6 +102,9 @@ void SettingsDialog::loadSettings()
// Tabs Tab
ui->openNewTabAfterActive->setChecked(settings->openNewTabAfterActive);
// Search Tab
ui->fuzzySearchCheckBox->setChecked(settings->fuzzySearchEnabled);
// Content Tab
ui->minimumFontSizeSpinBox->setValue(settings->minimumFontSize);
ui->darkModeCheckBox->setChecked(settings->darkModeEnabled);
@ -147,6 +150,9 @@ void SettingsDialog::saveSettings()
// Tabs Tab
settings->openNewTabAfterActive = ui->openNewTabAfterActive->isChecked();
// Search Tab
settings->fuzzySearchEnabled = ui->fuzzySearchCheckBox->isChecked();
// Content Tab
settings->minimumFontSize = ui->minimumFontSizeSpinBox->text().toInt();
settings->darkModeEnabled = ui->darkModeCheckBox->isChecked();

View File

@ -186,6 +186,42 @@
</item>
</layout>
</widget>
<widget class="QWidget" name="searchTab">
<attribute name="title">
<string>Search</string>
</attribute>
<layout class="QVBoxLayout" name="verticalLayout_4">
<item>
<widget class="QGroupBox" name="groupBox_5">
<property name="title">
<string>Local search</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_11">
<item>
<widget class="QCheckBox" name="fuzzySearchCheckBox">
<property name="text">
<string>Use fuzzy search (experimental)</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<spacer name="verticalSpacer_4">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
<widget class="QWidget" name="contentTab">
<attribute name="title">
<string>Content</string>