|
| 1 | +# ----------------------------------------------------------------------------- |
| 2 | +# Copyright © 2023- The Spyder Development Team |
| 3 | +# |
| 4 | +# Released under the terms of the MIT License |
| 5 | +# (see LICENSE.txt for details) |
| 6 | +# ----------------------------------------------------------------------------- |
| 7 | + |
| 8 | +"""Provides utility functions for use by QtPy itself.""" |
| 9 | + |
| 10 | +import qtpy |
| 11 | + |
| 12 | + |
| 13 | +def _wrap_missing_optional_dep_error( |
| 14 | + attr_error, |
| 15 | + *, |
| 16 | + import_error, |
| 17 | + wrapper=qtpy.QtModuleNotInstalledError, |
| 18 | + **wrapper_kwargs, |
| 19 | + ): |
| 20 | + """Create a __cause__-chained wrapper error for a missing optional dep.""" |
| 21 | + qtpy_error = wrapper(**wrapper_kwargs) |
| 22 | + import_error.__cause__ = attr_error |
| 23 | + qtpy_error.__cause__ = import_error |
| 24 | + return qtpy_error |
| 25 | + |
| 26 | + |
| 27 | +def getattr_missing_optional_dep(name, module_name, optional_names): |
| 28 | + """Wrap AttributeError in a special error if it matches.""" |
| 29 | + attr_error = AttributeError(f'module {module_name!r} has no attribute {name!r}') |
| 30 | + if name in optional_names: |
| 31 | + return _wrap_missing_optional_dep_error(attr_error, **optional_names[name]) |
| 32 | + return attr_error |
| 33 | + |
| 34 | + |
| 35 | +def possibly_static_exec(cls, *args, **kwargs): |
| 36 | + """Call `self.exec` when `self` is given or a static method otherwise.""" |
| 37 | + if not args and not kwargs: |
| 38 | + # A special case (`cls.exec_()`) to avoid the function resolving error |
| 39 | + return cls.exec() |
| 40 | + if isinstance(args[0], cls): |
| 41 | + if len(args) == 1 and not kwargs: |
| 42 | + # A special case (`self.exec_()`) to avoid the function resolving error |
| 43 | + return args[0].exec() |
| 44 | + return args[0].exec(*args[1:], **kwargs) |
| 45 | + else: |
| 46 | + return cls.exec(*args, **kwargs) |
| 47 | + |
| 48 | + |
| 49 | +def possibly_static_exec_(cls, *args, **kwargs): |
| 50 | + """Call `self.exec` when `self` is given or a static method otherwise.""" |
| 51 | + if not args and not kwargs: |
| 52 | + # A special case (`cls.exec()`) to avoid the function resolving error |
| 53 | + return cls.exec_() |
| 54 | + if isinstance(args[0], cls): |
| 55 | + if len(args) == 1 and not kwargs: |
| 56 | + # A special case (`self.exec()`) to avoid the function resolving error |
| 57 | + return args[0].exec_() |
| 58 | + return args[0].exec_(*args[1:], **kwargs) |
| 59 | + else: |
| 60 | + return cls.exec_(*args, **kwargs) |
| 61 | + |
| 62 | + |
| 63 | +def add_action(self, *args, old_add_action): |
| 64 | + """Re-order arguments of `addAction` to backport compatibility with Qt>=6.3.""" |
| 65 | + from qtpy.QtCore import QObject |
| 66 | + from qtpy.QtGui import QIcon, QKeySequence |
| 67 | + from qtpy.QtWidgets import QAction |
| 68 | + |
| 69 | + action: QAction |
| 70 | + icon: QIcon |
| 71 | + text: str |
| 72 | + shortcut: QKeySequence | QKeySequence.StandardKey | str | int |
| 73 | + receiver: QObject |
| 74 | + member: bytes |
| 75 | + if all(isinstance(arg, t) |
| 76 | + for arg, t in zip(args, [str, |
| 77 | + (QKeySequence, QKeySequence.StandardKey, str, int), |
| 78 | + QObject, |
| 79 | + bytes])): |
| 80 | + if len(args) == 2: |
| 81 | + text, shortcut = args |
| 82 | + action = old_add_action(self, text) |
| 83 | + action.setShortcut(shortcut) |
| 84 | + elif len(args) == 3: |
| 85 | + text, shortcut, receiver = args |
| 86 | + action = old_add_action(self, text, receiver) |
| 87 | + action.setShortcut(shortcut) |
| 88 | + elif len(args) == 4: |
| 89 | + text, shortcut, receiver, member = args |
| 90 | + action = old_add_action(self, text, receiver, member, shortcut) |
| 91 | + else: |
| 92 | + return old_add_action(self, *args) |
| 93 | + return action |
| 94 | + elif all(isinstance(arg, t) |
| 95 | + for arg, t in zip(args, [QIcon, |
| 96 | + str, |
| 97 | + (QKeySequence, QKeySequence.StandardKey, str, int), |
| 98 | + QObject, |
| 99 | + bytes])): |
| 100 | + if len(args) == 3: |
| 101 | + icon, text, shortcut = args |
| 102 | + action = old_add_action(self, icon, text) |
| 103 | + action.setShortcut(QKeySequence(shortcut)) |
| 104 | + elif len(args) == 4: |
| 105 | + icon, text, shortcut, receiver = args |
| 106 | + action = old_add_action(self, icon, text, receiver) |
| 107 | + action.setShortcut(QKeySequence(shortcut)) |
| 108 | + elif len(args) == 5: |
| 109 | + icon, text, shortcut, receiver, member = args |
| 110 | + action = old_add_action(self, icon, text, receiver, member, QKeySequence(shortcut)) |
| 111 | + else: |
| 112 | + return old_add_action(self, *args) |
| 113 | + return action |
| 114 | + return old_add_action(self, *args) |
0 commit comments