'Draw the text of an ItemViewItem.
This is the main part where we differ from the original implementation
in Qt: We use a QTextDocument to draw text.
Args:
index: The QModelIndex of the item to draw.'

def _draw_text(self, index):

    if (not self._opt.text):
        return
    text_rect_ = self._style.subElementRect(self._style.SE_ItemViewItemText, self._opt, self._opt.widget)
    qtutils.ensure_valid(text_rect_)
    margin = (self._style.pixelMetric(QStyle.PM_FocusFrameHMargin, self._opt, self._opt.widget) + 1)
    text_rect = text_rect_.adjusted(margin, 0, (- margin), 0)
    qtutils.ensure_valid(text_rect)
    if index.parent().isValid():
        text_rect.adjust(0, (-1), 0, (-1))
    else:
        text_rect.adjust(0, (-2), 0, (-2))
    self._painter.save()
    state = self._opt.state
    if ((state & QStyle.State_Enabled) and (state & QStyle.State_Active)):
        cg = QPalette.Normal
    elif (state & QStyle.State_Enabled):
        cg = QPalette.Inactive
    else:
        cg = QPalette.Disabled
    if (state & QStyle.State_Selected):
        self._painter.setPen(self._opt.palette.color(cg, QPalette.HighlightedText))
        text_rect.adjust(0, (-1), 0, 0)
    else:
        self._painter.setPen(self._opt.palette.color(cg, QPalette.Text))
    if (state & QStyle.State_Editing):
        self._painter.setPen(self._opt.palette.color(cg, QPalette.Text))
        self._painter.drawRect(text_rect_.adjusted(0, 0, (-1), (-1)))
    self._painter.translate(text_rect.left(), text_rect.top())
    self._get_textdoc(index)
    self._draw_textdoc(text_rect)
    self._painter.restore()
