adds a utility function to hide a QT5/QT4 discrepancy (#765)

Co-authored-by: erik <ersre>
This commit is contained in:
ngc92 2021-07-25 19:09:01 +03:00 committed by GitHub
parent c5971bffab
commit 58406b578a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 19 deletions

View File

@ -6,7 +6,7 @@ except ImportError:
from PyQt4.QtGui import * from PyQt4.QtGui import *
from PyQt4.QtCore import * from PyQt4.QtCore import *
from libs.utils import new_icon, label_validator from libs.utils import new_icon, label_validator, trimmed
BB = QDialogButtonBox BB = QDialogButtonBox
@ -47,22 +47,18 @@ class LabelDialog(QDialog):
self.setLayout(layout) self.setLayout(layout)
def validate(self): def validate(self):
try: if trimmed(self.edit.text()):
if self.edit.text().trimmed():
self.accept()
except AttributeError:
# PyQt5: AttributeError: 'str' object has no attribute 'trimmed'
if self.edit.text().strip():
self.accept() self.accept()
def post_process(self): def post_process(self):
try: self.edit.setText(trimmed(self.edit.text()))
self.edit.setText(self.edit.text().trimmed())
except AttributeError:
# PyQt5: AttributeError: 'str' object has no attribute 'trimmed'
self.edit.setText(self.edit.text())
def pop_up(self, text='', move=True): def pop_up(self, text='', move=True):
"""
Shows the dialog, setting the current text to `text`, and blocks the caller until the user has made a choice.
If the user entered a label, that label is returned, otherwise (i.e. if the user cancelled the action)
`None` is returned.
"""
self.edit.setText(text) self.edit.setText(text)
self.edit.setSelection(0, len(text)) self.edit.setSelection(0, len(text))
self.edit.setFocus(Qt.PopupFocusReason) self.edit.setFocus(Qt.PopupFocusReason)
@ -77,14 +73,10 @@ class LabelDialog(QDialog):
if cursor_pos.y() > max_global.y(): if cursor_pos.y() > max_global.y():
cursor_pos.setY(max_global.y()) cursor_pos.setY(max_global.y())
self.move(cursor_pos) self.move(cursor_pos)
return self.edit.text() if self.exec_() else None return trimmed(self.edit.text()) if self.exec_() else None
def list_item_click(self, t_qlist_widget_item): def list_item_click(self, t_qlist_widget_item):
try: text = trimmed(t_qlist_widget_item.text())
text = t_qlist_widget_item.text().trimmed()
except AttributeError:
# PyQt5: AttributeError: 'str' object has no attribute 'trimmed'
text = t_qlist_widget_item.text().strip()
self.edit.setText(text) self.edit.setText(text)
def list_item_double_click(self, t_qlist_widget_item): def list_item_double_click(self, t_qlist_widget_item):

View File

@ -8,9 +8,11 @@ try:
from PyQt5.QtGui import * from PyQt5.QtGui import *
from PyQt5.QtCore import * from PyQt5.QtCore import *
from PyQt5.QtWidgets import * from PyQt5.QtWidgets import *
QT5 = True
except ImportError: except ImportError:
from PyQt4.QtGui import * from PyQt4.QtGui import *
from PyQt4.QtCore import * from PyQt4.QtCore import *
QT5 = False
def new_icon(icon): def new_icon(icon):
@ -85,13 +87,16 @@ def generate_color_by_text(text):
b = int((hash_code / 16581375) % 255) b = int((hash_code / 16581375) % 255)
return QColor(r, g, b, 100) return QColor(r, g, b, 100)
def have_qstring(): def have_qstring():
"""p3/qt5 get rid of QString wrapper as py3 has native unicode str type""" """p3/qt5 get rid of QString wrapper as py3 has native unicode str type"""
return not (sys.version_info.major >= 3 or QT_VERSION_STR.startswith('5.')) return not (sys.version_info.major >= 3 or QT_VERSION_STR.startswith('5.'))
def util_qt_strlistclass(): def util_qt_strlistclass():
return QStringList if have_qstring() else list return QStringList if have_qstring() else list
def natural_sort(list, key=lambda s:s): def natural_sort(list, key=lambda s:s):
""" """
Sort the list into natural alphanumeric order. Sort the list into natural alphanumeric order.
@ -101,3 +106,12 @@ def natural_sort(list, key=lambda s:s):
return lambda s: [convert(c) for c in re.split('([0-9]+)', key(s))] return lambda s: [convert(c) for c in re.split('([0-9]+)', key(s))]
sort_key = get_alphanum_key_func(key) sort_key = get_alphanum_key_func(key)
list.sort(key=sort_key) list.sort(key=sort_key)
# QT4 has a trimmed method, in QT5 this is called strip
if QT5:
def trimmed(text):
return text.strip()
else:
def trimmed(text):
return text.trimmed()