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 Provides sorting and filtering capabilities for the MessageDataModel.
00047 Filtering is based on a collection of exclude and highlight filters.
00048 """
00049
00050 def __init__(self):
00051 super(MessageProxyModel, self).__init__()
00052 self.setDynamicSortFilter(True)
00053 self.setFilterRole(Qt.UserRole)
00054 self.setSortCaseSensitivity(Qt.CaseInsensitive)
00055 self.setSortRole(Qt.UserRole)
00056
00057 self._exclude_filters = FilterCollection()
00058 self._highlight_filters = FilterCollection()
00059 self._show_highlighted_only = False
00060
00061
00062 self._source_model = None
00063
00064 def setSourceModel(self, source_model):
00065 super(MessageProxyModel, self).setSourceModel(source_model)
00066 self._source_model = self.sourceModel()
00067
00068
00069
00070 def filterAcceptsRow(self, sourcerow, sourceparent):
00071 """
00072 returns: True if the row does not match any exclude filter AND (_show_highlighted_only is False OR it matches any highlight filter), ''bool''
00073 """
00074 msg = self._source_model._messages[sourcerow]
00075 if self._exclude_filters.test_message(msg):
00076
00077 return False
00078
00079 highlighted = True
00080 if self._highlight_filters.count_enabled_filters() > 0:
00081 highlighted = self._highlight_filters.test_message(msg, default=True)
00082 if self._show_highlighted_only and not highlighted:
00083
00084 return False
00085
00086
00087 msg.highlighted = highlighted
00088
00089 return True
00090
00091 def data(self, proxy_index, role=None):
00092 """
00093 Set colors of items based on highlight filters.
00094 """
00095 index = self.mapToSource(proxy_index)
00096 if role == Qt.ForegroundRole:
00097 msg = self._source_model._messages[index.row()]
00098 if not msg.highlighted:
00099 return QBrush(Qt.gray)
00100 return self._source_model.data(index, role)
00101
00102
00103
00104 def handle_exclude_filters_changed(self):
00105 """
00106 Invalidate filters and trigger refiltering.
00107 """
00108 self.invalidateFilter()
00109
00110 def handle_highlight_filters_changed(self):
00111 """
00112 Invalidate filters and trigger refiltering.
00113 """
00114 if self._show_highlighted_only:
00115 self.invalidateFilter()
00116 else:
00117 self.invalidateFilter()
00118 if qVersion().startswith('4.'):
00119 self.dataChanged.emit(self.index(0, 0), self.index(self.rowCount() - 1, self.columnCount() - 1))
00120 else:
00121 self.dataChanged.emit(self.index(0, 0), self.index(self.rowCount() - 1, self.columnCount() - 1), [])
00122
00123 def add_exclude_filter(self, newfilter):
00124 self._exclude_filters.append(newfilter)
00125
00126 def add_highlight_filter(self, newfilter):
00127 self._highlight_filters.append(newfilter)
00128
00129 def delete_exclude_filter(self, index):
00130 del self._exclude_filters[index]
00131 self.handle_exclude_filters_changed()
00132
00133 def delete_highlight_filter(self, index):
00134 del self._highlight_filters[index]
00135 self.handle_highlight_filters_changed()
00136
00137 def set_show_highlighted_only(self, show_highlighted_only):
00138 self._show_highlighted_only = show_highlighted_only
00139 self.invalidateFilter()