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"
45 #include <QtCore/QEvent>
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>
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 QPalette clearPalette = m_lineEdit->palette();
80 clearPalette.setBrush(QPalette::Base, QBrush(Qt::transparent));
81 m_lineEdit->setPalette(clearPalette);
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&)));
91 void ExLineEdit::setFont(const QFont &font) {
92 m_lineEdit->setFont(font);
96 void ExLineEdit::setLeftWidget(QWidget *widget)
98 m_leftWidget = widget;
101 QWidget *ExLineEdit::leftWidget() const
106 void ExLineEdit::resizeEvent(QResizeEvent *event)
108 Q_ASSERT(m_leftWidget);
110 QWidget::resizeEvent(event);
113 void ExLineEdit::updateGeometries()
115 QStyleOptionFrameV2 panel;
116 initStyleOption(&panel);
117 QRect rect = style()->subElementRect(QStyle::SE_LineEditContents, &panel, this);
120 // int height = rect.height() + padding*2;
121 int width = rect.width();
123 // int m_leftWidgetHeight = m_leftWidget->height();
124 m_leftWidget->setGeometry(rect.x() + 2, 0,
125 m_leftWidget->width(), m_leftWidget->height());
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);
131 m_clearButton->setGeometry(this->width() - clearButtonWidth, 0,
132 clearButtonWidth, this->height());
135 void ExLineEdit::initStyleOption(QStyleOptionFrameV2 *option) const
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
146 option->state |= QStyle::State_HasEditFocus;
148 option->features = QStyleOptionFrameV2::None;
151 QSize ExLineEdit::sizeHint() const
153 m_lineEdit->setFrame(true);
154 QSize size = m_lineEdit->sizeHint();
155 m_lineEdit->setFrame(false);
156 size = size + QSize(3, 3);
160 void ExLineEdit::focusInEvent(QFocusEvent *event)
162 m_lineEdit->event(event);
163 QWidget::focusInEvent(event);
166 void ExLineEdit::focusOutEvent(QFocusEvent *event)
168 m_lineEdit->event(event);
170 if (m_lineEdit->completer()) {
171 connect(m_lineEdit->completer(), SIGNAL(activated(QString)),
172 m_lineEdit, SLOT(setText(QString)));
173 connect(m_lineEdit->completer(), SIGNAL(highlighted(QString)),
174 m_lineEdit, SLOT(_q_completionHighlighted(QString)));
176 QWidget::focusOutEvent(event);
179 void ExLineEdit::keyPressEvent(QKeyEvent *event)
181 m_lineEdit->event(event);
182 QWidget::keyPressEvent(event);
185 bool ExLineEdit::event(QEvent *event)
187 if (event->type() == QEvent::ShortcutOverride ||
188 event->type() == QEvent::InputMethod)
189 m_lineEdit->event(event);
190 return QWidget::event(event);
193 void ExLineEdit::paintEvent(QPaintEvent *)
196 QStyleOptionFrameV2 panel;
197 initStyleOption(&panel);
198 style()->drawPrimitive(QStyle::PE_PanelLineEdit, &panel, &p, this);