Skip to content

Commit 2cba77e

Browse files
move to Pyside6 (leixingyu#12)
* pyside 6 support, fix crash ui files * change requirements to pyside6 * cleanup * Update README.md
1 parent e64b11d commit 2cba77e

File tree

14 files changed

+333
-65
lines changed

14 files changed

+333
-65
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ Hence the creation of this tool!
1919

2020
The tool needs the following library to be installed:
2121

22-
- Qt for Python: [PySide2](https://pypi.org/project/PySide2/) or [PyQt5](https://pypi.org/project/PyQt5/)
23-
- Python shim for all Qt bindings: [Qt.py](https://pypi.org/project/Qt.py/)
22+
- v0.0.2 uses [Qt.py](https://pypi.org/project/Qt.py/), and [PySide2](https://pypi.org/project/PySide2/) or [PyQt5](https://pypi.org/project/PyQt5/)
23+
- v0.0.3+ uses PySide6
2424

2525

2626
### Add as Menu Button

pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ classifiers = [
2222
"Programming Language :: Python :: 3.7",
2323
]
2424
#dynamic = ["dependencies"]
25-
dependencies = ['importlib-metadata; python_version<"3.7"', "Qt.py", "unreal-stylesheet"]
25+
dependencies = ['importlib-metadata; python_version<"3.7"', "unreal-stylesheet", "PySide6"]
2626
#dynamic = ["version"]
27-
version = "0.0.2"
27+
version = "0.0.3"
2828

2929
#[project.optional-dependencies]
3030
#yaml = ["pyyaml"]

requirements.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
Qt.py
21
unreal-stylesheet

unreal_script_editor/codeEditor/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ python qt bindings
3131

3232
or alternatively, change the code below to whatever qt binding you have on your machine.
3333
```python
34-
from Qt import QtWidgets, QtCore, QtGui
35-
from Qt import _loadUi
34+
from PySide6 import QtWidgets, QtCore, QtGui
35+
from PySide6 import _loadUi
3636
```
3737

3838
### Launch

unreal_script_editor/codeEditor/codeEditor.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
https://doc.qt.io/qtforpython/examples/example_widgets__codeeditor.html
44
"""
55

6-
from Qt import QtCore, QtGui, QtWidgets
6+
from PySide6 import QtCore, QtGui, QtWidgets
77

88

99
class LineNumberArea(QtWidgets.QWidget):
@@ -176,7 +176,7 @@ def __init__(self):
176176
self.setFont(self.font)
177177

178178
self.tab_size = 4
179-
self.setTabStopWidth(self.tab_size * self.fontMetrics().width(' '))
179+
self.setTabStopDistance(self.tab_size * self.fontMetrics().horizontalAdvance(' '))
180180

181181
self.blockCountChanged.connect(self.update_line_number_area_width)
182182
self.updateRequest.connect(self.update_line_number_area)
@@ -190,7 +190,7 @@ def line_number_area_width(self):
190190
max_num *= 0.1
191191
digits += 1
192192

193-
space = 20 + self.fontMetrics().width('9') * digits
193+
space = 20 + self.fontMetrics().horizontalAdvance('9') * digits
194194
return space
195195

196196
def resizeEvent(self, e):

unreal_script_editor/codeEditor/highlighter/jsonHighlight.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from Qt import QtWidgets, QtCore, QtGui
1+
from PySide6 import QtWidgets, QtCore, QtGui
22

33

44
class HighlightRule(object):

unreal_script_editor/codeEditor/highlighter/pyHighlight.py

Lines changed: 53 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
"""
44

55

6-
from Qt import QtCore, QtGui, QtWidgets
6+
from PySide6 import QtGui
7+
from PySide6.QtCore import QRegularExpression
78

89

910
def format(color, style=''):
@@ -70,8 +71,9 @@ def __init__(self, parent=None):
7071
super(PythonHighlighter, self).__init__(parent)
7172

7273
# Multi-line strings (expression, flag, style)
73-
self.tri_single = (QtCore.QRegExp("'''"), 1, STYLES['string2'])
74-
self.tri_double = (QtCore.QRegExp('"""'), 2, STYLES['string2'])
74+
75+
self.tri_single = (QRegularExpression("'''"), 1, STYLES['string2'])
76+
self.tri_double = (QRegularExpression('"""'), 2, STYLES['string2'])
7577

7678
rules = []
7779

@@ -108,7 +110,7 @@ def __init__(self, parent=None):
108110
]
109111

110112
# Build a QRegExp for each pattern
111-
self.rules = [(QtCore.QRegExp(pat), index, fmt)
113+
self.rules = [(QRegularExpression(pat), index, fmt)
112114
for (pat, index, fmt) in rules]
113115

114116
def highlightBlock(self, text):
@@ -118,32 +120,41 @@ def highlightBlock(self, text):
118120
self.tripleQuoutesWithinStrings = []
119121
# Do other syntax formatting
120122
for expression, nth, format in self.rules:
121-
index = expression.indexIn(text, 0)
122-
if index >= 0:
123-
# if there is a string we check
124-
# if there are some triple quotes within the string
125-
# they will be ignored if they are matched again
126-
if expression.pattern() in [r'"[^"\\]*(\\.[^"\\]*)*"', r"'[^'\\]*(\\.[^'\\]*)*'"]:
127-
innerIndex = self.tri_single[0].indexIn(text, index + 1)
128-
if innerIndex == -1:
129-
innerIndex = self.tri_double[0].indexIn(text, index + 1)
130-
131-
if innerIndex != -1:
132-
tripleQuoteIndexes = range(innerIndex, innerIndex + 3)
133-
self.tripleQuoutesWithinStrings.extend(tripleQuoteIndexes)
134-
135-
while index >= 0:
136-
# skipping triple quotes within strings
137-
if index in self.tripleQuoutesWithinStrings:
138-
index += 1
139-
expression.indexIn(text, index)
140-
continue
141-
142-
# We actually want the index of the nth match
143-
index = expression.pos(nth)
144-
length = len(expression.cap(nth))
145-
self.setFormat(index, length, format)
146-
index = expression.indexIn(text, index + length)
123+
match = expression.match(text, 0)
124+
while match.hasMatch():
125+
index = match.capturedStart(nth)
126+
if index >= 0:
127+
# if there is a string we check
128+
# if there are some triple quotes within the string
129+
# they will be ignored if they are matched again
130+
if expression.pattern() in [r'"[^"\\]*(\\.[^"\\]*)*"', r"'[^'\\]*(\\.[^'\\]*)*'"]:
131+
inner_match = self.tri_single[0].match(text, index + 1)
132+
innerIndex = inner_match.capturedStart() if inner_match.hasMatch() else -1
133+
if innerIndex == -1:
134+
inner_match = self.tri_double[0].match(text, index + 1)
135+
innerIndex = inner_match.capturedStart() if inner_match.hasMatch() else -1
136+
137+
if innerIndex != -1:
138+
tripleQuoteIndexes = range(innerIndex, innerIndex + 3)
139+
self.tripleQuoutesWithinStrings.extend(tripleQuoteIndexes)
140+
141+
while index >= 0:
142+
# skipping triple quotes within strings
143+
if index in self.tripleQuoutesWithinStrings:
144+
index += 1
145+
match = expression.match(text, index)
146+
if match.hasMatch():
147+
index = match.capturedStart(nth)
148+
continue
149+
150+
# We actually want the index of the nth match
151+
length = match.capturedLength(nth)
152+
self.setFormat(index, length, format)
153+
match = expression.match(text, index + length)
154+
if match.hasMatch():
155+
index = match.capturedStart(nth)
156+
else:
157+
index = -1
147158

148159
self.setCurrentBlockState(0)
149160

@@ -155,7 +166,7 @@ def highlightBlock(self, text):
155166
def match_multiline(self, text, delimiter, in_state, style):
156167
"""
157168
Do highlighting of multi-line strings. ``delimiter`` should be a
158-
``QRegExp`` for triple-single-quotes or triple-double-quotes, and
169+
``QRegularExpression`` for triple-single-quotes or triple-double-quotes, and
159170
``in_state`` should be a unique integer to represent the corresponding
160171
state changes when inside those strings. Returns True if we're still
161172
inside a multi-line string when this function is finished.
@@ -166,20 +177,23 @@ def match_multiline(self, text, delimiter, in_state, style):
166177
add = 0
167178
# Otherwise, look for the delimiter on this line
168179
else:
169-
start = delimiter.indexIn(text)
180+
match = delimiter.match(text)
181+
start = match.capturedStart()
182+
170183
# skipping triple quotes within strings
171184
if start in self.tripleQuoutesWithinStrings:
172185
return False
173186
# Move past this match
174-
add = delimiter.matchedLength()
187+
add = match.capturedLength()
175188

176189
# As long as there's a delimiter match on this line...
177190
while start >= 0:
191+
match = delimiter.match(text, start + add)
192+
end = match.capturedStart()
193+
178194
# Look for the ending delimiter
179-
end = delimiter.indexIn(text, start + add)
180-
# Ending delimiter on this line?
181195
if end >= add:
182-
length = end - start + add + delimiter.matchedLength()
196+
length = end - start + add + match.capturedLength()
183197
self.setCurrentBlockState(0)
184198
# No; multi-line string
185199
else:
@@ -188,10 +202,8 @@ def match_multiline(self, text, delimiter, in_state, style):
188202
# Apply formatting
189203
self.setFormat(start, length, style)
190204
# Look for the next match
191-
start = delimiter.indexIn(text, start + length)
205+
match = delimiter.match(text, start + length)
206+
start = match.capturedStart() if match.hasMatch() else -1
192207

193208
# Return True if still inside a multi-line string, False otherwise
194-
if self.currentBlockState() == in_state:
195-
return True
196-
else:
197-
return False
209+
return self.currentBlockState() == in_state

unreal_script_editor/codeEditor/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import sys
22

3-
from Qt import QtWidgets
3+
from PySide6 import QtWidgets
44

55
import codeEditor
66
from highlighter.pyHighlight import PythonHighlighter

unreal_script_editor/config.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[{'index': 0, 'label': 'Python', 'active': True, 'command': 'print("hello")'}]

unreal_script_editor/main.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@
1515
except ImportError:
1616
RUNNING_IN_UNREAL = False
1717

18-
from Qt import QtWidgets, QtCore, QtGui
19-
from Qt import _loadUi
18+
from PySide6 import QtWidgets, QtCore, QtGui
2019

21-
from . import outputTextWidget
22-
from .codeEditor import codeEditor
23-
from .codeEditor.highlighter import pyHighlight
20+
from unreal_script_editor import outputTextWidget
21+
from unreal_script_editor.codeEditor import codeEditor
22+
from unreal_script_editor.codeEditor.highlighter import pyHighlight
23+
24+
from unreal_script_editor.ui.script_editor import Ui_MainWindow
2425

2526

2627
LOGGER = logging.getLogger(__name__)
@@ -50,7 +51,7 @@ class TabConfig(namedtuple('TabConfig', ['index', 'label', 'active', 'command'])
5051
__slots__ = ()
5152

5253

53-
class ScriptEditorWindow(QtWidgets.QMainWindow):
54+
class ScriptEditorWindow(QtWidgets.QMainWindow, Ui_MainWindow):
5455
"""
5556
Script Editor main window
5657
"""
@@ -60,10 +61,11 @@ def __init__(self, parent=None):
6061
Initialization
6162
"""
6263
super(ScriptEditorWindow, self).__init__(parent)
63-
_loadUi(UI_PATH, self)
64+
self.setupUi(self) # Set up the UI
65+
6466
splitter = QtWidgets.QSplitter()
6567
splitter.setOrientation(QtCore.Qt.Vertical)
66-
self.centralwidget.layout().addWidget(splitter)
68+
self.centralWidget().layout().addWidget(splitter)
6769
self.ui_log_edit = outputTextWidget.OutputTextWidget()
6870
splitter.addWidget(self.ui_log_edit)
6971
splitter.addWidget(self.ui_tab_widget)
@@ -357,3 +359,9 @@ def show():
357359
unreal.parent_external_window_to_slate(int(WINDOW.winId()))
358360

359361
return WINDOW
362+
363+
364+
if __name__ == "__main__":
365+
APP = QtWidgets.QApplication.instance()
366+
w = show()
367+
APP.exec()

0 commit comments

Comments
 (0)