-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
150 lines (132 loc) · 5.22 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import os
import sys
from qtpy.compat import to_qvariant
from qtpy.QtCore import Qt
from qtpy.QtGui import QIcon
from qtpy.QtWidgets import (QMenu,
QToolButton,
QAction)
class UDPAppAction(QAction):
"""Spyder QAction class wrapper to handle cross platform patches."""
def __init__(self, *args, **kwargs):
"""QAction class wrapper to handle cross platform patches."""
super(UDPAppAction, self).__init__(*args, **kwargs)
self._action_no_icon = None
if sys.platform == 'darwin':
self._action_no_icon = QAction(*args, **kwargs)
self._action_no_icon.setIcon(QIcon())
self._action_no_icon.triggered.connect(self.triggered)
self._action_no_icon.toggled.connect(self.toggled)
self._action_no_icon.changed.connect(self.changed)
self._action_no_icon.hovered.connect(self.hovered)
else:
self._action_no_icon = self
def __getattribute__(self, name):
"""Intercept method calls and apply to both actions, except signals."""
attr = super(UDPAppAction, self).__getattribute__(name)
if hasattr(attr, '__call__') and name not in ['triggered', 'toggled',
'changed', 'hovered']:
def newfunc(*args, **kwargs):
result = attr(*args, **kwargs)
if name not in ['setIcon']:
action_no_icon = self.__dict__['_action_no_icon']
attr_no_icon = super(QAction,
action_no_icon).__getattribute__(name)
attr_no_icon(*args, **kwargs)
return result
return newfunc
else:
return attr
@property
def no_icon_action(self):
"""Return the action without an Icon."""
return self._action_no_icon
def add_actions(target, actions, insert_before=None):
"""Add actions to a QMenu or a QToolBar."""
previous_action = None
target_actions = list(target.actions())
if target_actions:
previous_action = target_actions[-1]
if previous_action.isSeparator():
previous_action = None
for action in actions:
if (action is None) and (previous_action is not None):
if insert_before is None:
target.addSeparator()
else:
target.insertSeparator(insert_before)
elif isinstance(action, QMenu):
if insert_before is None:
target.addMenu(action)
else:
target.insertMenu(insert_before, action)
elif isinstance(action, QAction):
if insert_before is None:
target.addAction(action)
else:
target.insertAction(insert_before, action)
previous_action = action
def create_toolbutton(parent, text=None, shortcut=None, icon=None, tip=None,
toggled=None, triggered=None,
autoraise=True, text_beside_icon=False):
"""Create a QToolButton"""
button = QToolButton(parent)
if text is not None:
button.setText(text)
if icon is not None:
# if is_text_string(icon):
# icon = get_icon(icon)
button.setIcon(icon)
if text is not None or tip is not None:
button.setToolTip(text if tip is None else tip)
if text_beside_icon:
button.setToolButtonStyle(Qt.ToolButtonTextBesideIcon)
button.setAutoRaise(autoraise)
if triggered is not None:
button.clicked.connect(triggered)
if toggled is not None:
button.toggled.connect(toggled)
button.setCheckable(True)
if shortcut is not None:
button.setShortcut(shortcut)
return button
def create_action(parent, text, shortcut=None, icon=None, tip=None,
toggled=None, triggered=None, data=None, menurole=None,
context=Qt.WindowShortcut):
"""Create a QAction"""
action = UDPAppAction(text, parent)
if triggered is not None:
action.triggered.connect(triggered)
if toggled is not None:
action.toggled.connect(toggled)
action.setCheckable(True)
# action.setIcon(icon)
if tip is not None:
action.setToolTip(tip)
action.setStatusTip(tip)
if data is not None:
action.setData(to_qvariant(data))
if menurole is not None:
action.setMenuRole(menurole)
# Workround for Mac because setting context=Qt.WidgetShortcut
# there doesn't have any effect
if sys.platform == 'darwin':
action._shown_shortcut = None
if context == Qt.WidgetShortcut:
if shortcut is not None:
action._shown_shortcut = shortcut
else:
# This is going to be filled by
# main.register_shortcut
action._shown_shortcut = 'missing'
else:
if shortcut is not None:
action.setShortcut(shortcut)
action.setShortcutContext(context)
else:
if shortcut is not None:
action.setShortcut(shortcut)
action.setShortcutContext(context)
return action