author | Eckhart Koeppen <eckhart.koppen@nokia.com> |
Thu, 29 Apr 2010 15:15:16 +0300 | |
branch | RCL_3 |
changeset 16 | 4b6ee5efea19 |
parent 4 | 3b1da2848fc7 |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 4 |
** All rights reserved. |
5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 |
** |
|
7 |
** This file is part of the demonstration applications of the Qt Toolkit. |
|
8 |
** |
|
9 |
** $QT_BEGIN_LICENSE:LGPL$ |
|
10 |
** No Commercial Usage |
|
11 |
** This file contains pre-release code and may not be distributed. |
|
12 |
** You may use this file in accordance with the terms and conditions |
|
13 |
** contained in the Technology Preview License Agreement accompanying |
|
14 |
** this package. |
|
15 |
** |
|
16 |
** GNU Lesser General Public License Usage |
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 |
** General Public License version 2.1 as published by the Free Software |
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 |
** packaging of this file. Please review the following information to |
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 |
** |
|
24 |
** In addition, as a special exception, Nokia gives you certain additional |
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 |
** |
|
28 |
** If you have questions regarding the use of this file, please contact |
|
29 |
** Nokia at qt-info@nokia.com. |
|
30 |
** |
|
31 |
** |
|
32 |
** |
|
33 |
** |
|
34 |
** |
|
35 |
** |
|
36 |
** |
|
37 |
** |
|
38 |
** $QT_END_LICENSE$ |
|
39 |
** |
|
40 |
****************************************************************************/ |
|
41 |
||
42 |
#include "searchlineedit.h" |
|
43 |
||
44 |
#include <QtGui/QPainter> |
|
45 |
#include <QtGui/QMouseEvent> |
|
46 |
#include <QtGui/QMenu> |
|
47 |
#include <QtGui/QStyle> |
|
48 |
#include <QtGui/QStyleOptionFrameV2> |
|
49 |
||
50 |
ClearButton::ClearButton(QWidget *parent) |
|
51 |
: QAbstractButton(parent) |
|
52 |
{ |
|
53 |
#ifndef QT_NO_CURSOR |
|
54 |
setCursor(Qt::ArrowCursor); |
|
55 |
#endif // QT_NO_CURSOR |
|
56 |
setToolTip(tr("Clear")); |
|
57 |
setVisible(false); |
|
58 |
setFocusPolicy(Qt::NoFocus); |
|
59 |
} |
|
60 |
||
61 |
void ClearButton::paintEvent(QPaintEvent *event) |
|
62 |
{ |
|
63 |
Q_UNUSED(event); |
|
64 |
QPainter painter(this); |
|
65 |
int height = this->height(); |
|
66 |
||
67 |
painter.setRenderHint(QPainter::Antialiasing, true); |
|
68 |
QColor color = palette().color(QPalette::Mid); |
|
69 |
painter.setBrush(isDown() |
|
70 |
? palette().color(QPalette::Dark) |
|
71 |
: palette().color(QPalette::Mid)); |
|
72 |
painter.setPen(painter.brush().color()); |
|
73 |
int size = width(); |
|
74 |
int offset = size / 5; |
|
75 |
int radius = size - offset * 2; |
|
76 |
painter.drawEllipse(offset, offset, radius, radius); |
|
77 |
||
78 |
painter.setPen(palette().color(QPalette::Base)); |
|
79 |
int border = offset * 2; |
|
80 |
painter.drawLine(border, border, width() - border, height - border); |
|
81 |
painter.drawLine(border, height - border, width() - border, border); |
|
82 |
} |
|
83 |
||
84 |
void ClearButton::textChanged(const QString &text) |
|
85 |
{ |
|
86 |
setVisible(!text.isEmpty()); |
|
87 |
} |
|
88 |
||
89 |
/* |
|
90 |
Search icon on the left hand side of the search widget |
|
91 |
When a menu is set a down arrow appears |
|
92 |
*/ |
|
93 |
class SearchButton : public QAbstractButton { |
|
94 |
public: |
|
95 |
SearchButton(QWidget *parent = 0); |
|
96 |
void paintEvent(QPaintEvent *event); |
|
97 |
QMenu *m_menu; |
|
98 |
||
99 |
protected: |
|
100 |
void mousePressEvent(QMouseEvent *event); |
|
101 |
}; |
|
102 |
||
103 |
SearchButton::SearchButton(QWidget *parent) |
|
104 |
: QAbstractButton(parent), |
|
105 |
m_menu(0) |
|
106 |
{ |
|
107 |
setObjectName(QLatin1String("SearchButton")); |
|
108 |
#ifndef QT_NO_CURSOR |
|
109 |
setCursor(Qt::ArrowCursor); |
|
110 |
#endif //QT_NO_CURSOR |
|
111 |
setFocusPolicy(Qt::NoFocus); |
|
112 |
} |
|
113 |
||
114 |
void SearchButton::mousePressEvent(QMouseEvent *event) |
|
115 |
{ |
|
116 |
if (m_menu && event->button() == Qt::LeftButton) { |
|
117 |
QWidget *p = parentWidget(); |
|
118 |
if (p) { |
|
119 |
QPoint r = p->mapToGlobal(QPoint(0, p->height())); |
|
120 |
m_menu->exec(QPoint(r.x() + height() / 2, r.y())); |
|
121 |
} |
|
122 |
event->accept(); |
|
123 |
} |
|
124 |
QAbstractButton::mousePressEvent(event); |
|
125 |
} |
|
126 |
||
127 |
void SearchButton::paintEvent(QPaintEvent *event) |
|
128 |
{ |
|
129 |
Q_UNUSED(event); |
|
130 |
QPainterPath myPath; |
|
131 |
||
132 |
int radius = (height() / 5) * 2; |
|
133 |
QRect circle(height() / 3 - 1, height() / 4, radius, radius); |
|
134 |
myPath.addEllipse(circle); |
|
135 |
||
136 |
myPath.arcMoveTo(circle, 300); |
|
137 |
QPointF c = myPath.currentPosition(); |
|
138 |
int diff = height() / 7; |
|
139 |
myPath.lineTo(qMin(width() - 2, (int)c.x() + diff), c.y() + diff); |
|
140 |
||
141 |
QPainter painter(this); |
|
142 |
painter.setRenderHint(QPainter::Antialiasing, true); |
|
143 |
painter.setPen(QPen(Qt::darkGray, 2)); |
|
144 |
painter.drawPath(myPath); |
|
145 |
||
146 |
if (m_menu) { |
|
147 |
QPainterPath dropPath; |
|
148 |
dropPath.arcMoveTo(circle, 320); |
|
149 |
QPointF c = dropPath.currentPosition(); |
|
150 |
c = QPointF(c.x() + 3.5, c.y() + 0.5); |
|
151 |
dropPath.moveTo(c); |
|
152 |
dropPath.lineTo(c.x() + 4, c.y()); |
|
153 |
dropPath.lineTo(c.x() + 2, c.y() + 2); |
|
154 |
dropPath.closeSubpath(); |
|
155 |
painter.setPen(Qt::darkGray); |
|
156 |
painter.setBrush(Qt::darkGray); |
|
157 |
painter.setRenderHint(QPainter::Antialiasing, false); |
|
158 |
painter.drawPath(dropPath); |
|
159 |
} |
|
160 |
painter.end(); |
|
161 |
} |
|
162 |
||
163 |
/* |
|
164 |
SearchLineEdit is an enhanced QLineEdit |
|
165 |
- A Search icon on the left with optional menu |
|
166 |
- When there is no text and doesn't have focus an "inactive text" is displayed |
|
167 |
- When there is text a clear button is displayed on the right hand side |
|
168 |
*/ |
|
169 |
SearchLineEdit::SearchLineEdit(QWidget *parent) : ExLineEdit(parent), |
|
170 |
m_searchButton(new SearchButton(this)) |
|
171 |
{ |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
172 |
connect(lineEdit(), SIGNAL(textChanged(QString)), |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
173 |
this, SIGNAL(textChanged(QString))); |
0 | 174 |
setLeftWidget(m_searchButton); |
175 |
m_inactiveText = tr("Search"); |
|
176 |
||
177 |
QSizePolicy policy = sizePolicy(); |
|
178 |
setSizePolicy(QSizePolicy::Preferred, policy.verticalPolicy()); |
|
179 |
} |
|
180 |
||
181 |
void SearchLineEdit::paintEvent(QPaintEvent *event) |
|
182 |
{ |
|
183 |
if (lineEdit()->text().isEmpty() && !hasFocus() && !m_inactiveText.isEmpty()) { |
|
184 |
ExLineEdit::paintEvent(event); |
|
185 |
QStyleOptionFrameV2 panel; |
|
186 |
initStyleOption(&panel); |
|
187 |
QRect r = style()->subElementRect(QStyle::SE_LineEditContents, &panel, this); |
|
188 |
QFontMetrics fm = fontMetrics(); |
|
189 |
int horizontalMargin = lineEdit()->x(); |
|
190 |
QRect lineRect(horizontalMargin + r.x(), r.y() + (r.height() - fm.height() + 1) / 2, |
|
191 |
r.width() - 2 * horizontalMargin, fm.height()); |
|
192 |
QPainter painter(this); |
|
193 |
painter.setPen(palette().brush(QPalette::Disabled, QPalette::Text).color()); |
|
194 |
painter.drawText(lineRect, Qt::AlignLeft|Qt::AlignVCenter, m_inactiveText); |
|
195 |
} else { |
|
196 |
ExLineEdit::paintEvent(event); |
|
197 |
} |
|
198 |
} |
|
199 |
||
200 |
void SearchLineEdit::resizeEvent(QResizeEvent *event) |
|
201 |
{ |
|
202 |
updateGeometries(); |
|
203 |
ExLineEdit::resizeEvent(event); |
|
204 |
} |
|
205 |
||
206 |
void SearchLineEdit::updateGeometries() |
|
207 |
{ |
|
208 |
int menuHeight = height(); |
|
209 |
int menuWidth = menuHeight + 1; |
|
210 |
if (!m_searchButton->m_menu) |
|
211 |
menuWidth = (menuHeight / 5) * 4; |
|
212 |
m_searchButton->resize(QSize(menuWidth, menuHeight)); |
|
213 |
} |
|
214 |
||
215 |
QString SearchLineEdit::inactiveText() const |
|
216 |
{ |
|
217 |
return m_inactiveText; |
|
218 |
} |
|
219 |
||
220 |
void SearchLineEdit::setInactiveText(const QString &text) |
|
221 |
{ |
|
222 |
m_inactiveText = text; |
|
223 |
} |
|
224 |
||
225 |
void SearchLineEdit::setMenu(QMenu *menu) |
|
226 |
{ |
|
227 |
if (m_searchButton->m_menu) |
|
228 |
m_searchButton->m_menu->deleteLater(); |
|
229 |
m_searchButton->m_menu = menu; |
|
230 |
updateGeometries(); |
|
231 |
} |
|
232 |
||
233 |
QMenu *SearchLineEdit::menu() const |
|
234 |
{ |
|
235 |
if (!m_searchButton->m_menu) { |
|
236 |
m_searchButton->m_menu = new QMenu(m_searchButton); |
|
237 |
if (isVisible()) |
|
238 |
(const_cast<SearchLineEdit*>(this))->updateGeometries(); |
|
239 |
} |
|
240 |
return m_searchButton->m_menu; |
|
241 |
} |
|
242 |