2014-12-20 20:29:17 +03:00
|
|
|
/****************************************************************************
|
|
|
|
**
|
|
|
|
** Copyright (c) 2007 Trolltech ASA <info@trolltech.com>
|
|
|
|
**
|
|
|
|
** Use, modification and distribution is allowed without limitation,
|
|
|
|
** warranty, liability or support of any kind.
|
|
|
|
**
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "lineedit.h"
|
2017-11-24 17:13:22 +08:00
|
|
|
|
2015-11-07 12:34:52 +08:00
|
|
|
#include <algorithm>
|
2017-11-24 17:13:22 +08:00
|
|
|
|
2019-05-22 22:22:12 +08:00
|
|
|
#include <QGuiApplication>
|
2017-11-24 17:13:22 +08:00
|
|
|
#include <QResizeEvent>
|
2014-12-20 20:29:17 +03:00
|
|
|
#include <QStyle>
|
2015-11-07 12:34:52 +08:00
|
|
|
#include <QToolButton>
|
2017-11-24 17:13:22 +08:00
|
|
|
|
2019-07-16 09:31:33 +05:30
|
|
|
#include "uithememanager.h"
|
2014-12-20 20:29:17 +03:00
|
|
|
|
|
|
|
LineEdit::LineEdit(QWidget *parent)
|
2015-11-07 16:24:16 +08:00
|
|
|
: QLineEdit(parent)
|
2014-12-20 20:29:17 +03:00
|
|
|
{
|
2017-11-24 17:13:22 +08:00
|
|
|
m_searchButton = new QToolButton(this);
|
2019-07-16 09:31:33 +05:30
|
|
|
m_searchButton->setIcon(UIThemeManager::instance()->getIcon("edit-find"));
|
2017-11-24 17:13:22 +08:00
|
|
|
m_searchButton->setCursor(Qt::ArrowCursor);
|
2017-12-03 15:32:58 +08:00
|
|
|
m_searchButton->setStyleSheet("QToolButton {border: none; padding: 2px;}");
|
2017-11-24 17:13:22 +08:00
|
|
|
|
2017-12-03 15:32:58 +08:00
|
|
|
// padding between text and widget borders
|
2020-03-22 18:35:16 +08:00
|
|
|
setStyleSheet(QString::fromLatin1("QLineEdit {padding-left: %1px;}").arg(m_searchButton->sizeHint().width()));
|
2017-11-24 17:13:22 +08:00
|
|
|
|
2015-11-07 16:24:16 +08:00
|
|
|
setClearButtonEnabled(true);
|
2015-11-07 12:34:52 +08:00
|
|
|
|
2017-11-24 17:13:22 +08:00
|
|
|
const int frameWidth = style()->pixelMetric(QStyle::PM_DefaultFrameWidth);
|
|
|
|
setMaximumHeight(std::max(sizeHint().height(), m_searchButton->sizeHint().height()) + frameWidth * 2);
|
2016-04-18 18:34:14 +08:00
|
|
|
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
|
2014-12-20 20:29:17 +03:00
|
|
|
}
|
|
|
|
|
2015-11-07 12:34:52 +08:00
|
|
|
void LineEdit::resizeEvent(QResizeEvent *e)
|
2014-12-20 20:29:17 +03:00
|
|
|
{
|
2017-11-24 17:13:22 +08:00
|
|
|
const int frameWidth = style()->pixelMetric(QStyle::PM_DefaultFrameWidth);
|
2019-05-22 22:22:12 +08:00
|
|
|
const int xPos = QGuiApplication::isLeftToRight()
|
|
|
|
? frameWidth
|
|
|
|
: (e->size().width() - m_searchButton->sizeHint().width() - frameWidth);
|
|
|
|
m_searchButton->move(xPos, (e->size().height() - m_searchButton->sizeHint().height()) / 2);
|
2014-12-20 20:29:17 +03:00
|
|
|
}
|
2018-05-23 22:41:13 +02:00
|
|
|
|
|
|
|
void LineEdit::keyPressEvent(QKeyEvent *event)
|
|
|
|
{
|
2020-11-16 10:02:11 +03:00
|
|
|
if ((event->modifiers() == Qt::NoModifier) && (event->key() == Qt::Key_Escape))
|
|
|
|
{
|
2018-05-23 22:41:13 +02:00
|
|
|
clear();
|
|
|
|
}
|
|
|
|
QLineEdit::keyPressEvent(event);
|
|
|
|
}
|