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, QSortFilterProxyModel
00035
00036 from .filters.filter_collection import FilterCollection
00037
00038
00039 class MessageProxyModel(QSortFilterProxyModel):
00040 """
00041 Provides sorting and filtering capabilities for the MessageDataModel.
00042 Filtering is based on subclassed Filters stored in the FilterCollections.
00043 """
00044 def __init__(self):
00045 super(MessageProxyModel, self).__init__()
00046 self._filters = []
00047 self.setDynamicSortFilter(True)
00048 self._show_highlighted_only = False
00049
00050 self._exclude_filters = FilterCollection(self)
00051 self._highlight_filters = FilterCollection(self)
00052
00053
00054 def filterAcceptsRow(self, sourcerow, sourceparent):
00055 """
00056 returns: True if the row does not match the exclude filters AND (_show_highlighted_only is False OR it matches the _highlight_filters, ''bool''
00057 OR
00058 returns: False if the row matches the exclude filters OR (_show_highlighted_only is True and it doesn't match the _highlight_filters, ''bool''
00059 """
00060 rowdata = []
00061 for index in range(self.sourceModel().columnCount()):
00062 rowdata.append(self.sourceModel().index(sourcerow, index, sourceparent).data())
00063
00064 if self._exclude_filters.test_message_array(rowdata):
00065 return False
00066 if self._highlight_filters.count_enabled_filters() == 0:
00067 return True
00068 match = self._highlight_filters.test_message_array(rowdata)
00069
00070 return not self._show_highlighted_only or match
00071
00072 def data(self, index, role=None):
00073 """
00074 Sets colors of items based on highlight filters and severity type
00075 """
00076 messagelist = self.sourceModel()._messages.get_message_list()
00077 index = self.mapToSource(index)
00078 if index.row() >= 0 or index.row() < len(messagelist):
00079 if index.column() >= 0 or index.column() < messagelist[index.row()].count():
00080 if role == Qt.ForegroundRole:
00081 if index.column() == 1:
00082 data = index.data()
00083 severity_levels = {'Debug': QBrush(Qt.cyan), \
00084 'Info': QBrush(Qt.darkCyan), \
00085 'Warn': QBrush(Qt.darkYellow), \
00086 'Error': QBrush(Qt.darkRed), \
00087 'Fatal': QBrush(Qt.red)}
00088 if data in severity_levels.keys():
00089 return severity_levels[data]
00090 else:
00091 raise KeyError('Unknown severity type: %s' % data)
00092 if self._highlight_filters.count_enabled_filters() > 0:
00093 if not self._highlight_filters.test_message(messagelist[index.row()]):
00094 return QBrush(Qt.gray)
00095 return self.sourceModel().data(index, role)
00096
00097
00098 def handle_filters_changed(self):
00099 self.invalidate()
00100
00101 def add_exclude_filter(self, newfilter):
00102 self._exclude_filters.append(newfilter)
00103
00104 def add_highlight_filter(self, newfilter):
00105 self._highlight_filters.append(newfilter)
00106
00107 def delete_exclude_filter(self, index):
00108 if index < self._exclude_filters.count() and index >= 0:
00109 del self._exclude_filters[index]
00110 self.reset()
00111 return True
00112 return False
00113
00114 def delete_highlight_filter(self, index):
00115 if index < self._highlight_filters.count() and index >= 0:
00116 del self._highlight_filters[index]
00117 self.reset()
00118 return True
00119 return False
00120
00121 def set_show_highlighted_only(self, show_highlighted_only):
00122 self._show_highlighted_only = show_highlighted_only
00123 self.reset()
00124
00125 def save_to_file(self, filehandle):
00126 """
00127 Saves to an already open filehandle.
00128 :return: True if file write is successful, ''bool''
00129 OR
00130 :return: False if file write fails, ''bool''
00131 """
00132 try:
00133 filehandle.write(self.sourceModel()._messages.header_print())
00134 for index in range(self.rowCount()):
00135 row = self.mapToSource(self.index(index, 0)).row()
00136 filehandle.write(self.sourceModel()._messages.get_message_list()[row].file_print())
00137 except Exception as e:
00138 qWarning('File save failed: %s' % str(e))
00139 return False
00140 return True