diff --git a/resources/i18n/en.json b/resources/i18n/en.json index 870568e..7e95787 100644 --- a/resources/i18n/en.json +++ b/resources/i18n/en.json @@ -103,5 +103,6 @@ "wikispecies":"Wikispecies", "wikiversity":"Wikiversity", "wikivoyage":"Wikivoyage", - "wiktionary":"Wiktionary" + "wiktionary":"Wiktionary", + "fulltext-search":"Fulltext search" } diff --git a/src/searchbar.cpp b/src/searchbar.cpp index 9a9cf80..9c29d1c 100644 --- a/src/searchbar.cpp +++ b/src/searchbar.cpp @@ -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);}); } - diff --git a/src/searchbar.h b/src/searchbar.h index ff2f9de..1ed9773 100644 --- a/src/searchbar.h +++ b/src/searchbar.h @@ -35,9 +35,11 @@ protected: private: QStringListModel m_completionModel; QCompleter m_completer; - std::vector m_urlList; + QVector m_urlList; SearchButton m_button; QString m_title; + QString m_searchbarInput; + bool m_returnPressed = false; private slots: void updateCompletion(const QString& text);