Refactor SearchBar to QToolBar

SearchBar becomes QToolBar instead of QLineEdit. The previous LineEdit is now a child of SearchBar
This commit is contained in:
ShaopengLin 2024-05-07 12:30:09 -04:00 committed by Kelson
parent ee3a2be2e8
commit 9f1c8ef497
4 changed files with 43 additions and 19 deletions

View File

@ -144,11 +144,11 @@ KiwixApp::~KiwixApp()
void KiwixApp::newTab() void KiwixApp::newTab()
{ {
getTabWidget()->createNewTab(true, false); getTabWidget()->createNewTab(true, false);
auto& searchBar = mp_mainWindow->getTopWidget()->getSearchBar(); auto& searchBarLineEdit = mp_mainWindow->getTopWidget()->getSearchBar().getLineEdit();
searchBar.setFocus(Qt::MouseFocusReason); searchBarLineEdit.setFocus(Qt::MouseFocusReason);
searchBar.clear(); searchBarLineEdit.clear();
searchBar.clearSuggestions(); searchBarLineEdit.clearSuggestions();
searchBar.hideSuggestions(); searchBarLineEdit.hideSuggestions();
} }
QString KiwixApp::findLibraryDirectory() QString KiwixApp::findLibraryDirectory()

View File

@ -61,7 +61,7 @@ MainWindow::MainWindow(QWidget *parent) :
}); });
connect(mp_ui->tabBar, &TabBar::currentTitleChanged, connect(mp_ui->tabBar, &TabBar::currentTitleChanged,
&(mp_ui->mainToolBar->getSearchBar()), &SearchBar::on_currentTitleChanged); &(mp_ui->mainToolBar->getSearchBar()), &SearchBar::currentTitleChanged);
// This signal emited more often than the history really updated // This signal emited more often than the history really updated
// but for now we have no better signal for it. // but for now we have no better signal for it.

View File

