def create_qscintilla_code_view(): """ Create a QScintilla based view containing a copy of this source code. """ from PyQt5.Qsci import QsciLexerPython, QsciScintilla view = QsciScintilla() view.setReadOnly(True) view.setUtf8(True) view.setLexer(QsciLexerPython()) view.setFolding(QsciScintilla.PlainFoldStyle) view.setText(get_source_code()) return view
def __init__(self): """ Invoked to return the default editor widget. """ from PyQt5.Qsci import QsciLexerPython, QsciScintilla editor = QsciScintilla() # Configure the editor for Python. editor.setUtf8(True) editor.setLexer(QsciLexerPython(editor)) editor.setFolding(QsciScintilla.PlainFoldStyle, 1) editor.setIndentationGuides(True) editor.setIndentationWidth(4) self.editor = editor
class CustomMainWindow(QMainWindow): def __init__(self): super(CustomMainWindow, self).__init__() # Window setup # -------------- # 1. Define the geometry of the main window self.setGeometry(300, 300, 800, 400) self.setWindowTitle("QScintilla Test") # 2. Create frame and layout self.__frm = QFrame(self) self.__frm.setStyleSheet("QWidget { background-color: #ffeaeaea }") self.__lyt = QVBoxLayout() self.__frm.setLayout(self.__lyt) self.setCentralWidget(self.__frm) self.__myFont = QFont() self.__myFont.setPointSize(14) # 3. Place a button self.__btn = QPushButton("Qsci") self.__btn.setFixedWidth(50) self.__btn.setFixedHeight(50) self.__btn.clicked.connect(self.__btn_action) self.__btn.setFont(self.__myFont) self.__lyt.addWidget(self.__btn) # QScintilla editor setup # ------------------------ # ! Make instance of QsciScintilla class! self.__editor = QsciScintilla() self.__editor.setText("Hello\n") self.__editor.append("world \n") self.__editor.setLexer(None) self.__editor.setUtf8(True) # Set encoding to UTF-8 self.__editor.setFont(self.__myFont) # Will be overridden by lexer! #simple editor options self.__editor.setEolVisibility(False) #sets the end of each line with an EOL character self.__editor.setIndentationsUseTabs(False) #determines whether indent uses tabs or whitespace char. self.__editor.setTabWidth(4) self.__editor.setIndentationGuides(True) self.__editor.setTabIndents(True) self.__editor.setAutoIndent(True) self.__editor.setCaretForegroundColor(QColor("#ff0000ff")) self.__editor.setCaretLineVisible(True) self.__editor.setCaretLineBackgroundColor(QColor("#1fff0000")) self.__editor.setCaretWidth(5) #Margin SetUp. self.__editor.setMarginType(3, self.__editor.NumberMargin) self.__editor.setMarginsForegroundColor(QColor("#ff888888")) # self.__editor.setMarginType(2, QsciScintilla.TextMargin) # Symbol Margin sym_0 = QImage("icons/sym_0.png").scaled(QSize(16, 16)) sym_1 = QImage("icons/sym_1.png").scaled(QSize(16, 16)) sym_2 = QImage("icons/sym_2.png").scaled(QSize(16, 16)) sym_3 = QImage("icons/sym_3.png").scaled(QSize(16, 16)) sym_4 = self.__editor.Circle self.__editor.markerDefine(sym_0, 0) self.__editor.markerDefine(sym_1, 1) self.__editor.markerDefine(sym_2, 2) self.__editor.markerDefine(sym_3, 3) self.__editor.markerDefine(sym_4, 4) self.__editor.setMarginType(3, self.__editor.SymbolMargin) # self.__editor.setMarginType(2, QsciScintilla.SymbolMarginDefaultBackgroundColor) # self.__editor.setMarginType(3, QsciScintilla.SymbolMarginDefaultForegroundColor) self.__editor.setMarginWidth(1, '00000') self.__editor.setMarginMarkerMask(1, 0b1111) self.__editor.setMarginMarkerMask(2, 0b1111) self.__editor.markerAdd(3, 2) # Display a few symbols, and keep their handles stored self.__editor.markerAdd(0, 0) # Green dot on line 0+1 self.__editor.markerAdd(4, 0) # Green dot on line 4+1 self.__editor.markerAdd(5, 0) # Green dot on line 5+1 self.__editor.markerAdd(8, 3) # Red arrow on line 8+1 self.__editor.markerAdd(9, 2) # Red dot on line 9+1 self.__editor.setFolding(self.__editor.BoxedFoldStyle, 4) self.__editor.SendScintilla(self.__editor.SCI_SETMULTIPLESELECTION, True) self.__editor.SendScintilla(self.__editor.SCI_SETMULTIPASTE, 1) self.__editor.SendScintilla(self.__editor.SCI_SETADDITIONALSELECTIONTYPING, True) self.__editor.SendScintilla(self.__editor.SCI_SETINDENTATIONGUIDES, self.__editor.SC_IV_REAL); self.__editor.SendScintilla(self.__editor.SCI_SETTABWIDTH, 4) # self.__editor.setMarginsBackgroundColor(QColor("#ff0000ff")) self.__editor.setWrapMode(QsciScintilla.WrapWord) self.__editor.setWrapIndentMode(QsciScintilla.WrapIndentIndented) # available wrap modes: # QsciScintilla.WrapNone, WrapWord, WrapCharacter, WrapWhitespace self.__editor.setWrapVisualFlags( QsciScintilla.WrapFlagByText, startFlag=QsciScintilla.WrapFlagByText, indent=4) # setWrapVisualFlags(endFlag, startFlag, indent) # see: readMe self.__editor.setWrapIndentMode(QsciScintilla.WrapIndentIndented) self.__editor.textChanged.connect(self.text_changed) #signal typing # ! Add editor to layout ! self.__lyt.addWidget(self.__editor) self.show() '''''' def __btn_action(self): self.__editor.append('Voila You just clicked the QSci button! \n') print("Hello World!") def text_changed(self): print({self.__editor.text()}) ''''''
class BrowserWindow(QWidget): launcherWindow = None logoPixmap = None def __init__(self, launcherWindow, app): super().__init__() self.app = app self.launcherWindow = launcherWindow self.fontSize = 14 self.fontSizeLarge = 24 self.fontSizeSmall = 10 self.setCursor(AppData.cursors['arrow']) self.initUI() def multiple_replace(self, string, rep_dict): pattern = re.compile("|".join([re.escape(k) for k in rep_dict.keys()]), re.M) return pattern.sub(lambda x: rep_dict[x.group(0)], string) def styleEsc(self, string): return self.multiple_replace(string, { '{': '{{', '}': '}}', '@<': '{', '>@': '}' }) def initUI(self): ## pal = QPalette(); ## pal.setColor(QPalette.Background, Qt.black); ## self.setPalette(pal); self.setSizePolicy(QSizePolicy.Maximum, QSizePolicy.Maximum) self.styleTemplate = self.styleEsc(""" QWidget{ background-color: black; border-color: red; border-style: solid; border-width: 1px; border-radius: 0px; font: bold @<fontSize>@px; color: red; padding: 0px; font-family: "Candara"; font-size: @<fontSize:d>@px; } QLabel{ font-size: @<fontSizeLarge:d>@px; border-width: 0px; } QTreeView::item:hover { background: black; border: 1px solid red; } QTreeView::item:selected { border: 1px solid red; color: red; } QTreeView::item:selected:active{ background: black; border: 1px solid red; } QTreeView::item:selected:!active { border: 1px solid red; color: red; } QTreeView::branch:has-siblings:!adjoins-item { border-image: url(Gui/stylesheet-vline.png) 0; } QTreeView::branch:has-siblings:adjoins-item { border-image: url(Gui/stylesheet-branch-more.png) 0; } QTreeView::branch:!has-children:!has-siblings:adjoins-item { border-image: url(Gui/stylesheet-branch-end.png) 0; } QTreeView::branch:has-children:!has-siblings:closed, QTreeView::branch:closed:has-children:has-siblings { border-image: none; image: url(Gui/stylesheet-branch-closed.png); } QTreeView::branch:open:has-children:!has-siblings, QTreeView::branch:open:has-children:has-siblings { border-image: none; image: url(Gui/stylesheet-branch-open.png); } QScrollBar:vertical { border: 2px solid red; background: black; width: 40px; margin: 22px 0 22px 0; } QScrollBar::handle:vertical { background: red; min-height: 20px; } QScrollBar::add-line:vertical { border: 2px red; background: black; height: 20px; subcontrol-position: bottom; subcontrol-origin: margin; } QScrollBar::sub-line:vertical { border: 2px red; background: black; height: 20px; subcontrol-position: top; subcontrol-origin: margin; } QScrollBar::up-arrow:vertical, QScrollBar::down-arrow:vertical { border: 2px solid black; width: 3px; height: 3px; background: red; } QScrollBar::add-page:vertical, QScrollBar::sub-page:vertical { background: none; } """) self.setStyleSheet( self.styleTemplate.format(fontSize=int(self.fontSize), fontSizeLarge=int(self.fontSizeLarge))) #gridWidget = QWidget(self) #self.gridWidget = gridWidget grid = QGridLayout() #treeMooker = QWidget(self) #treeMooker.setStyleSheet("background-color: transparent;") #treeMooker.setAttribute(Qt.WA_TransparentForMouseEvents) #treeMooker.installEventFilter(self) #treeMookerGrid = QGridLayout() #stack = QStackedLayout() #stack.setStackingMode(QStackedLayout.StackAll) logo = QLabel(self) self.logoPixmap = QPixmap("./Project/Media/Gears.png") logo.setPixmap(self.logoPixmap) logo.setAlignment(Qt.AlignHCenter | Qt.AlignVCenter) grid.addWidget(logo, 2, 3, 1, 1) self.titleTemplate = self.styleEsc(''' <p align="left"><strong> <span style='font-size:@<fontSizeLarge>@pt; font-weight:600; color:#ff0000;'>GPU Eye And Retina Stimulation </span></strong><span style='font-size:10pt; font-weight:600; color:#aa0000;'>beta test version 0.4</span></p> <table border="0" cellpadding="0" cellspacing="0"> <tbody> <tr> <td > <p align="left"><span style='font-size:@<fontSize>@pt; font-weight:600; color:#ff0000;'>László Szécsi</span></p> </td> <td > <p><span style='font-size:@<fontSizeSmall>@pt; font-weight:600; color:#aa0000;'>Budapest University of Technology and Economics</span></p> </td> </tr> <tr> <td > <p align="left"> <span style='font-size:@<fontSize>@pt; font-weight:600; color:#ff0000;'>Péter Hantz</span></p> </td> <td > <p><span style='font-size:@<fontSizeSmall>@pt; font-weight:600; color:#aa0000;'>University of Pécs, Medical School</span></p> </td> </tr> <tr> <td > <p align="left"> <span style='font-size:@<fontSize>@pt; font-weight:600; color:#ff0000;'>Ágota Kacsó</span></p> </td> <td > <p><span style='font-size:@<fontSizeSmall>@pt; font-weight:600; color:#aa0000;'>Budapest University of Technology and Economics</span></p> </td> </tr> <tr> <td > <p align="left"><span style='font-size:@<fontSize>@pt; font-weight:600; color:#ff0000;'>Günther Zeck</span></p> </td> <td > <p><span style='font-size:@<fontSizeSmall>@pt; font-weight:600; color:#aa0000;'>Natural and Medical Sciences Institute Reutlingen</span></p> </td> </tr> <tr> <td > <p align="left"><span style='font-size:@<fontSizeSmall>@pt; font-weight:600; color:#aa0000;'>http://www.gears.vision</span></p> </td> <td > <p><span style='font-size:@<fontSizeSmall>@pt; font-weight:600; color:#aa0000;'>[email protected]</span></p> </td> </tr> </tbody> </table> ''') self.titleLabel = QLabel( self.titleTemplate.format(fontSize=self.fontSize, fontSizeLarge=self.fontSizeLarge, fontSizeSmall=self.fontSizeSmall)) self.titleLabel.setTextFormat(Qt.RichText) self.titleLabel.setAlignment(Qt.AlignHCenter | Qt.AlignVCenter) grid.addWidget(self.titleLabel, 1, 3, 1, 1) self.instructionsTemplate = self.styleEsc(''' <p align="left"><strong> <table border="0" cellpadding="0" cellspacing="0"> <tbody> <tr> <td > <p align="left"><span style='font-size:@<fontSize>@pt; font-weight:600; color:#ff0000;'>Reset tree</span></p> </td> <td > <p><span style='font-size:@<fontSizeSmall>@pt; font-weight:600; color:#aa0000;'>press space</span></p> </td> </tr> <tr> <td > <p align="left"><span style='font-size:@<fontSize>@pt; font-weight:600; color:#ff0000;'>Navigate</span></p> </td> <td > <p><span style='font-size:@<fontSizeSmall>@pt; font-weight:600; color:#aa0000;'>press key for first character</span></p> </td> </tr> <tr> <td > <p align="left"> <span style='font-size:@<fontSize>@pt; font-weight:600; color:#ff0000;'>Change font size</span></p> </td> <td > <p><span style='font-size:@<fontSizeSmall>@pt; font-weight:600; color:#aa0000;'>ctrl+mouse wheel</span></p> </td> </tr> <tr> <td > <p align="left"><span style='font-size:@<fontSize>@pt; font-weight:600; color:#ff0000;'>Edit or configure</span></p> </td> <td > <p><span style='font-size:@<fontSizeSmall>@pt; font-weight:600; color:#aa0000;'>right click tree item</span></p> </td> </tr> <tr> <td > <p align="left"><span style='font-size:@<fontSize>@pt; font-weight:600; color:#ff0000;'>/</span></p> </td> <td > <p><span style='font-size:@<fontSizeSmall>@pt; font-weight:600; color:#aa0000;'> @<emptyFolderOp>@ empty folders</span></p> </td> </tr> </tbody> </table> ''') self.instructionsLabel = QLabel( self.instructionsTemplate.format(fontSize=self.fontSize, fontSizeLarge=self.fontSizeLarge, fontSizeSmall=self.fontSizeSmall, emptyFolderOp='show')) self.instructionsLabel.setStyleSheet(''' QLabel{ border-width: 1px; } ''') self.instructionsLabel.setTextFormat(Qt.RichText) self.instructionsLabel.setAlignment(Qt.AlignHCenter) grid.addWidget(self.instructionsLabel, 3, 1, 1, 1) specs = QLabel(gears.getSpecs()) #self.titleLabel.setTextFormat( Qt.RichText ) self.titleLabel.setAlignment(Qt.AlignHCenter | Qt.AlignVCenter) specs.setFocusPolicy(Qt.NoFocus) #specsButton.clicked.connect(self.onSpecs) grid.addWidget(specs, 4, 1, 1, 1) quitButton = QPushButton('Quit') quitButton.setFocusPolicy(Qt.NoFocus) quitButton.clicked.connect(self.onQuit) grid.addWidget(quitButton, 4, 3, 1, 1) #settingsButton = QPushButton('Measurement hardware') #settingsButton.setFocusPolicy(Qt.NoFocus) #settingsButton.clicked.connect(self.onSet) #grid.addWidget(settingsButton, 3, 3, 1, 1) tree = BrowserTree(self.launcherWindow, self) tree.setAttribute(Qt.WA_TransparentForMouseEvents, on=False) self.tree = tree tree.setFocusPolicy(Qt.NoFocus) tree.setHeaderHidden(True) tree.setStyleSheet("background-color: black;") item0 = QTreeWidgetItem(tree) item0.setText(0, 'Sequences') itemMap = dict() itemMap["./Project/Sequences"] = item0 rootPath = "./Project/Sequences" emptyDirs = find_empty_dirs(rootPath) for (path, dirs, files) in os.walk(rootPath): for subdir in dirs: if subdir != "__pycache__": fullPath = os.path.join(path, subdir) #if(rootPath == path): # item0 = QTreeWidgetItem(tree) # itemMap[fullPath] = item0 # item0.setText(0, subdir) #else: item1 = QTreeWidgetItem() itemMap[fullPath] = item1 item1.setText(0, subdir) itemMap[path].addChild(item1) if fullPath in emptyDirs: item1.setText( 1, "gears_folder_not_holding_any_sequence_scripts") item1.setHidden(True) for item in files: if item.endswith('.pyx'): #if(rootPath == path): # item0 = QTreeWidgetItem(tree) # itemMap[os.path.join(path, item)] = item0 # item0.setText(0, item) #else: item1 = QTreeWidgetItem() itemMap[os.path.join(path, item)] = item1 item1.setText(0, item) item1.setText(1, "tags spot spatial temporal") itemMap[path].addChild(item1) item0.setExpanded(True) grid.addWidget(tree, 1, 1, 4, 3, Qt.AlignLeft | Qt.AlignTop) #qbtn = QPushButton('Quit', self) #qbtn.clicked.connect(QCoreApplication.instance().quit) #qbtn.resize(qbtn.sizeHint()) #grid.addWidget(qbtn, 8, 9) #treeMooker.setLayout(treeMookerGrid) #gridWidget.setLayout(grid) #stack.addWidget(treeMooker) #stack.addWidget(gridWidget) self.setLayout(grid) self.setWindowTitle('O\u0398\u03a6O') # self.setWindowIcon(QIcon('web.png')) self.showFullScreen() self.setFixedSize(self.size()) self.tree.setCurrentIndex(self.tree.model().index(0, 0)) def keyPressEvent(self, e): if e.key() == Qt.Key_Escape or e.text() == 'q': box = QMessageBox(self) QGuiApplication.setOverrideCursor(AppData.cursors['arrow']) box.setText('Are you sure you want to quit?') box.setWindowTitle('Please confirm!') box.setWindowFlags(Qt.FramelessWindowHint | Qt.Dialog) box.setStandardButtons(QMessageBox.Yes | QMessageBox.No) box.setDefaultButton(QMessageBox.No) if box.exec() == QMessageBox.Yes: self.close() QGuiApplication.restoreOverrideCursor() elif e.key() == Qt.Key_Space or e.key() == Qt.Key_Backspace: self.tree.setCurrentItem(self.tree.topLevelItem(0)) self.tree.collapseAll() self.tree.topLevelItem(0).setExpanded(True) elif e.key() == Qt.Key_Down: ni = self.tree.itemBelow(self.tree.currentItem()) if (ni != None): self.tree.setCurrentItem(ni) elif e.key() == Qt.Key_Up: ni = self.tree.itemAbove(self.tree.currentItem()) if (ni != None): self.tree.setCurrentItem(ni) elif e.key() == Qt.Key_Left: ni = self.tree.currentItem().parent() if (ni != None): self.tree.setCurrentItem(ni) elif e.key() == Qt.Key_Right or e.key() == Qt.Key_Return or e.key( ) == Qt.Key_Enter: ni = self.tree.currentItem().child(0) if (ni != None): self.tree.setCurrentItem(ni) else: self.tree.open(self.tree.currentItem(), False) elif e.key() == Qt.Key_Slash: self.tree.hideEmptyFolders(not self.tree.isHidingEmptyFolders) self.instructionsLabel.setText( self.instructionsTemplate.format( fontSize=int(self.fontSize), fontSizeLarge=int(self.fontSizeLarge), fontSizeSmall=int(self.fontSizeSmall), emptyFolderOp=('show' if self.tree.isHidingEmptyFolders else 'hide'))) #elif e.key()==Qt.Key_0 or e.key()==Qt.Key_1 or e.key()==Qt.Key_2 or e.key()==Qt.Key_3 or e.key()==Qt.Key_4 or e.key()==Qt.Key_5 or e.key()==Qt.Key_6 or e.key()==Qt.Key_7 or e.key()==Qt.Key_8 or e.key()==Qt.Key_9 : else: self.tree.clearSelection() self.tree.keyboardSearch(e.text()) sel = self.tree.selectedItems() if len(sel) > 0: self.tree.onClick(sel[0], 0) def onSpecs(self): box = QMessageBox(self) horizontalSpacer = QSpacerItem(1000, 0, QSizePolicy.Minimum, QSizePolicy.Expanding) box.setText(gears.getSpecs()) box.setWindowTitle('System specs') box.setWindowFlags(Qt.FramelessWindowHint | Qt.Dialog) box.setStandardButtons(QMessageBox.Ok) box.setDefaultButton(QMessageBox.Ok) layout = box.layout() layout.addItem(horizontalSpacer, layout.rowCount(), 0, 1, layout.columnCount()) box.exec() def onQuit(self): box = QMessageBox(self) #box.setCursor(AppData.cursors['arrow']) QGuiApplication.setOverrideCursor(AppData.cursors['arrow']) box.setText('Are you sure you want to quit?') box.setWindowTitle('Please confirm!') box.setWindowFlags(Qt.FramelessWindowHint | Qt.Dialog) box.setStandardButtons(QMessageBox.Yes | QMessageBox.No) box.setDefaultButton(QMessageBox.No) if box.exec() == QMessageBox.Yes: self.close() QGuiApplication.restoreOverrideCursor() def onSet(self): #from subprocess import call #call("notepad ./Project/Sequences/DefaultSequence.py") self.editor = QsciScintilla() # define the font to use font = QFont() font.setFamily('Courier') font.setFixedPitch(True) font.setPointSize(10) # the font metrics here will help # building the margin width later fm = QFontMetrics(font) ## set the default font of the editor ## and take the same font for line numbers self.editor.setFont(font) self.editor.setMarginsFont(font) ## Line numbers # conventionnaly, margin 0 is for line numbers self.editor.setMarginWidth(0, fm.width("00000") + 5) self.editor.setMarginLineNumbers(0, True) ## Edge Mode shows a red vetical bar at 80 chars self.editor.setEdgeMode(QsciScintilla.EdgeLine) self.editor.setEdgeColumn(80) self.editor.setEdgeColor(QColor("#FF0000")) ## Folding visual : we will use boxes self.editor.setFolding(QsciScintilla.BoxedTreeFoldStyle) ## Braces matching self.editor.setBraceMatching(QsciScintilla.SloppyBraceMatch) ## Editing line color self.editor.setCaretLineVisible(True) self.editor.setCaretLineBackgroundColor(QColor("#CDA869")) ## Margins colors # line numbers margin self.editor.setMarginsBackgroundColor(QColor("#333333")) self.editor.setMarginsForegroundColor(QColor("#CCCCCC")) # folding margin colors (foreground,background) self.editor.setFoldMarginColors(QColor("#99CC66"), QColor("#333300")) ## Choose a lexer lexer = QsciLexerPython() lexer.setDefaultFont(font) api = QsciStrictAPIs(lexer) api.prepare() lexer.setDefaultFont(font) self.editor.setLexer(lexer) self.editor.setMinimumSize(1024, 768) self.editor.show() ## Show this file in the editor text = open("./Project/Sequences/DefaultSequence.py").read() self.editor.setText(text) def wheelEvent(self, event): if self.app.keyboardModifiers() & Qt.ControlModifier: self.fontSize += event.angleDelta().y() / 120 self.fontSizeLarge += event.angleDelta().y() / 120 self.fontSizeSmall += event.angleDelta().y() / 120 self.setStyleSheet( self.styleTemplate.format(fontSize=int(self.fontSize), fontSizeLarge=int( self.fontSizeLarge))) self.titleLabel.setText( self.titleTemplate.format( fontSize=int(self.fontSize), fontSizeLarge=int(self.fontSizeLarge), fontSizeSmall=int(self.fontSizeSmall))) self.instructionsLabel.setText( self.instructionsTemplate.format( fontSize=int(self.fontSize), fontSizeLarge=int(self.fontSizeLarge), fontSizeSmall=int(self.fontSizeSmall), emptyFolderOp=('show' if self.tree.isHidingEmptyFolders else 'hide'))) self.tree.resizeColumnToContents(0) self.updateGeometry() #def eventFilter(self, obj, event): # #if event.type() in [ QEvent.MouseMove, QEvent.MouseButtonPress, QEvent.MouseButtonRelease, QEvent.MouseButtonDblClick]: # self.app.sendEvent(self.gridWidget, event) # return super().eventFilter(obj, event) def sizeHint(self): return self.app.desktop().screenGeometry().size()
class ScriptTab(QWidget,): autocomplete_lst = [] def __init__(self, parent, name, script="", filemodified=0): QWidget.__init__(self) self.parent = parent self.tabWidget = self.parent.ui.tabWidget self.gridlayout = QGridLayout(self) self._dirty = False self.initialize_editor() self.gridlayout.addWidget(self.editor) self.setAcceptDrops(True) self.filename = name self.filemodified = filemodified if self.is_file(): self.title = os.path.basename(name) # if self.equals_saved(): # self.filemodified = os.path.getmtime(self.filename) else: self.filename = "" self.title = name # Show this file in the self.editor self.editor.setText(script) self.clean_txt = self.saved() self.update_dirty() self.editor.keyPressEvent = self.key_press_event @property def scriptRunner(self): return self.parent.controller.scriptRunner def wheelEvent(self, event): if event.modifiers() == Qt.ControlModifier: if event.delta() > 0: self.editor.zoomIn() else: self.editor.zoomOut() return QWidget.wheelEvent(self, event) def initialize_editor(self): self.editor = QsciScintilla() # self.editor.cursorPositionChanged.connect(self.e) # self.editor.copyAvailable.connect(self.e) # self.editor.indicatorClicked.connect(self.e) # self.editor.indicatorReleased.connect(self.e) # self.editor.linesChanged.connect(self.e) # self.editor.marginClicked.connect(self.e) # self.editor.modificationAttempted.connect(self.e) # self.editor.modificationChanged.connect(self.e) # self.editor.selectionChanged.connect(self.e) # self.editor.textChanged.connect(self.e) # self.editor.userListActivated.connect(self.e) if self.editor.__class__.__name__ == "LineTextWidget": return # When using PySide without QSciScintilla # define the font to use font = QFont() font.setFamily("Consolas") font.setFixedPitch(True) font.setPointSize(10) # the font metrics here will help # building the margin width later fm = QFontMetrics(font) # set the default font of the self.editor # and take the same font for line numbers self.editor.setFont(font) self.editor.setMarginsFont(font) # Line numbers # conventionnaly, margin 0 is for line numbers self.editor.setMarginWidth(0, fm.width("00000") + 5) self.editor.setMarginLineNumbers(0, True) self.editor.setTabWidth(4) # Folding visual : we will use boxes self.editor.setFolding(QsciScintilla.BoxedTreeFoldStyle) self.editor.setAutoIndent(True) # Braces matching self.editor.setBraceMatching(QsciScintilla.SloppyBraceMatch) # Editing line color self.editor.setCaretLineVisible(True) self.editor.setCaretLineBackgroundColor(QColor("#CDA869")) # Margins colors # line numbers margin self.editor.setMarginsBackgroundColor(QColor("#333333")) self.editor.setMarginsForegroundColor(QColor("#CCCCCC")) # folding margin colors (foreground,background) self.editor.setFoldMarginColors(QColor("#99CC66"), QColor("#333300")) # Choose a lexer self.lexer = QsciLexerPython() self.lexer.setDefaultFont(font) # Set the length of the string before the editor tries to autocomplete # In practise this would be higher than 1 # But its set lower here to make the autocompletion more obvious self.editor.setAutoCompletionThreshold(1) # Tell the editor we are using a QsciAPI for the autocompletion self.editor.setAutoCompletionSource(QsciScintilla.AcsAPIs) self.editor.setLexer(self.lexer) self.editor.setCallTipsStyle(QsciScintilla.CallTipsContext) # self.editor.setCallTipsVisible(0) # Create an API for us to populate with our autocomplete terms self.api = QsciAPIs(self.lexer) # Compile the api for use in the lexer self.api.prepare() # def tefocusInEvent(self, event): # self.parent.focusInEvent(event) # return QTextEdit.focusInEvent(self.textEditScript, event) def is_file(self): return os.path.isfile(self.filename) def is_modified(self): if self.is_file(): if self.equals_saved(): self.filemodified = os.path.getmtime(self.filename) return str(os.path.getmtime(self.filename)) != str(self.filemodified) else: return False def saved(self): if self.is_file(): f = open(self.filename) saved = f.read() f.close() return saved.strip() else: return "" def equals_saved(self): curr_lines = self.get_script().strip().splitlines() saved_lines = self.saved().strip().splitlines() if len(curr_lines) != len(saved_lines): return False for cl, sl in zip(curr_lines, saved_lines): if cl.strip() != sl.strip(): return False return True @property def dirty(self): if not self._dirty: self.dirty = self.clean_txt != self.get_script() return self._dirty @dirty.setter def dirty(self, dirty): if dirty is False: self.clean_txt = self.get_script() if self._dirty != dirty: self._dirty = dirty self.filename_changed() def update_dirty(self): return self.dirty def index(self): return self.tabWidget.indexOf(self) def filename_changed(self): index = self.parent.ui.tabWidget.indexOf(self) if self.dirty: self.tabWidget.setTabText(index, "%s*" % self.title) else: self.tabWidget.setTabText(index, self.title) def reload(self): self.set_script(self.parent._load_script(self.filename)) self.filemodified = os.path.getmtime(self.filename) def close(self): while self.dirty is True: # While avoids data loss, in case save operation is aborted if self.filename == "": text = "Save unsaved changes?" else: text = "Save %s?" % self.filename ans = QMessageBox.question(self, 'Save', text, QMessageBox.Yes, QMessageBox.Cancel, QMessageBox.No) if ans == QMessageBox.Cancel: return elif ans == QMessageBox.Yes: self.parent.actionSave(False) elif ans == QMessageBox.No: break self.tabWidget.removeTab(self.index()) def _save(self,): f = open(self.filename, 'w') f.write(self.get_script()) f.close() self.title = os.path.basename(self.filename) self.dirty = False def key_press_event(self, event): self.update_dirty() if self.editor.__class__.__name__ == "LineTextWidget": return self.editor.edit.keyReleaseEvent(event) # When using PySide without QSciScintilla QsciScintilla.keyPressEvent(self.editor, event) linenr, pos_in_line = self.editor.getCursorPosition() line = str(self.editor.text(linenr)[:pos_in_line]) if event.key() == Qt.Key_F1: try: self.parent.show_documentation(getattr(self.scriptRunner, str(self.editor.selectedText()))) except AttributeError: pass if event.key() == Qt.Key_Enter or event.key() == Qt.Key_Return: for tip in self.autocomplete_lst: try: if line.endswith(tip[:tip.index('(') ]): self.editor.insert(tip[tip.index('('):]) parent, residual = self.scriptRunner.split_line(line) self.parent.show_documentation(self.scriptRunner.get_function_dict(parent, residual)[residual]) return except ValueError: pass if event.key() < 100 or event.key() in [Qt.Key_Backspace, Qt.Key_Shift]: linenr, pos_in_line = self.editor.getCursorPosition() line = str(self.editor.text(linenr)[:pos_in_line]) lst = self.scriptRunner.get_autocomplete_list(line) if lst is not None: self.api.clear() list(map(self.api.add, lst)) self.api.prepare() if len(lst) > 0: self.autocomplete_lst = lst self.editor.autoCompleteFromAll() shift_event = QKeyEvent(QEvent.KeyPress, Qt.Key_Shift, Qt.NoModifier) QsciScintilla.keyPressEvent(self.editor, shift_event) # show autocomplete list def set_script(self, script): self.editor.setText(script) def get_script(self): return str(self.editor.text()).replace("\r", "").replace(">>> ", "").replace("\t", " ") def dropHandler(self, dataItemList, ctrl, shift, event): pass
class highlightEditor(QMainWindow): """ 语法高亮代码框 """ def __init__(self, parent=None, lineNumberOn=False): super(highlightEditor, self).__init__() self.editor = QsciScintilla(parent) font = QFont() font.setFamily("Consolas") font.setPointSize(12) font.setFixedPitch(True) self.editor.setFont(font) self.editor.setObjectName("editor") self.editor.setUtf8(True) self.editor.setMarginsFont(font) if lineNumberOn: self.editor.setMarginWidth( 0, len(str(len(self.editor.text().split('\n')))) * 20) self.editor.setMarginLineNumbers(0, lineNumberOn) self.editor.setBraceMatching(QsciScintilla.StrictBraceMatch) self.editor.setIndentationsUseTabs(True) self.editor.setIndentationWidth(4) self.editor.setTabIndents(True) self.editor.setAutoIndent(True) self.editor.setBackspaceUnindents(True) self.editor.setTabWidth(4) self.editor.setCaretLineVisible(True) self.editor.setCaretLineBackgroundColor(QColor('#DCDCDC')) self.editor.setIndentationGuides(True) self.editor.setFolding(QsciScintilla.PlainFoldStyle) self.editor.setMarginWidth(2, 12) self.editor.markerDefine(QsciScintilla.Minus, QsciScintilla.SC_MARKNUM_FOLDEROPEN) self.editor.markerDefine(QsciScintilla.Plus, QsciScintilla.SC_MARKNUM_FOLDER) self.editor.markerDefine(QsciScintilla.Minus, QsciScintilla.SC_MARKNUM_FOLDEROPENMID) self.editor.markerDefine(QsciScintilla.Plus, QsciScintilla.SC_MARKNUM_FOLDEREND) self.editor.setMarkerBackgroundColor( QColor("#FFFFFF"), QsciScintilla.SC_MARKNUM_FOLDEREND) self.editor.setMarkerForegroundColor( QColor("#272727"), QsciScintilla.SC_MARKNUM_FOLDEREND) self.editor.setMarkerBackgroundColor( QColor("#FFFFFF"), QsciScintilla.SC_MARKNUM_FOLDEROPENMID) self.editor.setMarkerForegroundColor( QColor("#272727"), QsciScintilla.SC_MARKNUM_FOLDEROPENMID) self.editor.setAutoCompletionSource(QsciScintilla.AcsAll) self.editor.setAutoCompletionCaseSensitivity(True) self.editor.setAutoCompletionReplaceWord(False) self.editor.setAutoCompletionThreshold(1) self.editor.setAutoCompletionUseSingle(QsciScintilla.AcusExplicit) self.lexer = highlight(self.editor) self.editor.setLexer(self.lexer) self.__api = QsciAPIs(self.lexer) autocompletions = keyword.kwlist + [] for ac in autocompletions: self.__api.add(ac) self.__api.prepare() self.editor.autoCompleteFromAll() self.editor.textChanged.connect(self.changed) def changed(self): self.editor.setMarginWidth( 0, len(str(len(self.editor.text().split('\n')))) * 20)