author | Eckhart Koeppen <eckhart.koppen@nokia.com> |
Wed, 21 Apr 2010 12:15:23 +0300 | |
branch | RCL_3 |
changeset 12 | cc75c76972ee |
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 test suite 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 |
||
43 |
#include <QtTest/QtTest> |
|
44 |
||
45 |
#include <qabstractitemview.h> |
|
46 |
#include <qstandarditemmodel.h> |
|
47 |
#include <qapplication.h> |
|
48 |
#include <qdatetimeedit.h> |
|
49 |
#include <qspinbox.h> |
|
50 |
#include <qlistview.h> |
|
51 |
#include <qtableview.h> |
|
52 |
#include <qtreeview.h> |
|
53 |
#include <qheaderview.h> |
|
54 |
#include <qitemeditorfactory.h> |
|
55 |
#include <qlineedit.h> |
|
56 |
#include <qvalidator.h> |
|
57 |
#include <qtablewidget.h> |
|
58 |
#include <qtreewidget.h> |
|
59 |
||
60 |
#include <QItemDelegate> |
|
61 |
#include <QAbstractItemDelegate> |
|
62 |
#include <QTextEdit> |
|
63 |
#include <QPlainTextEdit> |
|
64 |
#include <QDialog> |
|
65 |
||
66 |
#include "../../shared/util.h" |
|
67 |
||
68 |
Q_DECLARE_METATYPE(QAbstractItemDelegate::EndEditHint) |
|
69 |
||
70 |
//TESTED_CLASS= |
|
71 |
//TESTED_FILES= |
|
72 |
||
73 |
#if defined (Q_OS_WIN) && !defined(Q_OS_WINCE) |
|
74 |
#include <windows.h> |
|
75 |
#define Q_CHECK_PAINTEVENTS \ |
|
76 |
if (::SwitchDesktop(::GetThreadDesktop(::GetCurrentThreadId())) == 0) \ |
|
77 |
QSKIP("The widgets don't get the paint events", SkipSingle); |
|
78 |
#else |
|
79 |
#define Q_CHECK_PAINTEVENTS |
|
80 |
#endif |
|
81 |
||
82 |
//Begin of class definitions |
|
83 |
||
84 |
class TestItemDelegate : public QItemDelegate |
|
85 |
{ |
|
86 |
public: |
|
87 |
TestItemDelegate(QObject *parent = 0) : QItemDelegate(parent) {} |
|
88 |
~TestItemDelegate() {} |
|
89 |
||
90 |
void drawDisplay(QPainter *painter, |
|
91 |
const QStyleOptionViewItem &option, |
|
92 |
const QRect &rect, const QString &text) const |
|
93 |
{ |
|
94 |
displayText = text; |
|
95 |
displayFont = option.font; |
|
96 |
QItemDelegate::drawDisplay(painter, option, rect, text); |
|
97 |
} |
|
98 |
||
99 |
void drawDecoration(QPainter *painter, |
|
100 |
const QStyleOptionViewItem &option, |
|
101 |
const QRect &rect, const QPixmap &pixmap) const |
|
102 |
{ |
|
103 |
decorationPixmap = pixmap; |
|
104 |
decorationRect = rect; |
|
105 |
QItemDelegate::drawDecoration(painter, option, rect, pixmap); |
|
106 |
} |
|
107 |
||
108 |
||
109 |
inline QRect textRectangle(QPainter * painter, const QRect &rect, |
|
110 |
const QFont &font, const QString &text) const |
|
111 |
{ |
|
112 |
return QItemDelegate::textRectangle(painter, rect, font, text); |
|
113 |
} |
|
114 |
||
115 |
inline void doLayout(const QStyleOptionViewItem &option, |
|
116 |
QRect *checkRect, QRect *pixmapRect, |
|
117 |
QRect *textRect, bool hint) const |
|
118 |
{ |
|
119 |
QItemDelegate::doLayout(option, checkRect, pixmapRect, textRect, hint); |
|
120 |
} |
|
121 |
||
122 |
inline QRect rect(const QStyleOptionViewItem &option, |
|
123 |
const QModelIndex &index, int role) const |
|
124 |
{ |
|
125 |
return QItemDelegate::rect(option, index, role); |
|
126 |
} |
|
127 |
||
128 |
inline bool eventFilter(QObject *object, QEvent *event) |
|
129 |
{ |
|
130 |
return QItemDelegate::eventFilter(object, event); |
|
131 |
} |
|
132 |
||
133 |
inline bool editorEvent(QEvent *event, |
|
134 |
QAbstractItemModel *model, |
|
135 |
const QStyleOptionViewItem &option, |
|
136 |
const QModelIndex &index) |
|
137 |
{ |
|
138 |
return QItemDelegate::editorEvent(event, model, option, index); |
|
139 |
} |
|
140 |
||
141 |
// stored values for testing |
|
142 |
mutable QString displayText; |
|
143 |
mutable QFont displayFont; |
|
144 |
mutable QPixmap decorationPixmap; |
|
145 |
mutable QRect decorationRect; |
|
146 |
}; |
|
147 |
||
148 |
class TestItemModel : public QAbstractTableModel |
|
149 |
{ |
|
150 |
public: |
|
151 |
||
152 |
enum Roles { |
|
153 |
PixmapTestRole, |
|
154 |
ImageTestRole, |
|
155 |
IconTestRole, |
|
156 |
ColorTestRole, |
|
157 |
DoubleTestRole |
|
158 |
}; |
|
159 |
||
160 |
TestItemModel(const QSize &size) : size(size) {} |
|
161 |
||
162 |
~TestItemModel() {} |
|
163 |
||
164 |
int rowCount(const QModelIndex &parent) const |
|
165 |
{ |
|
166 |
Q_UNUSED(parent); |
|
167 |
return 1; |
|
168 |
} |
|
169 |
||
170 |
int columnCount(const QModelIndex &parent) const |
|
171 |
{ |
|
172 |
Q_UNUSED(parent); |
|
173 |
return 1; |
|
174 |
} |
|
175 |
||
176 |
QVariant data(const QModelIndex& index, int role) const |
|
177 |
{ |
|
178 |
Q_UNUSED(index); |
|
179 |
static QPixmap pixmap(size); |
|
180 |
static QImage image(size, QImage::Format_Mono); |
|
181 |
static QIcon icon(pixmap); |
|
182 |
static QColor color(Qt::green); |
|
183 |
||
184 |
switch (role) { |
|
185 |
case PixmapTestRole: return pixmap; |
|
186 |
case ImageTestRole: return image; |
|
187 |
case IconTestRole: return icon; |
|
188 |
case ColorTestRole: return color; |
|
189 |
case DoubleTestRole: return 10.00000001; |
|
190 |
default: break; |
|
191 |
} |
|
192 |
||
193 |
return QVariant(); |
|
194 |
} |
|
195 |
||
196 |
private: |
|
197 |
QSize size; |
|
198 |
}; |
|
199 |
||
200 |
class tst_QItemDelegate : public QObject |
|
201 |
{ |
|
202 |
Q_OBJECT |
|
203 |
||
204 |
public: |
|
205 |
tst_QItemDelegate(); |
|
206 |
virtual ~tst_QItemDelegate(); |
|
207 |
||
208 |
private slots: |
|
209 |
void initTestCase(); |
|
210 |
void cleanupTestCase(); |
|
211 |
void init(); |
|
212 |
void cleanup(); |
|
213 |
void getSetCheck(); |
|
214 |
void textRectangle_data(); |
|
215 |
void textRectangle(); |
|
216 |
void sizeHint_data(); |
|
217 |
void sizeHint(); |
|
218 |
void editorKeyPress_data(); |
|
219 |
void editorKeyPress(); |
|
220 |
void doubleEditorNegativeInput(); |
|
221 |
void font_data(); |
|
222 |
void font(); |
|
223 |
void doLayout_data(); |
|
224 |
void doLayout(); |
|
225 |
void rect_data(); |
|
226 |
void rect(); |
|
227 |
void eventFilter(); |
|
228 |
void dateTimeEditor_data(); |
|
229 |
void dateTimeEditor(); |
|
230 |
void decoration_data(); |
|
231 |
void decoration(); |
|
232 |
void editorEvent_data(); |
|
233 |
void editorEvent(); |
|
234 |
void enterKey_data(); |
|
235 |
void enterKey(); |
|
236 |
||
237 |
void task257859_finalizeEdit(); |
|
238 |
}; |
|
239 |
||
240 |
||
241 |
//End of class definitions |
|
242 |
||
243 |
// Testing get/set functions |
|
244 |
void tst_QItemDelegate::getSetCheck() |
|
245 |
{ |
|
246 |
QItemDelegate obj1; |
|
247 |
||
248 |
// QItemEditorFactory * QItemDelegate::itemEditorFactory() |
|
249 |
// void QItemDelegate::setItemEditorFactory(QItemEditorFactory *) |
|
250 |
QItemEditorFactory *var1 = new QItemEditorFactory; |
|
251 |
obj1.setItemEditorFactory(var1); |
|
252 |
QCOMPARE(var1, obj1.itemEditorFactory()); |
|
253 |
obj1.setItemEditorFactory((QItemEditorFactory *)0); |
|
254 |
QCOMPARE((QItemEditorFactory *)0, obj1.itemEditorFactory()); |
|
255 |
delete var1; |
|
256 |
||
257 |
QCOMPARE(obj1.hasClipping(), true); |
|
258 |
obj1.setClipping(false); |
|
259 |
QCOMPARE(obj1.hasClipping(), false); |
|
260 |
obj1.setClipping(true); |
|
261 |
QCOMPARE(obj1.hasClipping(), true); |
|
262 |
} |
|
263 |
||
264 |
tst_QItemDelegate::tst_QItemDelegate() |
|
265 |
{ |
|
266 |
} |
|
267 |
||
268 |
tst_QItemDelegate::~tst_QItemDelegate() |
|
269 |
{ |
|
270 |
} |
|
271 |
||
272 |
void tst_QItemDelegate::initTestCase() |
|
273 |
{ |
|
274 |
} |
|
275 |
||
276 |
void tst_QItemDelegate::cleanupTestCase() |
|
277 |
{ |
|
278 |
} |
|
279 |
||
280 |
void tst_QItemDelegate::init() |
|
281 |
{ |
|
282 |
} |
|
283 |
||
284 |
void tst_QItemDelegate::cleanup() |
|
285 |
{ |
|
286 |
} |
|
287 |
||
288 |
void tst_QItemDelegate::textRectangle_data() |
|
289 |
{ |
|
290 |
QFont font; |
|
291 |
QFontMetrics fontMetrics(font); |
|
292 |
int pm = QApplication::style()->pixelMetric(QStyle::PM_FocusFrameHMargin); |
|
293 |
int margins = 2 * (pm + 1); // margin on each side of the text |
|
294 |
int height = fontMetrics.height(); |
|
295 |
||
296 |
QTest::addColumn<QString>("text"); |
|
297 |
QTest::addColumn<QRect>("rect"); |
|
298 |
QTest::addColumn<QRect>("expected"); |
|
299 |
||
300 |
QTest::newRow("empty") << QString() |
|
301 |
<< QRect() |
|
302 |
<< QRect(0, 0, margins, height); |
|
303 |
} |
|
304 |
||
305 |
void tst_QItemDelegate::textRectangle() |
|
306 |
{ |
|
307 |
QFETCH(QString, text); |
|
308 |
QFETCH(QRect, rect); |
|
309 |
QFETCH(QRect, expected); |
|
310 |
||
311 |
QFont font; |
|
312 |
TestItemDelegate delegate; |
|
313 |
QRect result = delegate.textRectangle(0, rect, font, text); |
|
314 |
||
315 |
QCOMPARE(result, expected); |
|
316 |
} |
|
317 |
||
318 |
void tst_QItemDelegate::sizeHint_data() |
|
319 |
{ |
|
320 |
QTest::addColumn<QSize>("expected"); |
|
321 |
||
322 |
QFont font; |
|
323 |
QFontMetrics fontMetrics(font); |
|
324 |
//int m = QApplication::style()->pixelMetric(QStyle::PM_FocusFrameHMargin) + 1; |
|
325 |
QTest::newRow("empty") |
|
326 |
<< QSize(0, fontMetrics.height()); |
|
327 |
||
328 |
} |
|
329 |
||
330 |
void tst_QItemDelegate::sizeHint() |
|
331 |
{ |
|
332 |
QFETCH(QSize, expected); |
|
333 |
||
334 |
QModelIndex index; |
|
335 |
QStyleOptionViewItem option; |
|
336 |
||
337 |
TestItemDelegate delegate; |
|
338 |
QSize result = delegate.sizeHint(option, index); |
|
339 |
QCOMPARE(result, expected); |
|
340 |
} |
|
341 |
||
342 |
void tst_QItemDelegate::editorKeyPress_data() |
|
343 |
{ |
|
344 |
QTest::addColumn<QString>("initial"); |
|
345 |
QTest::addColumn<QString>("expected"); |
|
346 |
||
347 |
QTest::newRow("foo bar") |
|
348 |
<< QString("foo") |
|
349 |
<< QString("bar"); |
|
350 |
} |
|
351 |
||
352 |
void tst_QItemDelegate::editorKeyPress() |
|
353 |
{ |
|
354 |
QFETCH(QString, initial); |
|
355 |
QFETCH(QString, expected); |
|
356 |
||
357 |
QStandardItemModel model; |
|
358 |
model.appendRow(new QStandardItem(initial)); |
|
359 |
||
360 |
QListView view; |
|
361 |
view.setModel(&model); |
|
362 |
view.show(); |
|
363 |
||
364 |
QModelIndex index = model.index(0, 0); |
|
365 |
view.setCurrentIndex(index); // the editor will only selectAll on the current index |
|
366 |
view.edit(index); |
|
367 |
||
368 |
QList<QLineEdit*> lineEditors = qFindChildren<QLineEdit *>(view.viewport()); |
|
369 |
QCOMPARE(lineEditors.count(), 1); |
|
370 |
||
371 |
QLineEdit *editor = lineEditors.at(0); |
|
372 |
QCOMPARE(editor->selectedText(), initial); |
|
373 |
||
374 |
QTest::keyClicks(editor, expected); |
|
375 |
QTest::keyClick(editor, Qt::Key_Enter); |
|
376 |
QApplication::processEvents(); |
|
377 |
||
378 |
QCOMPARE(index.data().toString(), expected); |
|
379 |
} |
|
380 |
||
381 |
void tst_QItemDelegate::doubleEditorNegativeInput() |
|
382 |
{ |
|
383 |
QStandardItemModel model; |
|
384 |
||
385 |
QStandardItem *item = new QStandardItem; |
|
386 |
item->setData(10.0, Qt::DisplayRole); |
|
387 |
model.appendRow(item); |
|
388 |
||
389 |
QListView view; |
|
390 |
view.setModel(&model); |
|
391 |
view.show(); |
|
392 |
||
393 |
QModelIndex index = model.index(0, 0); |
|
394 |
view.setCurrentIndex(index); // the editor will only selectAll on the current index |
|
395 |
view.edit(index); |
|
396 |
||
397 |
QList<QDoubleSpinBox*> editors = qFindChildren<QDoubleSpinBox *>(view.viewport()); |
|
398 |
QCOMPARE(editors.count(), 1); |
|
399 |
||
400 |
QDoubleSpinBox *editor = editors.at(0); |
|
401 |
QCOMPARE(editor->value(), double(10)); |
|
402 |
||
403 |
QTest::keyClick(editor, Qt::Key_Minus); |
|
404 |
QTest::keyClick(editor, Qt::Key_1); |
|
405 |
QTest::keyClick(editor, Qt::Key_0); |
|
406 |
QTest::keyClick(editor, Qt::Key_Comma); //support both , and . locales |
|
407 |
QTest::keyClick(editor, Qt::Key_Period); |
|
408 |
QTest::keyClick(editor, Qt::Key_0); |
|
409 |
QTest::keyClick(editor, Qt::Key_Enter); |
|
410 |
QApplication::processEvents(); |
|
411 |
||
412 |
QCOMPARE(index.data().toString(), QString("-10")); |
|
413 |
} |
|
414 |
||
415 |
void tst_QItemDelegate::font_data() |
|
416 |
{ |
|
417 |
QTest::addColumn<QString>("itemText"); |
|
418 |
QTest::addColumn<QString>("properties"); |
|
419 |
QTest::addColumn<QFont>("itemFont"); |
|
420 |
QTest::addColumn<QFont>("viewFont"); |
|
421 |
||
422 |
QFont itemFont; |
|
423 |
itemFont.setItalic(true); |
|
424 |
QFont viewFont; |
|
425 |
||
426 |
QTest::newRow("foo italic") |
|
427 |
<< QString("foo") |
|
428 |
<< QString("italic") |
|
429 |
<< itemFont |
|
430 |
<< viewFont; |
|
431 |
||
432 |
itemFont.setItalic(true); |
|
433 |
||
434 |
QTest::newRow("foo bold") |
|
435 |
<< QString("foo") |
|
436 |
<< QString("bold") |
|
437 |
<< itemFont |
|
438 |
<< viewFont; |
|
439 |
||
440 |
itemFont.setFamily(itemFont.defaultFamily()); |
|
441 |
||
442 |
QTest::newRow("foo family") |
|
443 |
<< QString("foo") |
|
444 |
<< QString("family") |
|
445 |
<< itemFont |
|
446 |
<< viewFont; |
|
447 |
} |
|
448 |
||
449 |
void tst_QItemDelegate::font() |
|
450 |
{ |
|
451 |
Q_CHECK_PAINTEVENTS |
|
452 |
||
453 |
QFETCH(QString, itemText); |
|
454 |
QFETCH(QString, properties); |
|
455 |
QFETCH(QFont, itemFont); |
|
456 |
QFETCH(QFont, viewFont); |
|
457 |
||
458 |
QTableWidget table(1, 1); |
|
459 |
table.setFont(viewFont); |
|
460 |
||
461 |
TestItemDelegate *delegate = new TestItemDelegate(&table); |
|
462 |
table.setItemDelegate(delegate); |
|
463 |
table.show(); |
|
464 |
#ifdef Q_WS_X11 |
|
465 |
qt_x11_wait_for_window_manager(&table); |
|
466 |
#endif |
|
467 |
||
468 |
QTableWidgetItem *item = new QTableWidgetItem; |
|
469 |
item->setText(itemText); |
|
470 |
item->setFont(itemFont); |
|
471 |
table.setItem(0, 0, item); |
|
472 |
||
473 |
QApplication::processEvents(); |
|
474 |
#ifdef Q_WS_QWS |
|
475 |
QApplication::sendPostedEvents(); //glib workaround |
|
476 |
#endif |
|
477 |
||
478 |
QTRY_COMPARE(delegate->displayText, item->text()); |
|
479 |
if (properties.contains("italic")) { |
|
480 |
QCOMPARE(delegate->displayFont.italic(), item->font().italic()); |
|
481 |
} |
|
482 |
if (properties.contains("bold")){ |
|
483 |
QCOMPARE(delegate->displayFont.bold(), item->font().bold()); |
|
484 |
} |
|
485 |
if (properties.contains("family")){ |
|
486 |
QCOMPARE(delegate->displayFont.family(), item->font().family()); |
|
487 |
} |
|
488 |
} |
|
489 |
||
490 |
//Testing the different QRect created by the doLayout function. |
|
491 |
//Tests are made with different values for the QStyleOptionViewItem properties: |
|
492 |
//decorationPosition and position. |
|
493 |
||
494 |
void tst_QItemDelegate::doLayout_data() |
|
495 |
{ |
|
496 |
QTest::addColumn<int>("position"); |
|
497 |
QTest::addColumn<int>("direction"); |
|
498 |
QTest::addColumn<bool>("hint"); |
|
499 |
QTest::addColumn<QRect>("itemRect"); |
|
500 |
QTest::addColumn<QRect>("checkRect"); |
|
501 |
QTest::addColumn<QRect>("pixmapRect"); |
|
502 |
QTest::addColumn<QRect>("textRect"); |
|
503 |
QTest::addColumn<QRect>("expectedCheckRect"); |
|
504 |
QTest::addColumn<QRect>("expectedPixmapRect"); |
|
505 |
QTest::addColumn<QRect>("expectedTextRect"); |
|
506 |
||
507 |
int m = QApplication::style()->pixelMetric(QStyle::PM_FocusFrameHMargin) + 1; |
|
508 |
//int item = 400; |
|
509 |
//int check = 50; |
|
510 |
//int pixmap = 1000; |
|
511 |
//int text = 400; |
|
512 |
||
513 |
QTest::newRow("top, left to right, hint") |
|
514 |
<< (int)QStyleOptionViewItem::Top |
|
515 |
<< (int)Qt::LeftToRight |
|
516 |
<< true |
|
517 |
<< QRect(0, 0, 400, 400) |
|
518 |
<< QRect(0, 0, 50, 50) |
|
519 |
<< QRect(0, 0, 1000, 1000) |
|
520 |
<< QRect(0, 0, 400, 400) |
|
521 |
<< QRect(m, 0, 50 + 2*m, 1000) |
|
522 |
<< QRect(50 + 2*m, 0, 1000 + 2*m, 1000 + m) |
|
523 |
<< QRect(50 + 2*m, 1000 + m, 1000 + 2*m, 400); |
|
524 |
/* |
|
525 |
QTest::newRow("top, left to right, limited") |
|
526 |
<< (int)QStyleOptionViewItem::Top |
|
527 |
<< (int)Qt::LeftToRight |
|
528 |
<< false |
|
529 |
<< QRect(0, 0, 400, 400) |
|
530 |
<< QRect(0, 0, 50, 50) |
|
531 |
<< QRect(0, 0, 1000, 1000) |
|
532 |
<< QRect(0, 0, 400, 400) |
|
533 |
<< QRect(m, (400/2) - (50/2), 50, 50) |
|
534 |
<< QRect(50 + 2*m, 0, 1000, 1000) |
|
535 |
<< QRect(50 + 2*m, 1000 + m, 400 - (50 + 2*m), 400 - 1000 - m); |
|
536 |
*/ |
|
537 |
QTest::newRow("top, right to left, hint") |
|
538 |
<< (int)QStyleOptionViewItem::Top |
|
539 |
<< (int)Qt::RightToLeft |
|
540 |
<< true |
|
541 |
<< QRect(0, 0, 400, 400) |
|
542 |
<< QRect(0, 0, 50, 50) |
|
543 |
<< QRect(0, 0, 1000, 1000) |
|
544 |
<< QRect(0, 0, 400, 400) |
|
545 |
<< QRect(1000 + 2 * m, 0, 50 + 2 * m, 1000) |
|
546 |
<< QRect(0, 0, 1000 + 2 * m, 1000 + m) |
|
547 |
<< QRect(0, 1000 + m, 1000 + 2 * m, 400); |
|
548 |
||
549 |
QTest::newRow("bottom, left to right, hint") |
|
550 |
<< (int)QStyleOptionViewItem::Bottom |
|
551 |
<< (int)Qt::LeftToRight |
|
552 |
<< true |
|
553 |
<< QRect(0, 0, 400, 400) |
|
554 |
<< QRect(0, 0, 50, 50) |
|
555 |
<< QRect(0, 0, 1000, 1000) |
|
556 |
<< QRect(0, 0, 400, 400) |
|
557 |
<< QRect(m, 0, 50 + 2 * m, 1000) |
|
558 |
<< QRect(50 + 2 * m, 400 + m, 1000 + 2 * m, 1000) |
|
559 |
<< QRect(50 + 2 * m, 0, 1000 + 2 * m, 400 + m); |
|
560 |
||
561 |
QTest::newRow("bottom, right to left, hint") |
|
562 |
<< (int)QStyleOptionViewItem::Bottom |
|
563 |
<< (int)Qt::RightToLeft |
|
564 |
<< true |
|
565 |
<< QRect(0, 0, 400, 400) |
|
566 |
<< QRect(0, 0, 50, 50) |
|
567 |
<< QRect(0, 0, 1000, 1000) |
|
568 |
<< QRect(0, 0, 400, 400) |
|
569 |
<< QRect(1000 + 2 * m, 0, 50 + 2 * m, 1000) |
|
570 |
<< QRect(0, 400 + m, 1000 + 2 * m, 1000) |
|
571 |
<< QRect(0, 0, 1000 + 2 * m, 400 + m); |
|
572 |
||
573 |
QTest::newRow("left, left to right, hint") |
|
574 |
<< (int)QStyleOptionViewItem::Left |
|
575 |
<< (int)Qt::LeftToRight |
|
576 |
<< true |
|
577 |
<< QRect(0, 0, 400, 400) |
|
578 |
<< QRect(0, 0, 50, 50) |
|
579 |
<< QRect(0, 0, 1000, 1000) |
|
580 |
<< QRect(0, 0, 400, 400) |
|
581 |
<< QRect(m, 0, 50 + 2 * m, 1000) |
|
582 |
<< QRect(50 + 2 * m, 0, 1000 + 2 * m, 1000) |
|
583 |
<< QRect(1050 + 4 * m, 0, 400 + 2 * m, 1000); |
|
584 |
||
585 |
QTest::newRow("left, right to left, hint") |
|
586 |
<< (int)QStyleOptionViewItem::Left |
|
587 |
<< (int)Qt::RightToLeft |
|
588 |
<< true |
|
589 |
<< QRect(0, 0, 400, 400) |
|
590 |
<< QRect(0, 0, 50, 50) |
|
591 |
<< QRect(0, 0, 1000, 1000) |
|
592 |
<< QRect(0, 0, 400, 400) |
|
593 |
<< QRect(1400 + 4 * m, 0, 50 + 2 * m, 1000) |
|
594 |
<< QRect(400 + 2 * m, 0, 1000 + 2 * m, 1000) |
|
595 |
<< QRect(0, 0, 400 + 2 * m, 1000); |
|
596 |
||
597 |
QTest::newRow("right, left to right, hint") |
|
598 |
<< (int)QStyleOptionViewItem::Right |
|
599 |
<< (int)Qt::LeftToRight |
|
600 |
<< true |
|
601 |
<< QRect(0, 0, 400, 400) |
|
602 |
<< QRect(0, 0, 50, 50) |
|
603 |
<< QRect(0, 0, 1000, 1000) |
|
604 |
<< QRect(0, 0, 400, 400) |
|
605 |
<< QRect(m, 0, 50 + 2 * m, 1000) |
|
606 |
<< QRect(450 + 4 * m, 0, 1000 + 2 * m, 1000) |
|
607 |
<< QRect(50 + 2 * m, 0, 400 + 2 * m, 1000); |
|
608 |
||
609 |
QTest::newRow("right, right to left, hint") |
|
610 |
<< (int)QStyleOptionViewItem::Right |
|
611 |
<< (int)Qt::RightToLeft |
|
612 |
<< true |
|
613 |
<< QRect(0, 0, 400, 400) |
|
614 |
<< QRect(0, 0, 50, 50) |
|
615 |
<< QRect(0, 0, 1000, 1000) |
|
616 |
<< QRect(0, 0, 400, 400) |
|
617 |
<< QRect(1400 + 4 * m, 0, 50 + 2 * m, 1000) |
|
618 |
<< QRect(0, 0, 1000 + 2 * m, 1000) |
|
619 |
<< QRect(1000 + 2 * m, 0, 400 + 2 * m, 1000); |
|
620 |
} |
|
621 |
||
622 |
void tst_QItemDelegate::doLayout() |
|
623 |
{ |
|
624 |
QFETCH(int, position); |
|
625 |
QFETCH(int, direction); |
|
626 |
QFETCH(bool, hint); |
|
627 |
QFETCH(QRect, itemRect); |
|
628 |
QFETCH(QRect, checkRect); |
|
629 |
QFETCH(QRect, pixmapRect); |
|
630 |
QFETCH(QRect, textRect); |
|
631 |
QFETCH(QRect, expectedCheckRect); |
|
632 |
QFETCH(QRect, expectedPixmapRect); |
|
633 |
QFETCH(QRect, expectedTextRect); |
|
634 |
||
635 |
TestItemDelegate delegate; |
|
636 |
QStyleOptionViewItem option; |
|
637 |
||
638 |
option.rect = itemRect; |
|
639 |
option.decorationPosition = (QStyleOptionViewItem::Position)position; |
|
640 |
option.direction = (Qt::LayoutDirection)direction; |
|
641 |
||
642 |
delegate.doLayout(option, &checkRect, &pixmapRect, &textRect, hint); |
|
643 |
||
644 |
QCOMPARE(checkRect, expectedCheckRect); |
|
645 |
QCOMPARE(pixmapRect, expectedPixmapRect); |
|
646 |
QCOMPARE(textRect, expectedTextRect); |
|
647 |
} |
|
648 |
||
649 |
void tst_QItemDelegate::rect_data() |
|
650 |
{ |
|
651 |
QTest::addColumn<int>("role"); |
|
652 |
QTest::addColumn<QSize>("size"); |
|
653 |
QTest::addColumn<QRect>("expected"); |
|
654 |
||
655 |
QTest::newRow("pixmap") |
|
656 |
<< (int)TestItemModel::PixmapTestRole |
|
657 |
<< QSize(200, 300) |
|
658 |
<< QRect(0, 0, 200, 300); |
|
659 |
||
660 |
QTest::newRow("image") |
|
661 |
<< (int)TestItemModel::ImageTestRole |
|
662 |
<< QSize(200, 300) |
|
663 |
<< QRect(0, 0, 200, 300); |
|
664 |
||
665 |
QTest::newRow("icon") |
|
666 |
<< (int)TestItemModel::IconTestRole |
|
667 |
<< QSize(200, 300) |
|
668 |
<< QRect(0, 0, 200, 300); |
|
669 |
||
670 |
QTest::newRow("color") |
|
671 |
<< (int)TestItemModel::ColorTestRole |
|
672 |
<< QSize(200, 300) |
|
673 |
<< QRect(0, 0, 200, 300); |
|
674 |
||
675 |
QTest::newRow("double") |
|
676 |
<< (int)TestItemModel::DoubleTestRole |
|
677 |
<< QSize() |
|
678 |
<< QRect(); |
|
679 |
} |
|
680 |
||
681 |
void tst_QItemDelegate::rect() |
|
682 |
{ |
|
683 |
QFETCH(int, role); |
|
684 |
QFETCH(QSize, size); |
|
685 |
QFETCH(QRect, expected); |
|
686 |
||
687 |
TestItemModel model(size); |
|
688 |
QStyleOptionViewItem option; |
|
689 |
TestItemDelegate delegate; |
|
690 |
option.decorationSize = size; |
|
691 |
||
692 |
if (role == TestItemModel::DoubleTestRole) |
|
693 |
expected = delegate.textRectangle(0, QRect(), QFont(), QLatin1String("10.00000001")); |
|
694 |
||
695 |
QModelIndex index = model.index(0, 0); |
|
696 |
QVERIFY(index.isValid()); |
|
697 |
QRect result = delegate.rect(option, index, role); |
|
698 |
QCOMPARE(result, expected); |
|
699 |
} |
|
700 |
||
701 |
//TODO : Add a test for the keyPress event |
|
702 |
//with Qt::Key_Enter and Qt::Key_Return |
|
703 |
void tst_QItemDelegate::eventFilter() |
|
704 |
{ |
|
705 |
TestItemDelegate delegate; |
|
706 |
QWidget widget; |
|
707 |
QEvent *event; |
|
708 |
||
709 |
qRegisterMetaType<QAbstractItemDelegate::EndEditHint>("QAbstractItemDelegate::EndEditHint"); |
|
710 |
||
711 |
QSignalSpy commitDataSpy(&delegate, SIGNAL(commitData(QWidget *))); |
|
712 |
QSignalSpy closeEditorSpy(&delegate, |
|
713 |
SIGNAL(closeEditor(QWidget *, |
|
714 |
QAbstractItemDelegate::EndEditHint))); |
|
715 |
||
716 |
//Subtest KeyPress |
|
717 |
//For each test we send a key event and check if signals were emitted. |
|
718 |
event = new QKeyEvent(QEvent::KeyPress, Qt::Key_Tab, Qt::NoModifier); |
|
719 |
QVERIFY(delegate.eventFilter(&widget, event)); |
|
720 |
QCOMPARE(closeEditorSpy.count(), 1); |
|
721 |
QCOMPARE(commitDataSpy.count(), 1); |
|
722 |
delete event; |
|
723 |
||
724 |
event = new QKeyEvent(QEvent::KeyPress, Qt::Key_Backtab, Qt::NoModifier); |
|
725 |
QVERIFY(delegate.eventFilter(&widget, event)); |
|
726 |
QCOMPARE(closeEditorSpy.count(), 2); |
|
727 |
QCOMPARE(commitDataSpy.count(), 2); |
|
728 |
delete event; |
|
729 |
||
730 |
event = new QKeyEvent(QEvent::KeyPress, Qt::Key_Escape, Qt::NoModifier); |
|
731 |
QVERIFY(delegate.eventFilter(&widget, event)); |
|
732 |
QCOMPARE(closeEditorSpy.count(), 3); |
|
733 |
QCOMPARE(commitDataSpy.count(), 2); |
|
734 |
delete event; |
|
735 |
||
736 |
event = new QKeyEvent(QEvent::KeyPress, Qt::Key_A, Qt::NoModifier); |
|
737 |
QVERIFY(!delegate.eventFilter(&widget, event)); |
|
738 |
QCOMPARE(closeEditorSpy.count(), 3); |
|
739 |
QCOMPARE(commitDataSpy.count(), 2); |
|
740 |
delete event; |
|
741 |
||
742 |
//Subtest focusEvent |
|
743 |
event = new QFocusEvent(QEvent::FocusOut); |
|
744 |
QVERIFY(!delegate.eventFilter(&widget, event)); |
|
745 |
QCOMPARE(closeEditorSpy.count(), 4); |
|
746 |
QCOMPARE(commitDataSpy.count(), 3); |
|
747 |
delete event; |
|
748 |
} |
|
749 |
||
750 |
void tst_QItemDelegate::dateTimeEditor_data() |
|
751 |
{ |
|
752 |
QTest::addColumn<QTime>("time"); |
|
753 |
QTest::addColumn<QDate>("date"); |
|
754 |
||
755 |
QTest::newRow("data") |
|
756 |
<< QTime(7, 16, 34) |
|
757 |
<< QDate(2006, 10, 31); |
|
758 |
} |
|
759 |
||
760 |
void tst_QItemDelegate::dateTimeEditor() |
|
761 |
{ |
|
762 |
QFETCH(QTime, time); |
|
763 |
QFETCH(QDate, date); |
|
764 |
||
765 |
QTableWidgetItem *item1 = new QTableWidgetItem; |
|
766 |
item1->setData(Qt::DisplayRole, time); |
|
767 |
||
768 |
QTableWidgetItem *item2 = new QTableWidgetItem; |
|
769 |
item2->setData(Qt::DisplayRole, date); |
|
770 |
||
771 |
QTableWidgetItem *item3 = new QTableWidgetItem; |
|
772 |
item3->setData(Qt::DisplayRole, QDateTime(date, time)); |
|
773 |
||
774 |
QTableWidget widget(1, 3); |
|
775 |
widget.setItem(0, 0, item1); |
|
776 |
widget.setItem(0, 1, item2); |
|
777 |
widget.setItem(0, 2, item3); |
|
778 |
widget.show(); |
|
779 |
||
780 |
widget.editItem(item1); |
|
781 |
||
782 |
QTestEventLoop::instance().enterLoop(1); |
|
783 |
||
784 |
QTimeEdit *timeEditor = qFindChild<QTimeEdit *>(widget.viewport()); |
|
785 |
QVERIFY(timeEditor); |
|
786 |
QCOMPARE(timeEditor->time(), time); |
|
787 |
||
788 |
widget.clearFocus(); |
|
789 |
qApp->setActiveWindow(&widget); |
|
790 |
widget.setFocus(); |
|
791 |
widget.editItem(item2); |
|
792 |
||
793 |
QTestEventLoop::instance().enterLoop(1); |
|
794 |
||
795 |
QDateEdit *dateEditor = qFindChild<QDateEdit *>(widget.viewport()); |
|
796 |
QVERIFY(dateEditor); |
|
797 |
QCOMPARE(dateEditor->date(), date); |
|
798 |
||
799 |
widget.clearFocus(); |
|
800 |
widget.setFocus(); |
|
801 |
widget.editItem(item3); |
|
802 |
||
803 |
QTestEventLoop::instance().enterLoop(1); |
|
804 |
||
805 |
QList<QDateTimeEdit *> dateTimeEditors = widget.findChildren<QDateTimeEdit *>(); |
|
806 |
QDateTimeEdit *dateTimeEditor = 0; |
|
807 |
foreach(dateTimeEditor, dateTimeEditors) |
|
808 |
if (dateTimeEditor->metaObject()->className() == QLatin1String("QDateTimeEdit")) |
|
809 |
break; |
|
810 |
QVERIFY(dateTimeEditor); |
|
811 |
QCOMPARE(dateTimeEditor->date(), date); |
|
812 |
QCOMPARE(dateTimeEditor->time(), time); |
|
813 |
} |
|
814 |
||
815 |
void tst_QItemDelegate::decoration_data() |
|
816 |
{ |
|
817 |
QTest::addColumn<int>("type"); |
|
818 |
QTest::addColumn<QSize>("size"); |
|
819 |
QTest::addColumn<QSize>("expected"); |
|
820 |
||
821 |
int pm = QApplication::style()->pixelMetric(QStyle::PM_SmallIconSize); |
|
822 |
||
823 |
QTest::newRow("pixmap 30x30") |
|
824 |
<< (int)QVariant::Pixmap |
|
825 |
<< QSize(30, 30) |
|
826 |
<< QSize(30, 30); |
|
827 |
||
828 |
QTest::newRow("image 30x30") |
|
829 |
<< (int)QVariant::Image |
|
830 |
<< QSize(30, 30) |
|
831 |
<< QSize(30, 30); |
|
832 |
||
833 |
//The default engine scales pixmaps down if required, but never up. For WinCE we need bigger IconSize than 30 |
|
834 |
QTest::newRow("icon 30x30") |
|
835 |
<< (int)QVariant::Icon |
|
836 |
<< QSize(60, 60) |
|
837 |
<< QSize(pm, pm); |
|
838 |
||
839 |
QTest::newRow("color 30x30") |
|
840 |
<< (int)QVariant::Color |
|
841 |
<< QSize(30, 30) |
|
842 |
<< QSize(pm, pm); |
|
843 |
||
844 |
QTest::newRow("pixmap 30x30 big") |
|
845 |
<< (int)QVariant::Pixmap |
|
846 |
<< QSize(1024, 1024) // Over 1M |
|
847 |
<< QSize(1024, 1024); |
|
848 |
} |
|
849 |
||
850 |
void tst_QItemDelegate::decoration() |
|
851 |
{ |
|
852 |
if (QByteArray(QTest::currentDataTag()) == QByteArray("pixmap 30x30 big")) |
|
853 |
QSKIP("Skipping this as it demands too much memory and potential hangs", SkipSingle); |
|
854 |
Q_CHECK_PAINTEVENTS |
|
855 |
||
856 |
QFETCH(int, type); |
|
857 |
QFETCH(QSize, size); |
|
858 |
QFETCH(QSize, expected); |
|
859 |
||
860 |
QTableWidget table(1, 1); |
|
861 |
TestItemDelegate delegate; |
|
862 |
table.setItemDelegate(&delegate); |
|
863 |
table.show(); |
|
864 |
#ifdef Q_WS_X11 |
|
865 |
qt_x11_wait_for_window_manager(&table); |
|
866 |
#endif |
|
867 |
QApplication::setActiveWindow(&table); |
|
868 |
QTRY_COMPARE(QApplication::activeWindow(), static_cast<QWidget*>(&table)); |
|
869 |
||
870 |
QVariant value; |
|
871 |
switch ((QVariant::Type)type) { |
|
872 |
case QVariant::Pixmap: { |
|
873 |
QPixmap pm(size); |
|
874 |
pm.fill(Qt::black); |
|
875 |
value = pm; |
|
876 |
break; |
|
877 |
} |
|
878 |
case QVariant::Image: { |
|
879 |
QImage img(size, QImage::Format_Mono); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
880 |
qMemSet(img.bits(), 0, img.byteCount()); |
0 | 881 |
value = img; |
882 |
break; |
|
883 |
} |
|
884 |
case QVariant::Icon: { |
|
885 |
QPixmap pm(size); |
|
886 |
pm.fill(Qt::black); |
|
887 |
value = QIcon(pm); |
|
888 |
break; |
|
889 |
} |
|
890 |
case QVariant::Color: |
|
891 |
value = QColor(Qt::green); |
|
892 |
break; |
|
893 |
default: |
|
894 |
break; |
|
895 |
} |
|
896 |
||
897 |
QTableWidgetItem *item = new QTableWidgetItem; |
|
898 |
item->setData(Qt::DecorationRole, value); |
|
899 |
table.setItem(0, 0, item); |
|
900 |
item->setSelected(true); |
|
901 |
||
902 |
QApplication::processEvents(); |
|
903 |
||
904 |
QTRY_COMPARE(delegate.decorationRect.size(), expected); |
|
905 |
} |
|
906 |
||
907 |
void tst_QItemDelegate::editorEvent_data() |
|
908 |
{ |
|
909 |
QTest::addColumn<QRect>("rect"); |
|
910 |
QTest::addColumn<QString>("text"); |
|
911 |
QTest::addColumn<int>("checkState"); |
|
912 |
QTest::addColumn<int>("flags"); |
|
913 |
QTest::addColumn<bool>("inCheck"); |
|
914 |
QTest::addColumn<int>("type"); |
|
915 |
QTest::addColumn<int>("button"); |
|
916 |
QTest::addColumn<bool>("edited"); |
|
917 |
QTest::addColumn<int>("expectedCheckState"); |
|
918 |
||
919 |
QTest::newRow("unchecked, checkable, release") |
|
920 |
<< QRect(0, 0, 20, 20) |
|
921 |
<< QString("foo") |
|
922 |
<< (int)(Qt::Unchecked) |
|
923 |
<< (int)(Qt::ItemIsEditable |
|
924 |
|Qt::ItemIsSelectable |
|
925 |
|Qt::ItemIsUserCheckable |
|
926 |
|Qt::ItemIsEnabled |
|
927 |
|Qt::ItemIsDragEnabled |
|
928 |
|Qt::ItemIsDropEnabled) |
|
929 |
<< true |
|
930 |
<< (int)(QEvent::MouseButtonRelease) |
|
931 |
<< (int)(Qt::LeftButton) |
|
932 |
<< true |
|
933 |
<< (int)(Qt::Checked); |
|
934 |
||
935 |
QTest::newRow("checked, checkable, release") |
|
936 |
<< QRect(0, 0, 20, 20) |
|
937 |
<< QString("foo") |
|
938 |
<< (int)(Qt::Checked) |
|
939 |
<< (int)(Qt::ItemIsEditable |
|
940 |
|Qt::ItemIsSelectable |
|
941 |
|Qt::ItemIsUserCheckable |
|
942 |
|Qt::ItemIsEnabled |
|
943 |
|Qt::ItemIsDragEnabled |
|
944 |
|Qt::ItemIsDropEnabled) |
|
945 |
<< true |
|
946 |
<< (int)(QEvent::MouseButtonRelease) |
|
947 |
<< (int)(Qt::LeftButton) |
|
948 |
<< true |
|
949 |
<< (int)(Qt::Unchecked); |
|
950 |
||
951 |
QTest::newRow("unchecked, checkable, release") |
|
952 |
<< QRect(0, 0, 20, 20) |
|
953 |
<< QString("foo") |
|
954 |
<< (int)(Qt::Unchecked) |
|
955 |
<< (int)(Qt::ItemIsEditable |
|
956 |
|Qt::ItemIsSelectable |
|
957 |
|Qt::ItemIsUserCheckable |
|
958 |
|Qt::ItemIsEnabled |
|
959 |
|Qt::ItemIsDragEnabled |
|
960 |
|Qt::ItemIsDropEnabled) |
|
961 |
<< true |
|
962 |
<< (int)(QEvent::MouseButtonRelease) |
|
963 |
<< (int)(Qt::LeftButton) |
|
964 |
<< true |
|
965 |
<< (int)(Qt::Checked); |
|
966 |
||
967 |
QTest::newRow("unchecked, checkable, release, right button") |
|
968 |
<< QRect(0, 0, 20, 20) |
|
969 |
<< QString("foo") |
|
970 |
<< (int)(Qt::Unchecked) |
|
971 |
<< (int)(Qt::ItemIsEditable |
|
972 |
|Qt::ItemIsSelectable |
|
973 |
|Qt::ItemIsUserCheckable |
|
974 |
|Qt::ItemIsEnabled |
|
975 |
|Qt::ItemIsDragEnabled |
|
976 |
|Qt::ItemIsDropEnabled) |
|
977 |
<< true |
|
978 |
<< (int)(QEvent::MouseButtonRelease) |
|
979 |
<< (int)(Qt::RightButton) |
|
980 |
<< false |
|
981 |
<< (int)(Qt::Unchecked); |
|
982 |
||
983 |
QTest::newRow("unchecked, checkable, release outside") |
|
984 |
<< QRect(0, 0, 20, 20) |
|
985 |
<< QString("foo") |
|
986 |
<< (int)(Qt::Unchecked) |
|
987 |
<< (int)(Qt::ItemIsEditable |
|
988 |
|Qt::ItemIsSelectable |
|
989 |
|Qt::ItemIsUserCheckable |
|
990 |
|Qt::ItemIsEnabled |
|
991 |
|Qt::ItemIsDragEnabled |
|
992 |
|Qt::ItemIsDropEnabled) |
|
993 |
<< false |
|
994 |
<< (int)(QEvent::MouseButtonRelease) |
|
995 |
<< (int)(Qt::LeftButton) |
|
996 |
<< false |
|
997 |
<< (int)(Qt::Unchecked); |
|
998 |
||
999 |
QTest::newRow("unchecked, checkable, dblclick") |
|
1000 |
<< QRect(0, 0, 20, 20) |
|
1001 |
<< QString("foo") |
|
1002 |
<< (int)(Qt::Unchecked) |
|
1003 |
<< (int)(Qt::ItemIsEditable |
|
1004 |
|Qt::ItemIsSelectable |
|
1005 |
|Qt::ItemIsUserCheckable |
|
1006 |
|Qt::ItemIsEnabled |
|
1007 |
|Qt::ItemIsDragEnabled |
|
1008 |
|Qt::ItemIsDropEnabled) |
|
1009 |
<< true |
|
1010 |
<< (int)(QEvent::MouseButtonDblClick) |
|
1011 |
<< (int)(Qt::LeftButton) |
|
1012 |
<< true |
|
1013 |
<< (int)(Qt::Unchecked); |
|
1014 |
} |
|
1015 |
||
1016 |
void tst_QItemDelegate::editorEvent() |
|
1017 |
{ |
|
1018 |
QFETCH(QRect, rect); |
|
1019 |
QFETCH(QString, text); |
|
1020 |
QFETCH(int, checkState); |
|
1021 |
QFETCH(int, flags); |
|
1022 |
QFETCH(bool, inCheck); |
|
1023 |
QFETCH(int, type); |
|
1024 |
QFETCH(int, button); |
|
1025 |
QFETCH(bool, edited); |
|
1026 |
QFETCH(int, expectedCheckState); |
|
1027 |
||
1028 |
QStandardItemModel model(1, 1); |
|
1029 |
QModelIndex index = model.index(0, 0); |
|
1030 |
QVERIFY(index.isValid()); |
|
1031 |
||
1032 |
QStandardItem *item = model.itemFromIndex(index); |
|
1033 |
item->setText(text); |
|
1034 |
item->setCheckState((Qt::CheckState)checkState); |
|
1035 |
item->setFlags((Qt::ItemFlags)flags); |
|
1036 |
||
1037 |
QStyleOptionViewItem option; |
|
1038 |
option.rect = rect; |
|
1039 |
option.state |= QStyle::State_Enabled; |
|
1040 |
||
1041 |
const int checkMargin = qApp->style()->pixelMetric(QStyle::PM_FocusFrameHMargin, 0, 0) + 1; |
|
1042 |
QPoint pos = inCheck ? qApp->style()->subElementRect(QStyle::SE_ViewItemCheckIndicator, &option, 0).center() + QPoint(checkMargin, 0) : QPoint(200,200); |
|
1043 |
||
1044 |
QEvent *event = new QMouseEvent((QEvent::Type)type, |
|
1045 |
pos, |
|
1046 |
(Qt::MouseButton)button, |
|
1047 |
(Qt::MouseButton)button, |
|
1048 |
Qt::NoModifier); |
|
1049 |
TestItemDelegate delegate; |
|
1050 |
bool wasEdited = delegate.editorEvent(event, &model, option, index); |
|
1051 |
delete event; |
|
1052 |
||
1053 |
QApplication::processEvents(); |
|
1054 |
||
1055 |
QCOMPARE(wasEdited, edited); |
|
1056 |
QCOMPARE(index.data(Qt::CheckStateRole).toInt(), expectedCheckState); |
|
1057 |
} |
|
1058 |
||
1059 |
enum WidgetType |
|
1060 |
{ |
|
1061 |
LineEdit, |
|
1062 |
TextEdit, |
|
1063 |
PlainTextEdit |
|
1064 |
}; |
|
1065 |
Q_DECLARE_METATYPE(WidgetType); |
|
1066 |
||
1067 |
void tst_QItemDelegate::enterKey_data() |
|
1068 |
{ |
|
1069 |
QTest::addColumn<WidgetType>("widget"); |
|
1070 |
QTest::addColumn<int>("key"); |
|
1071 |
QTest::addColumn<bool>("expectedFocus"); |
|
1072 |
||
1073 |
QTest::newRow("lineedit enter") << LineEdit << int(Qt::Key_Enter) << false; |
|
1074 |
QTest::newRow("textedit enter") << TextEdit << int(Qt::Key_Enter) << true; |
|
1075 |
QTest::newRow("plaintextedit enter") << PlainTextEdit << int(Qt::Key_Enter) << true; |
|
1076 |
QTest::newRow("plaintextedit return") << PlainTextEdit << int(Qt::Key_Return) << true; |
|
1077 |
QTest::newRow("plaintextedit tab") << PlainTextEdit << int(Qt::Key_Tab) << false; |
|
1078 |
QTest::newRow("lineedit tab") << LineEdit << int(Qt::Key_Tab) << false; |
|
1079 |
} |
|
1080 |
||
1081 |
void tst_QItemDelegate::enterKey() |
|
1082 |
{ |
|
1083 |
QFETCH(WidgetType, widget); |
|
1084 |
QFETCH(int, key); |
|
1085 |
QFETCH(bool, expectedFocus); |
|
1086 |
||
1087 |
QStandardItemModel model; |
|
1088 |
model.appendRow(new QStandardItem()); |
|
1089 |
||
1090 |
QListView view; |
|
1091 |
view.setModel(&model); |
|
1092 |
view.show(); |
|
1093 |
QApplication::setActiveWindow(&view); |
|
1094 |
view.setFocus(); |
|
1095 |
QTest::qWait(30); |
|
1096 |
||
1097 |
struct TestDelegate : public QItemDelegate |
|
1098 |
{ |
|
1099 |
WidgetType widgetType; |
|
1100 |
virtual QWidget* createEditor(QWidget* parent, const QStyleOptionViewItem& /*option*/, const QModelIndex& /*index*/) const |
|
1101 |
{ |
|
1102 |
QWidget *editor = 0; |
|
1103 |
switch(widgetType) { |
|
1104 |
case LineEdit: |
|
1105 |
editor = new QLineEdit(parent); |
|
1106 |
break; |
|
1107 |
case TextEdit: |
|
1108 |
editor = new QTextEdit(parent); |
|
1109 |
break; |
|
1110 |
case PlainTextEdit: |
|
1111 |
editor = new QPlainTextEdit(parent); |
|
1112 |
break; |
|
1113 |
} |
|
1114 |
editor->setObjectName(QString::fromLatin1("TheEditor")); |
|
1115 |
return editor; |
|
1116 |
} |
|
1117 |
} delegate; |
|
1118 |
||
1119 |
delegate.widgetType = widget; |
|
1120 |
||
1121 |
view.setItemDelegate(&delegate); |
|
1122 |
QModelIndex index = model.index(0, 0); |
|
1123 |
view.setCurrentIndex(index); // the editor will only selectAll on the current index |
|
1124 |
view.edit(index); |
|
1125 |
QTest::qWait(30); |
|
1126 |
||
1127 |
QList<QWidget*> lineEditors = qFindChildren<QWidget *>(view.viewport(), QString::fromLatin1("TheEditor")); |
|
1128 |
QCOMPARE(lineEditors.count(), 1); |
|
1129 |
||
1130 |
QPointer<QWidget> editor = lineEditors.at(0); |
|
1131 |
QCOMPARE(editor->hasFocus(), true); |
|
1132 |
||
1133 |
QTest::keyClick(editor, Qt::Key(key)); |
|
1134 |
QApplication::processEvents(); |
|
1135 |
||
1136 |
// The line edit has already been destroyed, so avoid that case. |
|
1137 |
if (widget == TextEdit || widget == PlainTextEdit) { |
|
1138 |
QVERIFY(!editor.isNull()); |
|
1139 |
QCOMPARE(editor && editor->hasFocus(), expectedFocus); |
|
1140 |
} |
|
1141 |
} |
|
1142 |
||
1143 |
void tst_QItemDelegate::task257859_finalizeEdit() |
|
1144 |
{ |
|
1145 |
QStandardItemModel model; |
|
1146 |
model.appendRow(new QStandardItem()); |
|
1147 |
||
1148 |
QListView view; |
|
1149 |
view.setModel(&model); |
|
1150 |
view.show(); |
|
1151 |
QApplication::setActiveWindow(&view); |
|
1152 |
view.setFocus(); |
|
1153 |
QTest::qWait(30); |
|
1154 |
||
1155 |
QModelIndex index = model.index(0, 0); |
|
1156 |
view.edit(index); |
|
1157 |
QTest::qWait(30); |
|
1158 |
||
1159 |
QList<QLineEdit *> lineEditors = qFindChildren<QLineEdit *>(view.viewport()); |
|
1160 |
QCOMPARE(lineEditors.count(), 1); |
|
1161 |
||
1162 |
QPointer<QWidget> editor = lineEditors.at(0); |
|
1163 |
QCOMPARE(editor->hasFocus(), true); |
|
1164 |
||
1165 |
QDialog dialog; |
|
1166 |
QTimer::singleShot(500, &dialog, SLOT(close())); |
|
1167 |
dialog.exec(); |
|
1168 |
QTRY_VERIFY(!editor); |
|
1169 |
} |
|
1170 |
||
1171 |
||
1172 |
// ### _not_ covered: |
|
1173 |
||
1174 |
// editing with a custom editor factory |
|
1175 |
||
1176 |
// painting when editing |
|
1177 |
// painting elided text |
|
1178 |
// painting wrapped text |
|
1179 |
// painting focus |
|
1180 |
// painting icon |
|
1181 |
// painting color |
|
1182 |
// painting check |
|
1183 |
// painting selected |
|
1184 |
||
1185 |
// rect for invalid |
|
1186 |
// rect for pixmap |
|
1187 |
// rect for image |
|
1188 |
// rect for icon |
|
1189 |
// rect for check |
|
1190 |
||
1191 |
QTEST_MAIN(tst_QItemDelegate) |
|
1192 |
#include "tst_qitemdelegate.moc" |