2018-02-27 18:06:05 +01:00
|
|
|
/*
|
|
|
|
Copyright (C) 2011 by Mike McQuaid
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2018-07-01 22:26:46 +02:00
|
|
|
#include "qsearchfield.h"
|
|
|
|
|
2020-02-09 02:29:35 +01:00
|
|
|
#include <QtGlobal>
|
|
|
|
#include <QObject>
|
|
|
|
#include <QWidget>
|
2018-09-04 22:55:38 +02:00
|
|
|
#include <QApplication>
|
2020-02-09 02:29:35 +01:00
|
|
|
#include <QString>
|
|
|
|
#include <QIcon>
|
2019-07-09 00:05:08 +02:00
|
|
|
#include <QPointer>
|
2018-02-27 18:06:05 +01:00
|
|
|
#include <QLineEdit>
|
|
|
|
#include <QToolButton>
|
2018-07-01 22:26:46 +02:00
|
|
|
#include <QStyle>
|
2020-02-09 02:29:35 +01:00
|
|
|
#include <QSize>
|
|
|
|
#include <QBoxLayout>
|
2018-09-04 22:55:38 +02:00
|
|
|
#include <QEvent>
|
2020-02-09 02:29:35 +01:00
|
|
|
#include <QResizeEvent>
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2019-05-08 23:34:44 +02:00
|
|
|
#include "core/iconloader.h"
|
2018-07-01 22:26:46 +02:00
|
|
|
|
2018-09-04 22:55:38 +02:00
|
|
|
class QSearchFieldPrivate : public QObject {
|
|
|
|
public:
|
|
|
|
QSearchFieldPrivate(QSearchField *searchField, QLineEdit *lineEdit, QToolButton *clearButton)
|
|
|
|
: QObject(searchField), lineEdit(lineEdit), clearButton(clearButton) {}
|
2018-07-01 22:26:46 +02:00
|
|
|
|
2018-09-04 22:55:38 +02:00
|
|
|
int lineEditFrameWidth() const {
|
|
|
|
return lineEdit->style()->pixelMetric(QStyle::PM_DefaultFrameWidth);
|
|
|
|
}
|
2018-07-01 22:26:46 +02:00
|
|
|
|
2018-09-04 22:55:38 +02:00
|
|
|
int clearButtonPaddedWidth() const {
|
|
|
|
return clearButton->width() + lineEditFrameWidth() * 2;
|
|
|
|
}
|
2018-07-01 22:26:46 +02:00
|
|
|
|
2018-09-04 22:55:38 +02:00
|
|
|
int clearButtonPaddedHeight() const {
|
|
|
|
return clearButton->height() + lineEditFrameWidth() * 2;
|
|
|
|
}
|
2018-07-01 22:26:46 +02:00
|
|
|
|
2018-09-04 22:55:38 +02:00
|
|
|
QPointer<QLineEdit> lineEdit;
|
|
|
|
QPointer<QToolButton> clearButton;
|
2018-02-27 18:06:05 +01:00
|
|
|
};
|
|
|
|
|
2018-09-04 22:55:38 +02:00
|
|
|
QSearchField::QSearchField(QWidget *parent) : QWidget(parent) {
|
2018-07-01 22:26:46 +02:00
|
|
|
|
2018-09-04 22:55:38 +02:00
|
|
|
QLineEdit *lineEdit = new QLineEdit(this);
|
|
|
|
connect(lineEdit, SIGNAL(textChanged(QString)), this, SIGNAL(textChanged(QString)));
|
|
|
|
connect(lineEdit, SIGNAL(editingFinished()), this, SIGNAL(editingFinished()));
|
|
|
|
connect(lineEdit, SIGNAL(returnPressed()), this, SIGNAL(returnPressed()));
|
|
|
|
connect(lineEdit, SIGNAL(textChanged(QString)), this, SLOT(setText(QString)));
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2018-09-04 22:55:38 +02:00
|
|
|
QToolButton *clearButton = new QToolButton(this);
|
|
|
|
QIcon clearIcon(IconLoader::Load("edit-clear-locationbar-ltr"));
|
2018-07-01 22:26:46 +02:00
|
|
|
|
2018-09-04 22:55:38 +02:00
|
|
|
clearButton->setIcon(clearIcon);
|
|
|
|
clearButton->setIconSize(QSize(16, 16));
|
|
|
|
clearButton->setStyleSheet("border: none; padding: 0px;");
|
|
|
|
clearButton->resize(clearButton->sizeHint());
|
2018-07-01 22:26:46 +02:00
|
|
|
|
2018-09-04 22:55:38 +02:00
|
|
|
connect(clearButton, SIGNAL(clicked()), this, SLOT(clear()));
|
2018-07-01 22:26:46 +02:00
|
|
|
|
2018-09-04 22:55:38 +02:00
|
|
|
pimpl = new QSearchFieldPrivate(this, lineEdit, clearButton);
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2018-09-04 22:55:38 +02:00
|
|
|
const int frame_width = lineEdit->style()->pixelMetric(QStyle::PM_DefaultFrameWidth);
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2018-09-04 22:55:38 +02:00
|
|
|
lineEdit->setStyleSheet(QString("QLineEdit { padding-left: %1px; } ").arg(clearButton->width()));
|
|
|
|
const int width = frame_width + qMax(lineEdit->minimumSizeHint().width(), pimpl->clearButtonPaddedWidth());
|
|
|
|
const int height = frame_width + qMax(lineEdit->minimumSizeHint().height(), pimpl->clearButtonPaddedHeight());
|
|
|
|
lineEdit->setMinimumSize(width, height);
|
2018-07-01 22:26:46 +02:00
|
|
|
|
2018-09-04 22:55:38 +02:00
|
|
|
QVBoxLayout *layout = new QVBoxLayout(this);
|
|
|
|
layout->setMargin(0);
|
|
|
|
layout->addWidget(lineEdit);
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2018-09-04 22:55:38 +02:00
|
|
|
lineEdit->installEventFilter(this);
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-09-04 22:55:38 +02:00
|
|
|
void QSearchField::setText(const QString &text) {
|
|
|
|
|
|
|
|
Q_ASSERT(pimpl && pimpl->clearButton && pimpl->lineEdit);
|
|
|
|
if (!(pimpl && pimpl->clearButton && pimpl->lineEdit)) return;
|
|
|
|
if (text != this->text()) pimpl->lineEdit->setText(text);
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-09-04 22:55:38 +02:00
|
|
|
void QSearchField::setPlaceholderText(const QString &text) {
|
|
|
|
Q_ASSERT(pimpl && pimpl->lineEdit);
|
|
|
|
if (!(pimpl && pimpl->lineEdit)) return;
|
|
|
|
pimpl->lineEdit->setPlaceholderText(text);
|
|
|
|
}
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2018-09-04 22:55:38 +02:00
|
|
|
QString QSearchField::placeholderText() const {
|
|
|
|
return pimpl->lineEdit->placeholderText();
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
|
|
|
|
2018-09-04 22:55:38 +02:00
|
|
|
void QSearchField::setFocus(Qt::FocusReason reason) {
|
|
|
|
Q_ASSERT(pimpl && pimpl->lineEdit);
|
|
|
|
if (pimpl && pimpl->lineEdit) pimpl->lineEdit->setFocus(reason);
|
|
|
|
}
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2018-09-04 22:55:38 +02:00
|
|
|
void QSearchField::setFocus() {
|
|
|
|
setFocus(Qt::OtherFocusReason);
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
|
|
|
|
2018-09-04 22:55:38 +02:00
|
|
|
void QSearchField::clear() {
|
|
|
|
Q_ASSERT(pimpl && pimpl->lineEdit);
|
|
|
|
if (!(pimpl && pimpl->lineEdit)) return;
|
|
|
|
pimpl->lineEdit->clear();
|
2018-07-01 22:26:46 +02:00
|
|
|
}
|
|
|
|
|
2018-09-04 22:55:38 +02:00
|
|
|
void QSearchField::selectAll() {
|
|
|
|
Q_ASSERT(pimpl && pimpl->lineEdit);
|
|
|
|
if (!(pimpl && pimpl->lineEdit)) return;
|
|
|
|
pimpl->lineEdit->selectAll();
|
2018-07-01 22:26:46 +02:00
|
|
|
}
|
|
|
|
|
2018-09-04 22:55:38 +02:00
|
|
|
QString QSearchField::text() const {
|
|
|
|
Q_ASSERT(pimpl && pimpl->lineEdit);
|
|
|
|
if (!(pimpl && pimpl->lineEdit)) return QString();
|
|
|
|
return pimpl->lineEdit->text();
|
2018-07-01 22:26:46 +02:00
|
|
|
}
|
|
|
|
|
2018-09-04 22:55:38 +02:00
|
|
|
void QSearchField::resizeEvent(QResizeEvent *resizeEvent) {
|
|
|
|
Q_ASSERT(pimpl && pimpl->clearButton && pimpl->lineEdit);
|
|
|
|
if (!(pimpl && pimpl->clearButton && pimpl->lineEdit)) return;
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2018-09-04 22:55:38 +02:00
|
|
|
QWidget::resizeEvent(resizeEvent);
|
|
|
|
const int x = pimpl->lineEditFrameWidth();
|
|
|
|
const int y = (height() - pimpl->clearButton->height())/2;
|
|
|
|
pimpl->clearButton->move(x, y);
|
|
|
|
}
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2018-09-04 22:55:38 +02:00
|
|
|
bool QSearchField::eventFilter(QObject *o, QEvent *e) {
|
|
|
|
if (pimpl && pimpl->lineEdit && o == pimpl->lineEdit) {
|
|
|
|
// Forward some lineEdit events to QSearchField (only those we need for
|
|
|
|
// now, but some might be added later if needed)
|
|
|
|
switch (e->type()) {
|
|
|
|
case QEvent::FocusIn:
|
|
|
|
case QEvent::FocusOut:
|
|
|
|
QApplication::sendEvent(this, e);
|
|
|
|
break;
|
2019-05-08 23:34:44 +02:00
|
|
|
default:
|
|
|
|
break;
|
2018-09-04 22:55:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return QWidget::eventFilter(o, e);
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|