src/eric7/QScintilla/MiniEditor.py

branch
eric7
changeset 10431
64157aeb0312
parent 10417
c6011e501282
child 10439
21c28b0f9e41
equal deleted inserted replaced
10430:e440aaf179ce 10431:64157aeb0312
103 103
104 def editorCommand(self, cmd): 104 def editorCommand(self, cmd):
105 """ 105 """
106 Public method to perform a simple editor command. 106 Public method to perform a simple editor command.
107 107
108 @param cmd the scintilla command to be performed (integer) 108 @param cmd the scintilla command to be performed
109 @type int
109 """ 110 """
110 if cmd == QsciScintilla.SCI_DELETEBACK: 111 if cmd == QsciScintilla.SCI_DELETEBACK:
111 line, index = self.getCursorPosition() 112 line, index = self.getCursorPosition()
112 text = self.text(line)[index - 1 : index + 1] 113 text = self.text(line)[index - 1 : index + 1]
113 matchingPairs = ["()", "[]", "{}", "<>", "''", '""'] 114 matchingPairs = ["()", "[]", "{}", "<>", "''", '""']
264 265
265 def __init__(self, filename="", filetype="", parent=None, name=None): 266 def __init__(self, filename="", filetype="", parent=None, name=None):
266 """ 267 """
267 Constructor 268 Constructor
268 269
269 @param filename name of the file to open (string) 270 @param filename name of the file to open
270 @param filetype type of the source file (string) 271 @type str
271 @param parent reference to the parent widget (QWidget) 272 @param filetype type of the source file
272 @param name object name of the window (string) 273 @type str
274 @param parent reference to the parent widget
275 @type QWidget
276 @param name object name of the window
277 @type str
273 """ 278 """
274 super().__init__(parent) 279 super().__init__(parent)
275 if name is not None: 280 if name is not None:
276 self.setObjectName(name) 281 self.setObjectName(name)
277 self.setWindowIcon(EricPixmapCache.getIcon("editor")) 282 self.setWindowIcon(EricPixmapCache.getIcon("editor"))
384 389
385 def closeEvent(self, event): 390 def closeEvent(self, event):
386 """ 391 """
387 Protected method to handle the close event. 392 Protected method to handle the close event.
388 393
389 @param event close event (QCloseEvent) 394 @param event close event
395 @type QCloseEvent
390 """ 396 """
391 if self.__maybeSave(): 397 if self.__maybeSave():
392 self.__writeSettings() 398 self.__writeSettings()
393 event.accept() 399 event.accept()
394 else: 400 else:
416 422
417 def __save(self): 423 def __save(self):
418 """ 424 """
419 Private slot to save a file. 425 Private slot to save a file.
420 426
421 @return flag indicating success (boolean) 427 @return flag indicating success
428 @rtype bool
422 """ 429 """
423 if not self.__curFile: 430 if not self.__curFile:
424 return self.__saveAs() 431 return self.__saveAs()
425 else: 432 else:
426 return self.__saveFile(self.__curFile) 433 return self.__saveFile(self.__curFile)
427 434
428 def __saveAs(self): 435 def __saveAs(self):
429 """ 436 """
430 Private slot to save a file with a new name. 437 Private slot to save a file with a new name.
431 438
432 @return flag indicating success (boolean) 439 @return flag indicating success
440 @rtype bool
433 """ 441 """
434 fileName = EricFileDialog.getSaveFileName(self) 442 fileName = EricFileDialog.getSaveFileName(self)
435 if not fileName: 443 if not fileName:
436 return False 444 return False
437 445
488 """ 496 """
489 Private slot to check some actions for their enable/disable status 497 Private slot to check some actions for their enable/disable status
490 and set the statusbar info. 498 and set the statusbar info.
491 499
492 @param setSb flag indicating an update of the status bar is wanted 500 @param setSb flag indicating an update of the status bar is wanted
493 (boolean) 501 @type bool
494 """ 502 """
495 self.saveAct.setEnabled(self.__textEdit.isModified()) 503 self.saveAct.setEnabled(self.__textEdit.isModified())
496 504
497 self.undoAct.setEnabled(self.__textEdit.isUndoAvailable()) 505 self.undoAct.setEnabled(self.__textEdit.isUndoAvailable())
498 self.redoAct.setEnabled(self.__textEdit.isRedoAvailable()) 506 self.redoAct.setEnabled(self.__textEdit.isRedoAvailable())
554 562
555 def __readShortcut(self, act, category): 563 def __readShortcut(self, act, category):
556 """ 564 """
557 Private function to read a single keyboard shortcut from the settings. 565 Private function to read a single keyboard shortcut from the settings.
558 566
559 @param act reference to the action object (EricAction) 567 @param act reference to the action object
560 @param category category the action belongs to (string) 568 @type EricAction
569 @param category category the action belongs to
570 @type str
561 """ 571 """
562 if act.objectName(): 572 if act.objectName():
563 accel = Preferences.getSettings().value( 573 accel = Preferences.getSettings().value(
564 "Shortcuts/{0}/{1}/Accel".format(category, act.objectName()) 574 "Shortcuts/{0}/{1}/Accel".format(category, act.objectName())
565 ) 575 )
3156 3166
3157 def __maybeSave(self): 3167 def __maybeSave(self):
3158 """ 3168 """
3159 Private method to ask the user to save the file, if it was modified. 3169 Private method to ask the user to save the file, if it was modified.
3160 3170
3161 @return flag indicating, if it is ok to continue (boolean) 3171 @return flag indicating, if it is ok to continue
3172 @rtype bool
3162 """ 3173 """
3163 if self.__textEdit.isModified(): 3174 if self.__textEdit.isModified():
3164 ret = EricMessageBox.okToClearData( 3175 ret = EricMessageBox.okToClearData(
3165 self, 3176 self,
3166 self.tr("eric Mini Editor"), 3177 self.tr("eric Mini Editor"),
3172 3183
3173 def __loadFile(self, fileName, filetype=None): 3184 def __loadFile(self, fileName, filetype=None):
3174 """ 3185 """
3175 Private method to load the given file. 3186 Private method to load the given file.
3176 3187
3177 @param fileName name of the file to load (string) 3188 @param fileName name of the file to load
3178 @param filetype type of the source file (string) 3189 @type str
3190 @param filetype type of the source file
3191 @type str
3179 """ 3192 """
3180 self.__loadEditorConfig(fileName=fileName) 3193 self.__loadEditorConfig(fileName=fileName)
3181 3194
3182 try: 3195 try:
3183 with EricOverrideCursor(): 3196 with EricOverrideCursor():
3325 3338
3326 def __setCurrentFile(self, fileName): 3339 def __setCurrentFile(self, fileName):
3327 """ 3340 """
3328 Private method to register the file name of the current file. 3341 Private method to register the file name of the current file.
3329 3342
3330 @param fileName name of the file to register (string) 3343 @param fileName name of the file to register
3344 @type str
3331 """ 3345 """
3332 self.__curFile = fileName 3346 self.__curFile = fileName
3333 3347
3334 shownName = ( 3348 shownName = (
3335 self.tr("Untitled") 3349 self.tr("Untitled")
3350 3364
3351 def getFileName(self): 3365 def getFileName(self):
3352 """ 3366 """
3353 Public method to return the name of the file being displayed. 3367 Public method to return the name of the file being displayed.
3354 3368
3355 @return filename of the displayed file (string) 3369 @return filename of the displayed file
3370 @rtype str
3356 """ 3371 """
3357 return self.__curFile 3372 return self.__curFile
3358 3373
3359 def __strippedName(self, fullFileName): 3374 def __strippedName(self, fullFileName):
3360 """ 3375 """
3361 Private method to return the filename part of the given path. 3376 Private method to return the filename part of the given path.
3362 3377
3363 @param fullFileName full pathname of the given file (string) 3378 @param fullFileName full pathname of the given file
3364 @return filename part (string) 3379 @type str
3380 @return filename part
3381 @rtype str
3365 """ 3382 """
3366 return pathlib.Path(fullFileName).name 3383 return pathlib.Path(fullFileName).name
3367 3384
3368 def __modificationChanged(self, m): 3385 def __modificationChanged(self, m):
3369 """ 3386 """
3370 Private slot to handle the modificationChanged signal. 3387 Private slot to handle the modificationChanged signal.
3371 3388
3372 @param m modification status 3389 @param m modification status
3390 @type bool
3373 """ 3391 """
3374 self.setWindowModified(m) 3392 self.setWindowModified(m)
3375 self.__checkActions() 3393 self.__checkActions()
3376 3394
3377 def __cursorPositionChanged(self, line, pos): 3395 def __cursorPositionChanged(self, line, pos):
3378 """ 3396 """
3379 Private slot to handle the cursorPositionChanged signal. 3397 Private slot to handle the cursorPositionChanged signal.
3380 3398
3381 @param line line number of the cursor 3399 @param line line number of the cursor
3400 @type int
3382 @param pos position in line of the cursor 3401 @param pos position in line of the cursor
3402 @type int
3383 """ 3403 """
3384 lang = self.getLanguage() 3404 lang = self.getLanguage()
3385 self.__setSbFile(line + 1, pos, lang) 3405 self.__setSbFile(line + 1, pos, lang)
3386 3406
3387 if Preferences.getEditor("MarkOccurrencesEnabled"): 3407 if Preferences.getEditor("MarkOccurrencesEnabled"):
3613 3633
3614 def __setMonospaced(self, on): 3634 def __setMonospaced(self, on):
3615 """ 3635 """
3616 Private method to set/reset a monospaced font. 3636 Private method to set/reset a monospaced font.
3617 3637
3618 @param on flag to indicate usage of a monospace font (boolean) 3638 @param on flag to indicate usage of a monospace font
3639 @type bool
3619 """ 3640 """
3620 if on: 3641 if on:
3621 if not self.lexer_: 3642 if not self.lexer_:
3622 f = Preferences.getEditorOtherFonts("MonospacedFont") 3643 f = Preferences.getEditorOtherFonts("MonospacedFont")
3623 self.__textEdit.monospacedStyles(f) 3644 self.__textEdit.monospacedStyles(f)
3685 def __printPreview(self, printer): 3706 def __printPreview(self, printer):
3686 """ 3707 """
3687 Private slot to generate a print preview. 3708 Private slot to generate a print preview.
3688 3709
3689 @param printer reference to the printer object 3710 @param printer reference to the printer object
3690 (QScintilla.Printer.Printer) 3711 @type QScintilla.Printer.Printer
3691 """ 3712 """
3692 printer.printRange(self.__textEdit) 3713 printer.printRange(self.__textEdit)
3693 3714
3694 ######################################################### 3715 #########################################################
3695 ## Methods needed by the context menu 3716 ## Methods needed by the context menu
3697 3718
3698 def __contextMenuRequested(self, coord): 3719 def __contextMenuRequested(self, coord):
3699 """ 3720 """
3700 Private slot to show the context menu. 3721 Private slot to show the context menu.
3701 3722
3702 @param coord the position of the mouse pointer (QPoint) 3723 @param coord the position of the mouse pointer
3724 @type QPoint
3703 """ 3725 """
3704 self.contextMenu.popup(self.mapToGlobal(coord)) 3726 self.contextMenu.popup(self.mapToGlobal(coord))
3705 3727
3706 def __initContextMenu(self): 3728 def __initContextMenu(self):
3707 """ 3729 """
3728 3750
3729 def __initContextMenuLanguages(self): 3751 def __initContextMenuLanguages(self):
3730 """ 3752 """
3731 Private method used to setup the Languages context sub menu. 3753 Private method used to setup the Languages context sub menu.
3732 3754
3733 @return reference to the generated menu (QMenu) 3755 @return reference to the generated menu
3756 @rtype QMenu
3734 """ 3757 """
3735 menu = QMenu(self.tr("Languages")) 3758 menu = QMenu(self.tr("Languages"))
3736 3759
3737 self.languagesActGrp = QActionGroup(self) 3760 self.languagesActGrp = QActionGroup(self)
3738 self.noLanguageAct = menu.addAction(self.tr("No Language")) 3761 self.noLanguageAct = menu.addAction(self.tr("No Language"))
3782 3805
3783 def __showLanguagesMenu(self, pos): 3806 def __showLanguagesMenu(self, pos):
3784 """ 3807 """
3785 Private slot to show the Languages menu of the status bar. 3808 Private slot to show the Languages menu of the status bar.
3786 3809
3787 @param pos position the menu should be shown at (QPoint) 3810 @param pos position the menu should be shown at
3811 @type QPoint
3788 """ 3812 """
3789 self.languagesMenu.exec(pos) 3813 self.languagesMenu.exec(pos)
3790 3814
3791 def __selectPygmentsLexer(self): 3815 def __selectPygmentsLexer(self):
3792 """ 3816 """
3819 3843
3820 def __languageMenuTriggered(self, act): 3844 def __languageMenuTriggered(self, act):
3821 """ 3845 """
3822 Private method to handle the selection of a lexer language. 3846 Private method to handle the selection of a lexer language.
3823 3847
3824 @param act reference to the action that was triggered (QAction) 3848 @param act reference to the action that was triggered
3849 @type QAction
3825 """ 3850 """
3826 if act == self.noLanguageAct: 3851 if act == self.noLanguageAct:
3827 self.__resetLanguage() 3852 self.__resetLanguage()
3828 elif act == self.pygmentsAct: 3853 elif act == self.pygmentsAct:
3829 self.setLanguage("dummy.pygments") 3854 self.setLanguage("dummy.pygments")
3859 def setLanguage(self, filename, initTextDisplay=True, pyname=""): 3884 def setLanguage(self, filename, initTextDisplay=True, pyname=""):
3860 """ 3885 """
3861 Public method to set a lexer language. 3886 Public method to set a lexer language.
3862 3887
3863 @param filename filename used to determine the associated lexer 3888 @param filename filename used to determine the associated lexer
3864 language (string) 3889 language
3890 @type str
3865 @param initTextDisplay flag indicating an initialization of the text 3891 @param initTextDisplay flag indicating an initialization of the text
3866 display is required as well (boolean) 3892 display is required as well
3867 @param pyname name of the pygments lexer to use (string) 3893 @type bool
3894 @param pyname name of the pygments lexer to use
3895 @type str
3868 """ 3896 """
3869 self.__bindLexer(filename, pyname=pyname) 3897 self.__bindLexer(filename, pyname=pyname)
3870 self.__textEdit.recolor() 3898 self.__textEdit.recolor()
3871 self.__checkLanguage() 3899 self.__checkLanguage()
3872 3900
3921 def __bindLexer(self, filename, pyname=""): 3949 def __bindLexer(self, filename, pyname=""):
3922 """ 3950 """
3923 Private slot to set the correct lexer depending on language. 3951 Private slot to set the correct lexer depending on language.
3924 3952
3925 @param filename filename used to determine the associated lexer 3953 @param filename filename used to determine the associated lexer
3926 language (string) 3954 language
3927 @param pyname name of the pygments lexer to use (string) 3955 @type str
3956 @param pyname name of the pygments lexer to use
3957 @type str
3928 """ 3958 """
3929 if self.lexer_ is not None and ( 3959 if self.lexer_ is not None and (
3930 self.lexer_.lexer() == "container" or self.lexer_.lexer() is None 3960 self.lexer_.lexer() == "container" or self.lexer_.lexer() is None
3931 ): 3961 ):
3932 self.__textEdit.SCN_STYLENEEDED.disconnect(self.__styleNeeded) 3962 self.__textEdit.SCN_STYLENEEDED.disconnect(self.__styleNeeded)
3990 4020
3991 def __styleNeeded(self, position): 4021 def __styleNeeded(self, position):
3992 """ 4022 """
3993 Private slot to handle the need for more styling. 4023 Private slot to handle the need for more styling.
3994 4024
3995 @param position end position, that needs styling (integer) 4025 @param position end position, that needs styling
4026 @type int
3996 """ 4027 """
3997 self.lexer_.styleText(self.__textEdit.getEndStyled(), position) 4028 self.lexer_.styleText(self.__textEdit.getEndStyled(), position)
3998 4029
3999 def __bindName(self, line0): 4030 def __bindName(self, line0):
4000 """ 4031 """
4001 Private method to generate a dummy filename for binding a lexer. 4032 Private method to generate a dummy filename for binding a lexer.
4002 4033
4003 @param line0 first line of text to use in the generation process 4034 @param line0 first line of text to use in the generation process
4004 (string) 4035 @type str
4005 @return dummy file name to be used for binding a lexer (string) 4036 @return dummy file name to be used for binding a lexer
4037 @rtype str
4006 """ 4038 """
4007 bindName = "" 4039 bindName = ""
4008 line0 = line0.lower() 4040 line0 = line0.lower()
4009 4041
4010 # check first line if it does not start with #! 4042 # check first line if it does not start with #!
4074 def getSRHistory(self, key): 4106 def getSRHistory(self, key):
4075 """ 4107 """
4076 Public method to get the search or replace history list. 4108 Public method to get the search or replace history list.
4077 4109
4078 @param key list to return (must be 'search' or 'replace') 4110 @param key list to return (must be 'search' or 'replace')
4079 @return the requested history list (list of strings) 4111 @type str
4112 @return the requested history list
4113 @rtype list of str
4080 """ 4114 """
4081 return self.srHistory[key][:] 4115 return self.srHistory[key][:]
4082 4116
4083 def textForFind(self): 4117 def textForFind(self):
4084 """ 4118 """
4085 Public method to determine the selection or the current word for the 4119 Public method to determine the selection or the current word for the
4086 next find operation. 4120 next find operation.
4087 4121
4088 @return selection or current word (string) 4122 @return selection or current word
4123 @rtype str
4089 """ 4124 """
4090 if self.__textEdit.hasSelectedText(): 4125 if self.__textEdit.hasSelectedText():
4091 text = self.__textEdit.selectedText() 4126 text = self.__textEdit.selectedText()
4092 if "\r" in text or "\n" in text: 4127 if "\r" in text or "\n" in text:
4093 # the selection contains at least a newline, it is 4128 # the selection contains at least a newline, it is
4133 def __getCurrentWord(self): 4168 def __getCurrentWord(self):
4134 """ 4169 """
4135 Private method to get the word at the current position. 4170 Private method to get the word at the current position.
4136 4171
4137 @return the word at that current position 4172 @return the word at that current position
4173 @rtype str
4138 """ 4174 """
4139 line, index = self.getCursorPosition() 4175 line, index = self.getCursorPosition()
4140 return self.__getWord(line, index) 4176 return self.__getWord(line, index)
4141 4177
4142 def showSearchWidget(self): 4178 def showSearchWidget(self):
4171 4207
4172 def activeWindow(self): 4208 def activeWindow(self):
4173 """ 4209 """
4174 Public method to fulfill the ViewManager interface. 4210 Public method to fulfill the ViewManager interface.
4175 4211
4176 @return reference to the text edit component (QsciScintillaCompat) 4212 @return reference to the text edit component
4213 @rtype QsciScintillaCompat
4177 """ 4214 """
4178 return self.__textEdit 4215 return self.__textEdit
4179 4216
4180 def setSearchIndicator(self, startPos, indicLength): 4217 def setSearchIndicator(self, startPos, indicLength):
4181 """ 4218 """
4182 Public method to set a search indicator for the given range. 4219 Public method to set a search indicator for the given range.
4183 4220
4184 @param startPos start position of the indicator (integer) 4221 @param startPos start position of the indicator
4185 @param indicLength length of the indicator (integer) 4222 @type int
4223 @param indicLength length of the indicator
4224 @type int
4186 """ 4225 """
4187 self.__textEdit.setIndicatorRange(self.searchIndicator, startPos, indicLength) 4226 self.__textEdit.setIndicatorRange(self.searchIndicator, startPos, indicLength)
4188 4227
4189 def clearSearchIndicators(self): 4228 def clearSearchIndicators(self):
4190 """ 4229 """
4253 4292
4254 def setText(self, txt, filetype=None): 4293 def setText(self, txt, filetype=None):
4255 """ 4294 """
4256 Public method to set the text programatically. 4295 Public method to set the text programatically.
4257 4296
4258 @param txt text to be set (string) 4297 @param txt text to be set
4259 @param filetype type of the source file (string) 4298 @type str
4299 @param filetype type of the source file
4300 @type str
4260 """ 4301 """
4261 self.__textEdit.setText(txt) 4302 self.__textEdit.setText(txt)
4262 4303
4263 if filetype is None: 4304 if filetype is None:
4264 self.filetype = "" 4305 self.filetype = ""
4487 4528
4488 def __zoomTo(self, value): 4529 def __zoomTo(self, value):
4489 """ 4530 """
4490 Private slot to zoom to a given value. 4531 Private slot to zoom to a given value.
4491 4532
4492 @param value zoom value to be set (integer) 4533 @param value zoom value to be set
4534 @type int
4493 """ 4535 """
4494 self.zoomTo(value) 4536 self.zoomTo(value)
4495 self.sbZoom.setValue(self.getZoom()) 4537 self.sbZoom.setValue(self.getZoom())

eric ide

mercurial