mirror of
https://github.com/kiwix/kiwix-desktop.git
synced 2025-09-22 03:26:05 -04:00
Refactor SearchBar to QToolBar
SearchBar becomes QToolBar instead of QLineEdit. The previous LineEdit is now a child of SearchBar
This commit is contained in:
parent
ee3a2be2e8
commit
9f1c8ef497
@ -144,11 +144,11 @@ KiwixApp::~KiwixApp()
|
||||
void KiwixApp::newTab()
|
||||
{
|
||||
getTabWidget()->createNewTab(true, false);
|
||||
auto& searchBar = mp_mainWindow->getTopWidget()->getSearchBar();
|
||||
searchBar.setFocus(Qt::MouseFocusReason);
|
||||
searchBar.clear();
|
||||
searchBar.clearSuggestions();
|
||||
searchBar.hideSuggestions();
|
||||
auto& searchBarLineEdit = mp_mainWindow->getTopWidget()->getSearchBar().getLineEdit();
|
||||
searchBarLineEdit.setFocus(Qt::MouseFocusReason);
|
||||
searchBarLineEdit.clear();
|
||||
searchBarLineEdit.clearSuggestions();
|
||||
searchBarLineEdit.hideSuggestions();
|
||||
}
|
||||
|
||||
QString KiwixApp::findLibraryDirectory()
|
||||
|
@ -61,7 +61,7 @@ MainWindow::MainWindow(QWidget *parent) :
|
||||
});
|
||||
|
||||
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
|
||||
// but for now we have no better signal for it.
|
||||
|
@ -59,7 +59,7 @@ void SearchButton::on_buttonClicked()
|
||||
library->save();
|
||||
}
|
||||
|
||||
SearchBar::SearchBar(QWidget *parent) :
|
||||
SearchBarLineEdit::SearchBarLineEdit(QWidget *parent) :
|
||||
QLineEdit(parent),
|
||||
m_completer(&m_completionModel, this),
|
||||
m_button(this)
|
||||
@ -76,7 +76,7 @@ SearchBar::SearchBar(QWidget *parent) :
|
||||
m_completer.popup()->setStyleSheet(KiwixApp::instance()->parseStyleFromFile(":/css/popup.css"));
|
||||
|
||||
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,
|
||||
[=](const QString &text) {
|
||||
@ -103,19 +103,19 @@ SearchBar::SearchBar(QWidget *parent) :
|
||||
});
|
||||
}
|
||||
|
||||
void SearchBar::hideSuggestions()
|
||||
void SearchBarLineEdit::hideSuggestions()
|
||||
{
|
||||
m_completer.popup()->hide();
|
||||
}
|
||||
|
||||
void SearchBar::clearSuggestions()
|
||||
void SearchBarLineEdit::clearSuggestions()
|
||||
{
|
||||
QStringList empty;
|
||||
m_completionModel.setStringList(empty);
|
||||
m_urlList.clear();
|
||||
}
|
||||
|
||||
void SearchBar::on_currentTitleChanged(const QString& title)
|
||||
void SearchBarLineEdit::on_currentTitleChanged(const QString& title)
|
||||
{
|
||||
if (this->hasFocus()) {
|
||||
return;
|
||||
@ -129,7 +129,7 @@ void SearchBar::on_currentTitleChanged(const QString& title)
|
||||
m_title = title;
|
||||
}
|
||||
|
||||
void SearchBar::focusInEvent( QFocusEvent* event)
|
||||
void SearchBarLineEdit::focusInEvent( QFocusEvent* event)
|
||||
{
|
||||
setReadOnly(false);
|
||||
if (event->reason() == Qt::MouseFocusReason && text() == m_title) {
|
||||
@ -139,13 +139,13 @@ void SearchBar::focusInEvent( QFocusEvent* event)
|
||||
event->reason() == Qt::MouseFocusReason ||
|
||||
event->reason() == Qt::ShortcutFocusReason) {
|
||||
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);
|
||||
m_button.set_searchMode(true);
|
||||
}
|
||||
|
||||
void SearchBar::focusOutEvent(QFocusEvent* event)
|
||||
void SearchBarLineEdit::focusOutEvent(QFocusEvent* event)
|
||||
{
|
||||
setReadOnly(true);
|
||||
if (event->reason() == Qt::MouseFocusReason && text().isEmpty()) {
|
||||
@ -156,7 +156,7 @@ void SearchBar::focusOutEvent(QFocusEvent* event)
|
||||
return QLineEdit::focusOutEvent(event);
|
||||
}
|
||||
|
||||
void SearchBar::updateCompletion()
|
||||
void SearchBarLineEdit::updateCompletion()
|
||||
{
|
||||
mp_typingTimer->stop();
|
||||
clearSuggestions();
|
||||
@ -184,14 +184,14 @@ void SearchBar::updateCompletion()
|
||||
suggestionWorker->start();
|
||||
}
|
||||
|
||||
void SearchBar::openCompletion(const QModelIndex &index)
|
||||
void SearchBarLineEdit::openCompletion(const QModelIndex &index)
|
||||
{
|
||||
if (m_urlList.size() != 0) {
|
||||
openCompletion(index.data().toString(), index.row());
|
||||
}
|
||||
}
|
||||
|
||||
void SearchBar::openCompletion(const QString& text, int index)
|
||||
void SearchBarLineEdit::openCompletion(const QString& text, int index)
|
||||
{
|
||||
QUrl url;
|
||||
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);});
|
||||
}
|
||||
|
||||
SearchBar::SearchBar(QWidget *parent) :
|
||||
QToolBar(parent),
|
||||
m_searchBarLineEdit(this)
|
||||
{
|
||||
addWidget(&m_searchBarLineEdit);
|
||||
|
||||
connect(this, &SearchBar::currentTitleChanged, &m_searchBarLineEdit,
|
||||
&SearchBarLineEdit::on_currentTitleChanged);
|
||||
}
|
||||
|
@ -9,6 +9,7 @@
|
||||
#include <QUrl>
|
||||
#include <QTimer>
|
||||
#include <QThread>
|
||||
#include <QToolBar>
|
||||
|
||||
class SearchButton : public QPushButton {
|
||||
Q_OBJECT
|
||||
@ -23,11 +24,11 @@ protected:
|
||||
bool m_searchMode;
|
||||
};
|
||||
|
||||
class SearchBar : public QLineEdit
|
||||
class SearchBarLineEdit : public QLineEdit
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
SearchBar(QWidget *parent = nullptr);
|
||||
SearchBarLineEdit(QWidget *parent = nullptr);
|
||||
void hideSuggestions();
|
||||
|
||||
public slots:
|
||||
@ -54,4 +55,17 @@ private slots:
|
||||
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
|
||||
|
Loading…
x
Reference in New Issue
Block a user