]> git.sur5r.net Git - minitube/blob - src/urllineedit.cpp
Japanese translation credits
[minitube] / src / urllineedit.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: Qt Software Information (qt-info@nokia.com)
5 **
6 ** This file is part of the demonstration applications of the Qt Toolkit.
7 **
8 ** Commercial Usage
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.
13 **
14 **
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.
26 **
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.
32 **
33 ** If you are unsure which license is appropriate for your use, please
34 ** contact the sales department at qt-sales@nokia.com.
35 **
36 ****************************************************************************/
37
38 #include "urllineedit.h"
39 #include "searchlineedit.h"
40
41 #include <QtCore/QEvent>
42
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>
52
53 #include <QtCore/QDebug>
54
55 ExLineEdit::ExLineEdit(QWidget *parent)
56     : QWidget(parent)
57     , m_leftWidget(0)
58     , m_lineEdit(new QLineEdit(this))
59     , m_clearButton(0)
60 {
61     setFocusPolicy(m_lineEdit->focusPolicy());
62     setAttribute(Qt::WA_InputMethodEnabled);
63     setSizePolicy(m_lineEdit->sizePolicy());
64     setBackgroundRole(m_lineEdit->backgroundRole());
65     setMouseTracking(true);
66     setAcceptDrops(true);
67     setAttribute(Qt::WA_MacShowFocusRect, true);
68     QPalette p = m_lineEdit->palette();
69     setPalette(p);
70
71     // line edit
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);
78
79     // clearButton
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&)));
85 }
86
87 void ExLineEdit::setFont(const QFont &font) {
88     m_lineEdit->setFont(font);
89     updateGeometries();
90 }
91
92 void ExLineEdit::setLeftWidget(QWidget *widget)
93 {
94     m_leftWidget = widget;
95 }
96
97 QWidget *ExLineEdit::leftWidget() const
98 {
99     return m_leftWidget;
100 }
101
102 void ExLineEdit::resizeEvent(QResizeEvent *event)
103 {
104     Q_ASSERT(m_leftWidget);
105     updateGeometries();
106     QWidget::resizeEvent(event);
107 }
108
109 void ExLineEdit::updateGeometries()
110 {
111     QStyleOptionFrameV2 panel;
112     initStyleOption(&panel);
113     QRect rect = style()->subElementRect(QStyle::SE_LineEditContents, &panel, this);
114
115     int padding = 3;
116     // int height = rect.height() + padding*2;
117     int width = rect.width();
118
119     // int m_leftWidgetHeight = m_leftWidget->height();
120     m_leftWidget->setGeometry(rect.x() + 2,          0,
121                               m_leftWidget->width(), m_leftWidget->height());
122
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);
126
127     m_clearButton->setGeometry(this->width() - clearButtonWidth, 0,
128                                clearButtonWidth, this->height());
129 }
130
131 void ExLineEdit::initStyleOption(QStyleOptionFrameV2 *option) const
132 {
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
141     if (hasEditFocus())
142         option->state |= QStyle::State_HasEditFocus;
143 #endif
144     option->features = QStyleOptionFrameV2::None;
145 }
146
147 QSize ExLineEdit::sizeHint() const
148 {
149     m_lineEdit->setFrame(true);
150     QSize size = m_lineEdit->sizeHint();
151     m_lineEdit->setFrame(false);
152     return size;
153 }
154
155 void ExLineEdit::focusInEvent(QFocusEvent *event)
156 {
157     m_lineEdit->event(event);
158     QWidget::focusInEvent(event);
159 }
160
161 void ExLineEdit::focusOutEvent(QFocusEvent *event)
162 {
163     m_lineEdit->event(event);
164
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)));
170     }
171     QWidget::focusOutEvent(event);
172 }
173
174 void ExLineEdit::keyPressEvent(QKeyEvent *event)
175 {
176     m_lineEdit->event(event);
177     QWidget::keyPressEvent(event);
178 }
179
180 bool ExLineEdit::event(QEvent *event)
181 {
182     if (event->type() == QEvent::ShortcutOverride)
183         m_lineEdit->event(event);
184     return QWidget::event(event);
185 }
186
187 void ExLineEdit::paintEvent(QPaintEvent *)
188 {
189     QPainter p(this);
190     QStyleOptionFrameV2 panel;
191     initStyleOption(&panel);
192     style()->drawPrimitive(QStyle::PE_PanelLineEdit, &panel, &p, this);
193 }