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 <QtWidgets> |
52 | |
53 | #include "window.h" |
54 | |
55 | Window::Window() |
56 | { |
57 | proxyModel = new QSortFilterProxyModel; |
58 | |
59 | sourceView = new QTreeView; |
60 | sourceView->setRootIsDecorated(false); |
61 | sourceView->setAlternatingRowColors(true); |
62 | |
63 | proxyView = new QTreeView; |
64 | proxyView->setRootIsDecorated(false); |
65 | proxyView->setAlternatingRowColors(true); |
66 | proxyView->setModel(proxyModel); |
67 | proxyView->setSortingEnabled(true); |
68 | |
69 | sortCaseSensitivityCheckBox = new QCheckBox(tr(s: "Case sensitive sorting" )); |
70 | filterCaseSensitivityCheckBox = new QCheckBox(tr(s: "Case sensitive filter" )); |
71 | |
72 | filterPatternLineEdit = new QLineEdit; |
73 | filterPatternLabel = new QLabel(tr(s: "&Filter pattern:" )); |
74 | filterPatternLabel->setBuddy(filterPatternLineEdit); |
75 | |
76 | filterSyntaxComboBox = new QComboBox; |
77 | filterSyntaxComboBox->addItem(atext: tr(s: "Regular expression" ), auserData: QRegExp::RegExp); |
78 | filterSyntaxComboBox->addItem(atext: tr(s: "Wildcard" ), auserData: QRegExp::Wildcard); |
79 | filterSyntaxComboBox->addItem(atext: tr(s: "Fixed string" ), auserData: QRegExp::FixedString); |
80 | filterSyntaxLabel = new QLabel(tr(s: "Filter &syntax:" )); |
81 | filterSyntaxLabel->setBuddy(filterSyntaxComboBox); |
82 | |
83 | filterColumnComboBox = new QComboBox; |
84 | filterColumnComboBox->addItem(atext: tr(s: "Subject" )); |
85 | filterColumnComboBox->addItem(atext: tr(s: "Sender" )); |
86 | filterColumnComboBox->addItem(atext: tr(s: "Date" )); |
87 | filterColumnLabel = new QLabel(tr(s: "Filter &column:" )); |
88 | filterColumnLabel->setBuddy(filterColumnComboBox); |
89 | |
90 | connect(sender: filterPatternLineEdit, signal: &QLineEdit::textChanged, |
91 | receiver: this, slot: &Window::filterRegExpChanged); |
92 | connect(sender: filterSyntaxComboBox, signal: QOverload<int>::of(ptr: &QComboBox::currentIndexChanged), |
93 | receiver: this, slot: &Window::filterRegExpChanged); |
94 | connect(sender: filterColumnComboBox, signal: QOverload<int>::of(ptr: &QComboBox::currentIndexChanged), |
95 | receiver: this, slot: &Window::filterColumnChanged); |
96 | connect(sender: filterCaseSensitivityCheckBox, signal: &QAbstractButton::toggled, |
97 | receiver: this, slot: &Window::filterRegExpChanged); |
98 | connect(sender: sortCaseSensitivityCheckBox, signal: &QAbstractButton::toggled, |
99 | receiver: this, slot: &Window::sortChanged); |
100 | |
101 | sourceGroupBox = new QGroupBox(tr(s: "Original Model" )); |
102 | proxyGroupBox = new QGroupBox(tr(s: "Sorted/Filtered Model" )); |
103 | |
104 | QHBoxLayout *sourceLayout = new QHBoxLayout; |
105 | sourceLayout->addWidget(sourceView); |
106 | sourceGroupBox->setLayout(sourceLayout); |
107 | |
108 | QGridLayout *proxyLayout = new QGridLayout; |
109 | proxyLayout->addWidget(proxyView, row: 0, column: 0, rowSpan: 1, columnSpan: 3); |
110 | proxyLayout->addWidget(filterPatternLabel, row: 1, column: 0); |
111 | proxyLayout->addWidget(filterPatternLineEdit, row: 1, column: 1, rowSpan: 1, columnSpan: 2); |
112 | proxyLayout->addWidget(filterSyntaxLabel, row: 2, column: 0); |
113 | proxyLayout->addWidget(filterSyntaxComboBox, row: 2, column: 1, rowSpan: 1, columnSpan: 2); |
114 | proxyLayout->addWidget(filterColumnLabel, row: 3, column: 0); |
115 | proxyLayout->addWidget(filterColumnComboBox, row: 3, column: 1, rowSpan: 1, columnSpan: 2); |
116 | proxyLayout->addWidget(filterCaseSensitivityCheckBox, row: 4, column: 0, rowSpan: 1, columnSpan: 2); |
117 | proxyLayout->addWidget(sortCaseSensitivityCheckBox, row: 4, column: 2); |
118 | proxyGroupBox->setLayout(proxyLayout); |
119 | |
120 | QVBoxLayout *mainLayout = new QVBoxLayout; |
121 | |
122 | mainLayout->addWidget(sourceGroupBox); |
123 | mainLayout->addWidget(proxyGroupBox); |
124 | |
125 | setLayout(mainLayout); |
126 | |
127 | setWindowTitle(tr(s: "Basic Sort/Filter Model" )); |
128 | resize(w: 500, h: 450); |
129 | |
130 | proxyView->sortByColumn(column: 1, order: Qt::AscendingOrder); |
131 | filterColumnComboBox->setCurrentIndex(1); |
132 | |
133 | filterPatternLineEdit->setText("Andy|Grace" ); |
134 | filterCaseSensitivityCheckBox->setChecked(true); |
135 | sortCaseSensitivityCheckBox->setChecked(true); |
136 | } |
137 | |
138 | void Window::setSourceModel(QAbstractItemModel *model) |
139 | { |
140 | proxyModel->setSourceModel(model); |
141 | sourceView->setModel(model); |
142 | } |
143 | |
144 | void Window::filterRegExpChanged() |
145 | { |
146 | QRegExp::PatternSyntax syntax = |
147 | QRegExp::PatternSyntax(filterSyntaxComboBox->itemData( |
148 | index: filterSyntaxComboBox->currentIndex()).toInt()); |
149 | Qt::CaseSensitivity caseSensitivity = |
150 | filterCaseSensitivityCheckBox->isChecked() ? Qt::CaseSensitive |
151 | : Qt::CaseInsensitive; |
152 | |
153 | QRegExp regExp(filterPatternLineEdit->text(), caseSensitivity, syntax); |
154 | proxyModel->setFilterRegExp(regExp); |
155 | } |
156 | |
157 | void Window::filterColumnChanged() |
158 | { |
159 | proxyModel->setFilterKeyColumn(filterColumnComboBox->currentIndex()); |
160 | } |
161 | |
162 | void Window::sortChanged() |
163 | { |
164 | proxyModel->setSortCaseSensitivity( |
165 | sortCaseSensitivityCheckBox->isChecked() ? Qt::CaseSensitive |
166 | : Qt::CaseInsensitive); |
167 | } |
168 | |