Merge pull request #408 from kiwix/search-ux-improvement

This commit is contained in:
Matthieu Gautier 2020-04-17 17:25:49 +02:00 committed by GitHub
commit 55e290630f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 6 deletions

View File

@ -103,5 +103,6 @@
"wikispecies":"Wikispecies",
"wikiversity":"Wikiversity",
"wikivoyage":"Wikivoyage",
"wiktionary":"Wiktionary"
"wiktionary":"Wiktionary",
"fulltext-search":"Fulltext search"
}

View File

@ -65,6 +65,7 @@ SearchBar::SearchBar(QWidget *parent) :
setPlaceholderText(gt("search"));
m_completer.setCompletionMode(QCompleter::UnfilteredPopupCompletion);
m_completer.setCaseSensitivity(Qt::CaseInsensitive);
m_completer.setMaxVisibleItems(16);
setCompleter(&m_completer);
QFile styleFile(":/css/popup.css");
@ -77,6 +78,20 @@ SearchBar::SearchBar(QWidget *parent) :
connect(this, &QLineEdit::textEdited, this, &SearchBar::updateCompletion);
connect(KiwixApp::instance(), &KiwixApp::currentTitleChanged,
this, &SearchBar::on_currentTitleChanged);
connect(this, &QLineEdit::textEdited, this,
[=](const QString &text) {
m_searchbarInput = text;
m_returnPressed = false;
});
connect(this, &QLineEdit::textChanged, this,
[=](const QString &text) {
if (m_returnPressed) {
this->setText(m_searchbarInput);
}
});
connect(this, &QLineEdit::returnPressed, this, [=]() {
m_returnPressed = true;
});
}
void SearchBar::on_currentTitleChanged(const QString& title)
@ -134,7 +149,7 @@ void SearchBar::updateCompletion(const QString &text)
QUrl url;
url.setScheme("zim");
if (reader) {
url.setHost(qurl.host());
url.setHost(currentZimId + ".zim");
reader->searchSuggestionsSmart(text.toStdString(), 15);
std::string title, path;
while (reader->getNextSuggestion(title, path)) {
@ -158,14 +173,18 @@ void SearchBar::updateCompletion(const QString &text)
}
query.addQueryItem("pattern", text);
url.setQuery(query);
wordList << QString("Search for ") + text;
wordList << text + " (" + gt("fulltext-search") + ")";
m_urlList.push_back(url);
m_completionModel.setStringList(wordList);
}
void SearchBar::openCompletion(const QModelIndex &index)
{
auto url = m_urlList.at(index.row());
QUrl url;
if (this->text().compare(index.data().toString(), Qt::CaseInsensitive) == 0) {
url = m_urlList.at(index.row());
} else {
url = m_urlList.last();
}
QTimer::singleShot(0, [=](){KiwixApp::instance()->openUrl(url, false);});
}

View File

@ -35,9 +35,11 @@ protected:
private:
QStringListModel m_completionModel;
QCompleter m_completer;
std::vector<QUrl> m_urlList;
QVector<QUrl> m_urlList;
SearchButton m_button;
QString m_title;
QString m_searchbarInput;
bool m_returnPressed = false;
private slots:
void updateCompletion(const QString& text);