def addAttributeSlot(self): """ Adds a new attribute (column) to the table """ dialog = QDialog(self) dialog.setModal(True) dialog.setWindowTitle('Add Attribute') layout = QVBoxLayout() dialog.setLayout(layout) form = QFormLayout() nameBox = QLineEdit() typeCombo = QComboBox() for attrType in _attrTypes: typeName = partio.TypeName(attrType) typeCombo.addItem(typeName) typeCombo.setCurrentIndex(partio.FLOAT) countBox = QLineEdit() countBox.setValidator(QIntValidator()) countBox.setText('1') fixedCheckbox = QCheckBox() valueBox = QLineEdit() valueBox.setText('0') form.addRow('Name:', nameBox) form.addRow('Type:', typeCombo) form.addRow('Count:', countBox) form.addRow('Fixed:', fixedCheckbox) form.addRow('Default Value:', valueBox) layout.addLayout(form) buttons = QHBoxLayout() layout.addLayout(buttons) add = QPushButton('Add') add.clicked.connect(dialog.accept) buttons.addWidget(add) cancel = QPushButton('Cancel') cancel.clicked.connect(dialog.reject) buttons.addWidget(cancel) if not dialog.exec_(): return name = str(nameBox.text()) if not name: print('Please supply a name for the new attribute') # TODO: prompt return attrType = typeCombo.currentIndex() count = int(countBox.text()) fixed = fixedCheckbox.isChecked() values = list(str(valueBox.text()).strip().split()) for i in range(count): if i < len(values): value = values[i] else: value = values[-1] if attrType == partio.INT or attrType == partio.INDEXEDSTR: values[i] = int(value) elif attrType == partio.FLOAT or attrType == partio.VECTOR: values[i] = float(value) # pylint:disable=R0204 else: values[i] = 0.0 # pylint:disable=R0204 value = tuple(values) self.data.addAttribute(name, attrType, count, fixed, value)
class BaseFileFolderDialog(BaseDialog, abstract_dialog.AbstractFileFolderDialog): """ Base dialog classes for folders and files """ def_title = 'Select File' def_size = (200, 125) def_use_app_browser = False def __init__(self, name='BaseFileFolder', parent=None, **kwargs): super(BaseFileFolderDialog, self).__init__(name=name, parent=parent) self.directory = None self.filters = None self._use_app_browser = kwargs.pop('use_app_browser', self.def_use_app_browser) self.set_filters('All Files (*.*)') # By default, we set the directory to the user folder self.set_directory(os.path.expanduser('~')) self.center() def open_app_browser(self): return def ui(self): super(BaseFileFolderDialog, self).ui() from tpDcc.libs.qt.widgets import directory self.places = dict() self.grid = layouts.GridLayout() sub_grid = layouts.GridLayout() self.grid.addWidget(QLabel('Path:'), 0, 0, Qt.AlignRight) self.path_edit = QLineEdit(self) self.path_edit.setReadOnly(True) self.filter_box = QComboBox(self) self.file_edit = QLineEdit(self) self.view = directory.FileListWidget(self) self.view.setWrapping(True) self.view.setFocusPolicy(Qt.StrongFocus) self.open_button = QPushButton('Select', self) self.cancel_button = QPushButton('Cancel', self) size = QSize(32, 24) self.up_button = QPushButton('Up') self.up_button.setToolTip('Go up') self.up_button.setMinimumSize(size) self.up_button.setMaximumSize(size) size = QSize(56, 24) self.refresh_button = QPushButton('Reload') self.refresh_button.setToolTip('Reload file list') self.refresh_button.setMinimumSize(size) self.refresh_button.setMaximumSize(size) self.show_hidden = QCheckBox('Hidden') self.show_hidden.setChecked(False) self.show_hidden.setToolTip('Toggle show hidden files') sub_grid.addWidget(self.up_button, 0, 1) sub_grid.addWidget(self.path_edit, 0, 2) sub_grid.addWidget(self.refresh_button, 0, 3) sub_grid.addWidget(self.show_hidden, 0, 4) self.grid.addLayout(sub_grid, 0, 1) self.grid.addWidget(self.get_drives_widget(), 1, 0) self.grid.addWidget(self.view, 1, 1) self.grid.addWidget(QLabel('File name:'), 7, 0, Qt.AlignRight) self.grid.addWidget(self.file_edit, 7, 1) self.filter_label = QLabel('Filter:') self.grid.addWidget(self.filter_label, 8, 0, Qt.AlignRight) self.grid.addWidget(self.filter_box, 8, 1) hbox = layouts.GridLayout() hbox.addWidget(self.open_button, 0, 0, Qt.AlignRight) hbox.addWidget(self.cancel_button, 0, 1, Qt.AlignRight) self.grid.addLayout(hbox, 9, 1, Qt.AlignRight) self.main_layout.addLayout(self.grid) self.setGeometry(200, 100, 600, 400) self.open_button.clicked.connect(self.accept) self.cancel_button.clicked.connect(self.reject) self.up_button.clicked.connect(self.go_up) self.refresh_button.clicked.connect(self.update_view) self.show_hidden.stateChanged.connect(self.update_view) self.view.directory_activated.connect( self.activate_directory_from_view) self.view.file_activated.connect(self.activate_file_from_view) self.view.file_selected.connect(self.select_file_item) self.view.folder_selected.connect(self.select_folder_item) self.view.up_requested.connect(self.go_up) self.view.update_requested.connect(self.update_view) def exec_(self, *args, **kwargs): if self._use_app_browser: return self.open_app_browser() else: self.update_view() self.filter_box.currentIndexChanged.connect(self.update_view) accepted = super(BaseFileFolderDialog, self).exec_() self.filter_box.currentIndexChanged.disconnect(self.update_view) return self.get_result() if accepted == 1 else None def set_filters(self, filters, selected=0): self.filter_box.clear() filter_types = filters.split(';;') for ft in filter_types: extensions = string.extract(ft, '(', ')') filter_name = string.rstrips(ft, '({})'.format(extensions)) extensions = extensions.split(' ') self.filter_box.addItem( '{} ({})'.format(filter_name, ','.join(extensions)), extensions) if 0 <= selected < self.filter_box.count(): self.filter_box.setCurrentIndex(selected) self.filters = filters def get_drives_widget(self): """ Returns a QGroupBox widget that contains all disk drivers of the PC in a vertical layout :return: QGroupBox """ w = QGroupBox('') w.setParent(self) box = layouts.VerticalLayout() box.setAlignment(Qt.AlignTop) places = [(getpass.getuser(), os.path.realpath(os.path.expanduser('~')))] places += [ (q, q) for q in [os.path.realpath(x.absolutePath()) for x in QDir().drives()] ] for label, loc in places: icon = QFileIconProvider().icon(QFileInfo(loc)) drive_btn = QRadioButton(label) drive_btn.setIcon(icon) drive_btn.setToolTip(loc) drive_btn.setProperty('path', loc) drive_btn.clicked.connect(self.go_to_drive) self.places[loc] = drive_btn box.addWidget(drive_btn) w.setLayout(box) return w def go_to_drive(self): """ Updates widget to show the content of the selected disk drive """ sender = self.sender() self.set_directory(sender.property('path'), False) def get_result(self): tf = self.file_edit.text() sf = self.get_file_path(tf) return sf, os.path.dirname(sf), tf.split(os.pathsep) def get_filter_patterns(self): """ Get list of filter patterns that are being used by the widget :return: list<str> """ idx = self.filter_box.currentIndex() if idx >= 0: return self.filter_box.itemData(idx) else: return [] def get_file_path(self, file_name): """ Returns file path of the given file name taking account the selected directory :param file_name: str, name of the file without path :return: str """ sname = file_name.split(os.pathsep)[0] return os.path.realpath( os.path.join(os.path.abspath(self.directory), sname)) # def accept(self): # self._overlay.close() # super(BaseFileFolderDialog, self).accept() # # # def reject(self): # self._overlay.close() # super(BaseFileFolderDialog, self).reject() def update_view(self): """ Updates file/folder view :return: """ self.view.clear() qdir = QDir(self.directory) qdir.setNameFilters(self.get_filter_patterns()) filters = QDir.Dirs | QDir.AllDirs | QDir.Files | QDir.NoDot | QDir.NoDotDot if self.show_hidden.isChecked(): filters = filters | QDir.Hidden entries = qdir.entryInfoList(filters=filters, sort=QDir.DirsFirst | QDir.Name) file_path = self.get_file_path('..') if os.path.exists(file_path) and file_path != self.directory: icon = QFileIconProvider().icon(QFileInfo(self.directory)) QListWidgetItem(icon, '..', self.view, 0) for info in entries: icon = QFileIconProvider().icon(info) suf = info.completeSuffix() name, tp = (info.fileName(), 0) if info.isDir() else ( '%s%s' % (info.baseName(), '.%s' % suf if suf else ''), 1) QListWidgetItem(icon, name, self.view, tp) self.view.setFocus() def set_directory(self, path, check_drive=True): """ Sets the directory that you want to explore :param path: str, valid path :param check_drive: bool, :return: """ self.directory = os.path.realpath(path) self.path_edit.setText(self.directory) self.file_edit.setText('') # If necessary, update selected disk driver if check_drive: for loc in self.places: rb = self.places[loc] rb.setAutoExclusive(False) rb.setChecked(loc.lower() == self.directory.lower()) rb.setAutoExclusive(True) self.update_view() self.up_button.setEnabled(not self.cant_go_up()) def go_up(self): """ Updates the current directory to go to its parent directory """ self.set_directory(os.path.dirname(self.directory)) def cant_go_up(self): """ Checks whether we can naviage to current selected parent directory or not :return: bool """ return os.path.dirname(self.directory) == self.directory def activate_directory_from_view(self, name): """ Updates selected directory :param name: str, name of the directory """ self.set_directory(os.path.join(self.directory, name)) def activate_file_from_view(self, name): """ Updates selected file text and returns its info by accepting it :param name: str, name of the file """ self.select_file_item(name=name) self.accept() def select_file_item(self, name): """ Updates selected file text and returns its info by accepting it :param name: str, name of the file """ self.file_edit.setText(name) def select_folder_item(self, name): """ Updates selected folder text and returns its info by accepting it :param name: str, name of the folder """ self.file_edit.setText(name)
def addAttributeSlot(self): """ Adds a new attribute (column) to the table """ dialog = QDialog(self) dialog.setModal(True) dialog.setWindowTitle('Add Attribute') layout = QVBoxLayout() dialog.setLayout(layout) form = QFormLayout() nameBox = QLineEdit() typeCombo = QComboBox() for attrType in _attrTypes: typeName = partio.TypeName(attrType) typeCombo.addItem(typeName) typeCombo.setCurrentIndex(partio.FLOAT) countBox = QLineEdit() countBox.setValidator(QIntValidator()) countBox.setText('1') fixedCheckbox = QCheckBox() valueBox = QLineEdit() valueBox.setText('0') form.addRow('Name:', nameBox) form.addRow('Type:', typeCombo) form.addRow('Count:', countBox) form.addRow('Fixed:', fixedCheckbox) form.addRow('Default Value:', valueBox) layout.addLayout(form) buttons = QHBoxLayout() layout.addLayout(buttons) add = QPushButton('Add') add.clicked.connect(dialog.accept) buttons.addWidget(add) cancel = QPushButton('Cancel') cancel.clicked.connect(dialog.reject) buttons.addWidget(cancel) if not dialog.exec_(): return name = str(nameBox.text()) if not name: print 'Please supply a name for the new attribute' # TODO: prompt return attrType = typeCombo.currentIndex() count = int(countBox.text()) fixed = fixedCheckbox.isChecked() values = list(str(valueBox.text()).strip().split()) for i in range(count): if i < len(values): value = values[i] else: value = values[-1] if attrType == partio.INT or attrType == partio.INDEXEDSTR: values[i] = int(value) elif attrType == partio.FLOAT or attrType == partio.VECTOR: values[i] = float(value) # pylint:disable=R0204 else: values[i] = 0.0 # pylint:disable=R0204 value = tuple(values) self.data.addAttribute(name, attrType, count, fixed, value)