Пример #1
0
def cfgToTextDocument(cfg, doc=None):
    if doc is None:
        doc = QtGui.QTextDocument()

    cursor = QtGui.QTextCursor(doc)
    cursor.movePosition(QtGui.QTextCursor.End)

    # table style
    tableformat = QtGui.QTextTableFormat()
    tableformat.setTopMargin(10)
    tableformat.setBottomMargin(10)
    tableformat.setCellPadding(5)
    tableformat.setCellSpacing(0)
    tableformat.setBorderStyle(QtGui.QTextFrameFormat.BorderStyle_Solid)
    tableformat.setHeaderRowCount(1)

    # headers style
    titleblockformat = QtGui.QTextBlockFormat()
    titleblockformat.setTopMargin(20)
    titleblockformat.setBottomMargin(10)

    titleformat = QtGui.QTextCharFormat()
    titleformat.setFontWeight(QtGui.QFont.Bold)
    #titleformat.setPointSze(12)

    # headers style
    headerformat = QtGui.QTextCharFormat()
    headerformat.setFontWeight(QtGui.QFont.Bold)
    brush = headerformat.background()
    brush.setColor(QtCore.Qt.lightGray)
    brush.setStyle(QtCore.Qt.SolidPattern)
    headerformat.setBackground(brush)

    for section in cfg.sections():
        items = sorted(cfg.items(section))
        if not items:
            continue

        cursor.beginEditBlock()
        cursor.movePosition(QtGui.QTextCursor.End)

        # title
        cursor.insertBlock(titleblockformat)
        cursor.insertText(section, titleformat)

        nrows = len(items)
        ncols = 2
        table = cursor.insertTable(nrows, ncols, tableformat)

        #textformat = QtWidgets.QTextFormat()

        for index, (key, value) in enumerate(items):
            cell = table.cellAt(index, 0)
            cellCursor = cell.firstCursorPosition()
            cellCursor.insertText(key)

            cell = table.cellAt(index, 1)
            cellCursor = cell.firstCursorPosition()
            cellCursor.insertText(value)

        # horizontal header
        headers = [doc.tr('Key'), doc.tr('Value')]
        table.insertRows(0, 1)
        for col, text in enumerate(headers):
            cell = table.cellAt(0, col)
            cell.setFormat(headerformat)
            cellCursor = cell.firstCursorPosition()
            cellCursor.insertText(text)

        # vertical header
        table.insertColumns(0, 1)
        for row in range(1, nrows + 1):
            text = str(row)

            cell = table.cellAt(row, 0)
            cell.setFormat(headerformat)
            cellCursor = cell.firstCursorPosition()
            cellCursor.insertText(text, headerformat)

        cursor.endEditBlock()

    return doc
Пример #2
0
def modelToTextDocument(model, doc=None):
    if doc is None:
        doc = QtGui.QTextDocument()

    cursor = QtGui.QTextCursor(doc)
    cursor.movePosition(QtGui.QTextCursor.End)
    cursor.beginEditBlock()

    format = QtGui.QTextTableFormat()
    format.setCellPadding(5)
    format.setCellSpacing(0)
    format.setBorderStyle(QtGui.QTextFrameFormat.BorderStyle_Solid)
    format.setHeaderRowCount(1)

    nrows = model.rowCount()
    try:
        ncols = model.columnCount()
    except TypeError:
        # columnCount is a private method in QAbstractListModel
        ncols = 1
    table = cursor.insertTable(nrows, ncols, format)

    #textformat = QtWidgets.QTextFormat()

    for row in range(nrows):
        for col in range(ncols):
            text = model.index(row, col).data()
            if text is None:
                text = ''
            else:
                text = str(text)

            cell = table.cellAt(row, col)
            cellCursor = cell.firstCursorPosition()
            cellCursor.insertText(text)  # , textformat)

    # headers style
    headerformat = QtGui.QTextCharFormat()
    headerformat.setFontWeight(QtGui.QFont.Bold)
    brush = headerformat.background()
    brush.setColor(QtCore.Qt.lightGray)
    brush.setStyle(QtCore.Qt.SolidPattern)
    headerformat.setBackground(brush)

    # horizontal header
    headers = [
        model.headerData(col, QtCore.Qt.Horizontal) for col in range(ncols)
    ]
    if any(headers):
        table.insertRows(0, 1)
        for col, text in enumerate(headers):
            if text is None:
                text = ''
            else:
                text = str(text)

            cell = table.cellAt(0, col)
            cell.setFormat(headerformat)
            cellCursor = cell.firstCursorPosition()
            cellCursor.insertText(text)

    # vertical header
    headers = [
        model.headerData(row, QtCore.Qt.Vertical) for row in range(nrows)
    ]

    if any(headers):
        table.insertColumns(0, 1)
        for row, text in enumerate(headers):
            if text is None:
                text = ''
            else:
                text = str(text)

            cell = table.cellAt(row + 1, 0)
            cell.setFormat(headerformat)
            cellCursor = cell.firstCursorPosition()
            cellCursor.insertText(text, headerformat)

    cursor.endEditBlock()

    return doc