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 12:13:22 +03:00
|
|
|
|
2015-11-07 07:34:52 +03:00
|
|
|
#include <algorithm>
|
2017-11-24 12:13:22 +03:00
|
|
|
|
2019-05-22 17:22:12 +03:00
|
|
|
#include <QGuiApplication>
|
2017-11-24 12:13:22 +03:00
|
|
|
#include <QResizeEvent>
|
2014-12-20 20:29:17 +03:00
|
|
|
#include <QStyle>
|
2015-11-07 07:34:52 +03:00
|
|
|
#include <QToolButton>
|
2017-11-24 12:13:22 +03:00
|
|
|
|
2022-03-12 17:00:58 +03:00
|
|
|
#include "base/global.h"
|
2019-07-16 07:01:33 +03:00
|
|
|
#include "uithememanager.h"
|
2014-12-20 20:29:17 +03:00
|
|
|
|
|
|
|
LineEdit::LineEdit(QWidget *parent)
|
2015-11-07 11:24:16 +03:00
|
|
|
: QLineEdit(parent)
|
2014-12-20 20:29:17 +03:00
|
|
|
{
|
2017-11-24 12:13:22 +03:00
|
|
|
m_searchButton = new QToolButton(this);
|
2022-03-12 17:00:58 +03:00
|
|
|
m_searchButton->setIcon(UIThemeManager::instance()->getIcon(u"edit-find"_qs));
|
2017-11-24 12:13:22 +03:00
|
|
|
m_searchButton->setCursor(Qt::ArrowCursor);
|
2022-03-12 17:00:58 +03:00
|
|
|
m_searchButton->setStyleSheet(u"QToolButton {border: none; padding: 2px;}"_qs);
|
2017-11-24 12:13:22 +03:00
|
|
|
|
2017-12-03 10:32:58 +03:00
|
|
|
// padding between text and widget borders
|
2022-03-29 05:41:17 +03:00
|
|
|
setStyleSheet(u"QLineEdit {padding-left: %1px;}"_qs.arg(m_searchButton->sizeHint().width()));
|
2017-11-24 12:13:22 +03:00
|
|
|
|
2015-11-07 11:24:16 +03:00
|
|
|
setClearButtonEnabled(true);
|
2015-11-07 07:34:52 +03:00
|
|
|
|
2017-11-24 12:13:22 +03:00
|
|
|
const int frameWidth = style()->pixelMetric(QStyle::PM_DefaultFrameWidth);
|
|
|
|
setMaximumHeight(std::max(sizeHint().height(), m_searchButton->sizeHint().height()) + frameWidth * 2);
|
2016-04-18 13:34:14 +03:00
|
|
|
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
|
2014-12-20 20:29:17 +03:00
|
|
|
}
|
|
|
|
|
2015-11-07 07:34:52 +03:00
|
|
|
void LineEdit::resizeEvent(QResizeEvent *e)
|
2014-12-20 20:29:17 +03:00
|
|
|
{
|
2017-11-24 12:13:22 +03:00
|
|
|
const int frameWidth = style()->pixelMetric(QStyle::PM_DefaultFrameWidth);
|
2019-05-22 17:22:12 +03: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 23:41:13 +03: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 23:41:13 +03:00
|
|
|
clear();
|
|
|
|
}
|
|
|
|
QLineEdit::keyPressEvent(event);
|
|
|
|
}
|