forked from adrianpueyo/KnobScripter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeywordhotbox.py
179 lines (143 loc) · 5.46 KB
/
keywordhotbox.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# -*- coding: utf-8 -*-
""" KeywordHotbox: KnobScripter's floating panel for word suggestions while scripting.
adrianpueyo.com
"""
import nuke
from functools import partial
try:
if nuke.NUKE_VERSION_MAJOR < 11:
from PySide import QtCore, QtGui, QtGui as QtWidgets
from PySide.QtCore import Qt
else:
from PySide2 import QtWidgets, QtGui, QtCore
from PySide2.QtCore import Qt
except ImportError:
from Qt import QtCore, QtGui, QtWidgets
class KeywordHotbox(QtWidgets.QDialog):
"""
Floating panel with word suggestions
Based on the given keywords dictionary of lists. Example:
keyword_dict = {
"Access method": {
"keywords": ["eAccessPoint","eAccessRanged1D"],
"help": "Full help! <with html tags and whatever><li><ul><b>..."
},
}
When clicking on a button, the accept() signal is emitted, and the button's text is stored under self.selection
"""
def __init__(self, parent, category="", category_dict=None):
super(KeywordHotbox, self).__init__(parent)
category_dict = category_dict or {}
self.script_editor = parent
self.setWindowFlags(
QtCore.Qt.FramelessWindowHint | QtCore.Qt.Popup) # Without self.windowFlags() first, closes as intended
if not category or "keywords" not in category_dict:
self.reject()
return
self.category = category
self.category_dict = category_dict
self.selection = ""
self.initUI()
# Move hotbox to appropriate position
self.move(QtGui.QCursor().pos() - QtCore.QPoint((self.width() / 2), -6))
self.installEventFilter(self)
def initUI(self):
master_layout = QtWidgets.QVBoxLayout()
# 1. Main part: Hotbox Buttons
for keyword in self.category_dict["keywords"]:
button = KeywordHotboxButton(keyword, self)
button.clicked.connect(partial(self.pressed, keyword))
master_layout.insertWidget(-1, button)
# 2. ToolTip etc
if "help" in self.category_dict:
category_help = self.category_dict["help"]
else:
category_help = ""
if nuke.NUKE_VERSION_MAJOR < 11:
master_layout.setContentsMargins(0, 0, 0, 0)
else:
master_layout.setMargin(0)
master_layout.setSpacing(0)
self.setToolTip("<h2>{}</h2>".format(self.category) + category_help)
self.setStyleSheet('''QToolTip{
border: 1px solid black;
padding: 10px;
}
''')
self.setLayout(master_layout)
self.adjustSize()
def pressed(self, keyword=""):
if keyword != "":
self.selection = keyword
self.accept()
def focusOutEvent(self, event):
self.close()
QtWidgets.QDialog.focusOutEvent(event)
class KeywordHotboxButton(QtWidgets.QLabel):
"""
Keyword button for the KeywordHotbox. It's really a label, with a selection color and stuff.
"""
clicked = QtCore.Signal()
def __init__(self, name, parent=None):
super(KeywordHotboxButton, self).__init__(parent)
self.parent = parent
if hasattr(parent, 'script_editor') and hasattr(parent.script_editor, 'knob_scripter'):
self.knobScripter = parent.script_editor.knob_scripter
else:
self.knobScripter = None
self.name = name
self.highlighted = False
self.defaultStyle = self.style()
self.setMouseTracking(True)
# self.setTextFormat(QtCore.Qt.RichText)
# self.setWordWrap(True)
self.setText(self.name)
self.setHighlighted(False)
if self.knobScripter:
self.setFont(self.knobScripter.script_editor_font)
else:
font = QtGui.QFont()
font.setFamily("Monospace")
font.setStyleHint(QtGui.QFont.Monospace)
font.setFixedPitch(True)
font.setPointSize(11)
self.setFont(font)
def setHighlighted(self, highlighted=False):
"""
Define the style of the button for different states
"""
# Selected
if highlighted:
# self.setStyle(QtWidgets.QStyleFactory.create('Plastique')) #background:#e90;
self.setStyleSheet("""
border: 0px solid black;
background:#555;
color:#eeeeee;
padding: 6px 4px;
""")
# Deselected
else:
# self.setStyle(self.defaultStyle)
self.setStyleSheet("""
border: 0px solid #000;
background:#3e3e3e;
color:#eeeeee;
padding: 6px 4px;
""")
self.highlighted = highlighted
def enterEvent(self, event):
""" Mouse hovering """
self.setHighlighted(True)
return True
def leaveEvent(self, event):
""" Stopped hovering """
self.setHighlighted(False)
return True
def mouseReleaseEvent(self, event):
"""
Execute the buttons' self.function (str)
"""
if self.highlighted:
self.clicked.emit()
pass
super(KeywordHotboxButton, self).mouseReleaseEvent(event)