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