sqledit.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. """
  2. ***************************************************************************
  3. ScriptEdit.py
  4. ---------------------
  5. Date : February 2014
  6. Copyright : (C) 2014 by Alexander Bruy
  7. Email : alexander dot bruy at gmail dot com
  8. ***************************************************************************
  9. * *
  10. * This program is free software; you can redistribute it and/or modify *
  11. * it under the terms of the GNU General Public License as published by *
  12. * the Free Software Foundation; either version 2 of the License, or *
  13. * (at your option) any later version. *
  14. * *
  15. ***************************************************************************
  16. """
  17. __author__ = 'Alexander Bruy'
  18. __date__ = 'February 2014'
  19. __copyright__ = '(C) 2014, Alexander Bruy'
  20. from qgis.PyQt.QtCore import Qt
  21. from qgis.PyQt.QtGui import QColor, QFont, QKeySequence
  22. from qgis.PyQt.QtWidgets import QShortcut
  23. from qgis.PyQt.Qsci import QsciScintilla, QsciLexerSQL
  24. from qgis.core import QgsSettings
  25. class SqlEdit(QsciScintilla):
  26. LEXER_PYTHON = 0
  27. LEXER_R = 1
  28. def __init__(self, parent=None):
  29. QsciScintilla.__init__(self, parent)
  30. self.mylexer = None
  31. self.api = None
  32. self.setCommonOptions()
  33. self.initShortcuts()
  34. def setCommonOptions(self):
  35. # Enable non-ASCII characters
  36. self.setUtf8(True)
  37. # Default font
  38. font = QFont()
  39. font.setFamily('Courier')
  40. font.setFixedPitch(True)
  41. font.setPointSize(10)
  42. self.setFont(font)
  43. self.setMarginsFont(font)
  44. self.setBraceMatching(QsciScintilla.SloppyBraceMatch)
  45. self.setWrapMode(QsciScintilla.WrapWord)
  46. self.setWrapVisualFlags(QsciScintilla.WrapFlagByText,
  47. QsciScintilla.WrapFlagNone, 4)
  48. self.setSelectionForegroundColor(QColor('#2e3436'))
  49. self.setSelectionBackgroundColor(QColor('#babdb6'))
  50. # Show line numbers
  51. self.setMarginWidth(1, '000')
  52. self.setMarginLineNumbers(1, True)
  53. self.setMarginsForegroundColor(QColor('#2e3436'))
  54. self.setMarginsBackgroundColor(QColor('#babdb6'))
  55. # Highlight current line
  56. self.setCaretLineVisible(True)
  57. self.setCaretLineBackgroundColor(QColor('#d3d7cf'))
  58. # Folding
  59. self.setFolding(QsciScintilla.BoxedTreeFoldStyle)
  60. self.setFoldMarginColors(QColor('#d3d7cf'), QColor('#d3d7cf'))
  61. # Mark column 80 with vertical line
  62. self.setEdgeMode(QsciScintilla.EdgeLine)
  63. self.setEdgeColumn(80)
  64. self.setEdgeColor(QColor('#eeeeec'))
  65. # Indentation
  66. self.setAutoIndent(True)
  67. self.setIndentationsUseTabs(False)
  68. self.setIndentationWidth(4)
  69. self.setTabIndents(True)
  70. self.setBackspaceUnindents(True)
  71. self.setTabWidth(4)
  72. # Autocomletion
  73. self.setAutoCompletionThreshold(2)
  74. self.setAutoCompletionSource(QsciScintilla.AcsAPIs)
  75. self.setAutoCompletionCaseSensitivity(False)
  76. # Load font from Python console settings
  77. settings = QgsSettings()
  78. fontName = settings.value('pythonConsole/fontfamilytext', 'Monospace')
  79. fontSize = int(settings.value('pythonConsole/fontsize', 10))
  80. self.defaultFont = QFont(fontName)
  81. self.defaultFont.setFixedPitch(True)
  82. self.defaultFont.setPointSize(fontSize)
  83. self.defaultFont.setStyleHint(QFont.TypeWriter)
  84. self.defaultFont.setBold(False)
  85. self.boldFont = QFont(self.defaultFont)
  86. self.boldFont.setBold(True)
  87. self.italicFont = QFont(self.defaultFont)
  88. self.italicFont.setItalic(True)
  89. self.setFont(self.defaultFont)
  90. self.setMarginsFont(self.defaultFont)
  91. self.initLexer()
  92. def initShortcuts(self):
  93. (ctrl, shift) = (self.SCMOD_CTRL << 16, self.SCMOD_SHIFT << 16)
  94. # Disable some shortcuts
  95. self.SendScintilla(QsciScintilla.SCI_CLEARCMDKEY, ord('D') + ctrl)
  96. self.SendScintilla(QsciScintilla.SCI_CLEARCMDKEY, ord('L') + ctrl)
  97. self.SendScintilla(QsciScintilla.SCI_CLEARCMDKEY, ord('L') + ctrl +
  98. shift)
  99. self.SendScintilla(QsciScintilla.SCI_CLEARCMDKEY, ord('T') + ctrl)
  100. # self.SendScintilla(QsciScintilla.SCI_CLEARCMDKEY, ord("Z") + ctrl)
  101. # self.SendScintilla(QsciScintilla.SCI_CLEARCMDKEY, ord("Y") + ctrl)
  102. # Use Ctrl+Space for autocompletion
  103. self.shortcutAutocomplete = QShortcut(QKeySequence(Qt.CTRL +
  104. Qt.Key_Space), self)
  105. self.shortcutAutocomplete.setContext(Qt.WidgetShortcut)
  106. self.shortcutAutocomplete.activated.connect(self.autoComplete)
  107. def autoComplete(self):
  108. self.autoCompleteFromAll()
  109. def initLexer(self):
  110. self.mylexer = QsciLexerSQL()
  111. colorDefault = QColor('#2e3436')
  112. colorComment = QColor('#c00')
  113. colorCommentBlock = QColor('#3465a4')
  114. colorNumber = QColor('#4e9a06')
  115. colorType = QColor('#4e9a06')
  116. colorKeyword = QColor('#204a87')
  117. colorString = QColor('#ce5c00')
  118. self.mylexer.setDefaultFont(self.defaultFont)
  119. self.mylexer.setDefaultColor(colorDefault)
  120. self.mylexer.setColor(colorComment, 1)
  121. self.mylexer.setColor(colorNumber, 2)
  122. self.mylexer.setColor(colorString, 3)
  123. self.mylexer.setColor(colorString, 4)
  124. self.mylexer.setColor(colorKeyword, 5)
  125. self.mylexer.setColor(colorString, 6)
  126. self.mylexer.setColor(colorString, 7)
  127. self.mylexer.setColor(colorType, 8)
  128. self.mylexer.setColor(colorCommentBlock, 12)
  129. self.mylexer.setColor(colorString, 15)
  130. self.mylexer.setFont(self.italicFont, 1)
  131. self.mylexer.setFont(self.boldFont, 5)
  132. self.mylexer.setFont(self.boldFont, 8)
  133. self.mylexer.setFont(self.italicFont, 12)
  134. self.setLexer(self.mylexer)
  135. def lexer(self):
  136. return self.mylexer