]> git.sur5r.net Git - minitube/blob - src/urllineedit.cpp
d91ddd5cde9d93ef4b8ab866fbcc2d3f855e9129
[minitube] / src / urllineedit.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: Nokia Corporation (qt-info@nokia.com)
5 **
6 ** This file is part of the demonstration applications of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial Usage
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Nokia.
14 **
15 ** GNU Lesser General Public License Usage
16 ** Alternatively, this file may be used under the terms of the GNU Lesser
17 ** General Public License version 2.1 as published by the Free Software
18 ** Foundation and appearing in the file LICENSE.LGPL included in the
19 ** packaging of this file.  Please review the following information to
20 ** ensure the GNU Lesser General Public License version 2.1 requirements
21 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22 **
23 ** In addition, as a special exception, Nokia gives you certain
24 ** additional rights. These rights are described in the Nokia Qt LGPL
25 ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26 ** package.
27 **
28 ** GNU General Public License Usage
29 ** Alternatively, this file may be used under the terms of the GNU
30 ** General Public License version 3.0 as published by the Free Software
31 ** Foundation and appearing in the file LICENSE.GPL included in the
32 ** packaging of this file.  Please review the following information to
33 ** ensure the GNU General Public License version 3.0 requirements will be
34 ** met: http://www.gnu.org/copyleft/gpl.html.
35 **
36 ** If you are unsure which license is appropriate for your use, please
37 ** contact the sales department at http://www.qtsoftware.com/contact.
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "urllineedit.h"
43 #include "searchlineedit.h"
44
45 #include <QtCore/QEvent>
46
47 #include <QtGui/QApplication>
48 #include <QtGui/QCompleter>
49 #include <QtGui/QFocusEvent>
50 #include <QtGui/QHBoxLayout>
51 #include <QtGui/QLabel>
52 #include <QtGui/QLineEdit>
53 #include <QtGui/QPainter>
54 #include <QtGui/QStyle>
55 #include <QtGui/QStyleOptionFrameV2>
56
57 #include <QtCore/QDebug>
58
59 ExLineEdit::ExLineEdit(QWidget *parent)
60     : QWidget(parent)
61     , m_leftWidget(0)
62     , m_lineEdit(new QLineEdit(this))
63     , m_clearButton(0)
64 {
65     setFocusPolicy(m_lineEdit->focusPolicy());
66     setAttribute(Qt::WA_InputMethodEnabled);
67     setSizePolicy(m_lineEdit->sizePolicy());
68     setBackgroundRole(m_lineEdit->backgroundRole());
69     setMouseTracking(true);
70     setAcceptDrops(true);
71     setAttribute(Qt::WA_MacShowFocusRect, true);
72     QPalette p = m_lineEdit->palette();
73     setPalette(p);
74
75     // line edit
76     m_lineEdit->setFrame(false);
77     m_lineEdit->setFocusProxy(this);
78     m_lineEdit->setAttribute(Qt::WA_MacShowFocusRect, false);
79     QPalette clearPalette = m_lineEdit->palette();
80     clearPalette.setBrush(QPalette::Base, QBrush(Qt::transparent));
81     m_lineEdit->setPalette(clearPalette);
82
83     // clearButton
84     m_clearButton = new ClearButton(this);
85     connect(m_clearButton, SIGNAL(clicked()),
86             m_lineEdit, SLOT(clear()));
87     connect(m_lineEdit, SIGNAL(textChanged(const QString&)),
88             m_clearButton, SLOT(textChanged(const QString&)));
89 }
90
91 void ExLineEdit::setFont(const QFont &font) {
92     m_lineEdit->setFont(font);
93     updateGeometries();
94 }
95
96 void ExLineEdit::setLeftWidget(QWidget *widget)
97 {
98     m_leftWidget = widget;
99 }
100
101 QWidget *ExLineEdit::leftWidget() const
102 {
103     return m_leftWidget;
104 }
105
106 void ExLineEdit::resizeEvent(QResizeEvent *event)
107 {
108     Q_ASSERT(m_leftWidget);
109     updateGeometries();
110     QWidget::resizeEvent(event);
111 }
112
113 void ExLineEdit::updateGeometries()
114 {
115     QStyleOptionFrameV2 panel;
116     initStyleOption(&panel);
117     QRect rect = style()->subElementRect(QStyle::SE_LineEditContents, &panel, this);
118
119     int padding = 3;
120     // int height = rect.height() + padding*2;
121     int width = rect.width();
122
123     // int m_leftWidgetHeight = m_leftWidget->height();
124     m_leftWidget->setGeometry(rect.x() + 2,          0,
125                               m_leftWidget->width(), m_leftWidget->height());
126
127     int clearButtonWidth = this->height();
128     m_lineEdit->setGeometry(m_leftWidget->x() + m_leftWidget->width(),        padding,
129                             width - clearButtonWidth - m_leftWidget->width(), this->height() - padding*2);
130
131     m_clearButton->setGeometry(this->width() - clearButtonWidth, 0,
132                                clearButtonWidth, this->height());
133 }
134
135 void ExLineEdit::initStyleOption(QStyleOptionFrameV2 *option) const
136 {
137     option->initFrom(this);
138     option->rect = contentsRect();
139     option->lineWidth = style()->pixelMetric(QStyle::PM_DefaultFrameWidth, option, this);
140     option->midLineWidth = 0;
141     option->state |= QStyle::State_Sunken;
142     if (m_lineEdit->isReadOnly())
143         option->state |= QStyle::State_ReadOnly;
144 #ifdef QT_KEYPAD_NAVIGATION
145     if (hasEditFocus())
146         option->state |= QStyle::State_HasEditFocus;
147 #endif
148     option->features = QStyleOptionFrameV2::None;
149 }
150
151 QSize ExLineEdit::sizeHint() const
152 {
153     m_lineEdit->setFrame(true);
154     QSize size = m_lineEdit->sizeHint();
155     m_lineEdit->setFrame(false);
156     return size;
157 }
158
159 void ExLineEdit::focusInEvent(QFocusEvent *event)
160 {
161     m_lineEdit->event(event);
162     QWidget::focusInEvent(event);
163 }
164
165 void ExLineEdit::focusOutEvent(QFocusEvent *event)
166 {
167     m_lineEdit->event(event);
168
169     if (m_lineEdit->completer()) {
170         connect(m_lineEdit->completer(), SIGNAL(activated(QString)),
171                          m_lineEdit, SLOT(setText(QString)));
172         connect(m_lineEdit->completer(), SIGNAL(highlighted(QString)),
173                          m_lineEdit, SLOT(_q_completionHighlighted(QString)));
174     }
175     QWidget::focusOutEvent(event);
176 }
177
178 void ExLineEdit::keyPressEvent(QKeyEvent *event)
179 {
180     m_lineEdit->event(event);
181     QWidget::keyPressEvent(event);
182 }
183
184 bool ExLineEdit::event(QEvent *event)
185 {
186     if (event->type() == QEvent::ShortcutOverride ||
187         event->type() == QEvent::InputMethod)
188         m_lineEdit->event(event);
189     return QWidget::event(event);
190 }
191
192 void ExLineEdit::paintEvent(QPaintEvent *)
193 {
194     QPainter p(this);
195     QStyleOptionFrameV2 panel;
196     initStyleOption(&panel);
197     style()->drawPrimitive(QStyle::PE_PanelLineEdit, &panel, &p, this);
198 }