Esempio n. 1
0
    def initEditor(self):
        editor = QsciScintilla()

        ## define the font to use
        font = QtGui.QFont()
        #font.setFamily("Consolas")
        font.setFixedPitch(True)
        font.setPointSize(9)
        # the font metrics here will help
        # building the margin width later
        fm = QtGui.QFontMetrics(font)

        ## set the default font of the editor
        ## and take the same font for line numbers
        editor.setFont(font)
        editor.setMarginsFont(font)

        ## Line numbers
        # conventionnaly, margin 0 is for line numbers
        editor.setMarginWidth(0, fm.width("0000"))
        editor.setMarginLineNumbers(0, True)

        ## Edge Mode shows a red vetical bar at 80 chars
        editor.setEdgeMode(QsciScintilla.EdgeLine)
        editor.setEdgeColumn(80)
        editor.setEdgeColor(QtGui.QColor("#FF0000"))

        ## Folding visual : we will use boxes
        editor.setFolding(QsciScintilla.BoxedTreeFoldStyle)

        ## Braces matching
        editor.setBraceMatching(QsciScintilla.SloppyBraceMatch)

        ## Editing line color
        editor.setCaretLineVisible(True)
        #editor.setCaretLineBackgroundColor(QtGui.QColor("#F5F5DC"))

        ## Margins colors
        # line numbers margin
        #editor.setMarginsBackgroundColor(QtGui.QColor("#333333"))
        #editor.setMarginsForegroundColor(QtGui.QColor("#CCCCCC"))

        # folding margin colors (foreground,background)
        #editor.setFoldMarginColors(QtGui.QColor("#99CC66"),QtGui.QColor("#333300"))

        ## Choose a lexer
        lexer = QsciLexerPython()
        lexer.setDefaultFont(font)
        editor.setLexer(lexer)

        ## Render on screen
        #editor.show()

        ## Show this file in the editor
        #editor.setText(open("examples\charriot_obj.txt").read())

        # Show all the methods of the editor
        #methods = sorted(QsciScintilla.__dict__.keys())
        #for m in methods :
        #    print m
        #editor.setWidth(400)

        editor.setEolMode(QsciScintilla.EolUnix)
        return editor
Esempio n. 2
0
  def initEditor(self):
    editor = QsciScintilla()

    ## define the font to use
    font = QtGui.QFont()
    #font.setFamily("Consolas")
    font.setFixedPitch(True)
    font.setPointSize(9)
    # the font metrics here will help
    # building the margin width later
    fm = QtGui.QFontMetrics(font)

    ## set the default font of the editor
    ## and take the same font for line numbers
    editor.setFont(font)
    editor.setMarginsFont(font)

    ## Line numbers
    # conventionnaly, margin 0 is for line numbers
    editor.setMarginWidth(0, fm.width( "0000"))
    editor.setMarginLineNumbers(0, True)

    ## Edge Mode shows a red vetical bar at 80 chars
    editor.setEdgeMode(QsciScintilla.EdgeLine)
    editor.setEdgeColumn(80)
    editor.setEdgeColor(QtGui.QColor("#FF0000"))

    ## Folding visual : we will use boxes
    editor.setFolding(QsciScintilla.BoxedTreeFoldStyle)

    ## Braces matching
    editor.setBraceMatching(QsciScintilla.SloppyBraceMatch)

    ## Editing line color
    editor.setCaretLineVisible(True)
    #editor.setCaretLineBackgroundColor(QtGui.QColor("#F5F5DC"))

    ## Margins colors
    # line numbers margin
    #editor.setMarginsBackgroundColor(QtGui.QColor("#333333"))
    #editor.setMarginsForegroundColor(QtGui.QColor("#CCCCCC"))

    # folding margin colors (foreground,background)
    #editor.setFoldMarginColors(QtGui.QColor("#99CC66"),QtGui.QColor("#333300"))

    ## Choose a lexer
    lexer = QsciLexerPython()
    lexer.setDefaultFont(font)
    editor.setLexer(lexer)

    ## Render on screen
    #editor.show()

    ## Show this file in the editor
    #editor.setText(open("examples\charriot_obj.txt").read())
    
    # Show all the methods of the editor
    #methods = sorted(QsciScintilla.__dict__.keys())
    #for m in methods :
    #    print m
    #editor.setWidth(400)
    
    editor.setEolMode(QsciScintilla.EolUnix)
    return editor
