|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2006 - 2009 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing the browser sort filter proxy model. |
|
8 """ |
|
9 |
|
10 from PyQt4.QtCore import * |
|
11 from PyQt4.QtGui import * |
|
12 |
|
13 import Preferences |
|
14 |
|
15 class BrowserSortFilterProxyModel(QSortFilterProxyModel): |
|
16 """ |
|
17 Class implementing the browser sort filter proxy model. |
|
18 """ |
|
19 def __init__(self, parent = None): |
|
20 """ |
|
21 Constructor |
|
22 |
|
23 @param parent reference to the parent object (QObject) |
|
24 """ |
|
25 QSortFilterProxyModel.__init__(self, parent) |
|
26 self.hideNonPublic = Preferences.getUI("BrowsersHideNonPublic") |
|
27 |
|
28 def sort(self, column, order): |
|
29 """ |
|
30 Public method to sort the items. |
|
31 |
|
32 @param column column number to sort on (integer) |
|
33 @param order sort order for the sort (Qt.SortOrder) |
|
34 """ |
|
35 self.__sortColumn = column |
|
36 self.__sortOrder = order |
|
37 QSortFilterProxyModel.sort(self, column, order) |
|
38 |
|
39 def lessThan(self, left, right): |
|
40 """ |
|
41 Protected method used to sort the displayed items. |
|
42 |
|
43 It implements a special sorting function that takes into account, |
|
44 if folders should be shown first, and that __init__ is always the first |
|
45 method of a class. |
|
46 |
|
47 @param left index of left item (QModelIndex) |
|
48 @param right index of right item (QModelIndex) |
|
49 @return true, if left is less than right (boolean) |
|
50 """ |
|
51 l = left.model() and left.model().item(left) or None |
|
52 r = right.model() and right.model().item(right) or None |
|
53 |
|
54 if l and r: |
|
55 return l.lessThan(r, self.__sortColumn, self.__sortOrder) |
|
56 |
|
57 return False |
|
58 |
|
59 def item(self, index): |
|
60 """ |
|
61 Public method to get a reference to an item. |
|
62 |
|
63 @param index index of the data to retrieve (QModelIndex) |
|
64 @return requested item reference (BrowserItem) |
|
65 """ |
|
66 if not index.isValid(): |
|
67 return None |
|
68 |
|
69 sindex = self.mapToSource(index) |
|
70 return self.sourceModel().item(sindex) |
|
71 |
|
72 def hasChildren(self, parent = QModelIndex()): |
|
73 """ |
|
74 Public method to check for the presence of child items. |
|
75 |
|
76 We always return True for normal items in order to do lazy |
|
77 population of the tree. |
|
78 |
|
79 @param parent index of parent item (QModelIndex) |
|
80 @return flag indicating the presence of child items (boolean) |
|
81 """ |
|
82 sindex = self.mapToSource(parent) |
|
83 return self.sourceModel().hasChildren(sindex) |
|
84 |
|
85 def filterAcceptsRow(self, source_row, source_parent): |
|
86 """ |
|
87 Protected method to filter rows. |
|
88 |
|
89 It implements a filter to suppress the display of non public |
|
90 classes, methods and attributes. |
|
91 |
|
92 @param source_row row number (in the source model) of item (integer) |
|
93 @param source_parent index of parent item (in the source model) |
|
94 of item (QModelIndex) |
|
95 @return flag indicating, if the item should be shown (boolean) |
|
96 """ |
|
97 if self.hideNonPublic: |
|
98 sindex = self.sourceModel().index(source_row, 0, source_parent) |
|
99 return self.sourceModel().item(sindex).isPublic() |
|
100 else: |
|
101 return True |
|
102 |
|
103 def preferencesChanged(self): |
|
104 """ |
|
105 Public slot called to handle a change of the preferences settings. |
|
106 """ |
|
107 hideNonPublic = Preferences.getUI("BrowsersHideNonPublic") |
|
108 if self.hideNonPublic != hideNonPublic: |
|
109 self.hideNonPublic = hideNonPublic |
|
110 self.filterChanged() |