def update_minimap_doc(self, position, charsRemoved, charsAdded):

        def select_blocks(first_block,  last_block):
            first_block_pos = first_block.position()
            doc_cursor.setPosition(first_block_pos)
            last_block_pos = last_block.position()
            doc_cursor.setPosition(last_block_pos,  QTextCursor.KeepAnchor)
            doc_cursor.movePosition(
                QTextCursor.EndOfBlock,  QTextCursor.KeepAnchor)
            print("selected text :",  doc_cursor.selectedText())
            return doc_cursor.selectedText()

        doc_cursor = QTextCursor(self._doc)
        minimap_cursor = QTextCursor(self._minimap_doc)

        # IF one same block is modified
        if self._minimap_doc.blockCount() == self._doc.blockCount():
            doc_cursor.setPosition(position)
            doc_cursor.select(QTextCursor.BlockUnderCursor)
            minimap_cursor.setPosition(position)
            minimap_cursor.select(QTextCursor.BlockUnderCursor)
            minimap_cursor.insertFragment(doc_cursor.selection())
        # TODO: if the doc is modified on more than one block but resulting in
        # the same count of blocks (right now only the first block would be
        # updated)
        else:
            # ELSE
            doc_cursor.select(QTextCursor.Document)
            minimap_cursor.select(QTextCursor.Document)
            minimap_cursor.insertFragment(doc_cursor.selection())
Example #2
0
def pixmap(cursor, num_lines=6, scale=0.8):
    """Return a QPixmap displaying the selected lines of the document.

    If the cursor has no selection, num_lines are drawn.

    By default the text is drawn 0.8 * the normal font size. You can change
    that by supplying the scale parameter.

    """
    block = cursor.document().findBlock(cursor.selectionStart())
    c2 = QTextCursor(block)
    if cursor.hasSelection():
        c2.setPosition(cursor.selectionEnd(), QTextCursor.KeepAnchor)
        c2.movePosition(QTextCursor.EndOfBlock, QTextCursor.KeepAnchor)
    else:
        c2.movePosition(QTextCursor.NextBlock, QTextCursor.KeepAnchor, num_lines)

    data = textformats.formatData('editor')
    doc = QTextDocument()
    font = QFont(data.font)
    font.setPointSizeF(font.pointSizeF() * scale)
    doc.setDefaultFont(font)
    doc.setPlainText(c2.selection().toPlainText())
    if metainfo.info(cursor.document()).highlighting:
        highlighter.highlight(doc, state=tokeniter.state(block))
    size = doc.size().toSize() + QSize(8, -4)
    pix = QPixmap(size)
    pix.fill(data.baseColors['background'])
    doc.drawContents(QPainter(pix))
    return pix
Example #3
0
def pixmap(cursor, num_lines=6, scale=0.8):
    """Return a QPixmap displaying the selected lines of the document.

    If the cursor has no selection, num_lines are drawn.

    By default the text is drawn 0.8 * the normal font size. You can change
    that by supplying the scale parameter.

    """
    block = cursor.document().findBlock(cursor.selectionStart())
    c2 = QTextCursor(block)
    if cursor.hasSelection():
        c2.setPosition(cursor.selectionEnd(), QTextCursor.KeepAnchor)
        c2.movePosition(QTextCursor.EndOfBlock, QTextCursor.KeepAnchor)
    else:
        c2.movePosition(QTextCursor.NextBlock, QTextCursor.KeepAnchor,
                        num_lines)

    data = textformats.formatData('editor')
    doc = QTextDocument()
    font = QFont(data.font)
    font.setPointSizeF(font.pointSizeF() * scale)
    doc.setDefaultFont(font)
    doc.setPlainText(c2.selection().toPlainText())
    if metainfo.info(cursor.document()).highlighting:
        highlighter.highlight(doc, state=tokeniter.state(block))
    size = doc.size().toSize() + QSize(8, -4)
    pix = QPixmap(size)
    pix.fill(data.baseColors['background'])
    doc.drawContents(QPainter(pix))
    return pix
Example #4
0
    def _get_text(self, start, end):
        """Reimplemented to get a fragment of our text.

        This is faster than getting the whole text and using Python to slice it.

        """
        c = QTextCursor(self.document())
        c.setPosition(end)
        c.setPosition(start, QTextCursor.KeepAnchor)
        return c.selection().toPlainText()
Example #5
0
    def SaveAsPDF(self):
        """ Save Table as pdf """
        fileName, _ = QFileDialog.getSaveFileName(self, "Save File", "",
                                                  "PDF Files (*.pdf)")
        if fileName == "":
            return

        try:
            videoName = self.player.fileName
            timestamp = self.player.player.position()
            QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
            rows = self.VManager.rowCount()
            columns = self.VManager.columnCount()

            printer = QPrinter(QPrinter.HighResolution)
            innerRect = printer.pageRect()
            sizeF = QSizeF(innerRect.size().width(), innerRect.size().height())
            header = QTextDocument()
            header.setPageSize(sizeF)
            cursor_header = QTextCursor(header)
            format1 = QTextCharFormat()
            format1.setFontPointSize(16)
            cursor_header.insertHtml(
                "<p style='text-align: left;'><strong>Video</strong>: %s <strong>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;TimeStamp</strong>: %s </p>"
                % (videoName, timestamp))
            cursor_header.insertHtml("<br><br><br> ")

            cursor_header.select(QTextCursor.Document)
            fragment_header = cursor_header.selection()

            document = QTextDocument()
            cursor = QTextCursor(document)
            tableFormat = QTextTableFormat()
            tableFormat.setHeaderRowCount(1)
            tableFormat.setBorderBrush(QBrush(Qt.black))
            tableFormat.setAlignment(Qt.AlignHCenter)
            tableFormat.setCellPadding(5)
            tableFormat.setCellSpacing(5)

            cursor.movePosition(QTextCursor.Start, QTextCursor.MoveAnchor)
            cursor.insertFragment(fragment_header)

            textTable = cursor.insertTable(rows + 1, columns, tableFormat)

            tableHeaderFormat = QTextCharFormat()
            tableHeaderFormat.setBackground(QColor("#DADADA"))

            for column in range(columns):
                cell = textTable.cellAt(0, column)
                cell.setFormat(tableHeaderFormat)
                cellCursor = cell.firstCursorPosition()
                cellCursor.insertText(
                    self.VManager.horizontalHeaderItem(column).data(
                        Qt.DisplayRole))

            for row in range(rows):
                for column in range(columns):
                    item = self.VManager.item(row, column)
                    if item is not None:
                        cell = textTable.cellAt(row + 1, column)
                        cellCursor = cell.firstCursorPosition()
                        cellCursor.insertText(
                            self.VManager.item(row, column).text())

            cursor.movePosition(QTextCursor.End)
            printer.setOrientation(QPrinter.Portrait)

            printer.setPageMargins(30, 100, 10, 40, QPrinter.DevicePixel)
            printer.setFullPage(True)
            printer.setPageSize(QPrinter.A4)
            printer.setOutputFormat(QPrinter.PdfFormat)
            printer.setOutputFileName(fileName)

            document.print_(printer)
            QApplication.restoreOverrideCursor()
            qgsu.showUserAndLogMessage(
                QCoreApplication.translate("QgsFmvMetadata",
                                           "Succesfully creating PDF"))
        except Exception as e:
            QApplication.restoreOverrideCursor()
            qgsu.showUserAndLogMessage(QCoreApplication.translate(
                "QgsFmvMetadata", "Failed creating PDF"),
                                       level=QGis.Warning)
            return
        return