12path = os.path.dirname(os.path.abspath(__file__))
13sys.path.insert(0, os.path.join(path,
"rope.zip"))
16from rope.base.project
import get_no_project
17from rope.contrib.codeassist
import code_assist
21 from PyQt6.QtGui import (QBrush, QColor, QFont, QKeyEvent, QTextBlockUserData,
22 QTextCursor, QPainter, QPalette, QPen)
23 from PyQt6.QtWidgets import (QApplication, QFrame, QHBoxLayout, QMessageBox,
24 QPlainTextEdit, QVBoxLayout, QWidget)
27 from PyQt5.QtGui import (QBrush, QColor, QFont, QKeyEvent, QTextBlockUserData,
28 QTextCursor, QPainter, QPalette, QPen)
29 from PyQt5.QtWidgets import (QApplication, QFrame, QHBoxLayout, QMessageBox,
30 QPlainTextEdit, QVBoxLayout, QWidget)
32from indenter
import PythonCodeIndenter
33from assist
import AutoComplete, CallTip
35from highlighter
import PythonHighlighter, QtQmlHighlighter
41 QTextBlockUserData.__init__(self)
57 row = cursor.blockNumber()
60 block = cursor.block()
62 block = block.previous()
70 return unicode(self.
_get_block(line_no).text())
74 cursor = QTextCursor(block)
75 cursor.joinPreviousEditBlock()
76 cursor.movePosition(QTextCursor.MoveOperation.StartOfBlock, QTextCursor.MoveMode.MoveAnchor)
78 for i
in range(-indent_length):
81 cursor.insertText(
" " * indent_length)
84 QTextCursor.MoveOperation.StartOfBlock, QTextCursor.MoveMode.MoveAnchor)
85 line = unicode(cursor.block().text())
86 if len(line)
and line[0] ==
" ":
88 QTextCursor.MoveOperation.NextWord, QTextCursor.MoveMode.MoveAnchor)
96 EditorHighlighterClass=PythonHighlighter,
97 indenter=PythonCodeIndenter):
98 QPlainTextEdit.__init__(self, parent)
99 self.setFrameStyle(QFrame.Shape.NoFrame)
100 self.setTabStopWidth(4)
101 self.setLineWrapMode(QPlainTextEdit.LineWrapMode.NoWrap)
103 font.setFamily(
"lucidasanstypewriter")
104 font.setFixedPitch(
True)
105 font.setPointSize(10)
109 self.setPlainText(text)
124 self.
prj = get_no_project()
140 return self.document().blockCount()
143 cursor = self.textCursor()
144 block = cursor.block()
145 row = cursor.blockNumber()
147 block = block.previous()
152 cursor = QTextCursor(block)
153 self.setTextCursor(cursor)
156 cursor = self.textCursor()
157 cursor.setPosition(0)
158 self.setTextCursor(cursor)
161 cursor = self.textCursor()
162 block = cursor.block()
163 while block.isValid():
166 cursor.setPosition(last_block.position())
168 QTextCursor.MoveOperation.EndOfBlock, QTextCursor.MoveMode.MoveAnchor)
169 self.setTextCursor(cursor)
172 cursor = self.textCursor()
174 QTextCursor.MoveOperation.StartOfBlock, QTextCursor.MoveMode.MoveAnchor)
175 self.setTextCursor(cursor)
178 cursor = self.textCursor()
180 QTextCursor.MoveOperation.EndOfBlock, QTextCursor.MoveMode.MoveAnchor)
181 self.setTextCursor(cursor)
184 self.viewport().update()
187 cursor = self.textCursor()
188 row, col = cursor.blockNumber(), cursor.columnNumber()
195 self.cursorPositionChanged.emit(row, col)
199 self.
line = QLine(x, 0, x, self.height())
203 QPlainTextEdit.resizeEvent(self, event)
206 painter = QPainter(self.viewport())
208 r = self.cursorRect()
210 r.setWidth(self.viewport().width())
214 painter.drawLine(self.
line)
216 QPlainTextEdit.paintEvent(self, event)
219 QPlainTextEdit.setDocument(self, document)
223 self.
indenter.correct_indentation(self.textCursor().blockNumber())
229 self.
indenter.deindent(self.textCursor().blockNumber())
236 cursor = self.textCursor()
237 text = unicode(cursor.block().text())
238 col = cursor.columnNumber()
239 if col > 0
and text[:col].strip() ==
"":
240 self.
indenter.deindent(self.textCursor().blockNumber())
245 items = code_assist(self.
prj,
246 unicode(self.toPlainText()),
247 self.textCursor().position())
248 except Exception
as e:
255 self.
indenter.entering_new_line(self.textCursor().blockNumber())
265 m = event.modifiers()
270 m & Qt.KeyboardModifier.ControlModifier
and k
in [Qt.Key.Key_A, Qt.Key.Key_R,
271 Qt.Key.Key_C, Qt.Key.Key_K,
272 Qt.Key.Key_X, Qt.Key.Key_V,
273 Qt.Key.Key_Y, Qt.Key.Key_Z]:
274 new_ev = QKeyEvent(event.type(), k, m, t)
276 QCoreApplication.postEvent(self.parent(), new_ev)
278 elif k == Qt.Key.Key_Tab:
281 elif k == Qt.Key.Key_Backtab:
284 elif k == Qt.Key.Key_Backspace:
287 elif k == Qt.Key.Key_Period
or \
288 (k == Qt.Key.Key_Space
and event.modifiers() == Qt.KeyboardModifier.ControlModifier):
289 QPlainTextEdit.keyPressEvent(self, event)
292 elif k
in [Qt.Key.Key_ParenLeft, Qt.Key.Key_BraceLeft, Qt.Key.Key_BracketLeft]:
293 QPlainTextEdit.keyPressEvent(self, event)
296 QPlainTextEdit.keyPressEvent(self, event)
297 if k == Qt.Key.Key_Return
or k == Qt.Key.Key_Enter:
302 Qt.Key.Key_ParenLeft:
")",
303 Qt.Key.Key_BraceLeft:
" }",
304 Qt.Key.Key_BracketLeft:
"]"
306 cursor = self.textCursor()
307 cursor.insertText(close_char[key])
308 cursor.setPosition(cursor.position() - 1)
309 self.setTextCursor(cursor)
315 QWidget.__init__(self, editor)
318 self.
doc = editor.view.document
319 self.
fm = self.fontMetrics()
322 self.setAutoFillBackground(
True)
327 self.setBackgroundRole(QPalette.ColorRole.Base)
330 self.
view.verticalScrollBar().valueChanged.connect(self.
update)
334 width += self.
fm.width(
"00000")
335 self.setFixedWidth(width)
338 QWidget.paintEvent(self, event)
341 first = view.firstVisibleBlock()
342 first_row = first.blockNumber()
345 y = view.contentOffset().y()
348 view.verticalScrollBar().
value() + view.viewport().height())
351 while block.isValid():
352 txt = str(row).rjust(5)
353 y = view.blockBoundingGeometry(block).y()
356 x = w - fm.width(txt)
357 p.drawText(x, y, txt)
366 EditorSidebarClass=EditorSidebar,
367 EditorViewClass=EditorView):
368 QFrame.__init__(self, parent)
369 self.
view = EditorViewClass(self, text)
371 self.setFrameStyle(QFrame.Shape.StyledPanel | QFrame.Shadow.Sunken)
380 self.
vlayout.setContentsMargins(2, 2, 2, 2)
400class QtQmlEditorWidget(QPlainTextEdit):
403 QPlainTextEdit.__init__(self, parent)
410 QMessageBox.__init__(self)
411 self.setWindowTitle(
"Save")
413 self.setStandardButtons(QMessageBox.StandardButton.Save | QMessageBox.StandardButton.Discard | QMessageBox.StandardButton.Cancel)
414 self.setDefaultButton(QMessageBox.StandardButton.Save)
417if __name__ ==
"__main__":
418 if __file__ ==
"<stdin>":
419 __file__ =
"./widget.py"
421 app = QApplication(sys.argv)
422 src = open(__file__).read()
424 edit.resize(640, 480)
float value(const T *src, size_t ch)
connect(this, SIGNAL(optionsChanged()), this, SLOT(saveOptions()))