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)
class JobProgressBar(QtGui.QWidget): # Left, top, right, bottom __PEN = QtGui.QColor(33, 33, 33) Margins = [5, 2, 10, 4] def __init__(self, totals, parent=None): QtGui.QWidget.__init__(self, parent) self.setTotals(totals) self.setSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Preferred) ## Missing ability to detect size def setTotals(self, totals): self.__totals = totals colors = constants.COLOR_TASK_STATE state = plow.client.TaskState self.__values = [ (totals.dead, colors[state.DEAD]), (totals.eaten, colors[state.EATEN]), (totals.waiting, colors[state.WAITING]), (totals.depend, colors[state.DEPEND]), (totals.running, colors[state.RUNNING]), (totals.succeeded, colors[state.SUCCEEDED]), ] self.update() def paintEvent(self, event): total_width = self.width() - self.Margins[2] total_height = self.height() - self.Margins[3] total_tasks = float(self.__totals.total) vals = self.__values bar = [(total_width * (val / total_tasks), color) for val, color in vals if val != 0] painter = QtGui.QPainter() painter.begin(self) painter.setRenderHints(painter.HighQualityAntialiasing | painter.SmoothPixmapTransform | painter.Antialiasing) painter.setPen(self.__PEN) move = 0 x, y = self.Margins[:2] for width, color in reversed(bar): painter.setBrush(color) rect = QtCore.QRectF(x, y, total_width, total_height) if move: rect.setLeft(move) move += width painter.drawRoundedRect(rect, 3, 3) painter.end() event.accept()
class JobProgressBar(QtGui.QWidget): # Left, top, right, bottom __PEN = QtGui.QColor(33, 33, 33) Margins = [5, 2, 10, 4] def __init__(self, totals, parent=None): QtGui.QWidget.__init__(self, parent) self.setTotals(totals) self.setSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Preferred) ## Missing ability to detect size def setTotals(self, totals): self.__totals = totals self.__values = [ totals.waiting, totals.running, totals.dead, totals.eaten, totals.depend, totals.succeeded ] self.update() def paintEvent(self, event): total_width = self.width() - self.Margins[2] total_height = self.height() - self.Margins[3] total_tasks = float(self.__totals.total) bar = [] for i, v in enumerate(self.__values): if v == 0: continue bar.append((total_width * (v / total_tasks), constants.COLOR_TASK_STATE[i + 1])) painter = QtGui.QPainter() painter.begin(self) painter.setRenderHints(painter.HighQualityAntialiasing | painter.SmoothPixmapTransform | painter.Antialiasing) painter.setPen(self.__PEN) move = 0 for width, color in bar: painter.setBrush(color) rect = QtCore.QRectF(self.Margins[0], self.Margins[1], total_width, total_height) if move: rect.setLeft(move) move += width painter.drawRoundedRect(rect, 3, 3) painter.end() event.accept()
class SimplePercentageBarDelegate(QtGui.QStyledItemDelegate): """ A simple status bar, much like a heath meter, which is intended to show the ratio of two values. """ # Left, top, right, bottom Margins = [5, 4, 5, 4] __PEN = QtGui.QColor(33, 33, 33) __C1 = constants.RED __C2 = constants.GREEN def __init__(self, parent=None): QtGui.QStyledItemDelegate.__init__(self, parent) def paint(self, painter, option, index): if not index.isValid(): QtGui.QStyledItemDelegate.paint(self, painter, option, index) return ## Broken in PySide. opt = QtGui.QStyleOptionViewItemV4(option) self.initStyleOption(opt, index) # Broken in pyside 1.1.2 #if opt.state & QtGui.QStyle.State_Selected: # painter.fillRect(opt.rect, opt.palette.highlight()) rect = opt.rect rect.adjust(self.Margins[0], self.Margins[1], -self.Margins[2], -self.Margins[3]) data = index.data() painter.save() painter.setRenderHints(painter.HighQualityAntialiasing | painter.SmoothPixmapTransform | painter.Antialiasing) painter.setPen(self.__PEN) if data[1] == 0: painter.setBrush(self.__C1) painter.drawRoundedRect(rect, 3, 3) else: ratio = data[0] / float(data[1]) painter.setBrush(self.__C1) painter.drawRoundedRect(rect, 3, 3) rect.setWidth(ratio * rect.width()) painter.setBrush(self.__C2) painter.drawRoundedRect(rect, 3, 3) painter.restore()
class JobProgressDelegate(QtGui.QItemDelegate): """ A custom QItemDelegate that paints the progress from a Job's TaskStats """ PEN = QtGui.QColor(33, 33, 33) MARGINS = [5, 2, 10, 4] def __init__(self, dataRole=QtCore.Qt.UserRole, parent=None): super(JobProgressDelegate, self).__init__(parent) self._role = dataRole def paint(self, painter, opts, index): job = index.data(self._role) if not job: super(JobProgressDelegate, self).paint(painter, opts, index) return state = plow.client.TaskState colors = constants.COLOR_TASK_STATE totals = job.totals values = [ (totals.dead, colors[state.DEAD]), (totals.eaten, colors[state.EATEN]), (totals.waiting, colors[state.WAITING]), (totals.depend, colors[state.DEPEND]), (totals.running, colors[state.RUNNING]), (totals.succeeded, colors[state.SUCCEEDED]), ] rect = opts.rect total_width = rect.width() - self.MARGINS[2] total_height = rect.height() - self.MARGINS[3] total_tasks = float(totals.total) bar = [(total_width * (val / total_tasks), color) for val, color in values if val != 0] painter.setRenderHints(painter.HighQualityAntialiasing | painter.SmoothPixmapTransform | painter.Antialiasing) # self.drawBackground(painter, opt, index) painter.setPen(self.PEN) x, y = rect.x(), rect.y() x += self.MARGINS[0] y += self.MARGINS[1] move = 0 for width, color in reversed(bar): painter.setBrush(color) rect = QtCore.QRectF(x, y, total_width, total_height) if move: rect.setLeft(x + move) move += width painter.drawRoundedRect(rect, 3, 3)