def drawObject(self, painter, rect, doc, pos_in_document, char_format): """Draw the appropriate widget based on the supplied char format.""" # determine the bubble to draw bubble_id = char_format.property(self.BUBBLE_DATA_PROPERTY) bubble = self.get_bubble(bubble_id) bubble.setGeometry(rect.toRect()) # now paint! painter.save() try: painter.translate(rect.topLeft().toPoint()) # WEIRD! It seems pyside and pyqt actually have different signatures for this method if self.USING_PYQT: # pyqt is using the flags parameter, which seems inconsistent with QT # http://pyqt.sourceforge.net/Docs/PyQt4/qwidget.html#render bubble.render( painter, QtCore.QPoint(0, 1), QtGui.QRegion(), QtGui.QWidget.DrawChildren, ) else: # pyside is using the renderFlags parameter which seems correct bubble.render( painter, QtCore.QPoint(0, 1), renderFlags=QtGui.QWidget.DrawChildren ) finally: painter.restore()
def paint(self, painter, style_options, model_index): """ Paint method to handle all cells that are not being currently edited. :param painter: The painter instance to use when painting :param style_options: The style options to use when painting :param model_index: The index in the data model that needs to be painted """ # for performance reasons, we are not creating a widget every time # but merely moving the same widget around. paint_widget = self._get_painter_widget(model_index, self.parent()) if not paint_widget: # just paint using the base implementation: QtGui.QStyledItemDelegate.paint(self, painter, style_options, model_index) return # make sure that the widget that is just used for painting isn't visible otherwise # it'll appear in the wrong place! paint_widget.setVisible(False) # call out to have the widget set the right values self._on_before_paint(paint_widget, model_index, style_options) # now paint! painter.save() try: paint_widget.resize(style_options.rect.size()) painter.translate(style_options.rect.topLeft()) # note that we set the render flags NOT to render the background of the widget # this makes it consistent with the way the editor widget is mounted inside # each element upon hover. # WEIRD! It seems pyside and pyqt actually have different signatures for this method if USING_PYQT: # pyqt is using the flags parameter, which seems inconsistent with QT # http://pyqt.sourceforge.net/Docs/PyQt4/qwidget.html#render paint_widget.render( painter, QtCore.QPoint(0, 0), QtGui.QRegion(), QtGui.QWidget.DrawChildren, ) else: # pyside is using the renderFlags parameter which seems correct paint_widget.render(painter, QtCore.QPoint(0, 0), renderFlags=QtGui.QWidget.DrawChildren) finally: painter.restore()
def visualRegionForSelection(self, selection): """ Overriden base method that returns the region in the viewport encompasing all the selected items :param selection: The selection to return the region for :returns: A QRegion representing the region containing all the selected items """ viewport_offset = (-self.horizontalOffset(), -self.verticalOffset()) region = QtGui.QRegion() for index_range in selection: for row in range(index_range.top(), index_range.bottom() + 1): index = self.model().index(row, 0, index_range.parent()) rect = self._get_item_rect(index) rect = rect.translated(viewport_offset[0], viewport_offset[1]) region += rect return region