author | Eckhart Koeppen <eckhart.koppen@nokia.com> |
Fri, 16 Apr 2010 11:39:52 +0300 | |
branch | RCL_3 |
changeset 9 | 740e5562c97f |
parent 8 | 3f74d0d4af4c |
child 25 | e24348a560a6 |
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 QtGui module 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 |
#ifndef QCOMBOBOX_P_H |
|
43 |
#define QCOMBOBOX_P_H |
|
44 |
||
45 |
// |
|
46 |
// W A R N I N G |
|
47 |
// ------------- |
|
48 |
// |
|
49 |
// This file is not part of the Qt API. It exists purely as an |
|
50 |
// implementation detail. This header file may change from version to |
|
51 |
// version without notice, or even be removed. |
|
52 |
// |
|
53 |
// We mean it. |
|
54 |
// |
|
55 |
||
56 |
#include "QtGui/qcombobox.h" |
|
57 |
||
58 |
#ifndef QT_NO_COMBOBOX |
|
59 |
#include "QtGui/qabstractslider.h" |
|
60 |
#include "QtGui/qapplication.h" |
|
61 |
#include "QtGui/qitemdelegate.h" |
|
62 |
#include "QtGui/qstandarditemmodel.h" |
|
63 |
#include "QtGui/qlineedit.h" |
|
64 |
#include "QtGui/qlistview.h" |
|
65 |
#include "QtGui/qpainter.h" |
|
66 |
#include "QtGui/qstyle.h" |
|
67 |
#include "QtGui/qstyleoption.h" |
|
68 |
#include "QtCore/qhash.h" |
|
69 |
#include "QtCore/qpair.h" |
|
70 |
#include "QtCore/qtimer.h" |
|
71 |
#include "private/qwidget_p.h" |
|
72 |
#include "QtCore/qpointer.h" |
|
73 |
#include "QtGui/qcompleter.h" |
|
74 |
#include "QtGui/qevent.h" |
|
75 |
#include "QtCore/qdebug.h" |
|
76 |
||
77 |
#include <limits.h> |
|
78 |
||
79 |
QT_BEGIN_NAMESPACE |
|
80 |
||
81 |
class QAction; |
|
82 |
||
83 |
class QComboBoxListView : public QListView |
|
84 |
{ |
|
85 |
Q_OBJECT |
|
86 |
public: |
|
87 |
QComboBoxListView(QComboBox *cmb = 0) : combo(cmb) {} |
|
88 |
||
89 |
protected: |
|
90 |
void resizeEvent(QResizeEvent *event) |
|
91 |
{ |
|
92 |
resizeContents(viewport()->width(), contentsSize().height()); |
|
93 |
QListView::resizeEvent(event); |
|
94 |
} |
|
95 |
||
96 |
QStyleOptionViewItem viewOptions() const |
|
97 |
{ |
|
98 |
QStyleOptionViewItem option = QListView::viewOptions(); |
|
99 |
option.showDecorationSelected = true; |
|
100 |
if (combo) |
|
101 |
option.font = combo->font(); |
|
102 |
return option; |
|
103 |
} |
|
104 |
||
105 |
void paintEvent(QPaintEvent *e) |
|
106 |
{ |
|
107 |
if (combo) { |
|
108 |
QStyleOptionComboBox opt; |
|
109 |
opt.initFrom(combo); |
|
110 |
opt.editable = combo->isEditable(); |
|
111 |
if (combo->style()->styleHint(QStyle::SH_ComboBox_Popup, &opt, combo)) { |
|
112 |
//we paint the empty menu area to avoid having blank space that can happen when scrolling |
|
113 |
QStyleOptionMenuItem menuOpt; |
|
114 |
menuOpt.initFrom(this); |
|
115 |
menuOpt.palette = palette(); |
|
116 |
menuOpt.state = QStyle::State_None; |
|
117 |
menuOpt.checkType = QStyleOptionMenuItem::NotCheckable; |
|
118 |
menuOpt.menuRect = e->rect(); |
|
119 |
menuOpt.maxIconWidth = 0; |
|
120 |
menuOpt.tabWidth = 0; |
|
121 |
QPainter p(viewport()); |
|
122 |
combo->style()->drawControl(QStyle::CE_MenuEmptyArea, &menuOpt, &p, this); |
|
123 |
} |
|
124 |
} |
|
125 |
QListView::paintEvent(e); |
|
126 |
} |
|
127 |
||
128 |
private: |
|
129 |
QComboBox *combo; |
|
130 |
}; |
|
131 |
||
132 |
||
133 |
class QStandardItemModel; |
|
134 |
||
135 |
class Q_AUTOTEST_EXPORT QComboBoxPrivateScroller : public QWidget |
|
136 |
{ |
|
137 |
Q_OBJECT |
|
138 |
||
139 |
public: |
|
140 |
QComboBoxPrivateScroller(QAbstractSlider::SliderAction action, QWidget *parent) |
|
141 |
: QWidget(parent), sliderAction(action) |
|
142 |
{ |
|
143 |
setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed); |
|
144 |
setAttribute(Qt::WA_NoMousePropagation); |
|
145 |
} |
|
146 |
QSize sizeHint() const { |
|
147 |
return QSize(20, style()->pixelMetric(QStyle::PM_MenuScrollerHeight)); |
|
148 |
} |
|
149 |
||
150 |
protected: |
|
151 |
inline void stopTimer() { |
|
152 |
timer.stop(); |
|
153 |
} |
|
154 |
||
155 |
inline void startTimer() { |
|
156 |
timer.start(100, this); |
|
157 |
fast = false; |
|
158 |
} |
|
159 |
||
160 |
void enterEvent(QEvent *) { |
|
161 |
startTimer(); |
|
162 |
} |
|
163 |
||
164 |
void leaveEvent(QEvent *) { |
|
165 |
stopTimer(); |
|
166 |
} |
|
167 |
void timerEvent(QTimerEvent *e) { |
|
168 |
if (e->timerId() == timer.timerId()) { |
|
169 |
emit doScroll(sliderAction); |
|
170 |
if (fast) { |
|
171 |
emit doScroll(sliderAction); |
|
172 |
emit doScroll(sliderAction); |
|
173 |
} |
|
174 |
} |
|
175 |
} |
|
176 |
void hideEvent(QHideEvent *) { |
|
177 |
stopTimer(); |
|
178 |
} |
|
179 |
||
180 |
void mouseMoveEvent(QMouseEvent *e) |
|
181 |
{ |
|
182 |
// Enable fast scrolling if the cursor is directly above or below the popup. |
|
183 |
const int mouseX = e->pos().x(); |
|
184 |
const int mouseY = e->pos().y(); |
|
185 |
const bool horizontallyInside = pos().x() < mouseX && mouseX < rect().right() + 1; |
|
186 |
const bool verticallyOutside = (sliderAction == QAbstractSlider::SliderSingleStepAdd) ? |
|
187 |
rect().bottom() + 1 < mouseY : mouseY < pos().y(); |
|
188 |
||
189 |
fast = horizontallyInside && verticallyOutside; |
|
190 |
} |
|
191 |
||
192 |
void paintEvent(QPaintEvent *) { |
|
193 |
QPainter p(this); |
|
194 |
QStyleOptionMenuItem menuOpt; |
|
195 |
menuOpt.init(this); |
|
196 |
menuOpt.checkType = QStyleOptionMenuItem::NotCheckable; |
|
197 |
menuOpt.menuRect = rect(); |
|
198 |
menuOpt.maxIconWidth = 0; |
|
199 |
menuOpt.tabWidth = 0; |
|
200 |
menuOpt.menuItemType = QStyleOptionMenuItem::Scroller; |
|
201 |
if (sliderAction == QAbstractSlider::SliderSingleStepAdd) |
|
202 |
menuOpt.state |= QStyle::State_DownArrow; |
|
203 |
p.eraseRect(rect()); |
|
204 |
style()->drawControl(QStyle::CE_MenuScroller, &menuOpt, &p); |
|
205 |
} |
|
206 |
||
207 |
Q_SIGNALS: |
|
208 |
void doScroll(int action); |
|
209 |
||
210 |
private: |
|
211 |
QAbstractSlider::SliderAction sliderAction; |
|
212 |
QBasicTimer timer; |
|
213 |
bool fast; |
|
214 |
}; |
|
215 |
||
216 |
class Q_AUTOTEST_EXPORT QComboBoxPrivateContainer : public QFrame |
|
217 |
{ |
|
218 |
Q_OBJECT |
|
219 |
||
220 |
public: |
|
221 |
QComboBoxPrivateContainer(QAbstractItemView *itemView, QComboBox *parent); |
|
222 |
QAbstractItemView *itemView() const; |
|
223 |
void setItemView(QAbstractItemView *itemView); |
|
224 |
int spacing() const; |
|
225 |
void updateTopBottomMargin(); |
|
226 |
||
227 |
QTimer blockMouseReleaseTimer; |
|
228 |
QBasicTimer adjustSizeTimer; |
|
229 |
QPoint initialClickPosition; |
|
230 |
||
231 |
public Q_SLOTS: |
|
232 |
void scrollItemView(int action); |
|
233 |
void updateScrollers(); |
|
234 |
void viewDestroyed(); |
|
235 |
||
236 |
protected: |
|
237 |
void changeEvent(QEvent *e); |
|
238 |
bool eventFilter(QObject *o, QEvent *e); |
|
239 |
void mousePressEvent(QMouseEvent *e); |
|
240 |
void mouseReleaseEvent(QMouseEvent *e); |
|
241 |
void showEvent(QShowEvent *e); |
|
242 |
void hideEvent(QHideEvent *e); |
|
243 |
void timerEvent(QTimerEvent *timerEvent); |
|
244 |
void leaveEvent(QEvent *e); |
|
245 |
void resizeEvent(QResizeEvent *e); |
|
246 |
QStyleOptionComboBox comboStyleOption() const; |
|
247 |
||
248 |
Q_SIGNALS: |
|
249 |
void itemSelected(const QModelIndex &); |
|
250 |
void resetButton(); |
|
251 |
||
252 |
private: |
|
253 |
QComboBox *combo; |
|
254 |
QAbstractItemView *view; |
|
255 |
QComboBoxPrivateScroller *top; |
|
256 |
QComboBoxPrivateScroller *bottom; |
|
257 |
#ifdef QT_SOFTKEYS_ENABLED |
|
258 |
QAction *selectAction; |
|
259 |
QAction *cancelAction; |
|
260 |
#endif |
|
261 |
}; |
|
262 |
||
263 |
class QComboMenuDelegate : public QAbstractItemDelegate |
|
264 |
{ Q_OBJECT |
|
265 |
public: |
|
266 |
QComboMenuDelegate(QObject *parent, QComboBox *cmb) : QAbstractItemDelegate(parent), mCombo(cmb) {} |
|
267 |
||
268 |
protected: |
|
269 |
void paint(QPainter *painter, |
|
270 |
const QStyleOptionViewItem &option, |
|
271 |
const QModelIndex &index) const { |
|
272 |
QStyleOptionMenuItem opt = getStyleOption(option, index); |
|
8
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
273 |
#ifndef Q_WS_S60 |
0 | 274 |
painter->fillRect(option.rect, opt.palette.background()); |
8
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
275 |
#endif |
0 | 276 |
mCombo->style()->drawControl(QStyle::CE_MenuItem, &opt, painter, mCombo); |
277 |
} |
|
278 |
QSize sizeHint(const QStyleOptionViewItem &option, |
|
279 |
const QModelIndex &index) const { |
|
280 |
QStyleOptionMenuItem opt = getStyleOption(option, index); |
|
281 |
return mCombo->style()->sizeFromContents( |
|
282 |
QStyle::CT_MenuItem, &opt, option.rect.size(), mCombo); |
|
283 |
} |
|
284 |
||
285 |
private: |
|
286 |
QStyleOptionMenuItem getStyleOption(const QStyleOptionViewItem &option, |
|
287 |
const QModelIndex &index) const; |
|
288 |
QComboBox *mCombo; |
|
289 |
}; |
|
290 |
||
291 |
// Note that this class is intentionally not using QStyledItemDelegate |
|
292 |
// Vista does not use the new theme for combo boxes and there might |
|
293 |
// be other side effects from using the new class |
|
294 |
class QComboBoxDelegate : public QItemDelegate |
|
295 |
{ Q_OBJECT |
|
296 |
public: |
|
297 |
QComboBoxDelegate(QObject *parent, QComboBox *cmb) : QItemDelegate(parent), mCombo(cmb) {} |
|
298 |
||
299 |
static bool isSeparator(const QModelIndex &index) { |
|
300 |
return index.data(Qt::AccessibleDescriptionRole).toString() == QLatin1String("separator"); |
|
301 |
} |
|
302 |
static void setSeparator(QAbstractItemModel *model, const QModelIndex &index) { |
|
303 |
model->setData(index, QString::fromLatin1("separator"), Qt::AccessibleDescriptionRole); |
|
304 |
if (QStandardItemModel *m = qobject_cast<QStandardItemModel*>(model)) |
|
305 |
if (QStandardItem *item = m->itemFromIndex(index)) |
|
306 |
item->setFlags(item->flags() & ~(Qt::ItemIsSelectable|Qt::ItemIsEnabled)); |
|
307 |
} |
|
308 |
||
309 |
protected: |
|
310 |
void paint(QPainter *painter, |
|
311 |
const QStyleOptionViewItem &option, |
|
312 |
const QModelIndex &index) const { |
|
313 |
if (isSeparator(index)) { |
|
314 |
QRect rect = option.rect; |
|
315 |
if (const QStyleOptionViewItemV3 *v3 = qstyleoption_cast<const QStyleOptionViewItemV3*>(&option)) |
|
316 |
if (const QAbstractItemView *view = qobject_cast<const QAbstractItemView*>(v3->widget)) |
|
317 |
rect.setWidth(view->viewport()->width()); |
|
318 |
QStyleOption opt; |
|
319 |
opt.rect = rect; |
|
320 |
mCombo->style()->drawPrimitive(QStyle::PE_IndicatorToolBarSeparator, &opt, painter, mCombo); |
|
321 |
} else { |
|
322 |
QItemDelegate::paint(painter, option, index); |
|
323 |
} |
|
324 |
} |
|
325 |
||
326 |
QSize sizeHint(const QStyleOptionViewItem &option, |
|
327 |
const QModelIndex &index) const { |
|
328 |
if (isSeparator(index)) { |
|
329 |
int pm = mCombo->style()->pixelMetric(QStyle::PM_DefaultFrameWidth, 0, mCombo); |
|
330 |
return QSize(pm, pm); |
|
331 |
} |
|
332 |
return QItemDelegate::sizeHint(option, index); |
|
333 |
} |
|
334 |
private: |
|
335 |
QComboBox *mCombo; |
|
336 |
}; |
|
337 |
||
338 |
class QComboBoxPrivate : public QWidgetPrivate |
|
339 |
{ |
|
340 |
Q_DECLARE_PUBLIC(QComboBox) |
|
341 |
public: |
|
342 |
QComboBoxPrivate(); |
|
343 |
~QComboBoxPrivate() {} |
|
344 |
void init(); |
|
345 |
QComboBoxPrivateContainer* viewContainer(); |
|
346 |
void updateLineEditGeometry(); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
347 |
Qt::MatchFlags matchFlags() const; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
348 |
void _q_editingFinished(); |
0 | 349 |
void _q_returnPressed(); |
350 |
void _q_complete(); |
|
351 |
void _q_itemSelected(const QModelIndex &item); |
|
352 |
bool contains(const QString &text, int role); |
|
353 |
void emitActivated(const QModelIndex&); |
|
354 |
void _q_emitHighlighted(const QModelIndex&); |
|
355 |
void _q_emitCurrentIndexChanged(const QModelIndex &index); |
|
356 |
void _q_modelDestroyed(); |
|
357 |
void _q_modelReset(); |
|
358 |
#ifdef QT_KEYPAD_NAVIGATION |
|
359 |
void _q_completerActivated(); |
|
360 |
#endif |
|
361 |
void _q_resetButton(); |
|
362 |
void _q_dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
363 |
void _q_updateIndexBeforeChange(); |
0 | 364 |
void _q_rowsInserted(const QModelIndex & parent, int start, int end); |
365 |
void _q_rowsRemoved(const QModelIndex & parent, int start, int end); |
|
366 |
void updateArrow(QStyle::StateFlag state); |
|
367 |
bool updateHoverControl(const QPoint &pos); |
|
368 |
QRect popupGeometry(int screen = -1) const; |
|
369 |
QStyle::SubControl newHoverControl(const QPoint &pos); |
|
370 |
int computeWidthHint() const; |
|
371 |
QSize recomputeSizeHint(QSize &sh) const; |
|
372 |
void adjustComboBoxSize(); |
|
373 |
QString itemText(const QModelIndex &index) const; |
|
374 |
QIcon itemIcon(const QModelIndex &index) const; |
|
375 |
int itemRole() const; |
|
376 |
void updateLayoutDirection(); |
|
377 |
void setCurrentIndex(const QModelIndex &index); |
|
378 |
void updateDelegate(bool force = false); |
|
379 |
void keyboardSearchString(const QString &text); |
|
380 |
void modelChanged(); |
|
381 |
void updateViewContainerPaletteAndOpacity(); |
|
382 |
||
383 |
QAbstractItemModel *model; |
|
384 |
QLineEdit *lineEdit; |
|
385 |
QComboBoxPrivateContainer *container; |
|
386 |
QComboBox::InsertPolicy insertPolicy; |
|
387 |
QComboBox::SizeAdjustPolicy sizeAdjustPolicy; |
|
388 |
int minimumContentsLength; |
|
389 |
QSize iconSize; |
|
390 |
uint shownOnce : 1; |
|
391 |
uint autoCompletion : 1; |
|
392 |
uint duplicatesEnabled : 1; |
|
393 |
uint frame : 1; |
|
394 |
uint padding : 26; |
|
395 |
int maxVisibleItems; |
|
396 |
int maxCount; |
|
397 |
int modelColumn; |
|
398 |
bool inserting; |
|
399 |
mutable QSize minimumSizeHint; |
|
400 |
mutable QSize sizeHint; |
|
401 |
QStyle::StateFlag arrowState; |
|
402 |
QStyle::SubControl hoverControl; |
|
403 |
QRect hoverRect; |
|
404 |
QPersistentModelIndex currentIndex; |
|
405 |
QPersistentModelIndex root; |
|
406 |
Qt::CaseSensitivity autoCompletionCaseSensitivity; |
|
407 |
int indexBeforeChange; |
|
408 |
#ifndef QT_NO_COMPLETER |
|
409 |
QPointer<QCompleter> completer; |
|
410 |
#endif |
|
411 |
static QPalette viewContainerPalette(QComboBox *cmb) |
|
412 |
{ return cmb->d_func()->viewContainer()->palette(); } |
|
413 |
}; |
|
414 |
||
415 |
QT_END_NAMESPACE |
|
416 |
||
417 |
#endif // QT_NO_COMBOBOX |
|
418 |
||
419 |
#endif // QCOMBOBOX_P_H |