class ConfigManager(QtGui.QMainWindow, Ui_ConfigManager): def __init__(self, *args, **kwargs): super(ConfigManager, self).__init__(*args, **kwargs) self.setupUi() self.bindUi() self.windows = {} def setupUi(self): super(ConfigManager, self).setupUi(self) def bindUi(self): self.model = QFileSystemModel() self.model.setRootPath("/") self.model.setFilter(QDir.AllDirs | QDir.NoDotAndDotDot) self.treeView.setModel(self.model) if app_config.get_last_dir(): self.treeView.setCurrentIndex( self.model.index(app_config.get_last_dir())) self.treeView.setAnimated(True) self.treeView.setIndentation(20) self.treeView.setSortingEnabled(True) self.treeView.setWindowTitle("Branded App Configurations") self.treeView.doubleClicked.connect(self.config_selected) self.set_recent_config_actions() def set_recent_config_actions(self): history = app_config.get_config_history() self.historyActions = [] for k in history: a = QtGui.QAction(self) a.triggered.connect(lambda: self.open_config(k)) a.setText(k) self.historyActions.append(a) self.menuRecent.addAction(a) @QtCore.Slot() def publish_clicked(self): # index = self.treeView.currentIndex() pass def open_config(self, config_path): if os.path.exists(config_path): app_config.set_last_dir(os.path.dirname(config_path)) if not config_path in self.windows: c = ConfigWindow(self) c.set_configuration(config_path, None) app_config.add_config_to_history(config_path) c.closeEvent = lambda x: self.windows.pop(config_path) self.windows[config_path] = c self.windows[config_path].show() @QtCore.Slot() def config_selected(self): print "Config is selected" p = self.model.filePath(self.treeView.currentIndex()) config_path = os.path.join(p, "bc.json") config_path = config_path.replace('\\', '/') self.open_config(config_path)
class ConfigManager(QtGui.QMainWindow, Ui_ConfigManager): def __init__(self, *args, **kwargs): super(ConfigManager, self).__init__(*args, **kwargs) self.setupUi() self.bindUi() self.windows = {} def setupUi(self): super(ConfigManager, self).setupUi(self) def bindUi(self): self.model = QFileSystemModel() self.model.setRootPath("/") self.model.setFilter(QDir.AllDirs | QDir.NoDotAndDotDot) self.treeView.setModel(self.model) if app_config.get_last_dir(): self.treeView.setCurrentIndex(self.model.index(app_config.get_last_dir())) self.treeView.setAnimated(True) self.treeView.setIndentation(20) self.treeView.setSortingEnabled(True) self.treeView.setWindowTitle("Branded App Configurations") self.treeView.doubleClicked.connect(self.config_selected) self.set_recent_config_actions() def set_recent_config_actions(self): history = app_config.get_config_history() self.historyActions = [] for k in history: a = QtGui.QAction(self) a.triggered.connect(lambda: self.open_config(k)) a.setText(k) self.historyActions.append(a) self.menuRecent.addAction(a) @QtCore.Slot() def publish_clicked(self): # index = self.treeView.currentIndex() pass def open_config(self, config_path): if os.path.exists(config_path): app_config.set_last_dir(os.path.dirname(config_path)) if not config_path in self.windows: c = ConfigWindow(self) c.set_configuration(config_path, None) app_config.add_config_to_history(config_path) c.closeEvent = lambda x: self.windows.pop(config_path) self.windows[config_path] = c self.windows[config_path].show() @QtCore.Slot() def config_selected(self): print "Config is selected" p = self.model.filePath(self.treeView.currentIndex()) config_path = os.path.join(p, "bc.json") config_path = config_path.replace('\\', '/') self.open_config(config_path)
class getFilesDlg(QDialog): # sendPaths is a signal emitted by getPaths containing a list of file paths from # the users selection via this dialog. sendPaths = Signal(list) def __init__(self, parent=None): super(getFilesDlg, self).__init__(parent) self.setMinimumSize(500,500) self.fileDlgPaths = [] layout = QVBoxLayout() self.btnBox = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel) self.btnBox.accepted.connect(self.getPaths) self.btnBox.rejected.connect(self.close) self.fsModel = QFileSystemModel() self.treeView = QTreeView() self.treeView.setSelectionMode(QTreeView.ExtendedSelection) self.treeView.setModel(self.fsModel) self.treeView.setColumnWidth(0, 361) self.treeView.setColumnHidden(1, True) self.treeView.setColumnHidden(3, True) layout.addWidget(self.treeView) layout.addWidget(self.btnBox) self.setLayout(layout) self.fsModel.setRootPath(environ['HOMEPATH']) # self.treeView.setRootIndex(self.fsModel.index("\\")) self.treeView.expand(self.treeView.rootIndex()) def getPaths(self): # For some reason duplicates were being returned when they weren't supposed to. # This obtains the selected files from the dialog and only returns individual # paths. indexes = self.treeView.selectedIndexes() if indexes: self.fileDlgPaths = [] for i in indexes: # Possible permission error occuring here # unable to replicate at this time path = self.fsModel.filePath(i) if path not in self.fileDlgPaths: self.fileDlgPaths.append(path.replace('/','\\')) self.close() # To close the dialog on an accept signal self.sendPaths.emit(self.fileDlgPaths)
class FileManagerModel(QObject, ObsLightGuiObject): ''' Manage the file list widget and file-related buttons of the main window. ''' def __init__(self, gui, manager): QObject.__init__(self) ObsLightGuiObject.__init__(self, gui) self.__treeView = None self.__lineEdit = None self._project = None self._package = None self._baseDirPath = "/" self._curentDirPath = "/" self.__systemModel = None def setTreeView(self, treeView): self.__treeView = treeView def setLineEdit(self, lineEdit): self.__lineEdit = lineEdit def getTreeView(self): return self.__treeView def _init_connect(self): if self.__treeView is not None: self.__treeView.doubleClicked.connect(self.on_TreeView_activated) self.__treeView.clicked.connect(self.on_TreeView_clicked) self.__treeView.expanded.connect(self.on_TreeView_expanded) self.__treeView.collapsed.connect(self.on_TreeView_expanded) def _isInit(self): return True def setCurrentProjectAndPackage(self, project, package): self._project = project self._package = package #--------------------------------------------------------------------------------------------------- @popupOnException def on_TreeView_activated(self, index): """ When user double-clicks on an item, open it with default application. """ filePath = index.model().filePath(index) self.manager.openFile(filePath) @popupOnException def on_TreeView_clicked(self, index): """ When user clicks on an item, display the complete path of this item under the widget. """ filePath = index.model().filePath(index) if self.__lineEdit is not None: self.__lineEdit.setText(filePath) @popupOnException def on_TreeView_expanded(self, _index): if self.__treeView is not None: self.__treeView.resizeColumnToContents(0) self._baseDirPath = None self._curentDirPath = None def _initBaseDir(self): pass #--------------------------------------------------------------------------------------------------- def refresh(self): # --- view --------- self.__systemModel = QFileSystemModel() if self._project is not None and self._package is not None and self.__treeView is not None: if self._isInit(): self.__treeView.setEnabled(True) # Qt 4.6 do not know "directoryLoaded" if hasattr(self.__systemModel, "directoryLoaded"): self.__systemModel.directoryLoaded.connect( self.on_path_loaded) self._initBaseDir() self.__systemModel.setRootPath(self._baseDirPath) if self._baseDirPath != self._curentDirPath: self.__systemModel.setRootPath(self._curentDirPath) else: self.__treeView.setEnabled(False) self.mainWindow.packageTabWidget.setEnabled(True) else: self.mainWindow.packageTabWidget.setEnabled(False) if self.__treeView is not None: self.__treeView.setModel(self.__systemModel) # Qt 4.6 do not know "directoryLoaded" if not hasattr(self.__systemModel, "directoryLoaded"): self.on_path_loaded(self.__baseDirPath) self.on_path_loaded(self.__curentDirPath) def on_path_loaded(self, path): """ Called when the QFileSystem model loads paths. """ if self.__treeView is not None: if path == self._baseDirPath: # Set the root index of the QTreeView to the root directory of # the project file system, so user does not see outside if self.__systemModel is not None: self.__treeView.setRootIndex( self.__systemModel.index(path)) elif path == self._curentDirPath: # Set the current index of the QTreeView to the package directory # so it appears unfolded if self.__systemModel is not None: self.__treeView.setCurrentIndex( self.__systemModel.index(path)) self.__treeView.resizeColumnToContents(0)
class MainWindow(QMainWindow, Ui_MainWindow): def __init__(self, parent=None): super(MainWindow, self).__init__(parent) self.setupUi(self) self.build_dir_tree() self.setWindowIcon(QIcon('favicon.png')) self.actionAbout.triggered.connect(self.about) self.destButton.clicked.connect(self.destination_chooser) self.actionChoose_Destination.triggered.connect( self.destination_chooser) self.copyButton.clicked.connect(self.copy_files) self.actionStart_Copy.triggered.connect(self.copy_files) self.ckbxTrimDir.toggled.connect(self.update_table_view) self.treeView.expanded.connect(self.resize_tree_column) self.treeView.collapsed.connect(self.resize_tree_column) self.treeView.clicked.connect(self.update_table_view) self.trimdirCount.valueChanged.connect(self.update_table_view) self.listWidget.doubleClicked.connect(self.unselectItem) self.copyButton.setEnabled(False) self.lblTrimDir.setVisible(False) self.trimdirCount.setVisible(False) self.rbOWNewer.setVisible(False) self.rbOWLarger.setVisible(False) self.rbOWEither.setVisible(False) self.context = zmq.Context() self.socket = self.context.socket(zmq.PAIR) self.socket.bind("tcp://*:%s" % zmq_port) self.copyWorker = CopyWorker() self.connect( self.copyWorker, SIGNAL("copyComplete(QString, QString, QString, QString)"), self.copy_complete, Qt.QueuedConnection) self.connect(self.copyWorker, SIGNAL("spaceProblem(int, int)"), self.space_problem, Qt.QueuedConnection) def unselectItem(self, item): ##need to figure out how to remove from the model self.listWidget.takeItem(item.row()) def copy_complete(self, filecount, filesize, runtime, run_seconds): self.progress.setValue(self.progress.maximum()) transfer_rate = round((float(filesize) * 1024) / float(run_seconds), 3) filesize = round(float(filesize), 3) QMessageBox.information( self, "File Copy Complete", """Your file copy has been successfully completed.\n Files processed:\t%s\n Data copied:\t%sGB\n Total runtime:\t%s\n Transfer Rate:\t%sMB/Sec""" % (filecount, filesize, runtime, transfer_rate), WindowModility=True) self.copyButton.setEnabled(True) def space_problem(self, dirsize, filesize): """Display a dialog to the user advising that there is not enough space in the destination directory. Input: dirsize : integer - amount of space available in the destination directory filesize: integer - size of the selected files Output: None, dialog is displayed to the user.""" ##TODO: Set the messagebox modal property to true required_space = (filesize / 1024.00 / 1024.00 / 1024.00) - (dirsize / 1024.00 / 1024.00 / 1024.00) QMessageBox.critical( self, "Not enough space", """You do not have enough space in your selected destination to complete this operation\n %s more GB space required""" % required_space, WindowModility=True) self.copyWorker.quit() self.copyButton.setEnabled(True) def build_dir_tree(self): """Add a directory tree listing to the QTreeView and set the root to the drive that it was run from. Input: None Output: None""" ##TODO: add linux support for the model root drive. self.model = QFileSystemModel(self) if sys.platform == 'win32': self.model.setRootPath(os.path.splitdrive(os.getcwd())[0]) self.tree = self.treeView self.tree.setModel(self.model) self.tree.setAnimated(False) self.tree.setIndentation(20) self.tree.setSortingEnabled(True) def update_table_view(self): """Refresh listview with selected items in the treeView using the shared model. Input: None Output: None""" itemlist = [ os.path.abspath( self.model.filePath( self.model.index(selection.row(), 0, selection.parent()))) for selection in self.treeView.selectedIndexes() ] self.listWidget.clear() self.listWidget.addItems(itemlist) nitemlist = [] fileops = FileOperations() if not self.ckbxTrimDir.isChecked(): flattencount = 0 else: flattencount = self.trimdirCount.value() if self.lblDestPath.isEnabled(): self.previewView.clear() for item in itemlist: nitemlist.append( fileops.get_dest_filepath(item, self.lblDestPath.text(), flattencount)) self.previewView.addItems(nitemlist) else: self.previewView.clear() self.previewView.addItems(['No destination folder selected']) self.resize_tree_column() def resize_tree_column(self): """Resize the treeView column to fit the contents. Input: None Output: None""" self.treeView.resizeColumnToContents(0) def copy_files(self): """Initiate copy process. File size is calculated first to check that there is enough space in the destination. If there is enough space then we start the copy of the files to their destination. Input: None Output: None""" self.copyButton.setEnabled(False) self.copyWorker.must_run = True self.connect(self.copyWorker, SIGNAL("copyProgress(QString, QString, QString)"), self.copy_progress, Qt.QueuedConnection) dest_dir = self.lblDestPath.text() if dest_dir == '': QMessageBox.critical(self, "Destination not set", "Please specify a destination path", WindowModility=True) else: copy_filelist = [] for selection in self.treeView.selectedIndexes(): indexItem = self.model.index(selection.row(), 0, selection.parent()) copy_filelist.append(self.model.filePath(indexItem)) if self.cbOWDest.isChecked(): if self.rbOWEither.isChecked(): overwrite_option = 'either' elif self.rbOWLarger.isChecked(): overwrite_option = 'larger' elif self.rbOWNewer.isChecked(): overwrite_option = 'newer' else: QMessageBox.critical( self, "Overwrite option missing", """You did not select an overwrite option.""", WindowModility=True) self.copyButton.setEnabled(True) return else: overwrite_option = None if not self.ckbxTrimDir.isChecked(): flattencount = 0 else: flattencount = self.trimdirCount.value() self.progress = QProgressDialog("Copy in progress.", "Cancel", 0, 100, modal=True) self.progress.canceled.connect(self.cancel_copy) self.progress.setWindowTitle('Copy Progress') var_values = { 'destdir': dest_dir, 'filelist': copy_filelist, 'flattencount': flattencount, 'overwrite_opt': overwrite_option } self.socket.send(json.dumps(var_values)) self.copyWorker.start() def copy_progress(self, percentage_complete, filecount, filecomplete): """Display the progress bar with a completed percentage. Input: percentage_complete : integer - the amount complete in percent. filecount : integer - the total number of files being processed. filecomplete : integer - the number of files that have already been processed. Output: None, dialog is updated""" ##TODO: display the current transfer rate ##TODO: display the current file being transferred and possibly the progress thereof. ##Perhaps use the statusbar method for this self.progress.setValue(int(percentage_complete)) def cancel_copy(self): """Slot for the cancel command on the progress dialog. The must_run variable of the copyWorker class is set to False to terminate the copy. Input: None Output: None""" self.copyWorker.must_run = False self.copyButton.setEnabled(True) def statusbar_msg(self, msg): """Update the statusbar on the bottom of the screen. Input: msg : string - Message that you would like displayed on the form. Output: None """ self.statusbar.clearMessage() self.statusbar.showMessage(msg) def destination_chooser(self): """Show folder chooser dialog and update lblDestPath with path selected. Input: None Output: None""" dialog = QFileDialog() dialog.setFileMode(QFileDialog.Directory) dialog.setOption(QFileDialog.ShowDirsOnly) dialog.exec_() self.lblDestPath.setEnabled(True) self.lblDestPath.setText( os.path.abspath(dialog.directory().absolutePath())) self.update_table_view() self.copyButton.setEnabled(True) def about(self): """Popup a box with about message. Input: None Output: None""" QMessageBox.about( self, "About MClub Mover", """This program is designed to help make the process of copying \ files from multiple directories much easier and simpler.\n This software is provided as is with absolutely no warranties.""", WindowModility=True)
def _create_file_system_model(self, root_path): model = QFileSystemModel() model.setRootPath(root_path) self.setModel(model) self.setRootIndex(model.index(root_path)) return model
class FileBrowser(QMainWindow): """Example file browsing widget. Based of the C++ example.""" def __init__(self, parent=None, flags=Qt.Widget): super(FileBrowser, self).__init__(parent, flags) self.gallery = QDocumentGallery(self) self.fileSystemModel = QFileSystemModel(self) self.rootPath = QDesktopServices.storageLocation(QDesktopServices.HomeLocation) self.fileSystemModel.setRootPath(self.rootPath) self.view = QListView() self.view.setModel(self.fileSystemModel) self.view.activated.connect(self.activated) self.setCentralWidget(self.view) self.menuBar().addAction(self.tr("Documents"), self.browseDocuments) self.menuBar().addAction(self.tr("Audio"), self.browseAudio) self.menuBar().addAction(self.tr("Images"), self.browseImages) self.menuBar().addAction(self.tr("Videos"), self.browseVideos) self.browseDocuments() def activated(self, index): fileInfo = self.fileSystemModel.fileInfo(index) if fileInfo.isDir() and fileInfo.fileName() != '.': if fileInfo.fileName() == '..': parent = self.view.rootIndex().parent() fileInfo = self.fileSystemModel.fileInfo(parent) if fileInfo.absoluteFilePath() == self.rootPath: self.fileSystemModel.setFilter(QDir.AllEntries | QDir.NoDotAndDotDot | QDir.AllDirs) self.view.setRootIndex(parent) else: self.fileSystemModel.setFilter(QDir.AllEntries | QDir.AllDirs) self.view.setRootIndex(index) self.setWindowTitle(fileInfo.fileName()) else: if fileInfo.fileName() == '.': fileInfo = self.fileSystemModel.fileInfo(self.view.rootIndex()) widget = DocumentPropertiesWidget(fileInfo, self.gallery, self) widget.setWindowFlags(self.window().windowFlags() | Qt.Dialog) widget.setAttribute(Qt.WA_DeleteOnClose) widget.setWindowModality(Qt.WindowModal) widget.show() def browseAudio(self): self.rootPath = QDesktopServices.storageLocation(QDesktopServices.MusicLocation) self.fileSystemModel.setFilter(QDir.AllEntries | QDir.NoDotAndDotDot | QDir.AllDirs) self.view.setRootIndex(self.fileSystemModel.index(self.rootPath)) self.setWindowTitle(self.tr("Audio")) def browseDocuments(self): self.rootPath = QDesktopServices.storageLocation(QDesktopServices.DocumentsLocation) self.fileSystemModel.setFilter(QDir.AllEntries | QDir.NoDotAndDotDot | QDir.AllDirs) self.view.setRootIndex(self.fileSystemModel.index(self.rootPath)) self.setWindowTitle(self.tr("Documents")) def browseImages(self): self.rootPath = QDesktopServices.storageLocation(QDesktopServices.PicturesLocation) self.fileSystemModel.setFilter(QDir.AllEntries | QDir.NoDotAndDotDot | QDir.AllDirs) self.view.setRootIndex(self.fileSystemModel.index(self.rootPath)) self.setWindowTitle(self.tr("Images")) def browseVideos(self): self.rootPath = QDesktopServices.storageLocation(QDesktopServices.MoviesLocation) self.fileSystemModel.setFilter(QDir.AllEntries | QDir.NoDotAndDotDot | QDir.AllDirs) self.view.setRootIndex(self.fileSystemModel.index(self.rootPath)) self.setWindowTitle(self.tr("Videos"))
class FileBrowser(QMainWindow): """Example file browsing widget. Based of the C++ example.""" def __init__(self, parent=None, flags=Qt.Widget): super(FileBrowser, self).__init__(parent, flags) self.gallery = QDocumentGallery(self) self.fileSystemModel = QFileSystemModel(self) self.rootPath = QDesktopServices.storageLocation( QDesktopServices.HomeLocation) self.fileSystemModel.setRootPath(self.rootPath) self.view = QListView() self.view.setModel(self.fileSystemModel) self.view.activated.connect(self.activated) self.setCentralWidget(self.view) self.menuBar().addAction(self.tr("Documents"), self.browseDocuments) self.menuBar().addAction(self.tr("Audio"), self.browseAudio) self.menuBar().addAction(self.tr("Images"), self.browseImages) self.menuBar().addAction(self.tr("Videos"), self.browseVideos) self.browseDocuments() def activated(self, index): fileInfo = self.fileSystemModel.fileInfo(index) if fileInfo.isDir() and fileInfo.fileName() != '.': if fileInfo.fileName() == '..': parent = self.view.rootIndex().parent() fileInfo = self.fileSystemModel.fileInfo(parent) if fileInfo.absoluteFilePath() == self.rootPath: self.fileSystemModel.setFilter(QDir.AllEntries | QDir.NoDotAndDotDot | QDir.AllDirs) self.view.setRootIndex(parent) else: self.fileSystemModel.setFilter(QDir.AllEntries | QDir.AllDirs) self.view.setRootIndex(index) self.setWindowTitle(fileInfo.fileName()) else: if fileInfo.fileName() == '.': fileInfo = self.fileSystemModel.fileInfo(self.view.rootIndex()) widget = DocumentPropertiesWidget(fileInfo, self.gallery, self) widget.setWindowFlags(self.window().windowFlags() | Qt.Dialog) widget.setAttribute(Qt.WA_DeleteOnClose) widget.setWindowModality(Qt.WindowModal) widget.show() def browseAudio(self): self.rootPath = QDesktopServices.storageLocation( QDesktopServices.MusicLocation) self.fileSystemModel.setFilter(QDir.AllEntries | QDir.NoDotAndDotDot | QDir.AllDirs) self.view.setRootIndex(self.fileSystemModel.index(self.rootPath)) self.setWindowTitle(self.tr("Audio")) def browseDocuments(self): self.rootPath = QDesktopServices.storageLocation( QDesktopServices.DocumentsLocation) self.fileSystemModel.setFilter(QDir.AllEntries | QDir.NoDotAndDotDot | QDir.AllDirs) self.view.setRootIndex(self.fileSystemModel.index(self.rootPath)) self.setWindowTitle(self.tr("Documents")) def browseImages(self): self.rootPath = QDesktopServices.storageLocation( QDesktopServices.PicturesLocation) self.fileSystemModel.setFilter(QDir.AllEntries | QDir.NoDotAndDotDot | QDir.AllDirs) self.view.setRootIndex(self.fileSystemModel.index(self.rootPath)) self.setWindowTitle(self.tr("Images")) def browseVideos(self): self.rootPath = QDesktopServices.storageLocation( QDesktopServices.MoviesLocation) self.fileSystemModel.setFilter(QDir.AllEntries | QDir.NoDotAndDotDot | QDir.AllDirs) self.view.setRootIndex(self.fileSystemModel.index(self.rootPath)) self.setWindowTitle(self.tr("Videos"))
class MTTFilterFileDialog(QDialog): def __init__(self, define_path='', define_type=None): super(MTTFilterFileDialog, self).__init__(get_maya_window()) self.supported_node_type = sorted([ node_type for (node_type, nice, attr) in MTTSettings.SUPPORTED_TYPE ]) self.defined_path = (define_path if os.path.isdir(define_path) or define_path == SOURCEIMAGES_TAG else None) self.defined_type = (define_type if define_type in self.supported_node_type else None) self.path_edit = None self.filter_reset_btn = None self.filter_line = None self.parent_folder_btn = None self.files_model = None self.files_list = None self.bookmark_list = None self.bookmark_list_sel_model = None self.types = None # move window to cursor position win_geo = MTTSettings.value('FilterFileDialog/windowGeometry', QRect(0, 0, 400, 300)) self.setGeometry(win_geo) mouse_pos = QCursor.pos() mouse_pos.setX(mouse_pos.x() - (win_geo.width() * 0.5)) self.move(mouse_pos) self.__create_ui() self.filter_line.setFocus() self.on_change_root_path(self.defined_path or SOURCEIMAGES_TAG) def __create_ui(self): """ Create main UI """ self.setWindowTitle(CREATE_NODE_TITLE) # remove window decoration if path and type is set if self.defined_path and self.defined_type: self.setWindowFlags(Qt.FramelessWindowHint | Qt.Popup) main_layout = QVBoxLayout(self) main_layout.setSpacing(1) main_layout.setContentsMargins(2, 2, 2, 2) # content layout content_layout = QVBoxLayout() self.files_model = QFileSystemModel() self.files_model.setNameFilterDisables(False) self.files_list = MTTFileList() self.files_list.setAlternatingRowColors(True) self.files_list.setSelectionMode(QAbstractItemView.ExtendedSelection) self.files_list.selectionValidated.connect(self.do_validate_selection) self.files_list.goToParentDirectory.connect(self.on_go_up_parent) self.files_list.doubleClicked.connect(self.on_double_click) self.files_list.setModel(self.files_model) buttons_layout = QHBoxLayout() content_layout.addLayout(self.__create_filter_ui()) content_layout.addWidget(self.files_list) content_layout.addLayout(buttons_layout) self.files_list.filter_line = self.filter_line if not self.defined_path: # path line path_layout = QHBoxLayout() # bookmark button bookmark_btn = QPushButton('') bookmark_btn.setFlat(True) bookmark_btn.setIcon(QIcon(':/addBookmark.png')) bookmark_btn.setToolTip('Bookmark this Folder') bookmark_btn.setStatusTip('Bookmark this Folder') bookmark_btn.clicked.connect(self.on_add_bookmark) # path line edit self.path_edit = QLineEdit() self.path_edit.editingFinished.connect(self.on_enter_path) # parent folder button self.parent_folder_btn = QPushButton('') self.parent_folder_btn.setFlat(True) self.parent_folder_btn.setIcon( QIcon(':/SP_FileDialogToParent.png')) self.parent_folder_btn.setToolTip('Parent Directory') self.parent_folder_btn.setStatusTip('Parent Directory') self.parent_folder_btn.clicked.connect(self.on_go_up_parent) # browse button browse_btn = QPushButton('') browse_btn.setFlat(True) browse_btn.setIcon(QIcon(':/navButtonBrowse.png')) browse_btn.setToolTip('Browse Directory') browse_btn.setStatusTip('Browse Directory') browse_btn.clicked.connect(self.on_browse) # parent widget and layout path_layout.addWidget(bookmark_btn) path_layout.addWidget(self.path_edit) path_layout.addWidget(self.parent_folder_btn) path_layout.addWidget(browse_btn) main_layout.addLayout(path_layout) # bookmark list bookmark_parent_layout = QHBoxLayout() bookmark_frame = QFrame() bookmark_frame.setFixedWidth(120) bookmark_layout = QVBoxLayout() bookmark_layout.setSpacing(1) bookmark_layout.setContentsMargins(2, 2, 2, 2) bookmark_frame.setLayout(bookmark_layout) bookmark_frame.setFrameStyle(QFrame.Sunken) bookmark_frame.setFrameShape(QFrame.StyledPanel) self.bookmark_list = MTTBookmarkList() self.bookmark_list.bookmarkDeleted.connect(self.do_delete_bookmark) self.bookmark_list.setAlternatingRowColors(True) self.bookmark_list.dragEnabled() self.bookmark_list.setAcceptDrops(True) self.bookmark_list.setDropIndicatorShown(True) self.bookmark_list.setDragDropMode(QListView.InternalMove) self.bookmark_list_sel_model = self.bookmark_list.selectionModel() self.bookmark_list_sel_model.selectionChanged.connect( self.on_select_bookmark) bookmark_layout.addWidget(self.bookmark_list) bookmark_parent_layout.addWidget(bookmark_frame) bookmark_parent_layout.addLayout(content_layout) main_layout.addLayout(bookmark_parent_layout) self.do_populate_bookmarks() else: main_layout.addLayout(content_layout) if not self.defined_type: # type layout self.types = QComboBox() self.types.addItems(self.supported_node_type) self.types.currentIndexChanged.connect(self.on_node_type_changed) if cmds.optionVar(exists='MTT_lastNodeType'): last = cmds.optionVar(query='MTT_lastNodeType') if last in self.supported_node_type: self.types.setCurrentIndex( self.supported_node_type.index(last)) buttons_layout.addWidget(self.types) if not self.defined_path or not self.defined_type: create_btn = QPushButton('C&reate') create_btn.clicked.connect(self.accept) cancel_btn = QPushButton('&Cancel') cancel_btn.clicked.connect(self.reject) buttons_layout.addStretch() buttons_layout.addWidget(create_btn) buttons_layout.addWidget(cancel_btn) def __create_filter_ui(self): """ Create filter widgets """ filter_layout = QHBoxLayout() filter_layout.setSpacing(1) filter_layout.setContentsMargins(0, 0, 0, 0) self.filter_reset_btn = QPushButton() icon = QIcon(':/filtersOff.png') self.filter_reset_btn.setIcon(icon) self.filter_reset_btn.setIconSize(QSize(22, 22)) self.filter_reset_btn.setFixedSize(24, 24) self.filter_reset_btn.setToolTip('Reset filter') self.filter_reset_btn.setFlat(True) self.filter_reset_btn.clicked.connect( partial(self.on_filter_set_text, '')) self.filter_line = QLineEdit() self.filter_line.setPlaceholderText('Enter filter string here') self.filter_line.textChanged.connect(self.on_filter_change_text) completer = QCompleter(self) completer.setCaseSensitivity(Qt.CaseInsensitive) completer.setModel(QStringListModel([], self)) self.filter_line.setCompleter(completer) filter_layout.addWidget(self.filter_reset_btn) filter_layout.addWidget(self.filter_line) return filter_layout def on_filter_set_text(self, text=''): """ Set text in filter field """ self.filter_line.setText(text) def on_filter_change_text(self, text): """ Apply filter string """ if len(text): icon = QIcon(':/filtersOn.png') self.filter_reset_btn.setIcon(icon) else: icon = QIcon(':/filtersOff.png') self.filter_reset_btn.setIcon(icon) self.files_model.setNameFilters([ '*%s*' % item.strip() for item in text.split(',') if item.strip() ]) def on_node_type_changed(self, index): cmds.optionVar( sv=['MTT_lastNodeType', self.supported_node_type[index]]) def on_double_click(self, index): current_item = self.files_model.filePath(index) if os.path.isdir(current_item): self.on_change_root_path(current_item) elif os.path.isfile(current_item): self.accept() def on_change_root_path(self, current_path): if current_path == SOURCEIMAGES_TAG: current_path = os.path.join( cmds.workspace(query=True, rootDirectory=True), cmds.workspace(fileRuleEntry='sourceImages')) self.files_model.setRootPath(current_path) self.files_list.setRootIndex(self.files_model.index(current_path)) if self.path_edit: self.path_edit.setText(current_path) if self.parent_folder_btn: current_dir = QDir(current_path) self.parent_folder_btn.setEnabled(current_dir.cdUp()) def on_go_up_parent(self): current_path = QDir(self.files_model.rootPath()) current_path.cdUp() self.on_change_root_path(current_path.absolutePath()) def on_enter_path(self): new_path = self.path_edit.text() if os.path.isdir(new_path): self.on_change_root_path(new_path) else: self.path_edit.setText(self.files_model.rootPath()) def on_browse(self): current_path = self.files_model.rootPath() file_dialog = QFileDialog(self, 'Select a Folder', current_path) file_dialog.setFileMode(QFileDialog.Directory) file_dialog.setOption(QFileDialog.ShowDirsOnly) if file_dialog.exec_(): self.on_change_root_path(file_dialog.selectedFiles()[0]) def on_select_bookmark(self, selected, deselected): current_item = selected.indexes() if current_item: self.on_change_root_path( self.bookmark_list.selectedItems()[0].root_path) def on_add_bookmark(self): current_path = self.files_model.rootPath() self.on_add_bookmark_item( '%s|%s' % (os.path.basename(current_path), current_path)) def on_add_bookmark_item(self, item): if item == '': return current_item = MTTBookmarkItem() current_item.add_raw_data(item) current_item.setSizeHint(QSize(40, 25)) current_item.setFlags(Qt.ItemIsEnabled | Qt.ItemIsEditable | Qt.ItemIsSelectable | Qt.ItemIsDragEnabled) self.bookmark_list.addItem(current_item) def do_delete_bookmark(self): current_item = self.bookmark_list.selectedItems() if current_item: item_row = self.bookmark_list.row(current_item[0]) self.bookmark_list.takeItem(item_row) del current_item[0] def do_save_bookmark(self): if not self.bookmark_list: return row_count = self.bookmark_list.count() ordered_list = list() for i in range(row_count): item = self.bookmark_list.item(i) name = item.text().replace(',', '_').replace('|', '_') path = item.root_path if name != 'sourceimages': ordered_list.append('%s|%s' % (name, path)) MTTSettings.set_value('FilterFileDialog/bookmarks', ','.join(ordered_list)) def do_populate_bookmarks(self): bookmarks = ['sourceimages|%s' % SOURCEIMAGES_TAG] bookmarks.extend( MTTSettings.value('FilterFileDialog/bookmarks').split(',')) for bm in bookmarks: self.on_add_bookmark_item(bm) def do_validate_selection(self): selection = self.files_list.selectedIndexes() if len(selection) == 1: current_path = self.files_model.filePath(selection[0]) if os.path.isdir(current_path): self.on_change_root_path(current_path) return self.accept() def get_selected_files(self): selected_items = list() for item_index in self.files_list.selectedIndexes(): current_path = self.files_model.filePath(item_index) if os.path.isfile(current_path): selected_items.append(current_path) return selected_items def get_node_type(self): return self.types.currentText() if self.types else self.defined_type def closeEvent(self, event): MTTSettings.set_value('FilterFileDialog/windowGeometry', self.geometry()) self.do_save_bookmark() self.deleteLater() event.accept()
class MTTFilterFileDialog(QDialog): def __init__(self, define_path='', define_type=None): super(MTTFilterFileDialog, self).__init__(get_maya_window()) self.supported_node_type = sorted( [node_type for (node_type, nice, attr) in MTTSettings.SUPPORTED_TYPE]) self.defined_path = ( define_path if os.path.isdir(define_path) or define_path == SOURCEIMAGES_TAG else None) self.defined_type = ( define_type if define_type in self.supported_node_type else None) self.path_edit = None self.filter_reset_btn = None self.filter_line = None self.parent_folder_btn = None self.files_model = None self.files_list = None self.bookmark_list = None self.bookmark_list_sel_model = None self.types = None # move window to cursor position win_geo = MTTSettings.value( 'FilterFileDialog/windowGeometry', QRect(0, 0, 400, 300)) self.setGeometry(win_geo) mouse_pos = QCursor.pos() mouse_pos.setX(mouse_pos.x() - (win_geo.width() * 0.5)) self.move(mouse_pos) self.__create_ui() self.filter_line.setFocus() self.on_change_root_path(self.defined_path or SOURCEIMAGES_TAG) def __create_ui(self): """ Create main UI """ self.setWindowTitle(CREATE_NODE_TITLE) # remove window decoration if path and type is set if self.defined_path and self.defined_type: self.setWindowFlags(Qt.FramelessWindowHint | Qt.Popup) main_layout = QVBoxLayout(self) main_layout.setSpacing(1) main_layout.setContentsMargins(2, 2, 2, 2) # content layout content_layout = QVBoxLayout() self.files_model = QFileSystemModel() self.files_model.setNameFilterDisables(False) self.files_list = MTTFileList() self.files_list.setAlternatingRowColors(True) self.files_list.setSelectionMode(QAbstractItemView.ExtendedSelection) self.files_list.selectionValidated.connect(self.do_validate_selection) self.files_list.goToParentDirectory.connect(self.on_go_up_parent) self.files_list.doubleClicked.connect(self.on_double_click) self.files_list.setModel(self.files_model) buttons_layout = QHBoxLayout() content_layout.addLayout(self.__create_filter_ui()) content_layout.addWidget(self.files_list) content_layout.addLayout(buttons_layout) self.files_list.filter_line = self.filter_line if not self.defined_path: # path line path_layout = QHBoxLayout() # bookmark button bookmark_btn = QPushButton('') bookmark_btn.setFlat(True) bookmark_btn.setIcon(QIcon(':/addBookmark.png')) bookmark_btn.setToolTip('Bookmark this Folder') bookmark_btn.setStatusTip('Bookmark this Folder') bookmark_btn.clicked.connect(self.on_add_bookmark) # path line edit self.path_edit = QLineEdit() self.path_edit.editingFinished.connect(self.on_enter_path) # parent folder button self.parent_folder_btn = QPushButton('') self.parent_folder_btn.setFlat(True) self.parent_folder_btn.setIcon(QIcon(':/SP_FileDialogToParent.png')) self.parent_folder_btn.setToolTip('Parent Directory') self.parent_folder_btn.setStatusTip('Parent Directory') self.parent_folder_btn.clicked.connect(self.on_go_up_parent) # browse button browse_btn = QPushButton('') browse_btn.setFlat(True) browse_btn.setIcon(QIcon(':/navButtonBrowse.png')) browse_btn.setToolTip('Browse Directory') browse_btn.setStatusTip('Browse Directory') browse_btn.clicked.connect(self.on_browse) # parent widget and layout path_layout.addWidget(bookmark_btn) path_layout.addWidget(self.path_edit) path_layout.addWidget(self.parent_folder_btn) path_layout.addWidget(browse_btn) main_layout.addLayout(path_layout) # bookmark list bookmark_parent_layout = QHBoxLayout() bookmark_frame = QFrame() bookmark_frame.setFixedWidth(120) bookmark_layout = QVBoxLayout() bookmark_layout.setSpacing(1) bookmark_layout.setContentsMargins(2, 2, 2, 2) bookmark_frame.setLayout(bookmark_layout) bookmark_frame.setFrameStyle(QFrame.Sunken) bookmark_frame.setFrameShape(QFrame.StyledPanel) self.bookmark_list = MTTBookmarkList() self.bookmark_list.bookmarkDeleted.connect(self.do_delete_bookmark) self.bookmark_list.setAlternatingRowColors(True) self.bookmark_list.dragEnabled() self.bookmark_list.setAcceptDrops(True) self.bookmark_list.setDropIndicatorShown(True) self.bookmark_list.setDragDropMode(QListView.InternalMove) self.bookmark_list_sel_model = self.bookmark_list.selectionModel() self.bookmark_list_sel_model.selectionChanged.connect( self.on_select_bookmark) bookmark_layout.addWidget(self.bookmark_list) bookmark_parent_layout.addWidget(bookmark_frame) bookmark_parent_layout.addLayout(content_layout) main_layout.addLayout(bookmark_parent_layout) self.do_populate_bookmarks() else: main_layout.addLayout(content_layout) if not self.defined_type: # type layout self.types = QComboBox() self.types.addItems(self.supported_node_type) self.types.currentIndexChanged.connect(self.on_node_type_changed) if cmds.optionVar(exists='MTT_lastNodeType'): last = cmds.optionVar(query='MTT_lastNodeType') if last in self.supported_node_type: self.types.setCurrentIndex( self.supported_node_type.index(last)) buttons_layout.addWidget(self.types) if not self.defined_path or not self.defined_type: create_btn = QPushButton('C&reate') create_btn.clicked.connect(self.accept) cancel_btn = QPushButton('&Cancel') cancel_btn.clicked.connect(self.reject) buttons_layout.addStretch() buttons_layout.addWidget(create_btn) buttons_layout.addWidget(cancel_btn) def __create_filter_ui(self): """ Create filter widgets """ filter_layout = QHBoxLayout() filter_layout.setSpacing(1) filter_layout.setContentsMargins(0, 0, 0, 0) self.filter_reset_btn = QPushButton() icon = QIcon(':/filtersOff.png') self.filter_reset_btn.setIcon(icon) self.filter_reset_btn.setIconSize(QSize(22, 22)) self.filter_reset_btn.setFixedSize(24, 24) self.filter_reset_btn.setToolTip('Reset filter') self.filter_reset_btn.setFlat(True) self.filter_reset_btn.clicked.connect( partial(self.on_filter_set_text, '')) self.filter_line = QLineEdit() self.filter_line.setPlaceholderText('Enter filter string here') self.filter_line.textChanged.connect(self.on_filter_change_text) completer = QCompleter(self) completer.setCaseSensitivity(Qt.CaseInsensitive) completer.setModel(QStringListModel([], self)) self.filter_line.setCompleter(completer) filter_layout.addWidget(self.filter_reset_btn) filter_layout.addWidget(self.filter_line) return filter_layout def on_filter_set_text(self, text=''): """ Set text in filter field """ self.filter_line.setText(text) def on_filter_change_text(self, text): """ Apply filter string """ if len(text): icon = QIcon(':/filtersOn.png') self.filter_reset_btn.setIcon(icon) else: icon = QIcon(':/filtersOff.png') self.filter_reset_btn.setIcon(icon) self.files_model.setNameFilters( ['*%s*' % item.strip() for item in text.split(',') if item.strip()]) def on_node_type_changed(self, index): cmds.optionVar(sv=['MTT_lastNodeType', self.supported_node_type[index]]) def on_double_click(self, index): current_item = self.files_model.filePath(index) if os.path.isdir(current_item): self.on_change_root_path(current_item) elif os.path.isfile(current_item): self.accept() def on_change_root_path(self, current_path): if current_path == SOURCEIMAGES_TAG: current_path = os.path.join( cmds.workspace(query=True, rootDirectory=True), cmds.workspace(fileRuleEntry='sourceImages') ) self.files_model.setRootPath(current_path) self.files_list.setRootIndex(self.files_model.index(current_path)) if self.path_edit: self.path_edit.setText(current_path) if self.parent_folder_btn: current_dir = QDir(current_path) self.parent_folder_btn.setEnabled(current_dir.cdUp()) def on_go_up_parent(self): current_path = QDir(self.files_model.rootPath()) current_path.cdUp() self.on_change_root_path(current_path.absolutePath()) def on_enter_path(self): new_path = self.path_edit.text() if os.path.isdir(new_path): self.on_change_root_path(new_path) else: self.path_edit.setText(self.files_model.rootPath()) def on_browse(self): current_path = self.files_model.rootPath() file_dialog = QFileDialog(self, 'Select a Folder', current_path) file_dialog.setFileMode(QFileDialog.Directory) file_dialog.setOption(QFileDialog.ShowDirsOnly) if file_dialog.exec_(): self.on_change_root_path(file_dialog.selectedFiles()[0]) def on_select_bookmark(self, selected, deselected): current_item = selected.indexes() if current_item: self.on_change_root_path( self.bookmark_list.selectedItems()[0].root_path) def on_add_bookmark(self): current_path = self.files_model.rootPath() self.on_add_bookmark_item( '%s|%s' % (os.path.basename(current_path), current_path)) def on_add_bookmark_item(self, item): if item == '': return current_item = MTTBookmarkItem() current_item.add_raw_data(item) current_item.setSizeHint(QSize(40, 25)) current_item.setFlags( Qt.ItemIsEnabled | Qt.ItemIsEditable | Qt.ItemIsSelectable | Qt.ItemIsDragEnabled ) self.bookmark_list.addItem(current_item) def do_delete_bookmark(self): current_item = self.bookmark_list.selectedItems() if current_item: item_row = self.bookmark_list.row(current_item[0]) self.bookmark_list.takeItem(item_row) del current_item[0] def do_save_bookmark(self): if not self.bookmark_list: return row_count = self.bookmark_list.count() ordered_list = list() for i in range(row_count): item = self.bookmark_list.item(i) name = item.text().replace(',', '_').replace('|', '_') path = item.root_path if name != 'sourceimages': ordered_list.append('%s|%s' % (name, path)) MTTSettings.set_value( 'FilterFileDialog/bookmarks', ','.join(ordered_list)) def do_populate_bookmarks(self): bookmarks = ['sourceimages|%s' % SOURCEIMAGES_TAG] bookmarks.extend( MTTSettings.value('FilterFileDialog/bookmarks').split(',')) for bm in bookmarks: self.on_add_bookmark_item(bm) def do_validate_selection(self): selection = self.files_list.selectedIndexes() if len(selection) == 1: current_path = self.files_model.filePath(selection[0]) if os.path.isdir(current_path): self.on_change_root_path(current_path) return self.accept() def get_selected_files(self): selected_items = list() for item_index in self.files_list.selectedIndexes(): current_path = self.files_model.filePath(item_index) if os.path.isfile(current_path): selected_items.append(current_path) return selected_items def get_node_type(self): return self.types.currentText() if self.types else self.defined_type def closeEvent(self, event): MTTSettings.set_value( 'FilterFileDialog/windowGeometry', self.geometry()) self.do_save_bookmark() self.deleteLater() event.accept()
class RobocompDslGui(QMainWindow): def __init__(self, parent=None): super(RobocompDslGui, self).__init__(parent) self.setWindowTitle("Create new component") # self._idsl_paths = [] self._communications = { "implements": [], "requires": [], "subscribesTo": [], "publishes": [] } self._interfaces = {} self._cdsl_doc = CDSLDocument() self._command_process = QProcess() self._main_widget = QWidget() self._main_layout = QVBoxLayout() self.setCentralWidget(self._main_widget) self._name_layout = QHBoxLayout() self._name_line_edit = QLineEdit() self._name_line_edit.textEdited.connect(self.update_component_name) self._name_line_edit.setPlaceholderText("New component name") self._name_layout.addWidget(self._name_line_edit) self._name_layout.addStretch() # DIRECTORY SELECTION self._dir_line_edit = QLineEdit() # self._dir_line_edit.textEdited.connect(self.update_completer) self._dir_completer = QCompleter() self._dir_completer_model = QFileSystemModel() if os.path.isdir(ROBOCOMP_COMP_DIR): self._dir_line_edit.setText(ROBOCOMP_COMP_DIR) self._dir_completer_model.setRootPath(ROBOCOMP_COMP_DIR) self._dir_completer.setModel(self._dir_completer_model) self._dir_line_edit.setCompleter(self._dir_completer) self._dir_button = QPushButton("Select directory") self._dir_button.clicked.connect(self.set_output_directory) self._dir_layout = QHBoxLayout() self._dir_layout.addWidget(self._dir_line_edit) self._dir_layout.addWidget(self._dir_button) # LIST OF ROBOCOMP INTERFACES self._interface_list = QListWidget() self._interface_list.setSelectionMode( QAbstractItemView.ExtendedSelection) self._interface_list.itemSelectionChanged.connect( self.set_comunication) # LIST OF CONNECTION TyPES self._type_combo_box = QComboBox() self._type_combo_box.addItems( ["publishes", "implements", "subscribesTo", "requires"]) self._type_combo_box.currentIndexChanged.connect( self.reselect_existing) # BUTTON TO ADD A NEW CONNECTION # self._add_connection_button = QPushButton("Add") # self._add_connection_button.clicked.connect(self.add_new_comunication) self._add_connection_layout = QHBoxLayout() # self._add_connection_layout.addWidget(self._add_connection_button) self._language_combo_box = QComboBox() self._language_combo_box.addItems(["Python", "Cpp", "Cpp11"]) self._language_combo_box.currentIndexChanged.connect( self.update_language) self._add_connection_layout.addWidget(self._language_combo_box) self._add_connection_layout.addStretch() self._gui_check_box = QCheckBox() self._gui_check_box.stateChanged.connect(self.update_gui_selection) self._gui_label = QLabel("Use Qt GUI") self._add_connection_layout.addWidget(self._gui_label) self._add_connection_layout.addWidget(self._gui_check_box) # WIDGET CONTAINING INTERFACES AND TYPES self._selection_layout = QVBoxLayout() self._selection_layout.addWidget(self._type_combo_box) self._selection_layout.addWidget(self._interface_list) self._selection_layout.addLayout(self._add_connection_layout) self._selection_widget = QWidget() self._selection_widget.setLayout(self._selection_layout) # TEXT EDITOR WITH THE RESULTING CDSL CODE self._editor = QTextEdit(self) self._editor.setHtml("") self._document = self._editor.document() self._component_directory = None # SPLITTER WITH THE SELECTION AND THE CODE self._body_splitter = QSplitter(Qt.Horizontal) self._body_splitter.addWidget(self._selection_widget) self._body_splitter.addWidget(self._editor) self._body_splitter.setStretchFactor(0, 2) self._body_splitter.setStretchFactor(1, 9) # CREATION BUTTONS self._create_button = QPushButton("Create .cdsl") self._create_button.clicked.connect(self.write_cdsl_file) self._creation_layout = QHBoxLayout() self._creation_layout.addStretch() self._creation_layout.addWidget(self._create_button) self._console = QConsole() self._command_process.readyReadStandardOutput.connect( self._console.standard_output) self._command_process.readyReadStandardError.connect( self._console.error_output) # ADDING WIDGETS TO MAIN LAYOUT self._main_widget.setLayout(self._main_layout) self._main_layout.addLayout(self._name_layout) self._main_layout.addLayout(self._dir_layout) self._main_layout.addWidget(self._body_splitter) self._main_layout.addLayout(self._creation_layout) self._main_layout.addWidget(self._console) self.setMinimumSize(800, 500) self._editor.setText(self._cdsl_doc.generate_doc()) # self.editor->show(); # def update_completer(self, path): # print "update_completer %s"%path # info = QFileInfo(path) # if info.exists() and info.isDir(): # if not path.endswith(os.path.pathsep): # new_path = os.path.join(path, os.sep) # # self._dir_line_edit.setText(new_path) # all_dirs_output = [dI for dI in os.listdir(path) if os.path.isdir(os.path.join(path, dI))] # print all_dirs_output # self._dir_completer.complete() def load_idsl_files(self, fullpath=None): if fullpath is None: fullpath = ROBOCOMP_INTERFACES idsls_dir = os.path.join(ROBOCOMP_INTERFACES, "IDSLs") if os.path.isdir(idsls_dir): for full_filename in os.listdir(idsls_dir): file_name, file_extension = os.path.splitext(full_filename) if "idsl" in file_extension.lower(): full_idsl_path = os.path.join(idsls_dir, full_filename) # self._idsl_paths.append(os.path.join(idsls_dir,full_filename)) self.parse_idsl_file(full_idsl_path) self._interface_list.addItems(self._interfaces.keys()) def parse_idsl_file(self, fullpath): with open(fullpath, 'r') as fin: interface_name = None for line in fin: result = re.findall(r'^\s*interface\s+(\w+)\s*\{?\s*$', line, flags=re.MULTILINE) if len(result) > 0: interface_name = result[0] print("%s for idsl %s" % (interface_name, fullpath)) if interface_name is not None: self._interfaces[interface_name] = fullpath def add_new_comunication(self): interface_names = self._interface_list.selectedItems() com_type = str(self._type_combo_box.currentText()) for iface_name_item in interface_names: iface_name = str(iface_name_item.text()) self._communications[com_type].append(iface_name) idsl_full_path = self._interfaces[iface_name] idsl_full_filename = os.path.basename(idsl_full_path) self._cdsl_doc.add_comunication(com_type, iface_name) self._cdsl_doc.add_import(idsl_full_filename) self.update_editor() def set_comunication(self): interface_names = self._interface_list.selectedItems() com_type = str(self._type_combo_box.currentText()) self._communications[com_type] = [] self._cdsl_doc.clear_comunication(com_type) for iface_name_item in interface_names: iface_name = str(iface_name_item.text()) self._communications[com_type].append(iface_name) self._cdsl_doc.add_comunication(com_type, iface_name) self.update_imports() self.update_editor() def update_imports(self): self._cdsl_doc.clear_imports() for com_type in self._communications: for iface_name in self._communications[com_type]: idsl_full_path = self._interfaces[iface_name] idsl_full_filename = os.path.basename(idsl_full_path) self._cdsl_doc.add_import(idsl_full_filename) def update_language(self): language = self._language_combo_box.currentText() self._cdsl_doc.set_language(str(language)) self.update_editor() def update_gui_selection(self): checked = self._gui_check_box.isChecked() if checked: self._cdsl_doc.set_qui(True) else: self._cdsl_doc.set_qui(False) self.update_editor() def update_component_name(self, name): self._cdsl_doc.set_name(name) self.update_editor() def update_editor(self): self._editor.setText(self._cdsl_doc.generate_doc()) def set_output_directory(self): dir_set = False while not dir_set: dir = QFileDialog.getExistingDirectory( self, "Select Directory", ROBOCOMP_COMP_DIR, QFileDialog.ShowDirsOnly | QFileDialog.DontResolveSymlinks) if self.check_dir_is_empty(str(dir)): self._dir_line_edit.setText(dir) dir_set = True def write_cdsl_file(self): component_dir = str(self._dir_line_edit.text()) text = self._cdsl_doc.generate_doc() if not self._name_line_edit.text(): component_name, ok = QInputDialog.getText(self, 'No component name set', 'Enter component name:') if ok: self.update_component_name(component_name) self._name_line_edit.setText(component_name) else: return False if not os.path.exists(component_dir): if QMessageBox.Yes == QMessageBox.question( self, "Directory doesn't exist.", "Do you want create the directory %s?" % component_dir, QMessageBox.Yes | QMessageBox.No): os.makedirs(component_dir) else: QMessageBox.question( self, "Directory not exist", "Can't create a component witout a valid directory") return False file_path = os.path.join(component_dir, str(self._name_line_edit.text()) + ".cdsl") if os.path.exists(file_path): if QMessageBox.No == QMessageBox.question( self, "File already exists", "Do you want to overwrite?", QMessageBox.Yes | QMessageBox.No): return False with open(file_path, 'w') as the_file: the_file.write(text) self.execute_robocomp_cdsl() return True def execute_robocomp_cdsl(self): cdsl_file_path = os.path.join( str(self._dir_line_edit.text()), str(self._name_line_edit.text()) + ".cdsl") command = "python -u %s/robocompdsl.py %s %s" % ( ROBOCOMPDSL_DIR, cdsl_file_path, os.path.join(str(self._dir_line_edit.text()))) self._console.append_custom_text("%s\n" % command) self._command_process.start(command, QProcess.Unbuffered | QProcess.ReadWrite) def reselect_existing(self): com_type = self._type_combo_box.currentText() selected = self._communications[com_type] self._interface_list.clearSelection() for iface in selected: items = self._interface_list.findItems(iface, Qt.MatchFlag.MatchExactly) if len(items) > 0: item = items[0] item.setSelected(True) def check_dir_is_empty(self, dir_path): if len(os.listdir(dir_path)) > 0: msgBox = QMessageBox() msgBox.setWindowTitle("Directory not empty") msgBox.setText( "The selected directory is not empty.\n" "For a new Component you usually want a new directory.\n" "Do you want to use this directory anyway?") msgBox.setStandardButtons(QMessageBox.Yes) msgBox.addButton(QMessageBox.No) msgBox.setDefaultButton(QMessageBox.No) if msgBox.exec_() == QMessageBox.Yes: return True else: return False else: return True
class MainWindow(QMainWindow): def __init__(self, parent=None): super(MainWindow, self).__init__(parent) self.dirty = False # Models # - Filesystem Model self.fileSystemModel = QFileSystemModel() # rootPath = QDesktopServices.storageLocation( # QDesktopServices.HomeLocation) # fileSystemRoot = self.fileSystemModel.setRootPath(rootPath) fileSystemRoot = self.fileSystemModel.setRootPath( "/home/ryan/Programming/Python/projects/r3tagger/r3tagger/tests" ) # Views # - Filesystem View self.fileSystemView = QTreeView() self.fileSystemView.setModel(self.fileSystemModel) self.fileSystemView.setRootIndex(fileSystemRoot) self.fileSystemView.doubleClicked.connect(self.updateAlbumModel) self.fileSystemView.expanded.connect(self.fixFileSystemColumns) self.fileSystemView.collapsed.connect(self.fixFileSystemColumns) # - Album View self.albumView = albumcollection.MusicCollectionView() self.albumView.setSelectionMode(QAbstractItemView.MultiSelection) self.albumView.clicked.connect(self.updateEditing) self.albumView.expanded.connect(self.fixAlbumViewColumns) self.albumView.collapsed.connect(self.fixAlbumViewColumns) self.albumView.model().dataChanged.connect(self.fixAlbumViewColumns) self.albumView.model().dataChanged.connect(self._setDirty) model = self.albumView.model() model.dataChanged.connect(self.updateEditing) # Editing Group self.editingGroup = QFormLayout() self.lineArtist = QLineEdit() self.lineAlbum = QLineEdit() self.lineTitle = QLineEdit() self.lineTrack = QLineEdit() self.lineDate = QLineEdit() self.lineGenre = QLineEdit() self.editingGroup.addRow("Artist:", self.lineArtist) self.editingGroup.addRow("Album:", self.lineAlbum) self.editingGroup.addRow("Title:", self.lineTitle) self.editingGroup.addRow("Track:", self.lineTrack) self.editingGroup.addRow("Date:", self.lineDate) self.editingGroup.addRow("Genre:", self.lineGenre) self.tagsToAttribs = { "artist": self.lineArtist, "album": self.lineAlbum, "title": self.lineTitle, "tracknumber": self.lineTrack, "date": self.lineDate, "genre": self.lineGenre, } # Confirm / Cancel / Clear Group self.buttonGroup = QHBoxLayout() self.buttonGroup.addStretch() confirm = QPushButton("Confirm") confirm.clicked.connect(self.confirmChanges) self.buttonGroup.addWidget(confirm) cancel = QPushButton("Cancel") cancel.clicked.connect(self.cancelChanges) self.buttonGroup.addWidget(cancel) clear = QPushButton("Clear") clear.clicked.connect(self.clearAlbumView) self.buttonGroup.addWidget(clear) self.buttonGroup.addStretch() # Statusbar # status = self.statusBar() # status.setSizeGripEnabled(False) # status.showMessage("Ready", 5000) # Docks dockAllowed = Qt.LeftDockWidgetArea | Qt.RightDockWidgetArea # - Filesystem Dock fileSystemDock = QDockWidget("Navigate", self) fileSystemDock.setObjectName("fileSystemDock") fileSystemDock.setAllowedAreas(dockAllowed) fileSystemDock.setWidget(self.fileSystemView) self.addDockWidget(Qt.LeftDockWidgetArea, fileSystemDock) # - Editing Dock editingWidget = QWidget() editingWidget.setLayout(self.editingGroup) editingDock = QDockWidget("Editing", self) editingDock.setObjectName("editingDock") editingDock.setAllowedAreas(dockAllowed) editingDock.setWidget(editingWidget) self.addDockWidget(Qt.RightDockWidgetArea, editingDock) # Actions fileAddSongAction = self._createAction( text="Add &Songs", slot=self.fileAddSong, shortcut=QKeySequence.Open, icon="fileOpen", tip="Add files (songs)", ) fileAddAlbumAction = self._createAction( text="Add &Album", slot=self.fileAddAlbum, shortcut=QKeySequence(Qt.CTRL + Qt.SHIFT + Qt.Key_O), icon="fileOpen", tip="Add directory (album)", ) fileSaveAction = self._createAction( text="&Save Changes", slot=self.confirmChanges, shortcut=QKeySequence.Save, icon="fileSave", tip="Save Changes", ) fileQuitAction = self._createAction( text="&Quit", slot=self.close, shortcut=QKeySequence.Quit, icon="fileQuit", tip="Quit Program" ) editRecognizeAction = self._createAction( text="&Recognize", slot=self.editRecognize, shortcut=QKeySequence(Qt.CTRL + Qt.Key_R), icon="editRecognize", tip="Recognize music", ) editReorganizeAction = self._createAction( text="Reorganize", slot=self.editReorganize, shortcut=QKeySequence(Qt.CTRL + Qt.SHIFT + Qt.Key_R), icon="editReorganize", tip="Reorganize music", ) editSettingsAction = self._createAction( text="Settings", slot=self.editSettings, shortcut=QKeySequence.Preferences, icon="editSettings", tip="Edit settings", ) helpDocsAction = self._createAction( text="Documentation", slot=self.helpDocs, shortcut=QKeySequence.HelpContents, icon="helpDocs", tip="Documentation", ) helpAboutAction = self._createAction( text="About", slot=self.helpAbout, shortcut=QKeySequence(Qt.CTRL + Qt.SHIFT + Qt.Key_F1), icon="helpAbout", tip="About", ) toggleEditing = editingDock.toggleViewAction() toggleEditing.setIcon(QIcon(":/toggleEditing.png")) toggleFileNav = fileSystemDock.toggleViewAction() toggleFileNav.setIcon(QIcon(":/toggleFileNav.png")) # Menus fileMenu = self.menuBar().addMenu("&File") self._addActions(fileMenu, (fileAddSongAction, fileAddAlbumAction, fileSaveAction, fileQuitAction)) editMenu = self.menuBar().addMenu("&Edit") self._addActions(editMenu, (editReorganizeAction, editRecognizeAction, editSettingsAction)) helpMenu = self.menuBar().addMenu("&Help") self._addActions(helpMenu, (helpDocsAction, helpAboutAction)) # Toolbars editToolbar = self.addToolBar("EditToolbar") editToolbar.setObjectName("editToolbar") self._addActions(editToolbar, (editRecognizeAction, editReorganizeAction)) toggleToolbar = self.addToolBar("ToggleToolbar") toggleToolbar.setObjectName("toggleToolbar") self._addActions(toggleToolbar, (toggleFileNav, toggleEditing)) # Settings settings = QSettings() if settings.contains("MainWindow/Geometry"): self.restoreGeometry(settings.value("MainWindow/Geometry")) if settings.contains("MainWindow/State"): self.restoreState(settings.value("MainWindow/State")) self.setWindowTitle("r3tagger") # Final Layout centralWidget = QWidget() centralLayout = QVBoxLayout() centralLayout.addWidget(self.albumView) centralLayout.addLayout(self.buttonGroup) centralWidget.setLayout(centralLayout) self.setCentralWidget(centralWidget) def _createAction(self, text, slot=None, shortcut=None, icon=None, tip=None, checkable=False, signal="triggered"): action = QAction(text, self) if icon is not None: action.setIcon(QIcon(":/{0}.png".format(icon))) if shortcut is not None: action.setShortcut(shortcut) if tip is not None: action.setToolTip(tip) action.setStatusTip(tip) if slot is not None: getattr(action, signal).connect(slot) if checkable: action.setCheckable(True) return action def _addActions(self, target, actions): for action in actions: if action is None: target.addSeparator() else: target.addAction(action) def _setDirty(self): self.dirty = True def _fixColumns(self, index, view, model): for column in range(model.columnCount(index)): view.resizeColumnToContents(column) def closeEvent(self, event): if self.dirty: reply = QMessageBox.question( self, "r3tagger - Unsaved Changes", "Save unsaved changes?", QMessageBox.Yes | QMessageBox.No | QMessageBox.Cancel, ) if reply == QMessageBox.Cancel: event.ignore() return None elif reply == QMessageBox.Yes: self.confirmChanges() settings = QSettings() settings.setValue("MainWindow/Geometry", self.saveGeometry()) settings.setValue("MainWindow/State", self.saveState()) def fixFileSystemColumns(self, index): self._fixColumns(index, self.fileSystemView, self.fileSystemModel) def fixAlbumViewColumns(self, index): model = self.albumView.model() self._fixColumns(index, self.albumView, model) def updateAlbumModel(self, index): path = self.fileSystemModel.fileInfo(index).absoluteFilePath() self.addPath(path) self.fixAlbumViewColumns(index) def addPath(self, path): if os.path.isfile(path): track = controller.build_track(path) containerAlbum = controller.album_from_tracks([track], u"Singles") self.albumView.model().addAlbum(containerAlbum) else: for album in controller.build_albums(path, recursive=True): self.albumView.model().addAlbum(album) def updateEditing(self, index): self.albumView.correctListingSelection(index) selectedTracks = self.albumView.selectedTracks() albumOfSingles = controller.album_from_tracks(selectedTracks) selected = self.albumView.selectedAlbums() selected.append(albumOfSingles) tags = controller.find_shared_tags(*selected) if selected else {} for tag, edit in self.tagsToAttribs.items(): if not tags: self.clearEditing() break edit.setText(tags.get(tag, "")) edit.setCursorPosition(0) def confirmChanges(self): tags = {} for field, lineEdit in self.tagsToAttribs.items(): tag = lineEdit.text() tags[field] = tag view = self.albumView for album in view.selectedAlbums(): controller.retag_album(album, tags) for track in view.selectedTracks(): controller.retag_track(track, tags) self.saveChanges() def clearAlbumView(self): model = self.albumView.model() model.clear() model.setHeaders() def clearEditing(self): for lineEdit in self.tagsToAttribs.values(): lineEdit.setText("") def saveChanges(self): for track in self.albumView.model(): if track.dirty: track.saveChanges() self.dirty = False self.resetModel() def cancelChanges(self): for track in self.albumView.model(): track.reset() self.resetModel() def resetModel(self): model = self.albumView.model() model.beginResetModel() self.clearEditing() expanded = [] rowCount = model.rowCount(QModelIndex()) for row in range(rowCount): index = model.index(row, 0, QModelIndex()) if self.albumView.isExpanded(index): expanded.append(index) model.endResetModel() for expandedIndex in expanded: self.albumView.setExpanded(expandedIndex, True) def fileAddSong(self): selectedFiles, selectedFilter = QFileDialog.getOpenFileNames(parent=self, caption="Add Songs") for song in selectedFiles: self.addPath(song) def fileAddAlbum(self): selectedDir = QFileDialog.getExistingDirectory(parent=self, caption="Add Album") self.addPath(selectedDir) def editRecognize(self): pass def editReorganize(self): pass def editSettings(self): pass def helpDocs(self): pass def helpAbout(self): pass
class MainWindow(QMainWindow, Ui_MainWindow): def __init__(self, parent=None): super(MainWindow, self).__init__(parent) self.setupUi(self) self.build_dir_tree() self.setWindowIcon(QIcon('favicon.png')) self.actionAbout.triggered.connect(self.about) self.destButton.clicked.connect(self.destination_chooser) self.actionChoose_Destination.triggered.connect(self.destination_chooser) self.copyButton.clicked.connect(self.copy_files) self.actionStart_Copy.triggered.connect(self.copy_files) self.ckbxTrimDir.toggled.connect(self.update_table_view) self.treeView.expanded.connect(self.resize_tree_column) self.treeView.collapsed.connect(self.resize_tree_column) self.treeView.clicked.connect(self.update_table_view) self.trimdirCount.valueChanged.connect(self.update_table_view) self.listWidget.doubleClicked.connect(self.unselectItem) self.copyButton.setEnabled(False) self.lblTrimDir.setVisible(False) self.trimdirCount.setVisible(False) self.rbOWNewer.setVisible(False) self.rbOWLarger.setVisible(False) self.rbOWEither.setVisible(False) self.context = zmq.Context() self.socket = self.context.socket(zmq.PAIR) self.socket.bind("tcp://*:%s" % zmq_port) self.copyWorker = CopyWorker() self.connect(self.copyWorker, SIGNAL("copyComplete(QString, QString, QString, QString)"), self.copy_complete, Qt.QueuedConnection) self.connect(self.copyWorker, SIGNAL("spaceProblem(int, int)"), self.space_problem, Qt.QueuedConnection) def unselectItem(self, item): ##need to figure out how to remove from the model self.listWidget.takeItem(item.row()) def copy_complete(self, filecount, filesize, runtime, run_seconds): self.progress.setValue(self.progress.maximum()) transfer_rate = round((float(filesize) * 1024) / float(run_seconds), 3) filesize = round(float(filesize), 3) QMessageBox.information(self, "File Copy Complete", """Your file copy has been successfully completed.\n Files processed:\t%s\n Data copied:\t%sGB\n Total runtime:\t%s\n Transfer Rate:\t%sMB/Sec""" % (filecount, filesize, runtime, transfer_rate), WindowModility=True) self.copyButton.setEnabled(True) def space_problem(self, dirsize, filesize): """Display a dialog to the user advising that there is not enough space in the destination directory. Input: dirsize : integer - amount of space available in the destination directory filesize: integer - size of the selected files Output: None, dialog is displayed to the user.""" ##TODO: Set the messagebox modal property to true required_space = (filesize / 1024.00 / 1024.00 / 1024.00) - (dirsize / 1024.00 / 1024.00 / 1024.00) QMessageBox.critical(self, "Not enough space", """You do not have enough space in your selected destination to complete this operation\n %s more GB space required""" % required_space, WindowModility=True) self.copyWorker.quit() self.copyButton.setEnabled(True) def build_dir_tree(self): """Add a directory tree listing to the QTreeView and set the root to the drive that it was run from. Input: None Output: None""" ##TODO: add linux support for the model root drive. self.model = QFileSystemModel(self) if sys.platform == 'win32': self.model.setRootPath(os.path.splitdrive(os.getcwd())[0]) self.tree = self.treeView self.tree.setModel(self.model) self.tree.setAnimated(False) self.tree.setIndentation(20) self.tree.setSortingEnabled(True) def update_table_view(self): """Refresh listview with selected items in the treeView using the shared model. Input: None Output: None""" itemlist = [os.path.abspath( self.model.filePath( self.model.index(selection.row(), 0, selection.parent() ) ) ) for selection in self.treeView.selectedIndexes()] self.listWidget.clear() self.listWidget.addItems(itemlist) nitemlist = [] fileops = FileOperations() if not self.ckbxTrimDir.isChecked(): flattencount = 0 else: flattencount = self.trimdirCount.value() if self.lblDestPath.isEnabled(): self.previewView.clear() for item in itemlist: nitemlist.append(fileops.get_dest_filepath(item, self.lblDestPath.text(), flattencount)) self.previewView.addItems(nitemlist) else: self.previewView.clear() self.previewView.addItems(['No destination folder selected']) self.resize_tree_column() def resize_tree_column(self): """Resize the treeView column to fit the contents. Input: None Output: None""" self.treeView.resizeColumnToContents(0) def copy_files(self): """Initiate copy process. File size is calculated first to check that there is enough space in the destination. If there is enough space then we start the copy of the files to their destination. Input: None Output: None""" self.copyButton.setEnabled(False) self.copyWorker.must_run = True self.connect(self.copyWorker, SIGNAL("copyProgress(QString, QString, QString)"), self.copy_progress, Qt.QueuedConnection) dest_dir = self.lblDestPath.text() if dest_dir == '': QMessageBox.critical(self, "Destination not set", "Please specify a destination path", WindowModility=True) else: copy_filelist = [] for selection in self.treeView.selectedIndexes(): indexItem = self.model.index(selection.row(), 0, selection.parent()) copy_filelist.append(self.model.filePath(indexItem)) if self.cbOWDest.isChecked(): if self.rbOWEither.isChecked(): overwrite_option = 'either' elif self.rbOWLarger.isChecked(): overwrite_option = 'larger' elif self.rbOWNewer.isChecked(): overwrite_option = 'newer' else: QMessageBox.critical(self, "Overwrite option missing", """You did not select an overwrite option.""", WindowModility=True) self.copyButton.setEnabled(True) return else: overwrite_option = None if not self.ckbxTrimDir.isChecked(): flattencount = 0 else: flattencount = self.trimdirCount.value() self.progress = QProgressDialog("Copy in progress.", "Cancel", 0, 100, modal=True) self.progress.canceled.connect(self.cancel_copy) self.progress.setWindowTitle('Copy Progress') var_values = {'destdir': dest_dir, 'filelist': copy_filelist, 'flattencount': flattencount, 'overwrite_opt': overwrite_option} self.socket.send(json.dumps(var_values)) self.copyWorker.start() def copy_progress(self, percentage_complete, filecount, filecomplete): """Display the progress bar with a completed percentage. Input: percentage_complete : integer - the amount complete in percent. filecount : integer - the total number of files being processed. filecomplete : integer - the number of files that have already been processed. Output: None, dialog is updated""" ##TODO: display the current transfer rate ##TODO: display the current file being transferred and possibly the progress thereof. ##Perhaps use the statusbar method for this self.progress.setValue(int(percentage_complete)) def cancel_copy(self): """Slot for the cancel command on the progress dialog. The must_run variable of the copyWorker class is set to False to terminate the copy. Input: None Output: None""" self.copyWorker.must_run = False self.copyButton.setEnabled(True) def statusbar_msg(self, msg): """Update the statusbar on the bottom of the screen. Input: msg : string - Message that you would like displayed on the form. Output: None """ self.statusbar.clearMessage() self.statusbar.showMessage(msg) def destination_chooser(self): """Show folder chooser dialog and update lblDestPath with path selected. Input: None Output: None""" dialog = QFileDialog() dialog.setFileMode(QFileDialog.Directory) dialog.setOption(QFileDialog.ShowDirsOnly) dialog.exec_() self.lblDestPath.setEnabled(True) self.lblDestPath.setText(os.path.abspath(dialog.directory().absolutePath())) self.update_table_view() self.copyButton.setEnabled(True) def about(self): """Popup a box with about message. Input: None Output: None""" QMessageBox.about(self, "About MClub Mover", """This program is designed to help make the process of copying \ files from multiple directories much easier and simpler.\n This software is provided as is with absolutely no warranties.""", WindowModility=True)