multiselection of folders in sources

This commit is contained in:
Sam 2023-12-15 01:04:51 +05:30 committed by Manu
parent 0cecc56c8d
commit 13dc613255
2 changed files with 34 additions and 4 deletions

View File

@ -15,7 +15,13 @@ from typing import Any, Callable, Iterable, List, Optional, Tuple, TypeVar
import psutil
from PyQt6 import QtCore
from PyQt6.QtCore import QFileInfo, QThread, pyqtSignal
from PyQt6.QtWidgets import QApplication, QFileDialog, QSystemTrayIcon
from PyQt6.QtWidgets import (
QAbstractItemView,
QApplication,
QFileDialog,
QSystemTrayIcon,
QTreeView,
)
from vorta.borg._compatibility import BorgCompatibility
from vorta.log import logger
@ -50,6 +56,22 @@ class FilePathInfoAsync(QThread):
self.signal.emit(self.path, str(self.size), str(self.files_count))
class MultiSelectionFileDialog(QFileDialog):
def __init__(self, parent, title, initial_dir=os.path.expanduser('~')):
super().__init__(parent, title, initial_dir)
def selectedFiles(self, tree_view=None):
selected_files = super().selectedFiles()
if self.fileMode() == QFileDialog.FileMode.Directory:
if tree_view:
selected_indexes = tree_view.selectionModel().selectedIndexes()
selected_dirs = [tree_view.model().filePath(index) for index in selected_indexes]
selected_files += selected_dirs
return selected_files
def normalize_path(path):
"""normalize paths for MacOS (but do nothing on other platforms)"""
# HFS+ converts paths to a canonical form, so users shouldn't be required to enter an exact match.
@ -167,11 +189,16 @@ def get_dict_from_list(dataDict, mapList):
def choose_file_dialog(parent, title, want_folder=True):
dialog = QFileDialog(parent, title, os.path.expanduser('~'))
dialog = MultiSelectionFileDialog(parent, title, os.path.expanduser('~'))
dialog.setFileMode(QFileDialog.FileMode.Directory if want_folder else QFileDialog.FileMode.ExistingFiles)
dialog.setParent(parent, QtCore.Qt.WindowType.Sheet)
if want_folder:
dialog.setOption(QFileDialog.Option.ShowDirsOnly)
dialog.setOption(QFileDialog.Option.DontUseNativeDialog)
dir_dialog = dialog.findChild(QTreeView)
dir_dialog.setSelectionMode(QAbstractItemView.SelectionMode.MultiSelection)
return dialog, dir_dialog
return dialog

View File

@ -287,7 +287,7 @@ class SourceTab(SourceBase, SourceUI, BackupProfileMixin):
def source_add(self, want_folder):
def receive():
dirs = dialog.selectedFiles()
dirs = dialog.selectedFiles(dir_dialog) if want_folder else dialog.selectedFiles()
for dir in dirs:
if not os.access(dir, os.R_OK):
msg = QMessageBox()
@ -301,7 +301,10 @@ class SourceTab(SourceBase, SourceUI, BackupProfileMixin):
new_source.save()
msg = self.tr("Choose directory to back up") if want_folder else self.tr("Choose file(s) to back up")
dialog = choose_file_dialog(self, msg, want_folder=want_folder)
if want_folder:
dialog, dir_dialog = choose_file_dialog(self, msg, want_folder=want_folder)
else:
dialog = choose_file_dialog(self, msg, want_folder=want_folder)
dialog.open(receive)
def source_copy(self, index=None):