comparison examples/itemviews/customsortfiltermodel/window.d @ 146:22257add3152

add customsortfiltermodel example
author mandel
date Fri, 12 Jun 2009 22:20:37 +0000
parents
children 7c3067c2b803
comparison
equal deleted inserted replaced
145:7648ee2e023b 146:22257add3152
1 /****************************************************************************
2 **
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: Qt Software Information (qt-info@nokia.com)
5 **
6 ** This file is part of the examples of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial Usage
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Nokia.
14 **
15 ** GNU Lesser General Public License Usage
16 ** Alternatively, this file may be used under the terms of the GNU Lesser
17 ** General Public License version 2.1 as published by the Free Software
18 ** Foundation and appearing in the file LICENSE.LGPL included in the
19 ** packaging of this file. Please review the following information to
20 ** ensure the GNU Lesser General Public License version 2.1 requirements
21 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22 **
23 ** In addition, as a special exception, Nokia gives you certain
24 ** additional rights. These rights are described in the Nokia Qt LGPL
25 ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26 ** package.
27 **
28 ** GNU General Public License Usage
29 ** Alternatively, this file may be used under the terms of the GNU
30 ** General Public License version 3.0 as published by the Free Software
31 ** Foundation and appearing in the file LICENSE.GPL included in the
32 ** packaging of this file. Please review the following information to
33 ** ensure the GNU General Public License version 3.0 requirements will be
34 ** met: http://www.gnu.org/copyleft/gpl.html.
35 **
36 ** If you are unsure which license is appropriate for your use, please
37 ** contact the sales department at qt-sales@nokia.com.
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 module window;
42
43
44 import qt.core.QAbstractItemModel;
45 import qt.gui.QWidget;
46 import qt.gui.QCheckBox;
47 import qt.gui.QComboBox;
48 import qt.gui.QDateEdit;
49 import qt.gui.QGroupBox;
50 import qt.gui.QLabel;
51 import qt.gui.QLineEdit;
52 import qt.gui.QTreeView;
53 import qt.gui.QHBoxLayout;
54 import qt.gui.QVBoxLayout;
55 import qt.gui.QGridLayout;
56
57 import mysortfilterproxymodel;
58
59
60 class Window : public QWidget
61 {
62 public:
63
64 this()
65 {
66 proxyModel = new MySortFilterProxyModel(this);
67 proxyModel.setDynamicSortFilter(true);
68
69 sourceView = new QTreeView;
70 sourceView.setRootIsDecorated(false);
71 sourceView.setAlternatingRowColors(true);
72
73 QHBoxLayout sourceLayout = new QHBoxLayout;
74
75 sourceLayout.addWidget(sourceView);
76 sourceGroupBox = new QGroupBox(tr("Original Model"));
77 sourceGroupBox.setLayout(sourceLayout);
78
79 filterCaseSensitivityCheckBox = new QCheckBox(tr("Case sensitive filter"));
80 filterCaseSensitivityCheckBox.setChecked(true);
81
82 filterPatternLineEdit = new QLineEdit;
83 filterPatternLineEdit.setText("Grace|Sports");
84
85 filterPatternLabel = new QLabel(tr("&Filter pattern:"));
86 filterPatternLabel.setBuddy(filterPatternLineEdit);
87
88 filterSyntaxComboBox = new QComboBox;
89 filterSyntaxComboBox.addItem(tr("Regular expression"), new QVariant(cast(ulong) QRegExp.RegExp));
90 filterSyntaxComboBox.addItem(tr("Wildcard"), new QVariant(cast(ulong) QRegExp.Wildcard));
91 filterSyntaxComboBox.addItem(tr("Fixed string"), new QVariant(cast(ulong) QRegExp.FixedString));
92
93 fromDateEdit = new QDateEdit;
94 fromDateEdit.setDate(new QDate(1970, 01, 01));
95 fromLabel = new QLabel(tr("F&rom:"));
96 fromLabel.setBuddy(fromDateEdit);
97
98 toDateEdit = new QDateEdit;
99 toDateEdit.setDate(new QDate(2099, 12, 31));
100 toLabel = new QLabel(tr("&To:"));
101 toLabel.setBuddy(toDateEdit);
102
103 filterPatternLineEdit.textChanged.connect(&this.textFilterChanged);
104 filterSyntaxComboBox.currentIndexChanged.connect(&this.textFilterChanged);
105 filterCaseSensitivityCheckBox.toggled.connect(&this.textFilterChanged);
106 fromDateEdit.dateChanged.connect(&this.dateFilterChanged);
107 toDateEdit.dateChanged.connect(&this.dateFilterChanged);
108
109 proxyView = new QTreeView;
110 proxyView.setRootIsDecorated(false);
111 proxyView.setAlternatingRowColors(true);
112 proxyView.setModel(proxyModel);
113 proxyView.setSortingEnabled(true);
114 proxyView.sortByColumn(1, Qt.AscendingOrder);
115
116 QGridLayout proxyLayout = new QGridLayout;
117 proxyLayout.addWidget(proxyView, 0, 0, 1, 3);
118 proxyLayout.addWidget(filterPatternLabel, 1, 0);
119 proxyLayout.addWidget(filterPatternLineEdit, 1, 1);
120 proxyLayout.addWidget(filterSyntaxComboBox, 1, 2);
121 proxyLayout.addWidget(filterCaseSensitivityCheckBox, 2, 0, 1, 3);
122 proxyLayout.addWidget(fromLabel, 3, 0);
123 proxyLayout.addWidget(fromDateEdit, 3, 1, 1, 2);
124 proxyLayout.addWidget(toLabel, 4, 0);
125 proxyLayout.addWidget(toDateEdit, 4, 1, 1, 2);
126
127 proxyGroupBox = new QGroupBox(tr("Sorted/Filtered Model"));
128 proxyGroupBox.setLayout(proxyLayout);
129
130 QVBoxLayout mainLayout = new QVBoxLayout;
131 mainLayout.addWidget(sourceGroupBox);
132 mainLayout.addWidget(proxyGroupBox);
133 setLayout(mainLayout);
134
135 setWindowTitle(tr("Custom Sort/Filter Model"));
136 resize(500, 450);
137 }
138
139 void setSourceModel(QAbstractItemModel model)
140 {
141 proxyModel.setSourceModel(model);
142 sourceView.setModel(model);
143 }
144
145 private:
146
147 void textFilterChanged()
148 {
149 QRegExp.PatternSyntax syntax = cast(QRegExp.PatternSyntax) filterSyntaxComboBox.itemData(
150 filterSyntaxComboBox.currentIndex()).toInt();
151 Qt.CaseSensitivity caseSensitivity =
152 filterCaseSensitivityCheckBox.isChecked() ? Qt.CaseSensitive
153 : Qt.CaseInsensitive;
154
155 auto regExp = new QRegExp(filterPatternLineEdit.text(), caseSensitivity, syntax);
156 proxyModel.setFilterRegExp(regExp);
157 }
158
159 void dateFilterChanged()
160 {
161 proxyModel.setFilterMinimumDate(fromDateEdit.date());
162 proxyModel.setFilterMaximumDate(toDateEdit.date());
163 }
164
165 private:
166
167 MySortFilterProxyModel proxyModel;
168
169 QGroupBox sourceGroupBox;
170 QGroupBox proxyGroupBox;
171 QTreeView sourceView;
172 QTreeView proxyView;
173 QCheckBox filterCaseSensitivityCheckBox;
174 QLabel filterPatternLabel;
175 QLabel fromLabel;
176 QLabel toLabel;
177 QLineEdit filterPatternLineEdit;
178 QComboBox filterSyntaxComboBox;
179 QDateEdit fromDateEdit;
180 QDateEdit toDateEdit;
181 }