Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 from python_qt_binding.QtCore import Qt, qVersion, qWarning
00034 try:
00035 from python_qt_binding.QtCore import QSortFilterProxyModel
00036 except ImportError:
00037 from python_qt_binding.QtGui import QSortFilterProxyModel
00038 from python_qt_binding.QtGui import QBrush, QColor
00039
00040 from .filters.filter_collection import FilterCollection
00041 from .message import Message
00042
00043
00044 class MessageProxyModel(QSortFilterProxyModel):
00045
00046 """
00047 Provides sorting and filtering capabilities for the MessageDataModel.
00048 Filtering is based on a collection of exclude and highlight filters.
00049 """
00050
00051 def __init__(self):
00052 super(MessageProxyModel, self).__init__()
00053 self.setDynamicSortFilter(True)
00054 self.setFilterRole(Qt.UserRole)
00055 self.setSortCaseSensitivity(Qt.CaseInsensitive)
00056 self.setSortRole(Qt.UserRole)
00057
00058 self._exclude_filters = FilterCollection()
00059 self._highlight_filters = FilterCollection()
00060 self._show_highlighted_only = False
00061
00062
00063 self._source_model = None
00064
00065 def setSourceModel(self, source_model):
00066 super(MessageProxyModel, self).setSourceModel(source_model)
00067 self._source_model = self.sourceModel()
00068
00069
00070
00071 def filterAcceptsRow(self, sourcerow, sourceparent):
00072 """
00073 returns: True if the row does not match any exclude filter AND (_show_highlighted_only is
00074 False OR it matches any highlight filter), ''bool''
00075 """
00076 msg = self._source_model._messages[sourcerow]
00077 if self._exclude_filters.test_message(msg):
00078
00079 return False
00080
00081 highlighted = True
00082 if self._highlight_filters.count_enabled_filters() > 0:
00083 highlighted = self._highlight_filters.test_message(msg, default=True)
00084 if self._show_highlighted_only and not highlighted:
00085
00086
00087 return False
00088
00089
00090 msg.highlighted = highlighted
00091
00092 return True
00093
00094 def data(self, proxy_index, role=None):
00095 """
00096 Set colors of items based on highlight filters.
00097 """
00098 index = self.mapToSource(proxy_index)
00099 if role == Qt.ForegroundRole:
00100 msg = self._source_model._messages[index.row()]
00101 if not msg.highlighted:
00102 return QBrush(Qt.gray)
00103 return self._source_model.data(index, role)
00104
00105
00106
00107 def handle_exclude_filters_changed(self):
00108 """
00109 Invalidate filters and trigger refiltering.
00110 """
00111 self.invalidateFilter()
00112
00113 def handle_highlight_filters_changed(self):
00114 """
00115 Invalidate filters and trigger refiltering.
00116 """
00117 if self._show_highlighted_only:
00118 self.invalidateFilter()
00119 else:
00120 self.invalidateFilter()
00121 if qVersion().startswith('4.'):
00122 self.dataChanged.emit(
00123 self.index(0, 0), self.index(self.rowCount() - 1, self.columnCount() - 1))
00124 else:
00125 self.dataChanged.emit(
00126 self.index(0, 0), self.index(self.rowCount() - 1, self.columnCount() - 1), [])
00127
00128 def add_exclude_filter(self, newfilter):
00129 self._exclude_filters.append(newfilter)
00130
00131 def add_highlight_filter(self, newfilter):
00132 self._highlight_filters.append(newfilter)
00133
00134 def delete_exclude_filter(self, index):
00135 del self._exclude_filters[index]
00136 self.handle_exclude_filters_changed()
00137
00138 def delete_highlight_filter(self, index):
00139 del self._highlight_filters[index]
00140 self.handle_highlight_filters_changed()
00141
00142 def set_show_highlighted_only(self, show_highlighted_only):
00143 self._show_highlighted_only = show_highlighted_only
00144 self.invalidateFilter()