Skip to content

Commit

Permalink
move to Pyside6 (leixingyu#12)
Browse files Browse the repository at this point in the history
* pyside 6 support, fix crash ui files

* change requirements to pyside6

* cleanup

* Update README.md
  • Loading branch information
hannesdelbeke authored Oct 6, 2024
1 parent e64b11d commit 2cba77e
Show file tree
Hide file tree
Showing 14 changed files with 333 additions and 65 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ Hence the creation of this tool!

The tool needs the following library to be installed:

- Qt for Python: [PySide2](https://pypi.org/project/PySide2/) or [PyQt5](https://pypi.org/project/PyQt5/)
- Python shim for all Qt bindings: [Qt.py](https://pypi.org/project/Qt.py/)
- 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/)
- v0.0.3+ uses PySide6


### Add as Menu Button
Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ classifiers = [
"Programming Language :: Python :: 3.7",
]
#dynamic = ["dependencies"]
dependencies = ['importlib-metadata; python_version<"3.7"', "Qt.py", "unreal-stylesheet"]
dependencies = ['importlib-metadata; python_version<"3.7"', "unreal-stylesheet", "PySide6"]
#dynamic = ["version"]
version = "0.0.2"
version = "0.0.3"

#[project.optional-dependencies]
#yaml = ["pyyaml"]
Expand Down
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
Qt.py
unreal-stylesheet
4 changes: 2 additions & 2 deletions unreal_script_editor/codeEditor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ python qt bindings

or alternatively, change the code below to whatever qt binding you have on your machine.
```python
from Qt import QtWidgets, QtCore, QtGui
from Qt import _loadUi
from PySide6 import QtWidgets, QtCore, QtGui
from PySide6 import _loadUi
```

### Launch
Expand Down
6 changes: 3 additions & 3 deletions unreal_script_editor/codeEditor/codeEditor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
https://doc.qt.io/qtforpython/examples/example_widgets__codeeditor.html
"""

from Qt import QtCore, QtGui, QtWidgets
from PySide6 import QtCore, QtGui, QtWidgets


class LineNumberArea(QtWidgets.QWidget):
Expand Down Expand Up @@ -176,7 +176,7 @@ def __init__(self):
self.setFont(self.font)

self.tab_size = 4
self.setTabStopWidth(self.tab_size * self.fontMetrics().width(' '))
self.setTabStopDistance(self.tab_size * self.fontMetrics().horizontalAdvance(' '))

self.blockCountChanged.connect(self.update_line_number_area_width)
self.updateRequest.connect(self.update_line_number_area)
Expand All @@ -190,7 +190,7 @@ def line_number_area_width(self):
max_num *= 0.1
digits += 1

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

def resizeEvent(self, e):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from Qt import QtWidgets, QtCore, QtGui
from PySide6 import QtWidgets, QtCore, QtGui


class HighlightRule(object):
Expand Down
94 changes: 53 additions & 41 deletions unreal_script_editor/codeEditor/highlighter/pyHighlight.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"""


from Qt import QtCore, QtGui, QtWidgets
from PySide6 import QtGui
from PySide6.QtCore import QRegularExpression


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

# Multi-line strings (expression, flag, style)
self.tri_single = (QtCore.QRegExp("'''"), 1, STYLES['string2'])
self.tri_double = (QtCore.QRegExp('"""'), 2, STYLES['string2'])

self.tri_single = (QRegularExpression("'''"), 1, STYLES['string2'])
self.tri_double = (QRegularExpression('"""'), 2, STYLES['string2'])

rules = []

Expand Down Expand Up @@ -108,7 +110,7 @@ def __init__(self, parent=None):
]

# Build a QRegExp for each pattern
self.rules = [(QtCore.QRegExp(pat), index, fmt)
self.rules = [(QRegularExpression(pat), index, fmt)
for (pat, index, fmt) in rules]

def highlightBlock(self, text):
Expand All @@ -118,32 +120,41 @@ def highlightBlock(self, text):
self.tripleQuoutesWithinStrings = []
# Do other syntax formatting
for expression, nth, format in self.rules:
index = expression.indexIn(text, 0)
if index >= 0:
# if there is a string we check
# if there are some triple quotes within the string
# they will be ignored if they are matched again
if expression.pattern() in [r'"[^"\\]*(\\.[^"\\]*)*"', r"'[^'\\]*(\\.[^'\\]*)*'"]:
innerIndex = self.tri_single[0].indexIn(text, index + 1)
if innerIndex == -1:
innerIndex = self.tri_double[0].indexIn(text, index + 1)

if innerIndex != -1:
tripleQuoteIndexes = range(innerIndex, innerIndex + 3)
self.tripleQuoutesWithinStrings.extend(tripleQuoteIndexes)

while index >= 0:
# skipping triple quotes within strings
if index in self.tripleQuoutesWithinStrings:
index += 1
expression.indexIn(text, index)
continue

# We actually want the index of the nth match
index = expression.pos(nth)
length = len(expression.cap(nth))
self.setFormat(index, length, format)
index = expression.indexIn(text, index + length)
match = expression.match(text, 0)
while match.hasMatch():
index = match.capturedStart(nth)
if index >= 0:
# if there is a string we check
# if there are some triple quotes within the string
# they will be ignored if they are matched again
if expression.pattern() in [r'"[^"\\]*(\\.[^"\\]*)*"', r"'[^'\\]*(\\.[^'\\]*)*'"]:
inner_match = self.tri_single[0].match(text, index + 1)
innerIndex = inner_match.capturedStart() if inner_match.hasMatch() else -1
if innerIndex == -1:
inner_match = self.tri_double[0].match(text, index + 1)
innerIndex = inner_match.capturedStart() if inner_match.hasMatch() else -1

if innerIndex != -1:
tripleQuoteIndexes = range(innerIndex, innerIndex + 3)
self.tripleQuoutesWithinStrings.extend(tripleQuoteIndexes)

while index >= 0:
# skipping triple quotes within strings
if index in self.tripleQuoutesWithinStrings:
index += 1
match = expression.match(text, index)
if match.hasMatch():
index = match.capturedStart(nth)
continue

# We actually want the index of the nth match
length = match.capturedLength(nth)
self.setFormat(index, length, format)
match = expression.match(text, index + length)
if match.hasMatch():
index = match.capturedStart(nth)
else:
index = -1

self.setCurrentBlockState(0)

Expand All @@ -155,7 +166,7 @@ def highlightBlock(self, text):
def match_multiline(self, text, delimiter, in_state, style):
"""
Do highlighting of multi-line strings. ``delimiter`` should be a
``QRegExp`` for triple-single-quotes or triple-double-quotes, and
``QRegularExpression`` for triple-single-quotes or triple-double-quotes, and
``in_state`` should be a unique integer to represent the corresponding
state changes when inside those strings. Returns True if we're still
inside a multi-line string when this function is finished.
Expand All @@ -166,20 +177,23 @@ def match_multiline(self, text, delimiter, in_state, style):
add = 0
# Otherwise, look for the delimiter on this line
else:
start = delimiter.indexIn(text)
match = delimiter.match(text)
start = match.capturedStart()

# skipping triple quotes within strings
if start in self.tripleQuoutesWithinStrings:
return False
# Move past this match
add = delimiter.matchedLength()
add = match.capturedLength()

# As long as there's a delimiter match on this line...
while start >= 0:
match = delimiter.match(text, start + add)
end = match.capturedStart()

# Look for the ending delimiter
end = delimiter.indexIn(text, start + add)
# Ending delimiter on this line?
if end >= add:
length = end - start + add + delimiter.matchedLength()
length = end - start + add + match.capturedLength()
self.setCurrentBlockState(0)
# No; multi-line string
else:
Expand All @@ -188,10 +202,8 @@ def match_multiline(self, text, delimiter, in_state, style):
# Apply formatting
self.setFormat(start, length, style)
# Look for the next match
start = delimiter.indexIn(text, start + length)
match = delimiter.match(text, start + length)
start = match.capturedStart() if match.hasMatch() else -1

# Return True if still inside a multi-line string, False otherwise
if self.currentBlockState() == in_state:
return True
else:
return False
return self.currentBlockState() == in_state
2 changes: 1 addition & 1 deletion unreal_script_editor/codeEditor/main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import sys

from Qt import QtWidgets
from PySide6 import QtWidgets

import codeEditor
from highlighter.pyHighlight import PythonHighlighter
Expand Down
1 change: 1 addition & 0 deletions unreal_script_editor/config.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{'index': 0, 'label': 'Python', 'active': True, 'command': 'print("hello")'}]
24 changes: 16 additions & 8 deletions unreal_script_editor/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@
except ImportError:
RUNNING_IN_UNREAL = False

from Qt import QtWidgets, QtCore, QtGui
from Qt import _loadUi
from PySide6 import QtWidgets, QtCore, QtGui

from . import outputTextWidget
from .codeEditor import codeEditor
from .codeEditor.highlighter import pyHighlight
from unreal_script_editor import outputTextWidget
from unreal_script_editor.codeEditor import codeEditor
from unreal_script_editor.codeEditor.highlighter import pyHighlight

from unreal_script_editor.ui.script_editor import Ui_MainWindow


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


class ScriptEditorWindow(QtWidgets.QMainWindow):
class ScriptEditorWindow(QtWidgets.QMainWindow, Ui_MainWindow):
"""
Script Editor main window
"""
Expand All @@ -60,10 +61,11 @@ def __init__(self, parent=None):
Initialization
"""
super(ScriptEditorWindow, self).__init__(parent)
_loadUi(UI_PATH, self)
self.setupUi(self) # Set up the UI

splitter = QtWidgets.QSplitter()
splitter.setOrientation(QtCore.Qt.Vertical)
self.centralwidget.layout().addWidget(splitter)
self.centralWidget().layout().addWidget(splitter)
self.ui_log_edit = outputTextWidget.OutputTextWidget()
splitter.addWidget(self.ui_log_edit)
splitter.addWidget(self.ui_tab_widget)
Expand Down Expand Up @@ -357,3 +359,9 @@ def show():
unreal.parent_external_window_to_slate(int(WINDOW.winId()))

return WINDOW


if __name__ == "__main__":
APP = QtWidgets.QApplication.instance()
w = show()
APP.exec()
8 changes: 4 additions & 4 deletions unreal_script_editor/outputTextWidget.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

import os

from Qt import QtWidgets, QtCore, QtGui
from Qt import _loadUi
from PySide6 import QtWidgets, QtCore, QtGui
from unreal_script_editor.ui.output_text_widget import Ui_Form


MODULE_PATH = os.path.dirname(os.path.abspath(__file__))
Expand All @@ -26,7 +26,7 @@
REGULAR_FORMAT.setForeground(QtGui.QBrush(QtGui.QColor(200, 200, 200)))


class OutputTextWidget(QtWidgets.QWidget):
class OutputTextWidget(QtWidgets.QWidget, Ui_Form):
"""
Text Widget to display output information from Unreal command execution
"""
Expand All @@ -36,7 +36,7 @@ def __init__(self, parent=None):
Initialization
"""
super(OutputTextWidget, self).__init__(parent)
_loadUi(UI_PATH, self)
self.setupUi(self) # Set up the UI

def clear(self):
"""
Expand Down
Empty file.
51 changes: 51 additions & 0 deletions unreal_script_editor/ui/output_text_widget.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# -*- coding: utf-8 -*-

################################################################################
## Form generated from reading UI file 'output_text_widget.ui'
##
## Created by: Qt User Interface Compiler version 6.7.1
##
## WARNING! All changes made in this file will be lost when recompiling UI file!
################################################################################

from PySide6.QtCore import (QCoreApplication, QDate, QDateTime, QLocale,
QMetaObject, QObject, QPoint, QRect,
QSize, QTime, QUrl, Qt)
from PySide6.QtGui import (QBrush, QColor, QConicalGradient, QCursor,
QFont, QFontDatabase, QGradient, QIcon,
QImage, QKeySequence, QLinearGradient, QPainter,
QPalette, QPixmap, QRadialGradient, QTransform)
from PySide6.QtWidgets import (QApplication, QSizePolicy, QTextEdit, QVBoxLayout,
QWidget)

class Ui_Form(object):
def setupUi(self, Form):
if not Form.objectName():
Form.setObjectName(u"Form")
Form.resize(544, 274)
self.verticalLayout = QVBoxLayout(Form)
self.verticalLayout.setSpacing(0)
self.verticalLayout.setObjectName(u"verticalLayout")
self.verticalLayout.setContentsMargins(0, 0, 0, 0)
self.ui_log_edit = QTextEdit(Form)
self.ui_log_edit.setObjectName(u"ui_log_edit")
font = QFont()
font.setFamilies([u"MS Sans Serif"])
self.ui_log_edit.setFont(font)
self.ui_log_edit.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
self.ui_log_edit.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
self.ui_log_edit.setLineWrapMode(QTextEdit.NoWrap)
self.ui_log_edit.setReadOnly(True)

self.verticalLayout.addWidget(self.ui_log_edit)


self.retranslateUi(Form)

QMetaObject.connectSlotsByName(Form)
# setupUi

def retranslateUi(self, Form):
Form.setWindowTitle(QCoreApplication.translate("Form", u"Form", None))
# retranslateUi

Loading

0 comments on commit 2cba77e

Please sign in to comment.