annotate examples/itemviews/customsortfiltermodel/mysortfilterproxymodel.d @ 282:256ab6cb8e85

Signals look-up andNew syntax for connect. The old one will not work from now on. This will allow for the signals overload. Although changes are done for both D1 and D2 versions, D1 won't work because of compiler bugs. I am tired of waiting for fixes.
author eldar
date Fri, 16 Oct 2009 02:43:59 +0000
parents 26036621830e
children 7a3c43424dca
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
146
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
1 /****************************************************************************
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
2 **
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
4 ** Contact: Qt Software Information (qt-info@nokia.com)
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
5 **
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
6 ** This file is part of the examples of the Qt Toolkit.
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
7 **
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
8 ** $QT_BEGIN_LICENSE:LGPL$
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
9 ** Commercial Usage
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
10 ** Licensees holding valid Qt Commercial licenses may use this file in
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
11 ** accordance with the Qt Commercial License Agreement provided with the
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
12 ** Software or, alternatively, in accordance with the terms contained in
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
13 ** a written agreement between you and Nokia.
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
14 **
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
15 ** GNU Lesser General Public License Usage
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
16 ** Alternatively, this file may be used under the terms of the GNU Lesser
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
17 ** General Public License version 2.1 as published by the Free Software
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
18 ** Foundation and appearing in the file LICENSE.LGPL included in the
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
19 ** packaging of this file. Please review the following information to
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
20 ** ensure the GNU Lesser General Public License version 2.1 requirements
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
21 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
22 **
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
23 ** In addition, as a special exception, Nokia gives you certain
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
24 ** additional rights. These rights are described in the Nokia Qt LGPL
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
25 ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
26 ** package.
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
27 **
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
28 ** GNU General Public License Usage
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
29 ** Alternatively, this file may be used under the terms of the GNU
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
30 ** General Public License version 3.0 as published by the Free Software
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
31 ** Foundation and appearing in the file LICENSE.GPL included in the
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
32 ** packaging of this file. Please review the following information to
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
33 ** ensure the GNU General Public License version 3.0 requirements will be
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
34 ** met: http://www.gnu.org/copyleft/gpl.html.
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
35 **
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
36 ** If you are unsure which license is appropriate for your use, please
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
37 ** contact the sales department at qt-sales@nokia.com.
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
38 ** $QT_END_LICENSE$
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
39 **
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
40 ****************************************************************************/
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
41 module mysortfilterproxymodel;
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
42
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
43
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
44 import qt.core.QDate;
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
45 import qt.core.QVariant;
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
46 import qt.core.QDateTime;
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
47 import qt.gui.QSortFilterProxyModel;
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
48
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
49
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
50 class MySortFilterProxyModel : public QSortFilterProxyModel
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
51 {
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
52 public:
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
53
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
54 this(QObject parent = null)
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
55 {
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
56 super(parent);
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
57 minDate = new QDate();
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
58 maxDate = new QDate();
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
59 }
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
60
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
61 QDate filterMinimumDate()
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
62 {
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
63 return minDate;
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
64 }
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
65
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
66 void setFilterMinimumDate(QDate date)
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
67 {
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
68 minDate = date;
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
69 invalidateFilter();
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
70 }
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
71
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
72 QDate filterMaximumDate()
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
73 {
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
74 return maxDate;
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
75 }
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
76
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
77 void setFilterMaximumDate(QDate date)
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
78 {
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
79 maxDate = date;
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
80 invalidateFilter();
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
81 }
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
82
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
83 protected:
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
84
213
26036621830e more D2 examples fixes
SokoL_SD
parents: 207
diff changeset
85 override bool filterAcceptsRow(int sourceRow, const QModelIndex sourceParent)
146
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
86 {
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
87 QModelIndex index0 = sourceModel().index(sourceRow, 0, sourceParent);
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
88 QModelIndex index1 = sourceModel().index(sourceRow, 1, sourceParent);
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
89 QModelIndex index2 = sourceModel().index(sourceRow, 2, sourceParent);
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
90
207
71101646ab25 more D2 examples fixes
SokoL_SD
parents: 160
diff changeset
91 static bool contains(string str, QRegExp rx1)
146
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
92 {
157
7c3067c2b803 small issues
mandel
parents: 146
diff changeset
93 auto rx2 = new QRegExp(rx1);
7c3067c2b803 small issues
mandel
parents: 146
diff changeset
94 return (rx2.indexIn(str, 0) != -1);
146
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
95 }
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
96
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
97 return (contains(sourceModel().data(index0).toString(), filterRegExp())
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
98 || contains(sourceModel().data(index1).toString(), filterRegExp()))
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
99 && dateInRange(sourceModel().data(index2).toDate());
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
100 }
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
101
213
26036621830e more D2 examples fixes
SokoL_SD
parents: 207
diff changeset
102 override bool lessThan(const QModelIndex left, const QModelIndex right)
146
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
103 {
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
104 QVariant leftData = sourceModel().data(left);
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
105 QVariant rightData = sourceModel().data(right);
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
106
160
624b4a58556e fix sorting
mandel
parents: 157
diff changeset
107 if (leftData.type() == QVariant.Type.DateTime) {
146
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
108 return leftData.toDateTime() < rightData.toDateTime();
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
109 } else {
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
110 QRegExp emailPattern = new QRegExp("([\\w\\.]*@[\\w\\.]*)");
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
111
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
112 string leftString = leftData.toString();
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
113 if(left.column() == 1 && emailPattern.indexIn(leftString) != -1)
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
114 leftString = emailPattern.cap(1);
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
115
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
116 string rightString = rightData.toString();
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
117 if(right.column() == 1 && emailPattern.indexIn(rightString) != -1)
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
118 rightString = emailPattern.cap(1);
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
119
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
120 return leftString < rightString;
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
121 }
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
122 }
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
123
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
124 private:
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
125
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
126 bool dateInRange(QDate date)
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
127 {
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
128 return (!minDate.isValid() || date > minDate) && (!maxDate.isValid() || date < maxDate);
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
129 }
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
130
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
131 QDate minDate;
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
132 QDate maxDate;
22257add3152 add customsortfiltermodel example
mandel
parents:
diff changeset
133 }