Skip to content

Commit 81067d6

Browse files
committed
just about working under pyside6
1 parent d993806 commit 81067d6

File tree

5 files changed

+104
-20
lines changed

5 files changed

+104
-20
lines changed

PythonEditor/ui/editor.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -399,10 +399,12 @@ def wheelEvent(self, event):
399399
"""Restore focus and, if ctrl held, emit signal
400400
"""
401401
self.setFocus(Qt.MouseFocusReason)
402-
vertical = Qt.Orientation.Vertical
403-
is_vertical = (
404-
event.orientation() == vertical
405-
)
402+
# Qt6 removed orientation() - use angleDelta() instead
403+
if hasattr(event, 'angleDelta'):
404+
is_vertical = event.angleDelta().y() != 0
405+
else:
406+
# Qt5 fallback
407+
is_vertical = event.orientation() == Qt.Orientation.Vertical
406408
CTRL = Qt.ControlModifier
407409
ctrl_held = (event.modifiers() == CTRL)
408410
if ctrl_held and is_vertical:

PythonEditor/ui/features/actions.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1731,7 +1731,12 @@ def wheel_zoom(self, event):
17311731
"""
17321732
font = self.editor.font()
17331733
size = font.pointSize()
1734-
d = event.delta()
1734+
# Qt6 removed delta() - use angleDelta() instead
1735+
if hasattr(event, 'angleDelta'):
1736+
d = event.angleDelta().y()
1737+
else:
1738+
# Qt5 fallback
1739+
d = event.delta()
17351740
amount = int(d/10) if d > 1 or d < -1 else d
17361741
new_size = size + amount
17371742
new_size = new_size if new_size > 0 else 1

PythonEditor/ui/features/shortcuts.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,11 @@ def eventFilter(self, obj, event):
112112
# event.Destroy
113113
return False
114114

115-
if event.type() in [event.Shortcut, event.ShortcutOverride]: # accept all shortcuts
115+
if event.type() in [QEvent.Shortcut, QEvent.ShortcutOverride]:
116116
event.accept()
117117
return True
118118

119-
if event.type() == event.KeyPress:
119+
if event.type() == QEvent.KeyPress:
120120
# only let the editor receive keypress overrides
121121
if obj == self.editor:
122122
return self.handle_keypress(event)

PythonEditor/ui/features/syntaxhighlighter.py

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,35 @@ def format(self, rgb, style=''):
197197

198198
return textFormat
199199

200+
def _iter_rule_matches(self, expression, text, nth):
201+
if hasattr(expression, "indexIn"):
202+
index = expression.indexIn(text, 0)
203+
while index >= 0:
204+
start = expression.pos(nth)
205+
length = len(expression.cap(nth))
206+
if start >= 0 and length > 0:
207+
yield start, length
208+
index = expression.indexIn(text, start + max(length, 1))
209+
else:
210+
matches = expression.globalMatch(text)
211+
while matches.hasNext():
212+
match = matches.next()
213+
start = match.capturedStart(nth)
214+
length = match.capturedLength(nth)
215+
if start >= 0 and length > 0:
216+
yield start, length
217+
218+
def _regex_find(self, expression, text, start=0):
219+
if hasattr(expression, "indexIn"):
220+
index = expression.indexIn(text, start)
221+
if index >= 0:
222+
return index, expression.matchedLength()
223+
return -1, 0
224+
match = expression.match(text, start)
225+
if not match.hasMatch():
226+
return -1, 0
227+
return match.capturedStart(0), match.capturedLength(0)
228+
200229
def highlightBlock(self, text):
201230
"""
202231
Apply syntax highlighting to the given block of text.
@@ -207,14 +236,12 @@ def highlightBlock(self, text):
207236

208237
# apply rules
209238
for expression, nth, format in self.rules:
210-
index = expression.indexIn(text, 0)
211-
212-
while index >= 0:
213-
# We actually want the index of the nth match
214-
index = expression.pos(nth)
215-
length = len(expression.cap(nth))
239+
for index, length in self._iter_rule_matches(
240+
expression,
241+
text,
242+
nth
243+
):
216244
self.setFormat(index, length, format)
217-
index = expression.indexIn(text, index + length)
218245

219246
# apply comment rule
220247
if '#' in text:
@@ -295,17 +322,19 @@ def match_multiline(self, text, delimiter, in_state, style):
295322
add = 0
296323
# Otherwise, look for the delimiter on this line
297324
else:
298-
start = delimiter.indexIn(text)
299-
# Move past this match
300-
add = delimiter.matchedLength()
325+
start, add = self._regex_find(delimiter, text, 0)
301326

302327
# As long as there's a delimiter match on this line...
303328
while start >= 0:
304329
# Look for the ending delimiter
305-
end = delimiter.indexIn(text, start + add)
330+
end, end_length = self._regex_find(
331+
delimiter,
332+
text,
333+
start + add
334+
)
306335
# Ending delimiter on this line?
307336
if end >= add:
308-
length = end - start + add + delimiter.matchedLength()
337+
length = end - start + add + end_length
309338
self.setCurrentBlockState(0)
310339
# No; multi-line string
311340
else:
@@ -314,7 +343,11 @@ def match_multiline(self, text, delimiter, in_state, style):
314343
# Apply formatting
315344
self.setFormat(start, length, style)
316345
# Look for the next match
317-
start = delimiter.indexIn(text, start + length)
346+
start, add = self._regex_find(
347+
delimiter,
348+
text,
349+
start + length
350+
)
318351

319352
# Return True if still inside a multi-line string
320353
return self.currentBlockState() == in_state

tests/test_qt_binding.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,50 @@ def test_ide_smoke_event_loop():
7272
app.quit()
7373

7474

75+
@pytest.mark.gui
76+
def test_launch_steps_subprocess_syntax_highlighter_regexp():
77+
created_app = False
78+
app = QtWidgets.QApplication.instance()
79+
if app is None:
80+
app = QtWidgets.QApplication(sys.argv)
81+
created_app = True
82+
83+
from PythonEditor.ui import editor as editor_module
84+
from PythonEditor.ui.features import syntaxhighlighter
85+
86+
editor = editor_module.Editor(init_features=False)
87+
highlighter = syntaxhighlighter.Highlight(editor.document(), editor)
88+
editor.setPlainText("class Foo:\n def bar(self):\n return 1\n")
89+
highlighter.rehighlight()
90+
91+
editor.close()
92+
QtWidgets.QApplication.processEvents()
93+
if created_app:
94+
app.quit()
95+
96+
97+
@pytest.mark.gui
98+
def test_launch_steps_subprocess_shortcut_event_filter():
99+
created_app = False
100+
app = QtWidgets.QApplication.instance()
101+
if app is None:
102+
app = QtWidgets.QApplication(sys.argv)
103+
created_app = True
104+
105+
from PythonEditor.ui import editor as editor_module
106+
from PythonEditor.ui.features import shortcuts
107+
108+
editor = editor_module.Editor(init_features=False)
109+
handler = shortcuts.ShortcutHandler(editor=editor)
110+
paint_event = QtGui.QPaintEvent(QtCore.QRect(0, 0, 1, 1))
111+
assert handler.eventFilter(editor, paint_event) is False
112+
113+
editor.close()
114+
QtWidgets.QApplication.processEvents()
115+
if created_app:
116+
app.quit()
117+
118+
75119
@pytest.mark.gui
76120
def test_launch_steps_subprocess():
77121
import subprocess

0 commit comments

Comments
 (0)