-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHouTextEditor.py
More file actions
81 lines (64 loc) · 2.39 KB
/
HouTextEditor.py
File metadata and controls
81 lines (64 loc) · 2.39 KB
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
import inspect
import hou
from PySide6 import QtCore, QtGui, QtWidgets
from PySide6.QtCore import Qt
from PySide6.QtGui import QPalette
class HoudiniTypeHintPopup(QtWidgets.QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.setWindowFlags(QtCore.Qt.Popup)
self.setStyleSheet("background-color: #2a2a2a; color: #ffffff; border: 1px solid #555;")
layout = QtWidgets.QVBoxLayout()
layout.setContentsMargins(8, 8, 8, 8)
self.text_edit = QtWidgets.QTextEdit()
self.text_edit.setReadOnly(True)
self.text_edit.setMaximumSize(600, 400)
self.text_edit.setFont(parent.font())
layout.addWidget(self.text_edit)
self.setLayout(layout)
def show_hint(self, selected_text, pos):
"""Parse selected text and show type hints"""
hint = self.get_type_hint(selected_text)
self.text_edit.setText(hint)
self.move(pos.x() + 10, pos.y() + 10)
self.show()
def set_font(self, font):
self.text_edit.setFont(font)
def get_type_hint(self, text):
"""Attempt to resolve and get docstring for selected text"""
text = text.strip()
# Try to eval in hou namespace
try:
obj = eval(text, {"hou": hou, "__builtins__": __builtins__})
except:
return f"Could not resolve: {text}"
# Try to get signature
try:
if callable(obj):
sig = inspect.signature(obj)
hint = f"{text}{sig}\n\n"
else:
hint = f"{text}: {type(obj).__name__}\n\n"
except (ValueError, TypeError):
hint = f"{text}: {type(obj).__name__}\n\n"
# Get docstring
doc = inspect.getdoc(obj)
if doc:
hint += doc
else:
hint += "No documentation available"
return hint
class HoudiniTextEditor(QtWidgets.QPlainTextEdit):
def __init__(self, parent=None):
super().__init__(parent)
self.popup = HoudiniTypeHintPopup(self)
def mousePressEvent(self, event):
super().mousePressEvent(event)
# Show popup on selection
cursor = self.textCursor()
if cursor.hasSelection():
text = cursor.selectedText()
self.popup.show_hint(text, event.globalPos())
def set_font(self, font):
self.setFont(font)
self.popup.set_font(font)