1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the examples of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:BSD$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** BSD License Usage |
18 | ** Alternatively, you may use this file under the terms of the BSD license |
19 | ** as follows: |
20 | ** |
21 | ** "Redistribution and use in source and binary forms, with or without |
22 | ** modification, are permitted provided that the following conditions are |
23 | ** met: |
24 | ** * Redistributions of source code must retain the above copyright |
25 | ** notice, this list of conditions and the following disclaimer. |
26 | ** * Redistributions in binary form must reproduce the above copyright |
27 | ** notice, this list of conditions and the following disclaimer in |
28 | ** the documentation and/or other materials provided with the |
29 | ** distribution. |
30 | ** * Neither the name of The Qt Company Ltd nor the names of its |
31 | ** contributors may be used to endorse or promote products derived |
32 | ** from this software without specific prior written permission. |
33 | ** |
34 | ** |
35 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
36 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
37 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
38 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
39 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
40 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
41 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
42 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
43 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
44 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
45 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
46 | ** |
47 | ** $QT_END_LICENSE$ |
48 | ** |
49 | ****************************************************************************/ |
50 | |
51 | #include "window.h" |
52 | #include "mysortfilterproxymodel.h" |
53 | #include "filterwidget.h" |
54 | |
55 | #include <QtWidgets> |
56 | |
57 | //! [0] |
58 | Window::Window() |
59 | { |
60 | proxyModel = new MySortFilterProxyModel(this); |
61 | //! [0] |
62 | |
63 | //! [1] |
64 | sourceView = new QTreeView; |
65 | sourceView->setRootIsDecorated(false); |
66 | sourceView->setAlternatingRowColors(true); |
67 | //! [1] |
68 | |
69 | QHBoxLayout *sourceLayout = new QHBoxLayout; |
70 | //! [2] |
71 | sourceLayout->addWidget(sourceView); |
72 | sourceGroupBox = new QGroupBox(tr(s: "Original Model" )); |
73 | sourceGroupBox->setLayout(sourceLayout); |
74 | //! [2] |
75 | |
76 | //! [3] |
77 | filterWidget = new FilterWidget; |
78 | filterWidget->setText(tr(s: "Grace|Sports" )); |
79 | connect(sender: filterWidget, signal: &FilterWidget::filterChanged, receiver: this, slot: &Window::textFilterChanged); |
80 | |
81 | filterPatternLabel = new QLabel(tr(s: "&Filter pattern:" )); |
82 | filterPatternLabel->setBuddy(filterWidget); |
83 | |
84 | fromDateEdit = new QDateEdit; |
85 | fromDateEdit->setDate(QDate(1970, 01, 01)); |
86 | fromLabel = new QLabel(tr(s: "F&rom:" )); |
87 | fromLabel->setBuddy(fromDateEdit); |
88 | |
89 | toDateEdit = new QDateEdit; |
90 | toDateEdit->setDate(QDate(2099, 12, 31)); |
91 | toLabel = new QLabel(tr(s: "&To:" )); |
92 | toLabel->setBuddy(toDateEdit); |
93 | |
94 | connect(sender: filterWidget, signal: &QLineEdit::textChanged, |
95 | receiver: this, slot: &Window::textFilterChanged); |
96 | connect(sender: fromDateEdit, signal: &QDateTimeEdit::dateChanged, |
97 | receiver: this, slot: &Window::dateFilterChanged); |
98 | connect(sender: toDateEdit, signal: &QDateTimeEdit::dateChanged, |
99 | //! [3] //! [4] |
100 | receiver: this, slot: &Window::dateFilterChanged); |
101 | //! [4] |
102 | |
103 | //! [5] |
104 | proxyView = new QTreeView; |
105 | proxyView->setRootIsDecorated(false); |
106 | proxyView->setAlternatingRowColors(true); |
107 | proxyView->setModel(proxyModel); |
108 | proxyView->setSortingEnabled(true); |
109 | proxyView->sortByColumn(column: 1, order: Qt::AscendingOrder); |
110 | |
111 | QGridLayout *proxyLayout = new QGridLayout; |
112 | proxyLayout->addWidget(proxyView, row: 0, column: 0, rowSpan: 1, columnSpan: 3); |
113 | proxyLayout->addWidget(filterPatternLabel, row: 1, column: 0); |
114 | proxyLayout->addWidget(filterWidget, row: 1, column: 1); |
115 | proxyLayout->addWidget(fromLabel, row: 3, column: 0); |
116 | proxyLayout->addWidget(fromDateEdit, row: 3, column: 1, rowSpan: 1, columnSpan: 2); |
117 | proxyLayout->addWidget(toLabel, row: 4, column: 0); |
118 | proxyLayout->addWidget(toDateEdit, row: 4, column: 1, rowSpan: 1, columnSpan: 2); |
119 | |
120 | proxyGroupBox = new QGroupBox(tr(s: "Sorted/Filtered Model" )); |
121 | proxyGroupBox->setLayout(proxyLayout); |
122 | //! [5] |
123 | |
124 | //! [6] |
125 | QVBoxLayout *mainLayout = new QVBoxLayout; |
126 | mainLayout->addWidget(sourceGroupBox); |
127 | mainLayout->addWidget(proxyGroupBox); |
128 | setLayout(mainLayout); |
129 | |
130 | setWindowTitle(tr(s: "Custom Sort/Filter Model" )); |
131 | resize(w: 500, h: 450); |
132 | } |
133 | //! [6] |
134 | |
135 | //! [7] |
136 | void Window::setSourceModel(QAbstractItemModel *model) |
137 | { |
138 | proxyModel->setSourceModel(model); |
139 | sourceView->setModel(model); |
140 | |
141 | for (int i = 0; i < proxyModel->columnCount(); ++i) |
142 | proxyView->resizeColumnToContents(column: i); |
143 | for (int i = 0; i < model->columnCount(); ++i) |
144 | sourceView->resizeColumnToContents(column: i); |
145 | } |
146 | //! [7] |
147 | |
148 | //! [8] |
149 | void Window::textFilterChanged() |
150 | { |
151 | QRegExp regExp(filterWidget->text(), |
152 | filterWidget->caseSensitivity(), |
153 | filterWidget->patternSyntax()); |
154 | proxyModel->setFilterRegExp(regExp); |
155 | } |
156 | //! [8] |
157 | |
158 | //! [9] |
159 | void Window::dateFilterChanged() |
160 | { |
161 | proxyModel->setFilterMinimumDate(fromDateEdit->date()); |
162 | proxyModel->setFilterMaximumDate(toDateEdit->date()); |
163 | } |
164 | //! [9] |
165 | |