Example #1
0
    def __init__(self, appletID):
        # Initialise the base classes.
        super().__init__(appletID)

        # Instantiate and register a Scintilla widget.
        self.qteScintilla = self.qteAddWidget(QtmacsScintilla(self))

        # Add all the fields necessar in the mode bar.
        self._qteModeBar = QtmacsModeBar()
        self._qteModeBar.qteAddMode('EOL', 'U')
        self._qteModeBar.qteAddMode('READONLY', 'R')
        self._qteModeBar.qteAddMode('MODIFIED', '-')
        self._qteModeBar.qteAddMode('APPLETID', self.qteAppletID())
        self._qteModeBar.qteAddMode('POSITION', '(0,0)')
        self._qteModeBar.qteAddMode('OTHER', None)

        # Arrange the Scintilla editor widget and the mode bar in a layout.
        vbox = QtGui.QVBoxLayout()
        vbox.addWidget(self.qteScintilla)
        vbox.addWidget(self._qteModeBar)
        self.setLayout(vbox)

        # Intercept the position-changed signals.
        self.qteScintilla.cursorPositionChanged.connect(
            self.qteCursorPosChanged)

        # Intercept the modification changed signal.
        self.qteScintilla.modificationChanged.connect(
            self.qteModificationChanged)

        # Intercept the qtesigSaveState signal to know when
        # the document is in its last saved state again.
        self.qteScintilla.qteUndoStack.qtesigSavedState.connect(
            self.qteSavedState)

        # Query the end-of-line (EOL) mode and enforce it throughout
        # the document.
        eolmode = self.qteScintilla.eolMode()
        self.qteScintilla.setEolMode(eolmode)

        # Report the used EOL mode in the status buffer.
        if eolmode == QtmacsScintilla.EolUnix:
            self.qteMain.qteStatus('Using Unix EOL mode')
            self._qteModeBar.qteChangeModeValue('EOL', 'Unix')
        elif eolmode == QtmacsScintilla.EolWindows:
            self.qteMain.qteStatus('Using Windows EOL mode')
            self._qteModeBar.qteChangeModeValue('EOL', 'Dos')
        elif eolmode == QtmacsScintilla.EolMac:
            self.qteMain.qteStatus('Using Mac EOL mode')
            self._qteModeBar.qteChangeModeValue('EOL', 'Mac')
        else:
            self.qteMain.qteStatus('Unknown EOL mode')
            self._qteModeBar.qteChangeModeValue('EOL', 'Unknown')

        # Initialise the file handle and file name.
        self.file = self.fileName = None

        # Load the file with name 'appletID'.
        self.loadFile(appletID)

        # Extract the file extension.
        idx = appletID.rfind('.')
        if idx >= 0:
            ext = appletID[idx + 1:]
        else:
            ext = None

        # Prevent Scintilla from auto-indenting when asked to
        # add a new line. This is necessary because the new-line
        # macro takes care of the indentation in order to keep
        # the undo history consistent.
        self.qteScintilla.setAutoIndent(False)

        # Change indentation width and ensure that indents are actual
        # whitespaces, not tabs.
        self.qteScintilla.setTabWidth(4)
        self.qteScintilla.setIndentationsUseTabs(False)

        # Try to find a lexer for the file type to enable syntax
        # highlighting and folding. If no appropriate lexer exists,
        # then treat the file as pure text.
        if ext in lexer_type:
            self.qteScintilla.qteSetLexer(lexer_type[ext])
            self.qteScintilla.setFolding(Qsci.QsciScintilla.BoxedTreeFoldStyle)
        else:
            # Disable margin #1 which is (by default) the symbol margin for
            # eg. folding.
            self.qteScintilla.setMarginWidth(1, 0)

        # Increase all fonts by a factor of two for readability.
        self.qteScintilla.zoomTo(2)

        # Declare the content pristine.
        self.qteScintilla.setModified(False)
