]> git.sur5r.net Git - minitube/blob - src/exlineedit.cpp
1984eb65230d6096766ff571d3effa4c2c5400db
[minitube] / src / exlineedit.cpp
1 #include "exlineedit.h"
2 #include "iconutils.h"
3
4 ClearButton::ClearButton(QWidget *parent) : QAbstractButton(parent), hovered(false), mousePressed(false) {
5     setCursor(Qt::ArrowCursor);
6     setToolTip(tr("Clear"));
7     setVisible(false);
8     setFocusPolicy(Qt::NoFocus);
9 }
10
11 void ClearButton::paintEvent(QPaintEvent *e) {
12     Q_UNUSED(e);
13     QPainter painter(this);
14     const int h = height();
15     int iconSize = 16;
16     if (h > 30) iconSize = 22;
17     QIcon::Mode iconMode = QIcon::Normal;
18     if (mousePressed) iconMode = QIcon::Active;
19     QPixmap p = IconUtils::icon("edit-clear").pixmap(iconSize, iconSize, iconMode);
20     int x = (width() - p.width()) / 2;
21     int y = (h - p.height()) / 2;
22     painter.drawPixmap(x, y, p);
23 }
24
25 void ClearButton::textChanged(const QString &text) {
26     setVisible(!text.isEmpty());
27 }
28
29 void ClearButton::enterEvent(QEvent *e) {
30     hovered = true;
31     QAbstractButton::enterEvent(e);
32 }
33
34 void ClearButton::leaveEvent(QEvent *e) {
35     hovered = false;
36     QAbstractButton::leaveEvent(e);
37 }
38
39 void ClearButton::mousePressEvent(QMouseEvent *e) {
40     mousePressed = true;
41     QAbstractButton::mousePressEvent(e);
42 }
43
44 void ClearButton::mouseReleaseEvent(QMouseEvent *e) {
45     mousePressed = false;
46     QAbstractButton::mouseReleaseEvent(e);
47 }
48
49 ExLineEdit::ExLineEdit(QWidget *parent)
50     : QWidget(parent)
51     , m_leftWidget(0)
52     , m_lineEdit(new QLineEdit(this))
53     , m_clearButton(new ClearButton(this)) {
54     setFocusPolicy(m_lineEdit->focusPolicy());
55     setAttribute(Qt::WA_InputMethodEnabled);
56     setSizePolicy(m_lineEdit->sizePolicy());
57     setBackgroundRole(m_lineEdit->backgroundRole());
58     setMouseTracking(true);
59     setAcceptDrops(true);
60     setAttribute(Qt::WA_MacShowFocusRect, true);
61     QPalette p = m_lineEdit->palette();
62     setPalette(p);
63
64     // line edit
65     m_lineEdit->setFrame(false);
66     m_lineEdit->setFocusProxy(this);
67     m_lineEdit->setAttribute(Qt::WA_MacShowFocusRect, false);
68     m_lineEdit->setStyleSheet("background:transparent");
69     QPalette clearPalette = m_lineEdit->palette();
70     clearPalette.setBrush(QPalette::Base, QBrush(Qt::transparent));
71     m_lineEdit->setPalette(clearPalette);
72
73     // clearButton
74     connect(m_clearButton, SIGNAL(clicked()), m_lineEdit, SLOT(clear()));
75     connect(m_lineEdit, SIGNAL(textChanged(const QString&)), m_clearButton, SLOT(textChanged(const QString&)));
76 }
77
78 void ExLineEdit::setFont(const QFont &font) {
79     m_lineEdit->setFont(font);
80     updateGeometries();
81 }
82
83 void ExLineEdit::setLeftWidget(QWidget *widget) {
84     m_leftWidget = widget;
85 }
86
87 QWidget *ExLineEdit::leftWidget() const {
88     return m_leftWidget;
89 }
90
91 void ExLineEdit::clear() {
92     m_lineEdit->clear();
93 }
94
95 QString ExLineEdit::text() {
96     return m_lineEdit->text();
97 }
98
99 void ExLineEdit::resizeEvent(QResizeEvent *e) {
100     Q_ASSERT(m_leftWidget);
101     updateGeometries();
102     QWidget::resizeEvent(e);
103 }
104
105 void ExLineEdit::updateGeometries() {
106     QStyleOptionFrame panel;
107     initStyleOption(&panel);
108     QRect rect = style()->subElementRect(QStyle::SE_LineEditContents, &panel, this);
109
110     int padding = 3;
111     // int height = rect.height() + padding*2;
112     int width = rect.width();
113
114     // int m_leftWidgetHeight = m_leftWidget->height();
115     m_leftWidget->setGeometry(rect.x() + 2,          0,
116                               m_leftWidget->width(), m_leftWidget->height());
117
118     int clearButtonWidth = this->height();
119     m_lineEdit->setGeometry(m_leftWidget->x() + m_leftWidget->width(),        padding,
120                             width - clearButtonWidth - m_leftWidget->width(), this->height() - padding*2);
121
122     m_clearButton->setGeometry(this->width() - clearButtonWidth, 0,
123                                clearButtonWidth, this->height());
124 }
125
126 void ExLineEdit::initStyleOption(QStyleOptionFrame *option) const {
127     option->initFrom(this);
128     option->rect = contentsRect();
129     option->lineWidth = style()->pixelMetric(QStyle::PM_DefaultFrameWidth, option, this);
130     option->midLineWidth = 0;
131     option->state |= QStyle::State_Sunken;
132     if (m_lineEdit->isReadOnly())
133         option->state |= QStyle::State_ReadOnly;
134 #ifdef QT_KEYPAD_NAVIGATION
135     if (hasEditFocus())
136         option->state |= QStyle::State_HasEditFocus;
137 #endif
138     option->features = QStyleOptionFrame::None;
139 }
140
141 QSize ExLineEdit::sizeHint() const {
142     m_lineEdit->setFrame(true);
143     QSize size = m_lineEdit->sizeHint();
144     m_lineEdit->setFrame(false);
145     size = size + QSize(3, 3);
146     return size;
147 }
148
149 void ExLineEdit::focusInEvent(QFocusEvent *e) {
150     m_lineEdit->event(e);
151     QWidget::focusInEvent(e);
152 }
153
154 void ExLineEdit::focusOutEvent(QFocusEvent *e) {
155     m_lineEdit->event(e);
156
157     if (m_lineEdit->completer()) {
158         connect(m_lineEdit->completer(), SIGNAL(activated(QString)),
159                          m_lineEdit, SLOT(setText(QString)));
160         connect(m_lineEdit->completer(), SIGNAL(highlighted(QString)),
161                          m_lineEdit, SLOT(_q_completionHighlighted(QString)));
162     }
163     QWidget::focusOutEvent(e);
164 }
165
166 void ExLineEdit::keyPressEvent(QKeyEvent *e) {
167     if (e->key() == Qt::Key_Escape && !m_lineEdit->text().isEmpty()) {
168         m_lineEdit->clear();
169     }
170     m_lineEdit->event(e);
171     QWidget::keyPressEvent(e);
172 }
173
174 bool ExLineEdit::event(QEvent *e) {
175     if (e->type() == QEvent::ShortcutOverride || e->type() == QEvent::InputMethod)
176         m_lineEdit->event(e);
177     return QWidget::event(e);
178 }
179
180 void ExLineEdit::paintEvent(QPaintEvent *e) {
181     Q_UNUSED(e);
182     QPainter p(this);
183     QStyleOptionFrame panel;
184     initStyleOption(&panel);
185     style()->drawPrimitive(QStyle::PE_PanelLineEdit, &panel, &p, this);
186 }