qBittorrent
lineedit.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (c) 2007 Trolltech ASA <info@trolltech.com>
4 **
5 ** Use, modification and distribution is allowed without limitation,
6 ** warranty, liability or support of any kind.
7 **
8 ****************************************************************************/
9 
10 #include "lineedit.h"
11 
12 #include <algorithm>
13 
14 #include <QGuiApplication>
15 #include <QResizeEvent>
16 #include <QStyle>
17 #include <QToolButton>
18 
19 #include "uithememanager.h"
20 
21 LineEdit::LineEdit(QWidget *parent)
22  : QLineEdit(parent)
23 {
24  m_searchButton = new QToolButton(this);
25  m_searchButton->setIcon(UIThemeManager::instance()->getIcon("edit-find"));
26  m_searchButton->setCursor(Qt::ArrowCursor);
27  m_searchButton->setStyleSheet("QToolButton {border: none; padding: 2px;}");
28 
29  // padding between text and widget borders
30  setStyleSheet(QString::fromLatin1("QLineEdit {padding-left: %1px;}").arg(m_searchButton->sizeHint().width()));
31 
32  setClearButtonEnabled(true);
33 
34  const int frameWidth = style()->pixelMetric(QStyle::PM_DefaultFrameWidth);
35  setMaximumHeight(std::max(sizeHint().height(), m_searchButton->sizeHint().height()) + frameWidth * 2);
36  setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
37 }
38 
39 void LineEdit::resizeEvent(QResizeEvent *e)
40 {
41  const int frameWidth = style()->pixelMetric(QStyle::PM_DefaultFrameWidth);
42  const int xPos = QGuiApplication::isLeftToRight()
43  ? frameWidth
44  : (e->size().width() - m_searchButton->sizeHint().width() - frameWidth);
45  m_searchButton->move(xPos, (e->size().height() - m_searchButton->sizeHint().height()) / 2);
46 }
47 
48 void LineEdit::keyPressEvent(QKeyEvent *event)
49 {
50  if ((event->modifiers() == Qt::NoModifier) && (event->key() == Qt::Key_Escape))
51  {
52  clear();
53  }
54  QLineEdit::keyPressEvent(event);
55 }
void resizeEvent(QResizeEvent *e) override
Definition: lineedit.cpp:39
QToolButton * m_searchButton
Definition: lineedit.h:28
void keyPressEvent(QKeyEvent *event) override
Definition: lineedit.cpp:48
LineEdit(QWidget *parent)
Definition: lineedit.cpp:21
static UIThemeManager * instance()