Esempio n. 3
0
class ScriptManager(MDialog):
    def __init__(self, _parent):
        MDialog.__init__(self, _parent)
        from PyQt4.Qsci import QsciScintilla, QsciLexerPython, QsciAPIs

        if isActivePyKDE4:
            self.setButtons(MDialog.NoDefault)
        self.sciCommand = QsciScintilla()
        self.sciCommand.setUtf8(True)
        self.sciCommand.setAutoIndent(True)
        self.sciCommand.setIndentationGuides(True)
        self.sciCommand.setIndentationsUseTabs(True)
        self.sciCommand.setCaretLineVisible(True)
        self.sciCommand.setAutoCompletionThreshold(2)
        self.sciCommand.setAutoCompletionSource(QsciScintilla.AcsDocument)
        self.sciCommand.setLexer(QsciLexerPython(self))
        self.sciCommand.setMarginLineNumbers(1, True)
        self.sciCommand.setMarginWidth(1, '0000')
        self.sciCommand.setEolMode(QsciScintilla.EolUnix)
        self.sciCommand.setWrapMode(QsciScintilla.WrapWord)
        lblScriptList = MLabel(translate("ScriptManager", "Script List : "))
        self.currentScriptFileName = None
        self.lwScriptList = Options.MyListWidget(self, [], _currentRowChanged=self.getFromScriptList)
        self.refreshScriptList()
        pbtnCreate = MPushButton(translate("ScriptManager", "Create"))
        pbtnDelete = MPushButton(translate("ScriptManager", "Delete"))
        pbtnSave = MPushButton(translate("ScriptManager", "Save"))
        pbtnScriptManagerAndClose = MPushButton(translate("ScriptManager", "Run And Close"))
        pbtnScriptManager = MPushButton(translate("ScriptManager", "Run"))
        pbtnClose = MPushButton(translate("ScriptManager", "Close"))
        pbtnClear = MPushButton(translate("ScriptManager", "Clear"))
        self.cckbIsAutoSaveScripts = Options.MyCheckBox(self, translate("ScriptManager", "Auto Save"), 2,
                                                        "isAutoSaveScripts")
        self.connect(pbtnCreate, SIGNAL("clicked()"), self.create)
        self.connect(pbtnDelete, SIGNAL("clicked()"), self.delete)
        self.connect(pbtnSave, SIGNAL("clicked()"), self.save)
        self.connect(pbtnScriptManagerAndClose, SIGNAL("clicked()"), self.runScriptAndClose)
        self.connect(pbtnScriptManager, SIGNAL("clicked()"), self.runScript)
        self.connect(pbtnClose, SIGNAL("clicked()"), self.close)
        self.connect(pbtnClear, SIGNAL("clicked()"), self.clear)
        pnlMain = MWidget(self)
        vblMain = MVBoxLayout(pnlMain)
        vbox = MVBoxLayout()
        vbox.addWidget(lblScriptList)
        vbox.addWidget(self.lwScriptList)
        hbox2 = MHBoxLayout()
        hbox2.addWidget(pbtnCreate)
        hbox2.addWidget(pbtnDelete)
        vbox.addLayout(hbox2)
        hbox0 = MHBoxLayout()
        hbox0.addLayout(vbox)
        hbox0.addWidget(self.sciCommand)
        hbox1 = MHBoxLayout()
        hbox1.addWidget(self.cckbIsAutoSaveScripts)
        hbox1.addStretch(1)
        hbox1.addWidget(pbtnClear, 1)
        hbox1.addWidget(pbtnSave, 1)
        hbox1.addWidget(pbtnScriptManager, 1)
        hbox1.addWidget(pbtnScriptManagerAndClose, 1)
        hbox1.addWidget(pbtnClose, 1)
        vblMain.addLayout(hbox0)
        vblMain.addLayout(hbox1)
        if isActivePyKDE4:
            self.setMainWidget(pnlMain)
        else:
            self.setLayout(vblMain)
        self.setWindowTitle(translate("ScriptManager", "Script Manager"))
        self.setWindowIcon(MIcon("Images:scriptManager.png"))
        self.lwScriptList.setMaximumWidth(150)
        self.setMinimumWidth(650)
        self.setMinimumHeight(450)
        self.show()

    def closeEvent(self, _event):
        if self.checkForSave() is False:
            _event.ignore()

    @staticmethod
    def checkScriptManager(_isAlertIfNotAvailable=True):
        try:
            from PyQt4.Qsci import QsciScintilla

            return True
        except:
            if _isAlertIfNotAvailable:
                Dialogs.showError(translate("MenuBar", "Qsci Is Not Installed"),
                                  translate("MenuBar",
                                            "Qsci is not installed on your systems.<br>Please install Qsci on your system and try again."))
            return False

    def getFromScriptList(self, _index=None):
        try:
            if self.checkForSave():
                self.currentScriptFileName = self.scriptList[self.lwScriptList.currentRow()]
                codes = Scripts.getScript(fu.joinPath(Scripts.pathOfScripsDirectory, self.currentScriptFileName))
                self.sciCommand.setText(str(codes))
        except:
            ReportBug.ReportBug()

    def runScriptAndClose(self):
        try:
            if self.runScript():
                self.close()
        except:
            ReportBug.ReportBug()

    def runScript(self):
        try:
            return Scripts.runScript(str(self.sciCommand.text()))
        except:
            ReportBug.ReportBug()

    def checkForSave(self):
        try:
            if self.currentScriptFileName is not None:
                if fu.isFile(fu.joinPath(Scripts.pathOfScripsDirectory, self.currentScriptFileName)):
                    codes = Scripts.getScript(fu.joinPath(Scripts.pathOfScripsDirectory, self.currentScriptFileName))
                    if str(codes) != str(self.sciCommand.text()):
                        if self.cckbIsAutoSaveScripts.checkState() == Mt.Checked:
                            self.save()
                        else:
                            answer = Dialogs.ask(translate("ScriptManager", "Do You Wish To Save Your Codes?"),
                                                 translate("ScriptManager",
                                                           "Do you wish to save your codes so that you can continue later?"),
                                                 True)
                            if answer == Dialogs.Yes:
                                self.save()
                            elif answer == Dialogs.Cancel:
                                return False
            return True
        except:
            ReportBug.ReportBug()

    def save(self):
        try:
            codes = Scripts.getScript(fu.joinPath(Scripts.pathOfScripsDirectory, self.currentScriptFileName))
            Scripts.saveScript(fu.joinPath(Scripts.pathOfScripsDirectory, self.currentScriptFileName),
                               str(self.sciCommand.text()))
        except:
            ReportBug.ReportBug()

    def clear(self):
        try:
            answer = Dialogs.ask(translate("ScriptManager", "Your Codes Will Be Deleted!.."),
                                 translate("ScriptManager",
                                           "Your codes will be deleted and the default codes will be installed. Do you wish to clear the current codes?"))
            if answer == Dialogs.Yes:
                Scripts.clearScript(fu.joinPath(Scripts.pathOfScripsDirectory, self.currentScriptFileName))
        except:
            ReportBug.ReportBug()

    def delete(self):
        try:
            answer = Dialogs.ask(translate("ScriptManager", "Your Script Will Be Deleted!.."),
                                 translate("ScriptManager",
                                           "Your script will be deleted. Are you sure you want to delete current script?"))
            if answer == Dialogs.Yes:
                fu.removeFile(fu.joinPath(Scripts.pathOfScripsDirectory, self.currentScriptFileName))
                self.refreshScriptList()
        except:
            ReportBug.ReportBug()

    def create(self):
        try:
            newScriptFileName = Scripts.createNewScript()
            self.refreshScriptList()
            self.lwScriptList.setCurrentRow(self.scriptList.index(newScriptFileName))
        except:
            ReportBug.ReportBug()

    def refreshScriptList(self):
        try:
            self.scriptList = Scripts.getScriptList()
            self.lwScriptList.refresh(self.scriptList)
            scriptFileName = None
            if len(self.scriptList) > 0:
                scriptFileName = self.scriptList[self.lwScriptList.currentRow()]
            self.currentScriptFileName = scriptFileName
        except:
            ReportBug.ReportBug()
