mirror of
https://github.com/kiwix/kiwix-desktop.git
synced 2025-09-20 18:33:38 -04:00
Implement search suggestion.
This commit is contained in:
parent
c712a7a760
commit
ca309c0701
@ -39,7 +39,8 @@ SOURCES += \
|
||||
src/requestinterceptor.cpp \
|
||||
src/urlschemehandler.cpp \
|
||||
src/tabwidget.cpp \
|
||||
src/webview.cpp
|
||||
src/webview.cpp \
|
||||
src/searchbar.cpp
|
||||
|
||||
HEADERS += \
|
||||
src/mainwindow.h \
|
||||
@ -51,7 +52,8 @@ HEADERS += \
|
||||
src/requestinterceptor.h \
|
||||
src/urlschemehandler.h \
|
||||
src/tabwidget.h \
|
||||
src/webview.h
|
||||
src/webview.h \
|
||||
src/searchbar.h
|
||||
|
||||
FORMS += \
|
||||
ui/mainwindow.ui
|
||||
|
87
src/searchbar.cpp
Normal file
87
src/searchbar.cpp
Normal file
@ -0,0 +1,87 @@
|
||||
#include "searchbar.h"
|
||||
|
||||
#include <QCompleter>
|
||||
#include <QTimer>
|
||||
|
||||
#include "kiwixapp.h"
|
||||
|
||||
SearchBar::SearchBar(QWidget *parent) :
|
||||
QLineEdit(parent),
|
||||
m_completer(&m_completionModel, this),
|
||||
m_icon(":icons/search.svg")
|
||||
{
|
||||
setTextMargins(37, 1, 1, 1);
|
||||
setPlaceholderText("Search");
|
||||
setClearButtonEnabled(true);
|
||||
m_completer.setCompletionMode(QCompleter::UnfilteredPopupCompletion);
|
||||
setCompleter(&m_completer);
|
||||
connect(this, &QLineEdit::textEdited, this, &SearchBar::updateCompletion);
|
||||
#if 0 //The `activated` signal seems to not be emitted if user navigate in the page.
|
||||
// Something is broken here .. :/
|
||||
connect(&m_completer, QOverload<const QModelIndex &>::of(&QCompleter::activated),
|
||||
this, &SearchBar::openCompletion);
|
||||
#else
|
||||
connect(this, &QLineEdit::returnPressed, this, &SearchBar::openTitle);
|
||||
#endif
|
||||
}
|
||||
|
||||
void SearchBar::paintEvent(QPaintEvent *event)
|
||||
{
|
||||
QLineEdit::paintEvent(event);
|
||||
QPainter painter(this);
|
||||
QPixmap pxm = m_icon.pixmap(height() - 6, height() - 6);
|
||||
painter.drawPixmap(3, 3, pxm);
|
||||
}
|
||||
|
||||
void SearchBar::updateCompletion(const QString &text)
|
||||
{
|
||||
QStringList wordList;
|
||||
m_urlList.clear();
|
||||
auto tabWidget = KiwixApp::instance()->getTabWidget();
|
||||
auto qurl = tabWidget->currentWidget()->url();
|
||||
m_currentHost = qurl.host();
|
||||
auto reader = KiwixApp::instance()->getLibrary()->getReader(m_currentHost);
|
||||
if ( reader == nullptr) {
|
||||
m_completionModel.setStringList(wordList);
|
||||
return;
|
||||
}
|
||||
reader->searchSuggestionsSmart(text.toStdString(), 15);
|
||||
std::string title, url;
|
||||
while (reader->getNextSuggestion(title, url)) {
|
||||
wordList << QString::fromStdString(title);
|
||||
m_urlList.push_back(url);
|
||||
}
|
||||
m_completionModel.setStringList(wordList);
|
||||
}
|
||||
|
||||
void SearchBar::openTitle()
|
||||
{
|
||||
QString title = text();
|
||||
clear();
|
||||
auto tabWidget = KiwixApp::instance()->getTabWidget();
|
||||
auto qurl = tabWidget->currentWidget()->url();
|
||||
auto reader = KiwixApp::instance()->getLibrary()->getReader(qurl.host());
|
||||
if ( reader == nullptr) {
|
||||
return;
|
||||
}
|
||||
std::string url;
|
||||
if (reader->getPageUrlFromTitle(title.toStdString(), url))
|
||||
{
|
||||
QUrl qurl_;
|
||||
qurl_.setScheme("zim");
|
||||
qurl_.setHost(qurl.host());
|
||||
qurl_.setPath("/" + QString::fromStdString(url));
|
||||
QTimer::singleShot(0, [=](){KiwixApp::instance()->openUrl(qurl_, true);});
|
||||
}
|
||||
}
|
||||
|
||||
void SearchBar::openCompletion(const QModelIndex &index)
|
||||
{
|
||||
auto url = m_urlList.at(index.row());
|
||||
QUrl qurl;
|
||||
qurl.setScheme("zim");
|
||||
qurl.setHost(m_currentHost);
|
||||
qurl.setPath(QString::fromStdString(url));
|
||||
QTimer::singleShot(0, [=](){KiwixApp::instance()->openUrl(qurl, true);});
|
||||
}
|
||||
|
31
src/searchbar.h
Normal file
31
src/searchbar.h
Normal file
@ -0,0 +1,31 @@
|
||||
#ifndef SEARCHBAR_H
|
||||
#define SEARCHBAR_H
|
||||
|
||||
#include <QLineEdit>
|
||||
#include <QStringListModel>
|
||||
#include <QCompleter>
|
||||
#include <QIcon>
|
||||
|
||||
class SearchBar : public QLineEdit
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
SearchBar(QWidget *parent = nullptr);
|
||||
|
||||
protected:
|
||||
virtual void paintEvent(QPaintEvent *event);
|
||||
|
||||
private:
|
||||
QStringListModel m_completionModel;
|
||||
QCompleter m_completer;
|
||||
std::vector<std::string> m_urlList;
|
||||
QString m_currentHost;
|
||||
QIcon m_icon;
|
||||
|
||||
private slots:
|
||||
void updateCompletion(const QString& text);
|
||||
void openCompletion(const QModelIndex& index);
|
||||
void openTitle();
|
||||
};
|
||||
|
||||
#endif // SEARCHBAR_H
|
@ -5,6 +5,8 @@
|
||||
#include <QLineEdit>
|
||||
#include <QWebEnginePage>
|
||||
|
||||
#include "searchbar.h"
|
||||
|
||||
class TopWidget : public QToolBar
|
||||
{
|
||||
Q_OBJECT
|
||||
@ -17,7 +19,7 @@ protected:
|
||||
void mouseMoveEvent(QMouseEvent *event);
|
||||
|
||||
private:
|
||||
QLineEdit m_searchEntry;
|
||||
SearchBar m_searchEntry;
|
||||
QAction* mp_historyBackAction;
|
||||
QAction* mp_historyForwardAction;
|
||||
QAction* mp_fullScreenAction;
|
||||
|
Loading…
x
Reference in New Issue
Block a user