"""This module provide uncategorized misc toolkit.
"""
import sys, os, time
from PySide6.QtCore import Qt, QPropertyAnimation, QPoint
from PySide6.QtGui import QPainter, QPixmap, QBrush
from PySide6.QtWidgets import QApplication, QMainWindow, QMenu
#############################################
_ide = None
[docs]def getIde():
"""Get DearEditor ide instance.
Returns:
QApplication: The instance of Ide/QApplication.
"""
global _ide
if not _ide: _ide = QApplication.instance()
return _ide
[docs]def getMainWindow():
"""Get DearEditor ide main window instance.
Returns:
QMainWindow: The instance of ide's main window.
"""
app = QApplication.instance()
for widget in app.topLevelWidgets():
if isinstance(widget, QMainWindow):
return widget
[docs]def restartApp():
"""Restart DearEditor ide.
"""
quitApp()
py = sys.executable
os.execl(py, py, *sys.argv)
[docs]def quitApp():
"""Exit DearEditor ide.
"""
getMainWindow().close()
#############################################
#############################################
[docs]def toInt(text, default = 0):
try:
return int(text)
except ValueError:
return default
[docs]def toFloat(text, default = 0.0):
try:
return float(text)
except ValueError:
return default
#############################################
[docs]def fuzzyContains(str, substr):
len1, len2 = len(str), len(substr)
if len1 < len2: return False
if len1 == len2 and str == substr: return True
pos = 0
for c in substr:
idx = str.find(c, pos)
if idx < 0: return False
pos = idx + 1
return True
#############################################
_lastRecordTime = None
[docs]def record():
global _lastRecordTime
_lastRecordTime = time.perf_counter()
[docs]def report():
return (time.perf_counter() - _lastRecordTime) * 1000
#############################################
_transparentBgPixmap = None
_transparentBgBrush = None
[docs]def requestTransparentBgPixmap():
global _transparentBgPixmap
if not _transparentBgPixmap:
_transparentBgPixmap = QPixmap(8, 8)
_painter = QPainter(_transparentBgPixmap)
_painter.fillRect(0, 0, 4, 4, Qt.gray)
_painter.fillRect(4, 4, 4, 4, Qt.gray)
_painter.fillRect(4, 0, 4, 4, Qt.white)
_painter.fillRect(0, 4, 4, 4, Qt.white)
return _transparentBgPixmap
[docs]def requestTransparentBgBrush():
global _transparentBgBrush
if not _transparentBgBrush:
pixmap = requestTransparentBgPixmap()
_transparentBgBrush = QBrush(pixmap)
return _transparentBgBrush
#############################################