def headerData(self, section, orientation, role): if role == Qt.DisplayRole: if orientation == Qt.Horizontal: return QtCore.QString("%1").arg(section) else: return QtCore.QString("%1").arg(section)
def __init__(self, index, **kwds): super(GraphicsItemModel, self).__init__() self.index = QtCore.QPersistentModelIndex(index) self.rect = QtCore.QRectF(0,0,0,0) self.shape = QtGui.QPainterPath() self.brush = QtGui.QBrush() self.pen = QtGui.QPen() self.setFlags(self.ItemIsSelectable | self.ItemIsFocusable) self.onInit(**kwds)
def insertRows(self, position, rows, parent=QtCore.QModelIndex()): self.beginInsertRows(parent, position, position + rows - 1) for i in range(rows): self.__data.insert(position, '') self.endInsertRows() return True
def insertRows(self, position, rows, parent=QtCore.QModelIndex()): self.beginInsertRows(parent, position, position + rows - 1) for i in range(rows): default_values = ['' for i in range(self.columnCount(None))] self.__data.insert(position, default_values) self.endInsertRows() return True
def removeRows(self, position, rows, parent=QtCore.QModelIndex()): self.beginRemoveRows(parent, position, position + rows - 1) for i in range(rows): value = self.__data[position] self.__data.remove(value) self.endRemoveRows() return True
def insertNode(self, node=None, parent=QtCore.QModelIndex()): if parent.isValid() : childCount = parent.internalPointer().childCount() else: childCount = 0 self.model.insertRows(childCount, 1, parent, node) new_index = self.model.index(childCount, 0, parent) self.addIndex(new_index, False) return new_index
def removeNode(self, row, parent=QtCore.QModelIndex()): if parent.isValid() : childCount = parent.internalPointer().childCount() else: childCount = 0 for i in range(childCount) : child_index = self.model.index(i, 0, parent) self.removeIndex(child_index) self.model.removeRows(i, 1, parent)
def _readSettings(self): settings = QtCore.QSettings( Path.getTempPath().join('ui_settings', apiUtils.getClassName(self) + '.ini'), QtCore.QSettings.IniFormat) settings.beginGroup(apiUtils.getClassName(self)) self.readSettings(settings) settings.endGroup()
def insertColumns(self, position, columns, parent=QtCore.QModelIndex()): self.beginInsertColumns(parent, position, position + columns - 1) rowCount = len(self.__data) for i in range(columns): for j in range(rowCount): self.__data[j].insert(position, '') self.endInsertColumns() return True
def removeColumns(self, position, columns, parent=QtCore.QModelIndex()): self.beginRemoveRows(parent, position, position + columns - 1) rowCount = len(self.__data) for i in range(columns): for j in range(rowCount): value = self.__data[j][position] self.__data[j].remove(value) self.endRemoveRows() return True
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
def qInitResources(): QtCore.qRegisterResourceData(0x01, qt_resource_struct, qt_resource_name, qt_resource_data)
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)
def setData(self, column, value, role=Qt.EditRole): if self.scene() is None : return False self.scene().setData(self.getIndex(column), QtCore.QVariant(value), role)
def data(self, column, role=Qt.DisplayRole): if self.scene() is None : return QtCore.QVariant() return self.scene().data(self.getIndex(column), role)
def qCleanupResources(): QtCore.qUnregisterResourceData(0x01, qt_resource_struct, qt_resource_name, qt_resource_data)