def __init__(self, parent): super(TextHighlighter, self).__init__(parent) self.__highlightingRules = [] fmt = QtGui.QTextCharFormat() fmt.setForeground(QtCore.Qt.white) fmt.setBackground(QtCore.Qt.darkGreen) pattern = QtCore.QRegExp("", QtCore.Qt.CaseInsensitive) self.__foundMatchFormat = (fmt, pattern) fmt = QtGui.QTextCharFormat() fmt.setForeground(QtGui.QColor(QtCore.Qt.red).lighter(115)) errors = ["error", "critical", "failed", "fail", "crashed", "crash"] for pattern in errors: rx = QtCore.QRegExp(r'\b%s\b' % pattern, QtCore.Qt.CaseInsensitive) rule = (fmt, rx) self.__highlightingRules.append(rule) fmt = QtGui.QTextCharFormat() fmt.setForeground(QtGui.QColor(255, 168, 0)) for pattern in ("warning", "warn"): rx = QtCore.QRegExp(r'\b%s\b' % pattern, QtCore.Qt.CaseInsensitive) rule = (fmt, rx) self.__highlightingRules.append(rule)
def highlightBlock(self, text): for fmt, pattern in self.__highlightingRules: expression = QtCore.QRegExp(pattern) index = expression.indexIn(text) while index >= 0: length = expression.matchedLength() self.setFormat(index, length, fmt) index = expression.indexIn(text, index + length) fmt, pattern = self.__foundMatchFormat if pattern.isEmpty(): return expression = QtCore.QRegExp(pattern) index = expression.indexIn(text) while index >= 0: length = expression.matchedLength() self.setFormat(index, length, fmt) index = expression.indexIn(text, index + length)
class AlnumSortProxyModel(QtGui.QSortFilterProxyModel): RX_ALNUMS = QtCore.QRegExp('(\d+|\D+)') def __init__(self, *args, **kwargs): super(AlnumSortProxyModel, self).__init__(*args, **kwargs) self.setSortRole(DATA_ROLE) def lessThan(self, left, right): sortRole = self.sortRole() leftData = left.data(sortRole) if isinstance(leftData, (str, unicode)): rightData = right.data(sortRole) return self.lessThanAlphaNumeric(leftData, rightData) return super(AlnumSortProxyModel, self).lessThan(left, right) def lessThanAlphaNumeric(self, left, right): if left == right: return False alnums = self.RX_ALNUMS leftList = [] rightList = [] pos = 0 while True: pos = alnums.indexIn(left, pos) if pos == -1: break leftList.append(alnums.cap(1)) pos += alnums.matchedLength() pos = 0 while True: pos = alnums.indexIn(right, pos) if pos == -1: break rightList.append(alnums.cap(1)) pos += alnums.matchedLength() for leftItem, rightItem in zip(leftList, rightList): if leftItem != rightItem and leftItem.isdigit( ) and rightItem.isdigit(): return int(leftItem) < int(rightItem) if leftItem != rightItem: return leftItem < rightItem return left < right
class AlnumSortProxyModel(QtGui.QSortFilterProxyModel): RX_ALNUMS = QtCore.QRegExp('(\d+|\D+)') def __init__(self, *args, **kwargs): super(AlnumSortProxyModel, self).__init__(*args, **kwargs) self.setSortRole(DATA_ROLE) self.__validAlnum = (str, unicode) def lessThan(self, left, right): sortRole = self.sortRole() leftData = left.data(sortRole) if isinstance(leftData, self.__validAlnum): rightData = right.data(sortRole) if leftData == rightData: return False return alphaNumericKey(leftData) < alphaNumericKey(rightData) return super(AlnumSortProxyModel, self).lessThan(left, right)