2018-02-27 18:06:05 +01:00
|
|
|
/*
|
|
|
|
Copyright (C) 2011 by Mike McQuaid
|
2021-03-20 21:14:47 +01:00
|
|
|
Copyright (C) 2018-2021 by Jonas Kvinge <jonas@jkvinge.net>
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
2021-06-20 19:04:08 +02:00
|
|
|
class QSearchFieldPrivate : public QObject { // clazy:exclude=missing-qobject-macro
|
|
|
|
|
2020-04-23 21:08:28 +02:00
|
|
|
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 {
|
2020-04-23 21:08:28 +02:00
|
|
|
return lineedit_->style()->pixelMetric(QStyle::PM_DefaultFrameWidth);
|
2018-09-04 22:55:38 +02:00
|
|
|
}
|
2018-07-01 22:26:46 +02:00
|
|
|
|
2018-09-04 22:55:38 +02:00
|
|
|
int clearButtonPaddedWidth() const {
|
2020-04-23 21:08:28 +02:00
|
|
|
return clearbutton_->width() + lineEditFrameWidth() * 2;
|
2018-09-04 22:55:38 +02:00
|
|
|
}
|
2018-07-01 22:26:46 +02:00
|
|
|
|
2018-09-04 22:55:38 +02:00
|
|
|
int clearButtonPaddedHeight() const {
|
2020-04-23 21:08:28 +02:00
|
|
|
return clearbutton_->height() + lineEditFrameWidth() * 2;
|
2018-09-04 22:55:38 +02:00
|
|
|
}
|
2018-07-01 22:26:46 +02:00
|
|
|
|
2020-04-23 21:08:28 +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);
|
2021-01-26 16:48:04 +01:00
|
|
|
QObject::connect(lineEdit, &QLineEdit::textChanged, this, &QSearchField::textChanged);
|
|
|
|
QObject::connect(lineEdit, &QLineEdit::editingFinished, this, &QSearchField::editingFinished);
|
|
|
|
QObject::connect(lineEdit, &QLineEdit::returnPressed, this, &QSearchField::returnPressed);
|
|
|
|
QObject::connect(lineEdit, &QLineEdit::textChanged, this, &QSearchField::setText);
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2020-04-23 21:08:28 +02:00
|
|
|
QToolButton *clearbutton = new QToolButton(this);
|
2018-09-04 22:55:38 +02:00
|
|
|
QIcon clearIcon(IconLoader::Load("edit-clear-locationbar-ltr"));
|
2018-07-01 22:26:46 +02:00
|
|
|
|
2020-04-23 21:08:28 +02:00
|
|
|
clearbutton->setIcon(clearIcon);
|
2020-06-29 01:09:02 +02:00
|
|
|
clearbutton->setIconSize(QSize(20, 20));
|
2020-04-23 21:08:28 +02:00
|
|
|
clearbutton->setStyleSheet("border: none; padding: 0px;");
|
|
|
|
clearbutton->resize(clearbutton->sizeHint());
|
2018-07-01 22:26:46 +02:00
|
|
|
|
2021-01-26 16:48:04 +01:00
|
|
|
QObject::connect(clearbutton, &QToolButton::clicked, this, &QSearchField::clear);
|
2018-07-01 22:26:46 +02:00
|
|
|
|
2020-04-23 21:08:28 +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
|
|
|
|
2020-04-23 21:08:28 +02:00
|
|
|
lineEdit->setStyleSheet(QString("QLineEdit { padding-left: %1px; } ").arg(clearbutton->width()));
|
2018-09-04 22:55:38 +02:00
|
|
|
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);
|
2020-07-18 04:20:20 +02:00
|
|
|
layout->setContentsMargins(0, 0, 0, 0);
|
2018-09-04 22:55:38 +02:00
|
|
|
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
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-06-29 01:09:02 +02:00
|
|
|
void QSearchField::setIconSize(const int iconsize) {
|
|
|
|
|
|
|
|
pimpl->clearbutton_->setIconSize(QSize(iconsize, iconsize));
|
|
|
|
pimpl->clearbutton_->resize(pimpl->clearbutton_->sizeHint());
|
|
|
|
|
|
|
|
pimpl->lineedit_->setStyleSheet(QString("QLineEdit { padding-left: %1px; } ").arg(pimpl->clearbutton_->width()));
|
|
|
|
const int frame_width = pimpl->lineedit_->style()->pixelMetric(QStyle::PM_DefaultFrameWidth);
|
|
|
|
const int width = frame_width + qMax(pimpl->lineedit_->minimumSizeHint().width(), pimpl->clearButtonPaddedWidth());
|
|
|
|
const int height = frame_width + qMax(pimpl->lineedit_->minimumSizeHint().height(), pimpl->clearButtonPaddedHeight());
|
|
|
|
pimpl->lineedit_->setMinimumSize(width, height);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-09-04 22:55:38 +02:00
|
|
|
void QSearchField::setText(const QString &text) {
|
|
|
|
|
2020-04-23 21:08:28 +02:00
|
|
|
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) {
|
2020-04-23 21:08:28 +02:00
|
|
|
|
|
|
|
Q_ASSERT(pimpl && pimpl->lineedit_);
|
|
|
|
if (!(pimpl && pimpl->lineedit_)) return;
|
|
|
|
pimpl->lineedit_->setPlaceholderText(text);
|
|
|
|
|
2018-09-04 22:55:38 +02:00
|
|
|
}
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2018-09-04 22:55:38 +02:00
|
|
|
QString QSearchField::placeholderText() const {
|
2020-04-23 21:08:28 +02:00
|
|
|
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) {
|
2020-04-23 21:08:28 +02:00
|
|
|
Q_ASSERT(pimpl && pimpl->lineedit_);
|
|
|
|
if (pimpl && pimpl->lineedit_) pimpl->lineedit_->setFocus(reason);
|
2018-09-04 22:55:38 +02:00
|
|
|
}
|
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() {
|
2020-04-23 21:08:28 +02:00
|
|
|
|
|
|
|
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() {
|
2020-04-23 21:08:28 +02:00
|
|
|
|
|
|
|
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 {
|
2020-04-23 21:08:28 +02:00
|
|
|
|
|
|
|
Q_ASSERT(pimpl && pimpl->lineedit_);
|
|
|
|
|
|
|
|
if (!(pimpl && pimpl->lineedit_)) return QString();
|
|
|
|
return pimpl->lineedit_->text();
|
|
|
|
|
2018-07-01 22:26:46 +02:00
|
|
|
}
|
|
|
|
|
2021-03-15 22:38:06 +01:00
|
|
|
void QSearchField::showEvent(QShowEvent *e) {
|
|
|
|
|
|
|
|
QWidget::showEvent(e);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-09-04 22:55:38 +02:00
|
|
|
void QSearchField::resizeEvent(QResizeEvent *resizeEvent) {
|
2020-04-23 21:08:28 +02:00
|
|
|
|
|
|
|
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();
|
2020-04-23 21:08:28 +02:00
|
|
|
const int y = (height() - pimpl->clearbutton_->height())/2;
|
|
|
|
pimpl->clearbutton_->move(x, y);
|
|
|
|
|
2018-09-04 22:55:38 +02:00
|
|
|
}
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2018-09-04 22:55:38 +02:00
|
|
|
bool QSearchField::eventFilter(QObject *o, QEvent *e) {
|
2020-04-23 21:08:28 +02:00
|
|
|
|
|
|
|
if (pimpl && pimpl->lineedit_ && o == pimpl->lineedit_) {
|
2018-09-04 22:55:38 +02:00
|
|
|
// 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);
|
2020-04-23 21:08:28 +02:00
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|