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