forked from adrianpueyo/KnobScripter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdialogs.py
218 lines (174 loc) · 7.23 KB
/
dialogs.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# -*- coding: utf-8 -*-
""" Dialogs: Main dialog boxes for KnobScripter
adrianpueyo.com
"""
import nuke
import re
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
def ask(question, parent=None, default_yes = True):
msgBox = QtWidgets.QMessageBox(parent=parent)
msgBox.setText(question)
msgBox.setStandardButtons(QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No)
msgBox.setIcon(QtWidgets.QMessageBox.Question)
msgBox.setWindowFlags(msgBox.windowFlags() | Qt.WindowStaysOnTopHint)
if default_yes:
msgBox.setDefaultButton(QtWidgets.QMessageBox.Yes)
else:
msgBox.setDefaultButton(QtWidgets.QMessageBox.No)
reply = msgBox.exec_()
if reply == QtWidgets.QMessageBox.Yes:
return True
return False
class FileNameDialog(QtWidgets.QDialog):
'''
Dialog for creating new... (mode = "folder", "script" or "knob").
'''
def __init__(self, parent = None, mode = "folder", text = ""):
if parent.isPane:
super(FileNameDialog, self).__init__()
else:
super(FileNameDialog, self).__init__(parent)
self.mode = mode
self.text = text
title = "Create new {}.".format(self.mode)
self.setWindowTitle(title)
self.initUI()
def initUI(self):
# Widgets
self.name_label = QtWidgets.QLabel("Name: ")
self.name_label.setAlignment(QtCore.Qt.AlignRight | QtCore.Qt.AlignVCenter)
self.name_lineEdit = QtWidgets.QLineEdit()
self.name_lineEdit.setText(self.text)
self.name_lineEdit.textChanged.connect(self.nameChanged)
# Buttons
self.button_box = QtWidgets.QDialogButtonBox(QtWidgets.QDialogButtonBox.Ok | QtWidgets.QDialogButtonBox.Cancel)
self.button_box.button(QtWidgets.QDialogButtonBox.Ok).setEnabled(self.text != "")
self.button_box.accepted.connect(self.clickedOk)
self.button_box.rejected.connect(self.clickedCancel)
# Layout
self.master_layout = QtWidgets.QVBoxLayout()
self.name_layout = QtWidgets.QHBoxLayout()
self.name_layout.addWidget(self.name_label)
self.name_layout.addWidget(self.name_lineEdit)
self.master_layout.addLayout(self.name_layout)
self.master_layout.addWidget(self.button_box)
self.setLayout(self.master_layout)
self.name_lineEdit.setFocus()
self.setMinimumWidth(250)
def nameChanged(self):
txt = self.name_lineEdit.text()
m = r"[\w]*$"
if self.mode == "knob": # Knobs can't start with a number...
m = r"[a-zA-Z_]+" + m
if re.match(m, txt) or txt == "":
self.text = txt
else:
self.name_lineEdit.setText(self.text)
self.button_box.button(QtWidgets.QDialogButtonBox.Ok).setEnabled(self.text != "")
return
def clickedOk(self):
self.accept()
return
def clickedCancel(self):
self.reject()
return
class TextInputDialog(QtWidgets.QDialog):
'''
Simple dialog for a text input.
'''
def __init__(self, parent = None, name = "", text = "", title=""):
super(TextInputDialog, self).__init__(parent)
self.name = name # title of textinput
self.text = text # default content of textinput
self.setWindowTitle(title)
self.initUI()
def initUI(self):
# Widgets
self.name_label = QtWidgets.QLabel(self.name+": ")
self.name_label.setAlignment(QtCore.Qt.AlignRight | QtCore.Qt.AlignVCenter)
self.name_lineEdit = QtWidgets.QLineEdit()
self.name_lineEdit.setText(self.text)
self.name_lineEdit.textChanged.connect(self.nameChanged)
# Buttons
self.button_box = QtWidgets.QDialogButtonBox(QtWidgets.QDialogButtonBox.Ok | QtWidgets.QDialogButtonBox.Cancel)
#self.button_box.button(QtWidgets.QDialogButtonBox.Ok).setEnabled(self.text != "")
self.button_box.accepted.connect(self.clickedOk)
self.button_box.rejected.connect(self.clickedCancel)
# Layout
self.master_layout = QtWidgets.QVBoxLayout()
self.name_layout = QtWidgets.QHBoxLayout()
self.name_layout.addWidget(self.name_label)
self.name_layout.addWidget(self.name_lineEdit)
self.master_layout.addLayout(self.name_layout)
self.master_layout.addWidget(self.button_box)
self.setLayout(self.master_layout)
self.name_lineEdit.setFocus()
self.setMinimumWidth(250)
def nameChanged(self):
self.text = self.name_lineEdit.text()
def clickedOk(self):
self.accept()
return
def clickedCancel(self):
self.reject()
return
class ChooseNodeDialog(QtWidgets.QDialog):
'''
Dialog for selecting a node by its name. Only admits nodes that exist (including root, preferences...)
'''
def __init__(self, parent = None, name = ""):
if parent.isPane:
super(ChooseNodeDialog, self).__init__()
else:
super(ChooseNodeDialog, self).__init__(parent)
self.name = name # Name of node (will be "" by default)
self.allNodes = []
self.setWindowTitle("Enter the node's name...")
self.initUI()
def initUI(self):
# Widgets
self.name_label = QtWidgets.QLabel("Name: ")
self.name_label.setAlignment(QtCore.Qt.AlignRight | QtCore.Qt.AlignVCenter)
self.name_lineEdit = QtWidgets.QLineEdit()
self.name_lineEdit.setText(self.name)
self.name_lineEdit.textChanged.connect(self.nameChanged)
self.allNodes = self.getAllNodes()
completer = QtWidgets.QCompleter(self.allNodes, self)
completer.setCaseSensitivity(QtCore.Qt.CaseInsensitive)
self.name_lineEdit.setCompleter(completer)
# Buttons
self.button_box = QtWidgets.QDialogButtonBox(QtWidgets.QDialogButtonBox.Ok | QtWidgets.QDialogButtonBox.Cancel)
self.button_box.button(QtWidgets.QDialogButtonBox.Ok).setEnabled(nuke.exists(self.name))
self.button_box.accepted.connect(self.clickedOk)
self.button_box.rejected.connect(self.clickedCancel)
# Layout
self.master_layout = QtWidgets.QVBoxLayout()
self.name_layout = QtWidgets.QHBoxLayout()
self.name_layout.addWidget(self.name_label)
self.name_layout.addWidget(self.name_lineEdit)
self.master_layout.addLayout(self.name_layout)
self.master_layout.addWidget(self.button_box)
self.setLayout(self.master_layout)
self.name_lineEdit.setFocus()
self.setMinimumWidth(250)
def getAllNodes(self):
self.allNodes = [n.fullName() for n in nuke.allNodes(recurseGroups=True)] #if parent is in current context??
self.allNodes.extend(["root","preferences"])
return self.allNodes
def nameChanged(self):
self.name = self.name_lineEdit.text()
self.button_box.button(QtWidgets.QDialogButtonBox.Ok).setEnabled(self.name in self.allNodes)
def clickedOk(self):
self.accept()
return
def clickedCancel(self):
self.reject()
return