replaced sys.modules checks with try: PyQt6, except: PyQt5

This commit is contained in:
Dpeta 2022-09-01 06:55:08 +02:00
parent 3f267898d4
commit b549645a50
5 changed files with 70 additions and 32 deletions

View file

@ -454,10 +454,10 @@ class ConsoleText(QtWidgets.QTextEdit):
def mousePressEvent(self, event):
if event.button() == QtCore.Qt.MouseButton.LeftButton:
if 'PyQt6' in sys.modules:
try:
# PyQt6
url = self.anchorAt(event.position().toPoint())
elif 'PyQt5' in sys.modules:
except AttributeError:
# PyQt5
url = self.anchorAt(event.pos())
if url != "":
@ -474,9 +474,11 @@ class ConsoleText(QtWidgets.QTextEdit):
def mouseMoveEvent(self, event):
# Change our cursor when we roll over links (anchors).
super(ConsoleText, self).mouseMoveEvent(event)
if 'PyQt6' in sys.modules:
try:
# PyQt6
pos = event.position().toPoint()
elif 'PyQt5' in sys.modules:
except AttributeError:
# PyQt5
pos = event.pos()
if self.anchorAt(pos):
if self.viewport().cursor().shape != QtCore.Qt.CursorShape.PointingHandCursor:

View file

@ -530,9 +530,11 @@ class PesterText(QtWidgets.QTextEdit):
def mousePressEvent(self, event):
if event.button() == QtCore.Qt.MouseButton.LeftButton:
if 'PyQt6' in sys.modules:
try:
# PyQt6
url = self.anchorAt(event.position().toPoint())
if 'PyQt5' in sys.modules:
except AttributeError:
# PyQt5
url = self.anchorAt(event.pos())
if url != "":
if url[0] == "#" and url != "#pesterchum":
@ -549,9 +551,11 @@ class PesterText(QtWidgets.QTextEdit):
QtWidgets.QTextEdit.mousePressEvent(self, event)
def mouseMoveEvent(self, event):
QtWidgets.QTextEdit.mouseMoveEvent(self, event)
if 'PyQt6' in sys.modules:
try:
# PyQt6
pos = event.position().toPoint()
if 'PyQt5' in sys.modules:
except AttributeError:
# PyQt5
pos = event.pos()
if self.anchorAt(pos):
if self.viewport().cursor().shape != QtCore.Qt.CursorShape.PointingHandCursor:

View file

@ -277,9 +277,11 @@ class PesterLogText(PesterText):
def focusInEvent(self, event):
QtWidgets.QTextEdit.focusInEvent(self, event)
def mousePressEvent(self, event):
if 'PyQt6' in sys.modules:
try:
# PyQt6
url = self.anchorAt(event.position().toPoint())
if 'PyQt5' in sys.modules:
except AttributeError:
# PyQt5
url = self.anchorAt(event.pos())
if url != "":
if url[0] == "#" and url != "#pesterchum":
@ -292,9 +294,11 @@ class PesterLogText(PesterText):
QtWidgets.QTextEdit.mousePressEvent(self, event)
def mouseMoveEvent(self, event):
QtWidgets.QTextEdit.mouseMoveEvent(self, event)
if 'PyQt6' in sys.modules:
try:
# PyQt6
pos = event.position().toPoint()
if 'PyQt5' in sys.modules:
except AttributeError:
# PyQt5
pos = event.pos()
if self.anchorAt(pos):
if self.viewport().cursor().shape != QtCore.Qt.CursorShape.PointingHandCursor:

View file

