1 /****************************************************************************
3 ** Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: Qt Software Information (qt-info@nokia.com)
6 ** This file is part of the demonstration applications of the Qt Toolkit.
9 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** accordance with the Qt Commercial License Agreement provided with the
11 ** Software or, alternatively, in accordance with the terms contained in
12 ** a written agreement between you and Nokia.
15 ** GNU General Public License Usage
16 ** Alternatively, this file may be used under the terms of the GNU
17 ** General Public License versions 2.0 or 3.0 as published by the Free
18 ** Software Foundation and appearing in the file LICENSE.GPL included in
19 ** the packaging of this file. Please review the following information
20 ** to ensure GNU General Public Licensing requirements will be met:
21 ** http://www.fsf.org/licensing/licenses/info/GPLv2.html and
22 ** http://www.gnu.org/copyleft/gpl.html. In addition, as a special
23 ** exception, Nokia gives you certain additional rights. These rights
24 ** are described in the Nokia Qt GPL Exception version 1.3, included in
25 ** the file GPL_EXCEPTION.txt in this package.
27 ** Qt for Windows(R) Licensees
28 ** As a special exception, Nokia, as the sole copyright holder for Qt
29 ** Designer, grants users of the Qt/Eclipse Integration plug-in the
30 ** right for the Qt/Eclipse Integration to link to functionality
31 ** provided by Qt Designer and its related libraries.
33 ** If you are unsure which license is appropriate for your use, please
34 ** contact the sales department at qt-sales@nokia.com.
36 ****************************************************************************/
38 #include "urllineedit.h"
39 #include "searchlineedit.h"
41 #include <QtCore/QEvent>
43 #include <QtGui/QApplication>
44 #include <QtGui/QCompleter>
45 #include <QtGui/QFocusEvent>
46 #include <QtGui/QHBoxLayout>
47 #include <QtGui/QLabel>
48 #include <QtGui/QLineEdit>
49 #include <QtGui/QPainter>
50 #include <QtGui/QStyle>
51 #include <QtGui/QStyleOptionFrameV2>
53 #include <QtCore/QDebug>
55 ExLineEdit::ExLineEdit(QWidget *parent)
58 , m_lineEdit(new QLineEdit(this))
61 setFocusPolicy(m_lineEdit->focusPolicy());
62 setAttribute(Qt::WA_InputMethodEnabled);
63 setSizePolicy(m_lineEdit->sizePolicy());
64 setBackgroundRole(m_lineEdit->backgroundRole());
65 setMouseTracking(true);
67 setAttribute(Qt::WA_MacShowFocusRect, true);
68 QPalette p = m_lineEdit->palette();
72 m_lineEdit->setFrame(false);
73 m_lineEdit->setFocusProxy(this);
74 m_lineEdit->setAttribute(Qt::WA_MacShowFocusRect, false);
75 QPalette clearPalette = m_lineEdit->palette();
76 clearPalette.setBrush(QPalette::Base, QBrush(Qt::transparent));
77 m_lineEdit->setPalette(clearPalette);
80 m_clearButton = new ClearButton(this);
81 connect(m_clearButton, SIGNAL(clicked()),
82 m_lineEdit, SLOT(clear()));
83 connect(m_lineEdit, SIGNAL(textChanged(const QString&)),
84 m_clearButton, SLOT(textChanged(const QString&)));
87 void ExLineEdit::setFont(const QFont &font) {
88 m_lineEdit->setFont(font);
92 void ExLineEdit::setLeftWidget(QWidget *widget)
94 m_leftWidget = widget;
97 QWidget *ExLineEdit::leftWidget() const
102 void ExLineEdit::resizeEvent(QResizeEvent *event)
104 Q_ASSERT(m_leftWidget);
106 QWidget::resizeEvent(event);
109 void ExLineEdit::updateGeometries()
111 QStyleOptionFrameV2 panel;
112 initStyleOption(&panel);
113 QRect rect = style()->subElementRect(QStyle::SE_LineEditContents, &panel, this);
116 // int height = rect.height() + padding*2;
117 int width = rect.width();
119 // int m_leftWidgetHeight = m_leftWidget->height();
120 m_leftWidget->setGeometry(rect.x() + 2, 0,
121 m_leftWidget->width(), m_leftWidget->height());
123 int clearButtonWidth = this->height();
124 m_lineEdit->setGeometry(m_leftWidget->x() + m_leftWidget->width(), padding,
125 width - clearButtonWidth - m_leftWidget->width(), this->height() - padding*2);
127 m_clearButton->setGeometry(this->width() - clearButtonWidth, 0,
128 clearButtonWidth, this->height());
131 void ExLineEdit::initStyleOption(QStyleOptionFrameV2 *option) const
133 option->initFrom(this);
134 option->rect = contentsRect();
135 option->lineWidth = style()->pixelMetric(QStyle::PM_DefaultFrameWidth, option, this);
136 option->midLineWidth = 0;
137 option->state |= QStyle::State_Sunken;
138 if (m_lineEdit->isReadOnly())
139 option->state |= QStyle::State_ReadOnly;
140 #ifdef QT_KEYPAD_NAVIGATION
142 option->state |= QStyle::State_HasEditFocus;
144 option->features = QStyleOptionFrameV2::None;
147 QSize ExLineEdit::sizeHint() const
149 m_lineEdit->setFrame(true);
150 QSize size = m_lineEdit->sizeHint();
151 m_lineEdit->setFrame(false);
155 void ExLineEdit::focusInEvent(QFocusEvent *event)
157 m_lineEdit->event(event);
158 QWidget::focusInEvent(event);
161 void ExLineEdit::focusOutEvent(QFocusEvent *event)
163 m_lineEdit->event(event);
165 if (m_lineEdit->completer()) {
166 connect(m_lineEdit->completer(), SIGNAL(activated(QString)),
167 m_lineEdit, SLOT(setText(QString)));
168 connect(m_lineEdit->completer(), SIGNAL(highlighted(QString)),
169 m_lineEdit, SLOT(_q_completionHighlighted(QString)));
171 QWidget::focusOutEvent(event);
174 void ExLineEdit::keyPressEvent(QKeyEvent *event)
176 m_lineEdit->event(event);
177 QWidget::keyPressEvent(event);
180 bool ExLineEdit::event(QEvent *event)
182 if (event->type() == QEvent::ShortcutOverride)
183 m_lineEdit->event(event);
184 return QWidget::event(event);
187 void ExLineEdit::paintEvent(QPaintEvent *)
190 QStyleOptionFrameV2 panel;
191 initStyleOption(&panel);
192 style()->drawPrimitive(QStyle::PE_PanelLineEdit, &panel, &p, this);