Beispiel #1
0
    def createIconFromSvg(self, svg, color=None, colorsToBeReplaced=None):
        """ Creates a QIcon given an SVG string.

            Optionally replaces the colors in colorsToBeReplaced by color.

            :param svg: string containing Scalable Vector Graphics XML
            :param color: '#RRGGBB' string (e.g. '#FF0000' for red)
            :param colorsToBeReplaced: optional list of colors to be replaced by color
                If None, it will be set to the fill colors of the snip-icon libary
            :return: QtGui.QIcon
        """
        if colorsToBeReplaced is None:
            colorsToBeReplaced = self.colorsToBeReplaced

        if color:
            for oldColor in colorsToBeReplaced:
                svg = svg.replace(oldColor, color)

        # From http://stackoverflow.com/questions/15123544/change-the-color-of-an-svg-in-qt
        qByteArray = QtCore.QByteArray()
        qByteArray.append(svg)
        svgRenderer = QtSvg.QSvgRenderer(qByteArray)
        icon = QtGui.QIcon()
        for size in self.renderSizes:
            pixMap = QtGui.QPixmap(QtCore.QSize(size, size))
            pixMap.fill(QtCore.Qt.transparent)
            pixPainter = QtGui.QPainter(pixMap)
            pixPainter.setRenderHint(QtGui.QPainter.TextAntialiasing, True)
            pixPainter.setRenderHint(QtGui.QPainter.Antialiasing, True)
            svgRenderer.render(pixPainter)
            pixPainter.end()
            icon.addPixmap(pixMap)

        return icon
Beispiel #2
0
 def sizeHint(self):
     """ The recommended size for the widget.
     """
     size = QtCore.QSize()
     size.setWidth(600)
     size.setHeight(700)
     return size
Beispiel #3
0
    def resizeEvent(self, event):
        """ Resizes the details box if present (i.e. when 'Show Details' button was clicked)
        """
        result = super(ResizeDetailsMessageBox, self).resizeEvent(event)

        details_box = self.findChild(QtWidgets.QTextEdit)
        if details_box is not None:
            #details_box.setFixedSize(details_box.sizeHint())
            details_box.setFixedSize(QtCore.QSize(self.detailsBoxWidth, self.detailBoxHeight))

        return result
Beispiel #4
0
    def __init__(self, parent=None):
        """ Constructor
        """
        super(SyntaxTreeWidget, self).__init__(parent=parent)

        self.setAlternatingRowColors(True)
        self.setSelectionBehavior(QtWidgets.QAbstractItemView.SelectRows)
        self.setUniformRowHeights(True)
        self.setAnimated(False)

        self.setHeaderLabels(SyntaxTreeWidget.HEADER_LABELS)
        tree_header = self.header()
        self.add_header_context_menu(checked={'Node': True},
                                     checkable={'Node': True},
                                     enabled={'Node': False})

        # Don't stretch last column, it doesn't play nice when columns hidden and then shown again.
        tree_header.setStretchLastSection(False)

        self.icon_factory = IconFactory.singleton()

        self.row_size_hint = QtCore.QSize()
        self.row_size_hint.setHeight(20)
        self.setIconSize(QtCore.QSize(20, 20))
Beispiel #5
0
    def _load_file(self, file_name):
        """ Opens a file and sets self._file_name and self._source code if successful
        """
        logger.debug("Opening {!r}".format(file_name))

        in_file = QtCore.QFile(file_name)
        if in_file.open(QtCore.QFile.ReadOnly | QtCore.QFile.Text):
            text = in_file.readAll()
            try:
                source_code = str(text, encoding='utf-8')  # Python 3
            except TypeError:
                source_code = str(text)  # Python 2

            self._file_name = file_name
            self._source_code = source_code
        else:
            msg = "Unable to open file: {}".format(file_name)
            logger.warning(msg)
            QtWidgets.QMessageBox.warning(self, 'error', msg)
Beispiel #6
0
class SourceEditor(QtWidgets.QPlainTextEdit):
    """ Source read-ony editor that can detect double clicks.
    """
    sigTextClicked = QtCore.Signal(int, int)

    def __init__(self, parent=None):
        """ Constructor
        """
        super(SourceEditor, self).__init__(parent=parent)

        font = QtGui.QFont()
        font.setFamily('Courier')
        font.setFixedPitch(True)

        # It's hard to make a platform independend font size
        # http://stackoverflow.com/a/7381441/625350
        if sys.platform.startswith('linux'):
            font.setPointSize(12)

        self.setReadOnly(True)
        self.setFont(font)
        self.setWordWrapMode(QtGui.QTextOption.NoWrap)
        self.setCenterOnScroll(True)
        self.setStyleSheet(
            "selection-color: black; selection-background-color: #FFE000;")

    def sizeHint(self):
        """ The recommended size for the widget.
        """
        size = QtCore.QSize()
        size.setWidth(700)
        size.setHeight(700)
        return size

    def mousePressEvent(self, mouseEvent):
        """ On mouse press, the sigTextClicked(line_nr, column_nr) is emitted.
        """
        if mouseEvent.button() == QtCore.Qt.LeftButton:
            cursor = self.cursorForPosition(mouseEvent.pos())
            # Since the word wrap is off, there is one block per line. Block numbers are zero-based
            # but code lines start at 1.
            self.sigTextClicked.emit(cursor.blockNumber() + 1,
                                     cursor.positionInBlock())

    def select_text(self, from_pos, to_line_pos):
        """ Selects a text in the range from_line:col ... to_line:col

            from_pos and to_line_pos should be a (line, column) tuple.
            If from_pos is None, the selection starts at the beginning of the document.
            If to_line_pos is None, the selection goes to the end of the document.
        """
        text_cursor = self.textCursor()

        # Select from back to front. This makes block better visible after scrolling.
        if to_line_pos is None:
            text_cursor.movePosition(QtGui.QTextCursor.End,
                                     QtGui.QTextCursor.MoveAnchor)
        else:
            to_line, to_col = to_line_pos
            to_text_block = self.document().findBlockByLineNumber(to_line - 1)
            to_pos = to_text_block.position() + to_col
            text_cursor.setPosition(to_pos, QtGui.QTextCursor.MoveAnchor)

        if from_pos is None:
            text_cursor.movePosition(QtGui.QTextCursor.Start,
                                     QtGui.QTextCursor.KeepAnchor)
        else:
            from_line, from_col = from_pos
            # findBlockByLineNumber seems to be 0-based.
            from_text_block = self.document().findBlockByLineNumber(from_line -
                                                                    1)
            from_pos = from_text_block.position() + from_col
            text_cursor.setPosition(from_pos, QtGui.QTextCursor.KeepAnchor)

        self.setTextCursor(text_cursor)
        self.ensureCursorVisible()

    def get_last_pos(self):
        """ Gets the linenr and column of the last character.
        """
        text_cursor = self.textCursor()
        text_cursor.movePosition(QtGui.QTextCursor.End,
                                 QtGui.QTextCursor.MoveAnchor)
        return (text_cursor.blockNumber() + 1, text_cursor.positionInBlock())
Beispiel #7
0
def get_qsettings():
    """ Creates a QSettings object for this application.
        We do not set the application and organization in the QApplication object to
        prevent side-effects in case the AstViewer is imported.
    """
    return QtCore.QSettings("titusjan.nl", PROGRAM_NAME)