Example #1
0
    def __init__(self, parent=None):
        '''Custom with handling keysequences like copy/cut/paste.'''
        self.parent = parent

        self.setIconSize(QtCore.QSize(32, 32))
        self.setDragEnabled(True)
        self.setAcceptDrops(True)
        self.setDropIndicatorShown(True)
        self.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
        self.setDragDropMode(QtGui.QAbstractItemView.DragDrop)
        self.setSelectionMode(QtGui.QAbstractItemView.ExtendedSelection)

        # QFileSystemModel settings
        self.model = FileManagerModel(self.parent)
        self.model.setFilter(QtCore.QDir.AllEntries | QtCore.QDir.NoDot)
        # self.model.setSorting(QtCore.QDir.DirsFirst)
        # self.model.setReadOnly(False)
        self.setModel(self.model)
        # set the URL for the model
        # self.setRootIndex(self.model.index(self.parent.url))
        self.setStyleSheet('''QListView::item:selected:active {background: 99bbff;}
                              QListView::item:selected:!active {background: gray;}''')
Example #2
0
class FileManagerBase:
    def __init__(self, parent=None):
        '''Custom with handling keysequences like copy/cut/paste.'''
        self.parent = parent

        self.setIconSize(QtCore.QSize(32, 32))
        self.setDragEnabled(True)
        self.setAcceptDrops(True)
        self.setDropIndicatorShown(True)
        self.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
        self.setDragDropMode(QtGui.QAbstractItemView.DragDrop)
        self.setSelectionMode(QtGui.QAbstractItemView.ExtendedSelection)

        # QFileSystemModel settings
        self.model = FileManagerModel(self.parent)
        self.model.setFilter(QtCore.QDir.AllEntries | QtCore.QDir.NoDot)
        # self.model.setSorting(QtCore.QDir.DirsFirst)
        # self.model.setReadOnly(False)
        self.setModel(self.model)
        # set the URL for the model
        # self.setRootIndex(self.model.index(self.parent.url))
        self.setStyleSheet('''QListView::item:selected:active {background: 99bbff;}
                              QListView::item:selected:!active {background: gray;}''')

    def keyPressEvent(self, event):
        '''Handle Copy, Paste, Cut, Delete from keyboard shortcuts.'''
        # modelindexes of selected items
        items = self.selectedIndexes()
        # the list to which we save the items
        container = self.parent.mainWindow.filemanagerContainer
        if len(items) > 0:  # Copy, Cut - save the list of items to a global container, Delete
            itm = []
            # we want nice paths to selected items, as modelindexes would be useless in another folder
            for i in items:
                itm.append(str(self.model.filePath(i)))

            if event.matches(QtGui.QKeySequence.Copy):
                event.accept()
                print('copy')
                # save items to the container
                self.parent.mainWindow.filemanagerContainer = itm
                self.parent.mainWindow.filemanagerContainerType = COPY
            elif event.matches(QtGui.QKeySequence.Cut):
                event.accept()
                print('cut')
                # save items to the container
                self.parent.mainWindow.filemanagerContainer = itm
                self.parent.mainWindow.filemanagerContainerType = MOVE
            elif event.matches(QtGui.QKeySequence.Delete):
                event.accept()
                print('delete-move-to-trash / NOT IMPLEMENTED')
            else:
                event.ignore()
        if len(container) > 0:  # Paste action - copy items from global container to current directory
            if event.matches(QtGui.QKeySequence.Paste):
                event.accept()
                print('paste')
                # current folder is the destination folder
                parent = self.parent.listView.rootIndex()
                dst = str(self.model.filePath(parent))

                if self.parent.mainWindow.filemanagerContainerType == MOVE:
                    for src in container:
                        if isdir(src):
                            # If we move/copy a dir the destination must have it's name at the end
                            qdir = QtCore.QDir(src)
                            dst = join(dst, str(qdir.dirName()))
                        else:
                            dst = str(self.model.filePath(parent))

                        try:
                            shutil.move(src, dst)
                        except:
                            showException('moving items', 'moving files/folders')
                    self.parent.reloadClicked()

                if self.parent.mainWindow.filemanagerContainerType == COPY:
                    for src in container:
                        if isfile(src):
                            dst = str(self.model.filePath(parent))
                            try:
                                shutil.copy(src, dst)
                            except:
                                showException('copying files', 'copying files')
                        else:
                            # If we move/copy a dir the destination must have it's name at the end
                            qdir = QtCore.QDir(src)
                            dst = join(dst, str(qdir.dirName()))
                            try:
                                shutil.copytree(src, dst)
                            except:
                                showException('copying a folder', 'copying a folder')
                    self.parent.reloadClicked()
            else:
                event.ignore()
        else:
            event.ignore()