]> git.sur5r.net Git - minitube/blob - src/urllineedit.cpp
Imported Upstream version 2.3
[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 <QEvent>
46
47 #include <QApplication>
48 #include <QCompleter>
49 #include <QFocusEvent>
50 #include <QHBoxLayout>
51 #include <QLabel>
52 #include <QLineEdit>
53 #include <QPainter>
54 #include <QStyle>
55 #include <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     m_lineEdit->setStyleSheet("background:transparent");
80     QPalette clearPalette = m_lineEdit->palette();
81     clearPalette.setBrush(QPalette::Base, QBrush(Qt::transparent));
82     m_lineEdit->setPalette(clearPalette);
83
84     // clearButton
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&)));
90 }
91
92 void ExLineEdit::setFont(const QFont &font) {
93     m_lineEdit->setFont(font);
94     updateGeometries();
95 }
96
97 void ExLineEdit::setLeftWidget(QWidget *widget)
98 {
99     m_leftWidget = widget;
100 }
101
102 QWidget *ExLineEdit::leftWidget() const
103 {
104     return m_leftWidget;
105 }
106
107 void ExLineEdit::resizeEvent(QResizeEvent *event)
108 {
109     Q_ASSERT(m_leftWidget);
110     updateGeometries();
111     QWidget::resizeEvent(event);
112 }
113
114 void ExLineEdit::updateGeometries()
115 {
116     QStyleOptionFrameV2 panel;
117     initStyleOption(&panel);
118     QRect rect = style()->subElementRect(QStyle::SE_LineEditContents, &panel, this);
119
120     int padding = 3;
121     // int height = rect.height() + padding*2;
122     int width = rect.width();
123
124     // int m_leftWidgetHeight = m_leftWidget->height();
125     m_leftWidget->setGeometry(rect.x() + 2,          0,
126                               m_leftWidget->width(), m_leftWidget->height());
127
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);
131
132     m_clearButton->setGeometry(this->width() - clearButtonWidth, 0,
133                                clearButtonWidth, this->height());
134 }
135
136 void ExLineEdit::initStyleOption(QStyleOptionFrameV2 *option) const
137 {
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
146     if (hasEditFocus())
147         option->state |= QStyle::State_HasEditFocus;
148 #endif
149     option->features = QStyleOptionFrameV2::None;
150 }
151
152 QSize ExLineEdit::sizeHint() const
153 {
154     m_lineEdit->setFrame(true);
155     QSize size = m_lineEdit->sizeHint();
156     m_lineEdit->setFrame(false);
157     size = size + QSize(3, 3);
158     return size;
159 }
160
161 void ExLineEdit::focusInEvent(QFocusEvent *event)
162 {
163     m_lineEdit->event(event);
164     QWidget::focusInEvent(event);
165 }
166
167 void ExLineEdit::focusOutEvent(QFocusEvent *event)
168 {
169     m_lineEdit->event(event);
170
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)));
176     }
177     QWidget::focusOutEvent(event);
178 }
179
180 void ExLineEdit::keyPressEvent(QKeyEvent *event)
181 {
182     if (event->key() == Qt::Key_Escape && !m_lineEdit->text().isEmpty()) {
183         m_lineEdit->clear();
184     }
185     m_lineEdit->event(event);
186     QWidget::keyPressEvent(event);
187 }
188
189 bool ExLineEdit::event(QEvent *event)
190 {
191     if (event->type() == QEvent::ShortcutOverride ||
192         event->type() == QEvent::InputMethod)
193         m_lineEdit->event(event);
194     return QWidget::event(event);
195 }
196
197 void ExLineEdit::paintEvent(QPaintEvent *)
198 {
199     QPainter p(this);
200     QStyleOptionFrameV2 panel;
201     initStyleOption(&panel);
202     style()->drawPrimitive(QStyle::PE_PanelLineEdit, &panel, &p, this);
203 }