1 /****************************************************************************
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 ** This file is part of the demonstration applications of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
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.
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.
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
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.
36 ** If you are unsure which license is appropriate for your use, please
37 ** contact the sales department at http://www.qtsoftware.com/contact.
40 ****************************************************************************/
42 #include "urllineedit.h"
43 #include "searchlineedit.h"
47 #include <QApplication>
49 #include <QFocusEvent>
50 #include <QHBoxLayout>
55 #include <QStyleOptionFrameV2>
57 #include <QtCore/QDebug>
59 ExLineEdit::ExLineEdit(QWidget *parent)
62 , m_lineEdit(new QLineEdit(this))
65 setFocusPolicy(m_lineEdit->focusPolicy());
66 setAttribute(Qt::WA_InputMethodEnabled);
67 setSizePolicy(m_lineEdit->sizePolicy());
68 setBackgroundRole(m_lineEdit->backgroundRole());
69 setMouseTracking(true);
71 setAttribute(Qt::WA_MacShowFocusRect, true);
72 QPalette p = m_lineEdit->palette();
76 m_lineEdit->setFrame(false);
77 m_lineEdit->setFocusProxy(this);
78 m_lineEdit->setAttribute(Qt::WA_MacShowFocusRect, false);
79 m_lineEdit->setStyleSheet("background:transparent");
80 QPalette clearPalette = m_lineEdit->palette();
81 clearPalette.setBrush(QPalette::Base, QBrush(Qt::transparent));
82 m_lineEdit->setPalette(clearPalette);
85 m_clearButton = new ClearButton(this);
86 connect(m_clearButton, SIGNAL(clicked()),
87 m_lineEdit, SLOT(clear()));
88 connect(m_lineEdit, SIGNAL(textChanged(const QString&)),
89 m_clearButton, SLOT(textChanged(const QString&)));
92 void ExLineEdit::setFont(const QFont &font) {
93 m_lineEdit->setFont(font);
97 void ExLineEdit::setLeftWidget(QWidget *widget)
99 m_leftWidget = widget;
102 QWidget *ExLineEdit::leftWidget() const
107 void ExLineEdit::resizeEvent(QResizeEvent *event)
109 Q_ASSERT(m_leftWidget);
111 QWidget::resizeEvent(event);
114 void ExLineEdit::updateGeometries()
116 QStyleOptionFrameV2 panel;
117 initStyleOption(&panel);
118 QRect rect = style()->subElementRect(QStyle::SE_LineEditContents, &panel, this);
121 // int height = rect.height() + padding*2;
122 int width = rect.width();
124 // int m_leftWidgetHeight = m_leftWidget->height();
125 m_leftWidget->setGeometry(rect.x() + 2, 0,
126 m_leftWidget->width(), m_leftWidget->height());
128 int clearButtonWidth = this->height();
129 m_lineEdit->setGeometry(m_leftWidget->x() + m_leftWidget->width(), padding,
130 width - clearButtonWidth - m_leftWidget->width(), this->height() - padding*2);
132 m_clearButton->setGeometry(this->width() - clearButtonWidth, 0,
133 clearButtonWidth, this->height());
136 void ExLineEdit::initStyleOption(QStyleOptionFrameV2 *option) const
138 option->initFrom(this);
139 option->rect = contentsRect();
140 option->lineWidth = style()->pixelMetric(QStyle::PM_DefaultFrameWidth, option, this);
141 option->midLineWidth = 0;
142 option->state |= QStyle::State_Sunken;
143 if (m_lineEdit->isReadOnly())
144 option->state |= QStyle::State_ReadOnly;
145 #ifdef QT_KEYPAD_NAVIGATION
147 option->state |= QStyle::State_HasEditFocus;
149 option->features = QStyleOptionFrameV2::None;
152 QSize ExLineEdit::sizeHint() const
154 m_lineEdit->setFrame(true);
155 QSize size = m_lineEdit->sizeHint();
156 m_lineEdit->setFrame(false);
157 size = size + QSize(3, 3);
161 void ExLineEdit::focusInEvent(QFocusEvent *event)
163 m_lineEdit->event(event);
164 QWidget::focusInEvent(event);
167 void ExLineEdit::focusOutEvent(QFocusEvent *event)
169 m_lineEdit->event(event);
171 if (m_lineEdit->completer()) {
172 connect(m_lineEdit->completer(), SIGNAL(activated(QString)),
173 m_lineEdit, SLOT(setText(QString)));
174 connect(m_lineEdit->completer(), SIGNAL(highlighted(QString)),
175 m_lineEdit, SLOT(_q_completionHighlighted(QString)));
177 QWidget::focusOutEvent(event);
180 void ExLineEdit::keyPressEvent(QKeyEvent *event)
182 if (event->key() == Qt::Key_Escape && !m_lineEdit->text().isEmpty()) {
185 m_lineEdit->event(event);
186 QWidget::keyPressEvent(event);
189 bool ExLineEdit::event(QEvent *event)
191 if (event->type() == QEvent::ShortcutOverride ||
192 event->type() == QEvent::InputMethod)
193 m_lineEdit->event(event);
194 return QWidget::event(event);
197 void ExLineEdit::paintEvent(QPaintEvent *)
200 QStyleOptionFrameV2 panel;
201 initStyleOption(&panel);
202 style()->drawPrimitive(QStyle::PE_PanelLineEdit, &panel, &p, this);