@ -1,3 +1,4 @@
import sys
import re
from os import remove
@ -723,20 +724,40 @@ class PesterChooseQuirks(QtWidgets.QDialog):
elif vdict["type"] == "replace":
vdict["from"] = str(page.itemAt(1).layout().itemAt(1).widget().text())
vdict["to"] = str(page.itemAt(2).layout().itemAt(1).widget().text())
vdict["checkstate"] = str(page.itemAt(3).layout().itemAt(0).widget().checkState().value)
try:
# PyQt6
vdict["checkstate"] = str(page.itemAt(3).layout().itemAt(0).widget().checkState().value)
except AttributeError:
# PyQt5
vdict["checkstate"] = str(page.itemAt(3).layout().itemAt(0).widget().checkState())
elif vdict["type"] == "regexp":
vdict["from"] = str(page.itemAt(2).layout().itemAt(1).layout().itemAt(1).widget().text())
vdict["to"] = str(page.itemAt(2).layout().itemAt(2).layout().itemAt(1).widget().text())
vdict["checkstate"] = str(page.itemAt(2).layout().itemAt(3).layout().itemAt(0).widget().checkState().value)
try:
# PyQt6
vdict["checkstate"] = str(page.itemAt(2).layout().itemAt(3).layout().itemAt(0).widget().checkState().value)
except AttributeError:
# PyQt5
vdict["checkstate"] = str(page.itemAt(2).layout().itemAt(3).layout().itemAt(0).widget().checkState())
elif vdict["type"] == "random":
vdict["from"] = str(self.quirkadd.regexp.text())
vdict["checkstate"] = str(page.itemAt(2).layout().itemAt(2).layout().itemAt(0).widget().checkState().value)
try:
# PyQt6
vdict["checkstate"] = str(page.itemAt(2).layout().itemAt(2).layout().itemAt(0).widget().checkState().value)
except AttributeError:
# PyQt5
vdict["checkstate"] = str(page.itemAt(2).layout().itemAt(2).layout().itemAt(0).widget().checkState())
randomlist = [str(self.quirkadd.replacelist.item(i).text())
for i in range(0,self.quirkadd.replacelist.count())]
vdict["randomlist"] = randomlist
elif vdict["type"] == "spelling":
vdict["percentage"] = self.quirkadd.slider.value()
vdict["checkstate"] = str(page.itemAt(3).layout().itemAt(0).widget().checkState().value)
try:
# PyQt6
vdict["checkstate"] = str(page.itemAt(3).layout().itemAt(0).widget().checkState().value)
except AttributeError:
# PyQt5
vdict["checkstate"] = str(page.itemAt(3).layout().itemAt(0).widget().checkState())
if vdict["type"] in ("regexp", "random"):
try:
re.compile(vdict["from"])

View file

@ -515,9 +515,11 @@ class chumArea(RightClickTree):
if thisitem.rfind(" (") != -1:
thisitem = thisitem[0:thisitem.rfind(" (")]
if thisitem == "Chums" or thisitem in self.groups:
if 'PyQt6' in sys.modules:
try:
# PyQt6
droppos = self.itemAt(event.position().toPoint())
elif 'PyQt5' in sys.modules:
except AttributeError:
# PyQt5
droppos = self.itemAt(event.pos())
if not droppos: return
droppos = str(droppos.text(0))
@ -525,9 +527,11 @@ class chumArea(RightClickTree):
droppos = droppos[0:droppos.rfind(" ")]
if droppos == "Chums" or droppos in self.groups:
saveOpen = event.source().currentItem().isExpanded()
if 'PyQt6' in sys.modules:
try:
# PyQt6
saveDrop = self.itemAt(event.position().toPoint())
if 'PyQt5' in sys.modules:
except AttributeError:
# PyQt5
saveDrop = self.itemAt(event.pos())
saveItem = self.takeTopLevelItem(self.indexOfTopLevelItem(event.source().currentItem()))
self.insertTopLevelItems(self.indexOfTopLevelItem(saveDrop)+1, [saveItem])
@ -543,9 +547,11 @@ class chumArea(RightClickTree):
self.mainwindow.config.saveGroups(gTemp)
# Drop item is a chum
else:
if 'PyQt6' in sys.modules:
try:
# PyQt6
eventpos = event.position().toPoint()
if 'PyQt5' in sys.modules:
except AttributeError:
# PyQt5
eventpos = event.pos()
item = self.itemAt(eventpos)
if item:
@ -2099,16 +2105,17 @@ class PesterWindow(MovingWindow):
def _setup_sounds(self, soundclass=None):
"""Set up the event sounds for later use."""
# Set up the sounds we're using.
if 'pygame' in sys.modules:
# Pygame is imported (Linux)
try:
# Pygame
soundclass = pygame.mixer.Sound
elif 'PyQt6.QtMultimedia' in sys.modules:
# QtMultimedia is imported (Windows, MacOS)
soundclass = QtMultimedia.QSoundEffect
else:
# death
soundclass = NoneSound
PchumLog.warning("No sound module loaded?")
except:
try:
# QtMultimedia
soundclass = QtMultimedia.QSoundEffect
except:
# death
soundclass = NoneSound
PchumLog.warning("No sound module loaded?")
self.sound_type = soundclass
@ -3792,7 +3799,7 @@ class MainProgram(QtCore.QObject):
# we could set the frequency higher but i love how cheesy it sounds
try:
pygame.mixer.init()
except pygame.error as err:
except (pygame.error, Exception) as err:
print("Warning: No sound! (pygame error: %s)" % err)
self.widget = PesterWindow(options, parent=self, app=self.app)