author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Fri, 19 Feb 2010 23:40:16 +0200 | |
branch | RCL_3 |
changeset 4 | 3b1da2848fc7 |
parent 0 | 1918ee327afb |
child 30 | 5dc02b23752f |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
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 Qt Designer 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 "filterwidget_p.h" |
|
43 |
#include "iconloader_p.h" |
|
44 |
||
45 |
#include <QtGui/QVBoxLayout> |
|
46 |
#include <QtGui/QHBoxLayout> |
|
47 |
#include <QtGui/QPushButton> |
|
48 |
#include <QtGui/QLineEdit> |
|
49 |
#include <QtGui/QFocusEvent> |
|
50 |
#include <QtGui/QPalette> |
|
51 |
#include <QtGui/QCursor> |
|
52 |
||
53 |
#include <QtCore/QDebug> |
|
54 |
||
55 |
enum { debugFilter = 0 }; |
|
56 |
||
57 |
QT_BEGIN_NAMESPACE |
|
58 |
||
59 |
namespace qdesigner_internal { |
|
60 |
||
61 |
HintLineEdit::HintLineEdit(QWidget *parent) : |
|
62 |
QLineEdit(parent), |
|
63 |
m_defaultFocusPolicy(focusPolicy()), |
|
64 |
m_hintColor(QColor(0xbbbbbb)), |
|
65 |
m_refuseFocus(false), |
|
66 |
m_showingHintText(false) |
|
67 |
{ |
|
68 |
} |
|
69 |
||
70 |
bool HintLineEdit::refuseFocus() const |
|
71 |
{ |
|
72 |
return m_refuseFocus; |
|
73 |
} |
|
74 |
||
75 |
void HintLineEdit::setRefuseFocus(bool v) |
|
76 |
{ |
|
77 |
if (v == m_refuseFocus) |
|
78 |
return; |
|
79 |
m_refuseFocus = v; |
|
80 |
setFocusPolicy(m_refuseFocus ? Qt::NoFocus : m_defaultFocusPolicy); |
|
81 |
} |
|
82 |
||
83 |
void HintLineEdit::mousePressEvent(QMouseEvent *e) |
|
84 |
{ |
|
85 |
if (debugFilter) |
|
86 |
qDebug() << Q_FUNC_INFO; |
|
87 |
// Explicitly focus on click. |
|
88 |
if (m_refuseFocus && !hasFocus()) |
|
89 |
setFocus(Qt::OtherFocusReason); |
|
90 |
QLineEdit::mousePressEvent(e); |
|
91 |
} |
|
92 |
||
93 |
void HintLineEdit::focusInEvent(QFocusEvent *e) |
|
94 |
{ |
|
95 |
if (debugFilter) |
|
96 |
qDebug() << Q_FUNC_INFO; |
|
97 |
if (m_refuseFocus) { |
|
98 |
// Refuse the focus if the mouse it outside. In addition to the mouse |
|
99 |
// press logic, this prevents a re-focussing which occurs once |
|
100 |
// we actually had focus |
|
101 |
const Qt::FocusReason reason = e->reason(); |
|
102 |
if (reason == Qt::ActiveWindowFocusReason || reason == Qt::PopupFocusReason) { |
|
103 |
const QPoint mousePos = mapFromGlobal(QCursor::pos()); |
|
104 |
const bool refuse = !geometry().contains(mousePos); |
|
105 |
if (debugFilter) |
|
106 |
qDebug() << Q_FUNC_INFO << refuse ; |
|
107 |
if (refuse) { |
|
108 |
e->ignore(); |
|
109 |
return; |
|
110 |
} |
|
111 |
} |
|
112 |
} |
|
113 |
||
114 |
hideHintText(); |
|
115 |
QLineEdit::focusInEvent(e); |
|
116 |
} |
|
117 |
||
118 |
void HintLineEdit::focusOutEvent(QFocusEvent *e) |
|
119 |
{ |
|
120 |
if (debugFilter) |
|
121 |
qDebug() << Q_FUNC_INFO; |
|
122 |
// Focus out: Switch to displaying the hint text unless there is user input |
|
123 |
showHintText(); |
|
124 |
QLineEdit::focusOutEvent(e); |
|
125 |
} |
|
126 |
||
127 |
QString HintLineEdit::hintText() const |
|
128 |
{ |
|
129 |
return m_hintText; |
|
130 |
} |
|
131 |
||
132 |
void HintLineEdit::setHintText(const QString &ht) |
|
133 |
{ |
|
134 |
if (ht == m_hintText) |
|
135 |
return; |
|
136 |
hideHintText(); |
|
137 |
m_hintText = ht; |
|
138 |
if (!hasFocus() && !ht.isEmpty()) |
|
139 |
showHintText(); |
|
140 |
} |
|
141 |
||
142 |
void HintLineEdit::showHintText(bool force) |
|
143 |
{ |
|
144 |
if (m_showingHintText || m_hintText.isEmpty()) |
|
145 |
return; |
|
146 |
if (force || text().isEmpty()) { |
|
147 |
m_showingHintText = true; |
|
148 |
setText(m_hintText); |
|
149 |
setTextColor(m_hintColor, &m_textColor); |
|
150 |
} |
|
151 |
} |
|
152 |
void HintLineEdit::hideHintText() |
|
153 |
{ |
|
154 |
if (m_showingHintText && !m_hintText.isEmpty()) { |
|
155 |
m_showingHintText = false; |
|
156 |
setText(QString()); |
|
157 |
setTextColor(m_textColor); |
|
158 |
} |
|
159 |
} |
|
160 |
||
161 |
bool HintLineEdit::isShowingHintText() const |
|
162 |
{ |
|
163 |
return m_showingHintText; |
|
164 |
} |
|
165 |
||
166 |
QString HintLineEdit::typedText() const |
|
167 |
{ |
|
168 |
return m_showingHintText ? QString() : text(); |
|
169 |
} |
|
170 |
||
171 |
void HintLineEdit::setTextColor(const QColor &newColor, QColor *oldColor) |
|
172 |
{ |
|
173 |
QPalette pal = palette(); |
|
174 |
if (oldColor) |
|
175 |
*oldColor = pal.color(QPalette::Text); |
|
176 |
pal.setColor(QPalette::Text, newColor); |
|
177 |
setPalette(pal);} |
|
178 |
||
179 |
// ------------------- FilterWidget |
|
180 |
FilterWidget::FilterWidget(QWidget *parent, LayoutMode lm) : |
|
181 |
QWidget(parent), |
|
182 |
m_button(new QPushButton), |
|
183 |
m_editor(new HintLineEdit) |
|
184 |
{ |
|
185 |
m_editor->setHintText(tr("<Filter>")); |
|
186 |
QHBoxLayout *l = new QHBoxLayout(this); |
|
187 |
l->setMargin(0); |
|
188 |
l->setSpacing(0); |
|
189 |
||
190 |
if (lm == LayoutAlignRight) |
|
191 |
l->addItem(new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Minimum)); |
|
192 |
||
193 |
l->addWidget(m_editor); |
|
194 |
||
195 |
m_button->setIcon(createIconSet(QLatin1String("resetproperty.png"))); |
|
196 |
m_button->setIconSize(QSize(8, 8)); |
|
197 |
m_button->setFlat(true); |
|
198 |
l->addWidget(m_button); |
|
199 |
||
200 |
connect(m_button, SIGNAL(clicked()), this, SLOT(reset())); |
|
201 |
connect(m_editor, SIGNAL(textChanged(QString)), this, SLOT(checkButton(QString))); |
|
202 |
connect(m_editor, SIGNAL(textEdited(QString)), this, SIGNAL(filterChanged(QString))); |
|
203 |
} |
|
204 |
||
205 |
QString FilterWidget::text() const |
|
206 |
{ |
|
207 |
return m_editor->typedText(); |
|
208 |
} |
|
209 |
||
210 |
void FilterWidget::checkButton(const QString &) |
|
211 |
{ |
|
212 |
m_button->setEnabled(!text().isEmpty()); |
|
213 |
} |
|
214 |
||
215 |
void FilterWidget::reset() |
|
216 |
{ |
|
217 |
if (debugFilter) |
|
218 |
qDebug() << Q_FUNC_INFO; |
|
219 |
if (!text().isEmpty()) { |
|
220 |
// Editor has lost focus once this is pressed |
|
221 |
m_editor->showHintText(true); |
|
222 |
emit filterChanged(QString()); |
|
223 |
} |
|
224 |
} |
|
225 |
||
226 |
bool FilterWidget::refuseFocus() const |
|
227 |
{ |
|
228 |
return m_editor->refuseFocus(); |
|
229 |
} |
|
230 |
||
231 |
void FilterWidget::setRefuseFocus(bool v) |
|
232 |
{ |
|
233 |
m_editor->setRefuseFocus(v); |
|
234 |
} |
|
235 |
} // namespace qdesigner_internal |
|
236 |
||
237 |
QT_END_NAMESPACE |