mirror of
https://github.com/kiwix/kiwix-desktop.git
synced 2025-08-03 12:46:29 -04:00
Fix right-to-left layout in SearchBar
Use QLineEdit::addAction() to place a button inside the widget. It will automatically positioned on the left, or on the right, depending on the current language. Fixes: #594
This commit is contained in:
parent
adad44aced
commit
f2157f1335
@ -32,23 +32,14 @@ QToolButton {
|
||||
|
||||
SearchBar {
|
||||
background-color: white;
|
||||
padding: 2px 2px 2px 40px;
|
||||
padding: 2px;
|
||||
max-height: 38px;
|
||||
margin: 2px 5px;
|
||||
color: #666;
|
||||
font-size: 16px;
|
||||
border: 1px solid #ccc;
|
||||
border-radius: 3px;
|
||||
}
|
||||
|
||||
SearchBar > QPushButton {
|
||||
margin: 8px 0px 8px 12px;
|
||||
padding: 0;
|
||||
border: 0 solid #fff;
|
||||
background-color: white;
|
||||
height: 32px;
|
||||
width: 32px;
|
||||
}
|
||||
|
||||
TopWidget QToolButton:pressed,
|
||||
TopWidget QToolButton::hover {
|
||||
|
@ -2,37 +2,29 @@
|
||||
|
||||
#include <QCompleter>
|
||||
#include <QFocusEvent>
|
||||
#include <QCursor>
|
||||
|
||||
#include "kiwixapp.h"
|
||||
#include "suggestionlistworker.h"
|
||||
|
||||
SearchButton::SearchButton(QWidget *parent) :
|
||||
QPushButton(parent),
|
||||
m_searchMode(true)
|
||||
{
|
||||
setFlat(true);
|
||||
setIcon(QIcon(":/icons/search.svg"));
|
||||
connect(this, &QPushButton::clicked, this, &SearchButton::on_buttonClicked);
|
||||
}
|
||||
|
||||
void SearchButton::set_searchMode(bool searchMode)
|
||||
|
||||
void SearchBar::set_searchMode(bool searchMode)
|
||||
{
|
||||
m_searchMode = searchMode;
|
||||
if (m_searchMode) {
|
||||
setIcon(QIcon(":/icons/search.svg"));
|
||||
setIconSize(QSize(27, 27));
|
||||
m_action->setIcon(QIcon(":/icons/search.svg"));
|
||||
} else {
|
||||
auto kiwixApp = KiwixApp::instance();
|
||||
if (kiwixApp->isCurrentArticleBookmarked()) {
|
||||
setIcon(QIcon(":/icons/reading-list-active.svg"));
|
||||
m_action->setIcon(QIcon(":/icons/reading-list-active.svg"));
|
||||
} else {
|
||||
setIcon(QIcon(":/icons/reading-list.svg"));
|
||||
m_action->setIcon(QIcon(":/icons/reading-list.svg"));
|
||||
}
|
||||
setIconSize(QSize(25, 25));
|
||||
}
|
||||
}
|
||||
|
||||
void SearchButton::on_buttonClicked()
|
||||
void SearchBar::on_action_triggered()
|
||||
{
|
||||
if (m_searchMode)
|
||||
return;
|
||||
@ -60,7 +52,7 @@ void SearchButton::on_buttonClicked()
|
||||
SearchBar::SearchBar(QWidget *parent) :
|
||||
QLineEdit(parent),
|
||||
m_completer(&m_completionModel, this),
|
||||
m_button(this)
|
||||
m_action(new QAction(this))
|
||||
{
|
||||
mp_typingTimer = new QTimer(this);
|
||||
mp_typingTimer->setSingleShot(true);
|
||||
@ -71,6 +63,11 @@ SearchBar::SearchBar(QWidget *parent) :
|
||||
m_completer.setMaxVisibleItems(16);
|
||||
setCompleter(&m_completer);
|
||||
|
||||
connect(m_action, &QAction::triggered,
|
||||
this, &SearchBar::on_action_triggered);
|
||||
addAction(m_action, QLineEdit::LeadingPosition);
|
||||
set_searchMode(true);
|
||||
|
||||
QFile styleFile(":/css/popup.css");
|
||||
styleFile.open(QIODevice::ReadOnly);
|
||||
auto byteContent = styleFile.readAll();
|
||||
@ -121,30 +118,41 @@ void SearchBar::on_currentTitleChanged(const QString& title)
|
||||
} else {
|
||||
setText("");
|
||||
}
|
||||
m_button.set_searchMode(title.isEmpty());
|
||||
set_searchMode(title.isEmpty());
|
||||
m_title = title;
|
||||
}
|
||||
|
||||
void SearchBar::focusInEvent( QFocusEvent* event)
|
||||
{
|
||||
if (event->reason() == Qt::MouseFocusReason) {
|
||||
if (QApplication::widgetAt(QCursor::pos()) != this) {
|
||||
// drop focus, mouse clicked on one of inner widget (m_action)
|
||||
clearFocus();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
setReadOnly(false);
|
||||
if (event->reason() == Qt::MouseFocusReason && text() == m_title) {
|
||||
clear();
|
||||
}
|
||||
if (event->reason() == Qt::ActiveWindowFocusReason ||
|
||||
event->reason() == Qt::MouseFocusReason) {
|
||||
/* need to connect every time because QLineEdit::focusOutEvent does:
|
||||
* QObject::disconnect(d->control->completer(), 0, this, 0);
|
||||
*/
|
||||
connect(&m_completer, QOverload<const QModelIndex &>::of(&QCompleter::activated),
|
||||
this, QOverload<const QModelIndex &>::of(&SearchBar::openCompletion));
|
||||
}
|
||||
QLineEdit::focusInEvent(event);
|
||||
m_button.set_searchMode(true);
|
||||
set_searchMode(true);
|
||||
}
|
||||
|
||||
void SearchBar::focusOutEvent(QFocusEvent* event)
|
||||
{
|
||||
setReadOnly(true);
|
||||
if (event->reason() == Qt::MouseFocusReason && text().isEmpty()) {
|
||||
m_button.set_searchMode(false);
|
||||
set_searchMode(false);
|
||||
setText(m_title);
|
||||
}
|
||||
deselect();
|
||||
|
@ -5,23 +5,10 @@
|
||||
#include <QStringListModel>
|
||||
#include <QCompleter>
|
||||
#include <QIcon>
|
||||
#include <QPushButton>
|
||||
#include <QUrl>
|
||||
#include <QTimer>
|
||||
#include <QThread>
|
||||
|
||||
class SearchButton : public QPushButton {
|
||||
Q_OBJECT
|
||||
public:
|
||||
SearchButton(QWidget *parent = nullptr);
|
||||
|
||||
public slots:
|
||||
void set_searchMode(bool searchMode);
|
||||
void on_buttonClicked();
|
||||
|
||||
protected:
|
||||
bool m_searchMode;
|
||||
};
|
||||
|
||||
class SearchBar : public QLineEdit
|
||||
{
|
||||
@ -41,17 +28,20 @@ private:
|
||||
QStringListModel m_completionModel;
|
||||
QCompleter m_completer;
|
||||
QVector<QUrl> m_urlList;
|
||||
SearchButton m_button;
|
||||
QAction *m_action;
|
||||
QString m_title;
|
||||
QString m_searchbarInput;
|
||||
bool m_returnPressed = false;
|
||||
QTimer* mp_typingTimer;
|
||||
int m_token;
|
||||
void set_searchMode(bool searchMode);
|
||||
bool m_searchMode;
|
||||
|
||||
private slots:
|
||||
void updateCompletion();
|
||||
void openCompletion(const QModelIndex& index);
|
||||
void openCompletion(const QString& text, int index);
|
||||
void on_action_triggered();
|
||||
};
|
||||
|
||||
#endif // SEARCHBAR_H
|
||||
|
Loading…
x
Reference in New Issue
Block a user