-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathold.py
222 lines (168 loc) · 8.62 KB
/
old.py
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
#!/usr/bin/python3
import sys
import PyQt5.QtSql as QtSql
from PyQt5.QtGui import QStandardItemModel
from PyQt5.QtCore import QDate, QDateTime, QRegExp, QSortFilterProxyModel, Qt, QTime
from PyQt5.QtWidgets import QApplication, QCheckBox, QComboBox, QGridLayout, QGroupBox, QHBoxLayout, QMessageBox
from PyQt5.QtWidgets import QLineEdit, QTreeView, QVBoxLayout, QPushButton, QWidget, QLabel, QAbstractItemView
SUBJECT, SENDER, DATE = range(3)
class SortFilterProxyModel(QSortFilterProxyModel):
def filterAcceptsRow(self, sourceRow, sourceParent):
# Do we filter for the date column?
if self.filterKeyColumn() == DATE:
# Fetch datetime value.
index = self.sourceModel().index(sourceRow, DATE, sourceParent)
data = self.sourceModel().data(index)
# Return, if regExp match in displayed format.
return (self.filterRegExp().indexIn(data.toString(Qt.DefaultLocaleShortDate)) >= 0)
# Not our business.
return super(SortFilterProxyModel, self).filterAcceptsRow(sourceRow, sourceParent)
class Window(QWidget):
def __init__(self):
super(Window, self).__init__()
mainLayout = QVBoxLayout()
mainLayout.addWidget(self.source_gbox())
# mainLayout.addWidget(self.proxy_gbox())
button = QPushButton('Merge')
button.clicked.connect(self.merge)
mainLayout.addWidget(button)
self.setLayout(mainLayout)
self.setWindowTitle("Basic Sort/Filter Model")
self.resize(600, 650)
def merge(self):
mb = QMessageBox()
mb.setText('Merge! %s' % str(self.sourceView.selectedIndexes()))
mb.exec_()
def source_gbox(self):
self.sourceGroupBox = QGroupBox("Original Model")
self.sourceView = QTreeView()
self.sourceView.setRootIsDecorated(False)
self.sourceView.setAlternatingRowColors(True)
self.sourceView.setSortingEnabled(True)
self.sourceView.sortByColumn(SENDER, Qt.AscendingOrder)
self.sourceView.setSelectionMode(QAbstractItemView.MultiSelection)
sourceLayout = QHBoxLayout()
sourceLayout.addWidget(self.sourceView)
self.sourceGroupBox.setLayout(sourceLayout)
return self.sourceGroupBox
def proxy_gbox(self):
self.proxyModel = SortFilterProxyModel()
self.proxyModel.setDynamicSortFilter(True)
self.proxyGroupBox = QGroupBox("Sorted/Filtered Model")
self.proxyView = QTreeView()
self.proxyView.setRootIsDecorated(False)
self.proxyView.setAlternatingRowColors(True)
self.proxyView.setModel(self.proxyModel)
self.proxyView.setSortingEnabled(True)
self.sortCaseSensitivityCheckBox = QCheckBox("Case sensitive sorting")
self.filterCaseSensitivityCheckBox = QCheckBox("Case sensitive filter")
self.filterPatternLineEdit = QLineEdit()
self.filterPatternLabel = QLabel("&Filter pattern:")
self.filterPatternLabel.setBuddy(self.filterPatternLineEdit)
self.filterSyntaxComboBox = QComboBox()
self.filterSyntaxComboBox.addItem("Regular expression", QRegExp.RegExp)
self.filterSyntaxComboBox.addItem("Wildcard", QRegExp.Wildcard)
self.filterSyntaxComboBox.addItem("Fixed string", QRegExp.FixedString)
self.filterSyntaxLabel = QLabel("Filter &syntax:")
self.filterSyntaxLabel.setBuddy(self.filterSyntaxComboBox)
self.filterColumnComboBox = QComboBox()
self.filterColumnComboBox.addItem("Subject")
self.filterColumnComboBox.addItem("Sender")
self.filterColumnComboBox.addItem("Date")
self.filterColumnLabel = QLabel("Filter &column:")
self.filterColumnLabel.setBuddy(self.filterColumnComboBox)
self.filterPatternLineEdit.textChanged.connect(self.filterRegExpChanged)
self.filterSyntaxComboBox.currentIndexChanged.connect(self.filterRegExpChanged)
self.filterColumnComboBox.currentIndexChanged.connect(self.filterColumnChanged)
self.filterCaseSensitivityCheckBox.toggled.connect(self.filterRegExpChanged)
self.sortCaseSensitivityCheckBox.toggled.connect(self.sortChanged)
proxyLayout = QGridLayout()
proxyLayout.addWidget(self.proxyView, 0, 0, 1, 3)
proxyLayout.addWidget(self.filterPatternLabel, 1, 0)
proxyLayout.addWidget(self.filterPatternLineEdit, 1, 1, 1, 2)
proxyLayout.addWidget(self.filterSyntaxLabel, 2, 0)
proxyLayout.addWidget(self.filterSyntaxComboBox, 2, 1, 1, 2)
proxyLayout.addWidget(self.filterColumnLabel, 3, 0)
proxyLayout.addWidget(self.filterColumnComboBox, 3, 1, 1, 2)
proxyLayout.addWidget(self.filterCaseSensitivityCheckBox, 4, 0, 1, 2)
proxyLayout.addWidget(self.sortCaseSensitivityCheckBox, 4, 2)
self.proxyGroupBox.setLayout(proxyLayout)
self.proxyView.sortByColumn(SENDER, Qt.AscendingOrder)
self.filterColumnComboBox.setCurrentIndex(SENDER)
self.filterPatternLineEdit.setText("Andy|Grace")
self.filterCaseSensitivityCheckBox.setChecked(True)
self.sortCaseSensitivityCheckBox.setChecked(True)
return self.proxyGroupBox
def setSourceModel(self, model):
# self.proxyModel.setSourceModel(model)
self.sourceView.setModel(model)
def filterRegExpChanged(self):
syntax_nr = self.filterSyntaxComboBox.itemData(self.filterSyntaxComboBox.currentIndex())
syntax = QRegExp.PatternSyntax(syntax_nr)
if self.filterCaseSensitivityCheckBox.isChecked():
caseSensitivity = Qt.CaseSensitive
else:
caseSensitivity = Qt.CaseInsensitive
regExp = QRegExp(self.filterPatternLineEdit.text(),
caseSensitivity, syntax)
self.proxyModel.setFilterRegExp(regExp)
def filterColumnChanged(self):
self.proxyModel.setFilterKeyColumn(self.filterColumnComboBox.currentIndex())
def sortChanged(self):
if self.sortCaseSensitivityCheckBox.isChecked():
caseSensitivity = Qt.CaseSensitive
else:
caseSensitivity = Qt.CaseInsensitive
self.proxyModel.setSortCaseSensitivity(caseSensitivity)
def addMail(model, subject, sender, date):
model.insertRow(0)
model.setData(model.index(0, SUBJECT), subject)
model.setData(model.index(0, SENDER), sender)
model.setData(model.index(0, DATE), date)
def createMailModel(parent):
model = QStandardItemModel(0, 3, parent)
model.setHeaderData(SUBJECT, Qt.Horizontal, "Subject")
model.setHeaderData(SENDER, Qt.Horizontal, "Sender")
model.setHeaderData(DATE, Qt.Horizontal, "Date")
addMail(model, "Happy New Year!", "Grace K. <[email protected]>",
QDateTime(QDate(2006, 12, 31), QTime(17, 3)))
addMail(model, "Radically new concept", "Grace K. <[email protected]>",
QDateTime(QDate(2006, 12, 22), QTime(9, 44)))
addMail(model, "Accounts", "[email protected]",
QDateTime(QDate(2006, 12, 31), QTime(12, 50)))
addMail(model, "Expenses", "Joe Bloggs <[email protected]>",
QDateTime(QDate(2006, 12, 25), QTime(11, 39)))
addMail(model, "Re: Expenses", "Andy <[email protected]>",
QDateTime(QDate(2007, 1, 2), QTime(16, 5)))
addMail(model, "Re: Accounts", "Joe Bloggs <[email protected]>",
QDateTime(QDate(2007, 1, 3), QTime(14, 18)))
addMail(model, "Re: Accounts", "Andy <[email protected]>",
QDateTime(QDate(2007, 1, 3), QTime(14, 26)))
addMail(model, "Sports", "Linda Smith <[email protected]>",
QDateTime(QDate(2007, 1, 5), QTime(11, 33)))
addMail(model, "AW: Sports", "Rolf Newschweinstein <[email protected]>",
QDateTime(QDate(2007, 1, 5), QTime(12, 0)))
addMail(model, "RE: Sports", "Petra Schmidt <[email protected]>",
QDateTime(QDate(2007, 1, 5), QTime(12, 1)))
return model
def createDB():
db = QtSql.QSqlDatabase.addDatabase('QSQLITE')
db.setDatabaseName('sports.db')
if not db.open():
return False
query = QtSql.QSqlQuery()
query.exec_("create table sportsmen(id int primary key, "
"firstname varchar(20), lastname varchar(20))")
query.exec_("insert into sportsmen values(101, 'Roger', 'Federer')")
query.exec_("insert into sportsmen values(102, 'Christiano', 'Ronaldo')")
query.exec_("insert into sportsmen values(103, 'Ussain', 'Bolt')")
query.exec_("insert into sportsmen values(104, 'Sachin', 'Tendulkar')")
query.exec_("insert into sportsmen values(105, 'Saina', 'Nehwal')")
return True
if __name__ == '__main__':
app = QApplication(sys.argv)
createDB()
window = Window()
window.setSourceModel(createMailModel(window))
window.show()
sys.exit(app.exec_())