Esempio n. 4
0
    def createNewTab(self, filename, msg, lexer):
        if type(msg) is bytes:
            msg = msg.decode(encoding='utf-8')
        if str(msg).find("\r\n") >= 0:
            msg = msg.replace('\n', '')
        elif str(msg).find("\n") >= 0 and str(msg).find("\r") < 0:
            msg = msg.replace("\n", "\r")
        else:
            print("creatNewTab has other endswith.")

        editor = QsciScintilla()
        editor.setUtf8(True)
        editor.setLexer(lexer)
        editor.setMarginsBackgroundColor(QColor(220, 220, 220))
        editor.setAutoCompletionThreshold(2)
        editor.setAutoCompletionSource(QsciScintilla.AcsAll)
        editor.setEolMode(QsciScintilla.EolUnix)

        if str(filename).find("/") >= 0:
            tabname = filename.split("/")
            tabname = tabname[-1]
        elif str(filename) == "untitled":
            tabname = "untitled"
        else:
            tabname = filename

        self.addTab(editor, tabname)
        self.setTabToolTip(self.count() - 1, filename)

        if filename == "untitled":
            self.tabBar().setTabTextColor(self.count() - 1, QColor(Qt.red))
            self.setTabIcon(self.count() - 1, QIcon(':/pc.png'))
            if self.ui.currentBoard == "microbit":
                msg = "from microbit import *\r#write your program:\r"
        elif str(filename).find(":") > 0:
            self.tabBar().setTabTextColor(self.count() - 1, QColor(Qt.red))
            self.setTabIcon(self.count() - 1, QIcon(':/pc.png'))
        else:
            self.tabBar().setTabTextColor(self.count() - 1, QColor(Qt.blue))
            self.setTabIcon(self.count() - 1, QIcon(':/ic.png'))

        editor.setText(msg)

        editor.setContextMenuPolicy(Qt.CustomContextMenu)
        self.connect(editor,
                     SIGNAL("customContextMenuRequested(const QPoint&)"),
                     self.slotEditorRightClickMenu)

        if self.editorRightMenu == None:
            self.editorRightMenu = QMenu(self)
            self.editorRightMenu.setStyleSheet(
                "QMenu::item{padding:4px 16px;}"
                "QMenu::item::selected{background-color:rgb(135,206,255);}")

            undo = QAction(self.tr("Undo"), self)
            undo.setShortcut("Ctrl+Z")
            self.connect(undo, SIGNAL("triggered()"), self.slotUndo)

            redo = QAction(self.tr("Redo"), self)
            redo.setShortcut("Ctrl+Y")
            self.connect(redo, SIGNAL("triggered()"), self.slotRedo)

            cut = QAction(self.tr("Cut"), self)
            cut.setShortcut("Ctrl+X")
            self.connect(cut, SIGNAL("triggered()"), self.slotCut)

            copy = QAction(self.tr("Copy"), self)
            copy.setShortcut("Ctrl+C")
            self.connect(copy, SIGNAL("triggered()"), self.slotCopy)

            paste = QAction(self.tr("Paste"), self)
            paste.setShortcut("Ctrl+V")
            self.connect(paste, SIGNAL("triggered()"), self.slotPaste)

            self.editorRightMenu.addAction(undo)
            self.editorRightMenu.addAction(redo)
            self.editorRightMenu.addAction(cut)
            self.editorRightMenu.addAction(copy)
            self.editorRightMenu.addAction(paste)

        #set brace match
        editor.setBraceMatching(editor.StrictBraceMatch)

        #set indent replace 4 space
        editor.setIndentationsUseTabs(False)
        editor.setTabWidth(2)

        #The line number display area
        editor.setMarginType(0, QsciScintilla.NumberMargin)
        editor.setMarginLineNumbers(0, True)
        editor.setMarginWidth(0, 30)

        #set auto indentation
        editor.setAutoIndent(True)

        #syntax check
        editor.setMarginType(1, QsciScintilla.SymbolMargin)
        editor.setMarginLineNumbers(1, False)
        editor.setMarginWidth(1, 5)
        editor.setMarginSensitivity(1, False)
        editor.setMarginMarkerMask(1, 0x1FFFFFF)
        editor.markerDefine(QsciScintilla.Background, 1)

        #Automatic folding area
        editor.setFolding(QsciScintilla.CircledFoldStyle)

        #set tab's stylesheet
        editor.setStyleSheet(
            "QWidget{font-size:20px;border: 1px solid white;border-radius:1px}"
        )

        self.setCurrentWidget(editor)
        if filename != "untitled":
            self.fileitem.size += 1
            self.fileitem.list.append(filename)

        self.connect(editor, SIGNAL("textChanged()"), self.editorTextChange)
        self.connect(editor, SIGNAL("selectionChanged()"),
                     self.selectionChanged)
        self.connect(editor, SIGNAL("linesChanged()"), self.linesChanged)
        self.connect(editor, SIGNAL("cursorPositionChanged(int,int)"),
                     self.cursorPositionChanged)
        self.connect(editor, SIGNAL("userListActivated(int,const QString)"),
                     self.userListActivated)