qBittorrent/src/gui/lineedit.cpp

56 lines
1.8 KiB
C++
Raw Normal View History

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"
#include <algorithm>
#include <QGuiApplication>
#include <QResizeEvent>
2014-12-20 20:29:17 +03:00
#include <QStyle>
#include <QToolButton>
#include "uithememanager.h"
2014-12-20 20:29:17 +03:00
LineEdit::LineEdit(QWidget *parent)
: QLineEdit(parent)
2014-12-20 20:29:17 +03:00
{
m_searchButton = new QToolButton(this);
m_searchButton->setIcon(UIThemeManager::instance()->getIcon("edit-find"));
m_searchButton->setCursor(Qt::ArrowCursor);
m_searchButton->setStyleSheet("QToolButton {border: none; padding: 2px;}");
// padding between text and widget borders
2020-03-22 13:35:16 +03:00
setStyleSheet(QString::fromLatin1("QLineEdit {padding-left: %1px;}").arg(m_searchButton->sizeHint().width()));
setClearButtonEnabled(true);
const int frameWidth = style()->pixelMetric(QStyle::PM_DefaultFrameWidth);
setMaximumHeight(std::max(sizeHint().height(), m_searchButton->sizeHint().height()) + frameWidth * 2);
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
2014-12-20 20:29:17 +03:00
}
void LineEdit::resizeEvent(QResizeEvent *e)
2014-12-20 20:29:17 +03:00
{
const int frameWidth = style()->pixelMetric(QStyle::PM_DefaultFrameWidth);
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);
}