Example #2
0
class SciEditor(QtmacsApplet):
    def __init__(self, appletID):
        # Initialise the base classes.
        super().__init__(appletID)

        # Instantiate and register a Scintilla widget.
        self.qteScintilla = self.qteAddWidget(QtmacsScintilla(self))

        # Add all the fields necessar in the mode bar.
        self._qteModeBar = QtmacsModeBar()
        self._qteModeBar.qteAddMode('EOL', 'U')
        self._qteModeBar.qteAddMode('READONLY', 'R')
        self._qteModeBar.qteAddMode('MODIFIED', '-')
        self._qteModeBar.qteAddMode('APPLETID', self.qteAppletID())
        self._qteModeBar.qteAddMode('POSITION', '(0,0)')
        self._qteModeBar.qteAddMode('OTHER', None)

        # Arrange the Scintilla editor widget and the mode bar in a layout.
        vbox = QtGui.QVBoxLayout()
        vbox.addWidget(self.qteScintilla)
        vbox.addWidget(self._qteModeBar)
        self.setLayout(vbox)

        # Intercept the position-changed signals.
        self.qteScintilla.cursorPositionChanged.connect(
            self.qteCursorPosChanged)

        # Intercept the modification changed signal.
        self.qteScintilla.modificationChanged.connect(
            self.qteModificationChanged)

        # Intercept the qtesigSaveState signal to know when
        # the document is in its last saved state again.
        self.qteScintilla.qteUndoStack.qtesigSavedState.connect(
            self.qteSavedState)

        # Query the end-of-line (EOL) mode and enforce it throughout
        # the document.
        eolmode = self.qteScintilla.eolMode()
        self.qteScintilla.setEolMode(eolmode)

        # Report the used EOL mode in the status buffer.
        if eolmode == QtmacsScintilla.EolUnix:
            self.qteMain.qteStatus('Using Unix EOL mode')
            self._qteModeBar.qteChangeModeValue('EOL', 'Unix')
        elif eolmode == QtmacsScintilla.EolWindows:
            self.qteMain.qteStatus('Using Windows EOL mode')
            self._qteModeBar.qteChangeModeValue('EOL', 'Dos')
        elif eolmode == QtmacsScintilla.EolMac:
            self.qteMain.qteStatus('Using Mac EOL mode')
            self._qteModeBar.qteChangeModeValue('EOL', 'Mac')
        else:
            self.qteMain.qteStatus('Unknown EOL mode')
            self._qteModeBar.qteChangeModeValue('EOL', 'Unknown')

        # Initialise the file handle and file name.
        self.file = self.fileName = None

        # Load the file with name 'appletID'.
        self.loadFile(appletID)

        # Extract the file extension.
        idx = appletID.rfind('.')
        if idx >= 0:
            ext = appletID[idx + 1:]
        else:
            ext = None

        # Prevent Scintilla from auto-indenting when asked to
        # add a new line. This is necessary because the new-line
        # macro takes care of the indentation in order to keep
        # the undo history consistent.
        self.qteScintilla.setAutoIndent(False)

        # Change indentation width and ensure that indents are actual
        # whitespaces, not tabs.
        self.qteScintilla.setTabWidth(4)
        self.qteScintilla.setIndentationsUseTabs(False)

        # Try to find a lexer for the file type to enable syntax
        # highlighting and folding. If no appropriate lexer exists,
        # then treat the file as pure text.
        if ext in lexer_type:
            self.qteScintilla.qteSetLexer(lexer_type[ext])
            self.qteScintilla.setFolding(Qsci.QsciScintilla.BoxedTreeFoldStyle)
        else:
            # Disable margin #1 which is (by default) the symbol margin for
            # eg. folding.
            self.qteScintilla.setMarginWidth(1, 0)

        # Increase all fonts by a factor of two for readability.
        self.qteScintilla.zoomTo(2)

        # Declare the content pristine.
        self.qteScintilla.setModified(False)

    @classmethod
    def __qteRegisterAppletInit__(cls):
        """
        Assciate various file types with this applet so that the
        find-file macro will instantiate us automatically for
        any of them.
        """
        tmp = qte_global.findFile_types
        tmp.insert(0, ('.*\.txt$', cls.__name__))
        tmp.insert(0, ('.*\.py$', cls.__name__))
        tmp.insert(0, ('.*\.sh$', cls.__name__))
        tmp.insert(0, ('.*\.tex$', cls.__name__))
        tmp.insert(0, ('.*\.cpp$', cls.__name__))

    def qteCursorPosChanged(self, line, col):
        # Update the line- and column number in the mode bar.
        msg = '({},{})'.format(line, col)
        self._qteModeBar.qteChangeModeValue('POSITION', msg)

    def qteModificationChanged(self, mod):
        """
        Update the modification status in the mode bar.

        This slot is Connected to the ``modificationChanged`` signal
        from the ``QtmacsScintilla`` widget.
        """
        if mod:
            s = '*'
        else:
            s = '-'
        self._qteModeBar.qteChangeModeValue('MODIFIED', s)

    def qteSavedState(self, msg):
        """
        Set the modified state to 'Saved'.

        This slot is connected to the ``qtesigSavedState`` signal
        from the ``QtmacsUndoStack`` class. It calls the
        ``setModified`` method of the ``QtmacsScintilla`` widget
        which, in turn, triggers the ``qteModificationChanged``
        slot to ensure the mode bar status is updated accordingly.
        """
        self.qteScintilla.setModified(False)

    def loadFile(self, fileName):
        """
        Display the file ``fileName``.
        """

        self.fileName = fileName
        # Assign QFile object with the current name.
        self.file = QtCore.QFile(fileName)
        if self.file.exists():
            # Load the file into the widget and reset the undo stack
            # to delete the undo object create by the setText method.
            # Without it, an undo operation would delete the content
            # of the widget which is intuitive.
            self.qteScintilla.setText(open(fileName).read())
            self.qteScintilla.qteUndoStack.reset()
        else:
            msg = "File <b>{}</b> does not exist".format(self.qteAppletID())
            self.qteLogger.info(msg)