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 {
|
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);
|
|
|
|
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
|
|
|
|
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);
|
|
|
|
clearbutton->setIconSize(QSize(16, 16));
|
|
|
|
clearbutton->setStyleSheet("border: none; padding: 0px;");
|
|
|
|
clearbutton->resize(clearbutton->sizeHint());
|
2018-07-01 22:26:46 +02:00
|
|
|
|
2020-04-23 21:08:28 +02:00
|
|
|
connect(clearbutton, SIGNAL(clicked()), this, SLOT(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);
|
|
|
|
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) {
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|