@ -59,7 +59,7 @@ void SearchButton::on_buttonClicked()
library->save(); library->save();
} }
SearchBar::SearchBar(QWidget *parent) : SearchBarLineEdit::SearchBarLineEdit(QWidget *parent) :
QLineEdit(parent), QLineEdit(parent),
m_completer(&m_completionModel, this), m_completer(&m_completionModel, this),
m_button(this) m_button(this)
@ -76,7 +76,7 @@ SearchBar::SearchBar(QWidget *parent) :
m_completer.popup()->setStyleSheet(KiwixApp::instance()->parseStyleFromFile(":/css/popup.css")); m_completer.popup()->setStyleSheet(KiwixApp::instance()->parseStyleFromFile(":/css/popup.css"));
qRegisterMetaType<QVector<QUrl>>("QVector<QUrl>"); qRegisterMetaType<QVector<QUrl>>("QVector<QUrl>");
connect(mp_typingTimer, &QTimer::timeout, this, &SearchBar::updateCompletion); connect(mp_typingTimer, &QTimer::timeout, this, &SearchBarLineEdit::updateCompletion);
connect(this, &QLineEdit::textEdited, this, connect(this, &QLineEdit::textEdited, this,
[=](const QString &text) { [=](const QString &text) {
@ -103,19 +103,19 @@ SearchBar::SearchBar(QWidget *parent) :
}); });
} }
void SearchBar::hideSuggestions() void SearchBarLineEdit::hideSuggestions()
{ {
m_completer.popup()->hide(); m_completer.popup()->hide();
} }
void SearchBar::clearSuggestions() void SearchBarLineEdit::clearSuggestions()
{ {
QStringList empty; QStringList empty;
m_completionModel.setStringList(empty); m_completionModel.setStringList(empty);
m_urlList.clear(); m_urlList.clear();
} }
void SearchBar::on_currentTitleChanged(const QString& title) void SearchBarLineEdit::on_currentTitleChanged(const QString& title)
{ {
if (this->hasFocus()) { if (this->hasFocus()) {
return; return;
@ -129,7 +129,7 @@ void SearchBar::on_currentTitleChanged(const QString& title)
m_title = title; m_title = title;
} }
void SearchBar::focusInEvent( QFocusEvent* event) void SearchBarLineEdit::focusInEvent( QFocusEvent* event)
{ {
setReadOnly(false); setReadOnly(false);
if (event->reason() == Qt::MouseFocusReason && text() == m_title) { if (event->reason() == Qt::MouseFocusReason && text() == m_title) {
@ -139,13 +139,13 @@ void SearchBar::focusInEvent( QFocusEvent* event)
event->reason() == Qt::MouseFocusReason || event->reason() == Qt::MouseFocusReason ||
event->reason() == Qt::ShortcutFocusReason) { event->reason() == Qt::ShortcutFocusReason) {
connect(&m_completer, QOverload<const QModelIndex &>::of(&QCompleter::activated), connect(&m_completer, QOverload<const QModelIndex &>::of(&QCompleter::activated),
this, QOverload<const QModelIndex &>::of(&SearchBar::openCompletion)); this, QOverload<const QModelIndex &>::of(&SearchBarLineEdit::openCompletion));
} }
QLineEdit::focusInEvent(event); QLineEdit::focusInEvent(event);
m_button.set_searchMode(true); m_button.set_searchMode(true);
} }
void SearchBar::focusOutEvent(QFocusEvent* event) void SearchBarLineEdit::focusOutEvent(QFocusEvent* event)
{ {
setReadOnly(true); setReadOnly(true);
if (event->reason() == Qt::MouseFocusReason && text().isEmpty()) { if (event->reason() == Qt::MouseFocusReason && text().isEmpty()) {
@ -156,7 +156,7 @@ void SearchBar::focusOutEvent(QFocusEvent* event)
return QLineEdit::focusOutEvent(event); return QLineEdit::focusOutEvent(event);
} }
void SearchBar::updateCompletion() void SearchBarLineEdit::updateCompletion()
{ {
mp_typingTimer->stop(); mp_typingTimer->stop();
clearSuggestions(); clearSuggestions();
@ -184,14 +184,14 @@ void SearchBar::updateCompletion()
suggestionWorker->start(); suggestionWorker->start();
} }
void SearchBar::openCompletion(const QModelIndex &index) void SearchBarLineEdit::openCompletion(const QModelIndex &index)
{ {
if (m_urlList.size() != 0) { if (m_urlList.size() != 0) {
openCompletion(index.data().toString(), index.row()); openCompletion(index.data().toString(), index.row());
} }
} }
void SearchBar::openCompletion(const QString& text, int index) void SearchBarLineEdit::openCompletion(const QString& text, int index)
{ {
QUrl url; QUrl url;
if (this->text().compare(text, Qt::CaseInsensitive) == 0) { if (this->text().compare(text, Qt::CaseInsensitive) == 0) {
@ -201,3 +201,13 @@ void SearchBar::openCompletion(const QString& text, int index)
} }
QTimer::singleShot(0, [=](){KiwixApp::instance()->openUrl(url, false);}); QTimer::singleShot(0, [=](){KiwixApp::instance()->openUrl(url, false);});
} }
SearchBar::SearchBar(QWidget *parent) :
QToolBar(parent),
m_searchBarLineEdit(this)
{
addWidget(&m_searchBarLineEdit);
connect(this, &SearchBar::currentTitleChanged, &m_searchBarLineEdit,
&SearchBarLineEdit::on_currentTitleChanged);
}

View File

@ -9,6 +9,7 @@
#include <QUrl> #include <QUrl>
#include <QTimer> #include <QTimer>
#include <QThread> #include <QThread>
#include <QToolBar>
class SearchButton : public QPushButton { class SearchButton : public QPushButton {
Q_OBJECT Q_OBJECT
@ -23,11 +24,11 @@ protected:
bool m_searchMode; bool m_searchMode;
}; };
class SearchBar : public QLineEdit class SearchBarLineEdit : public QLineEdit
{ {
Q_OBJECT Q_OBJECT
public: public:
SearchBar(QWidget *parent = nullptr); SearchBarLineEdit(QWidget *parent = nullptr);
void hideSuggestions(); void hideSuggestions();
public slots: public slots:
@ -54,4 +55,17 @@ private slots:
void openCompletion(const QString& text, int index); void openCompletion(const QString& text, int index);
}; };
class SearchBar : public QToolBar {
Q_OBJECT
public:
SearchBar(QWidget *parent = nullptr);
SearchBarLineEdit& getLineEdit() { return m_searchBarLineEdit; };
signals:
void currentTitleChanged(const QString &title);
private:
SearchBarLineEdit m_searchBarLineEdit;
};
#endif // SEARCHBAR_H #endif // SEARCHBAR_H