class ChoiceWidget(Dialog): choiceSubmitted = QtCore.pyqtSignal(int) #------------------------------------------------------------ def onFinalize(self, msg='Choose', choices=[]): apiUtils.synthesize(self, 'msg', msg) apiUtils.synthesize(self, 'submitted', False) self.setModal(True) self.ui_message.setText(self.msg) self.ui_submit.clicked.connect(self.emitChoiceSubmitted) for choice in choices: self.ui_choices.addItem(choice) self.center() #------------------------------------------------------------ def emitChoiceSubmitted(self): if not self.signalsBlocked(): choice = self.ui_choices.currentIndex() self.choiceSubmitted.emit(choice) self.setSubmitted(True) self.close() #------------------------------------------------------------ @staticmethod def getChoice(*args, **kwds): success, choice = False, None widget = ChoiceWidget(*args, **kwds) if not widget.submitted: widget.exec_() success = widget.submitted if success: choice = widget.ui_choices.currentIndex() return success, choice
class PathWidget(Widget): pickerTypes = Enum('File', 'Folder', 'Node') pathChanged = QtCore.pyqtSignal(str) editingFinished = QtCore.pyqtSignal() #------------------------------------------------------------ def onFinalize(self, pickerType=None, ext='', label='', field=''): apiUtils.synthesize(self, 'pickerType', PathWidget.pickerTypes.File) self.setPickerType(pickerType) apiUtils.synthesize(self, 'ext', ext) self.setLabel(label) self.setField(field) self.ui_Field.contextMenuEvent = self.extendContextMenuEvent self.actionShow_in_explorer.triggered.connect(self.openPath) self.ui_Picker.clicked.connect(self.pickFile) self.ui_Field.textChanged.connect(self.emitPathChanged) self.ui_Field.editingFinished.connect(self.editingFinished) def loadUi(self, parent=None): self.setObjectName("PathWidget") self.resize(459, 38) sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Minimum) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(self.sizePolicy().hasHeightForWidth()) self.setSizePolicy(sizePolicy) self.horizontalLayout = QtGui.QHBoxLayout(self) self.horizontalLayout.setSpacing(6) self.horizontalLayout.setContentsMargins(6, 0, 6, 0) self.horizontalLayout.setObjectName("horizontalLayout") self.ui_Label = QtGui.QLabel(self) sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Fixed) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth( self.ui_Label.sizePolicy().hasHeightForWidth()) self.ui_Label.setSizePolicy(sizePolicy) self.ui_Label.setText("") self.ui_Label.setObjectName("ui_Label") self.horizontalLayout.addWidget(self.ui_Label) self.ui_Field = QtGui.QLineEdit(self) self.ui_Field.setObjectName("ui_Field") self.horizontalLayout.addWidget(self.ui_Field) self.ui_Picker = QtGui.QToolButton(self) self.ui_Picker.setObjectName("ui_Picker") self.horizontalLayout.addWidget(self.ui_Picker) self.actionShow_in_explorer = QtGui.QAction(self) self.actionShow_in_explorer.setObjectName("actionShow_in_explorer") self.setWindowTitle( QtGui.QApplication.translate("PathWidget", "Path Widget", None, QtGui.QApplication.UnicodeUTF8)) self.ui_Picker.setText( QtGui.QApplication.translate("PathWidget", "...", None, QtGui.QApplication.UnicodeUTF8)) self.actionShow_in_explorer.setText( QtGui.QApplication.translate("PathWidget", "Show in explorer", None, QtGui.QApplication.UnicodeUTF8)) QtCore.QMetaObject.connectSlotsByName(self) #------------------------------------------------------------ def extendContextMenuEvent(self, event): menu = self.ui_Field.createStandardContextMenu() menu.addAction(self.actionShow_in_explorer) menu.exec_(event.globalPos()) del menu #------------------------------------------------------------ def openPath(self): userInputPath = Path(str(self.ui_Field.text())) if userInputPath: subprocess.call('explorer ' + userInputPath.dir().caseSensative()) #------------------------------------------------------------ def pickFile(self): picked = None if self.pickerType == PathWidget.pickerTypes.Node: if Core.environment == Core.EnvironmentTypes.Maya: picked = Core.getMayaSelection()[0] if picked is None: return else: #Begin Testing for path types if self.pickerType == PathWidget.pickerTypes.File: picked = guiUtils.getFileFromUser(ext=self.ext) if self.pickerType == PathWidget.pickerTypes.Folder: picked = guiUtils.getDirFromUser() if not picked: return self.ui_Field.setText(str(picked)) #------------------------------------------------------------ def setPickerType(self, pickerType): temp = None if isinstance(pickerType, str): temp = PathWidget.pickerTypes.names.index(pickerType) else: temp = pickerType if temp and PathWidget.pickerTypes.names[temp]: self._pickerType = temp #------------------------------------------------------------ def setLabel(self, label): self.ui_Label.setText(label) #------------------------------------------------------------ def setField(self, field): self.ui_Field.setText(field) #------------------------------------------------------------ def emitPathChanged(self, path): if not self.signalsBlocked(): self.pathChanged.emit(str(path))
class LoginWidget(Dialog): loginSubmitted = QtCore.pyqtSignal(str, str) #------------------------------------------------------------ def onFinalize(self, loginMsg='Login', credentialsFile=''): apiUtils.synthesize(self, 'loginMsg', loginMsg) apiUtils.synthesize(self, 'credentialsFile', Path(credentialsFile)) apiUtils.synthesize(self, 'submitted', False) self.setModal(True) self.ui_loginMessage.setText(self.loginMsg) self.ui_submit.clicked.connect(self.emitLoginSubmitted) self.ui_username.returnPressed.connect(self.emitLoginSubmitted) self.ui_password.returnPressed.connect(self.emitLoginSubmitted) self.ui_password.setEchoMode(QtGui.QLineEdit.Password) self.center() if self.credentialsFile: self._readCredentials() #------------------------------------------------------------ def emitLoginSubmitted(self): if not self.signalsBlocked(): username = self.ui_username.text() password = self.ui_password.text() if self.ui_saveOption.checkState(): self._saveCredentials(username, password) self.loginSubmitted.emit(username, password) self.setSubmitted(True) self.close() #------------------------------------------------------------ def _saveCredentials(self, username, password): if self.credentialsFile: data = Document(filepath=self.credentialsFile) data['Username'] = base64.b64encode(str(username)) data['Password'] = base64.b64encode(str(password)) data.save() #------------------------------------------------------------ def _readCredentials(self): if self.credentialsFile and self.credentialsFile.exists(): data = Document(filepath=self.credentialsFile) self.ui_username.setText(base64.b64decode(data['Username'])) self.ui_password.setText(base64.b64decode(data['Password'])) self.ui_saveOption.setCheckState(True) self.setSubmitted(True) #------------------------------------------------------------ @staticmethod def getCredentials(force=False, *args, **kwds): success, username, password = False, '', '' widget = LoginWidget(*args, **kwds) if not widget.submitted or force: if force: widget.setSubmitted(False) widget.exec_() success = widget.submitted if success: username = widget.ui_username.text() password = widget.ui_password.text() return success, str(username), str(password)