def __init__(self,
                 menu: bool = False,
                 file_type: FileType = None,
                 parent=None) -> None:
        """
        :param menu: whether to render a drop down menu
        :param file_type: whether for photos or videos. Relevant only for menu display.
        """

        super().__init__(parent)
        self.rapidApp = parent
        if parent is not None:
            self.prefs = self.rapidApp.prefs
        else:
            self.prefs = None

        self.storage_space = None  # type: StorageSpace

        self.map_action = dict()  # type: Dict[int, QAction]

        if menu:
            menuIcon = QIcon(':/icons/settings.svg')
            self.file_type = file_type
            self.createActionsAndMenu()
            self.mouse_pos = DestinationDisplayMousePos.normal
            self.tooltip_display_state = DestinationDisplayTooltipState.path
        else:
            menuIcon = None
            self.menu = None
            self.mouse_pos = None
            self.tooltip_display_state = None

        self.deviceDisplay = DeviceDisplay(menuButtonIcon=menuIcon)
        size = icon_size()
        self.icon = QIcon(':/icons/folder.svg').pixmap(QSize(
            size, size))  # type: QPixmap
        self.display_name = ''
        self.photos_size_to_download = self.videos_size_to_download = 0
        self.files_to_display = None  # type: DisplayingFilesOfType
        self.marked = FileTypeCounter()
        self.display_type = None  # type: DestinationDisplayType
        self.setSizePolicy(QSizePolicy.MinimumExpanding, QSizePolicy.Fixed)

        # default number of built-in subfolder generation defaults
        self.no_builtin_defaults = 5
        self.max_presets = 5
        # maximum number of menu entries showing name generation presets
        self.max_menu_entries = self.no_builtin_defaults + self.max_presets

        self.sample_rpd_file = None  # type: Union[Photo, Video]

        self.os_stat_device = 0  # type: int
        self._downloading_to = defaultdict(
            list)  # type: DefaultDict[int, Set[FileType]]
    def _initValues(self):
        self.rows = RowTracker()  # type: RowTracker
        self.row_id_counter = 0  # type: int
        # {row_id}
        self.headers = set()  # type: Set[int]
        # path: BackupViewRow
        self.backup_devices = dict()  # type: Dict[str, BackupViewRow]
        self.path_to_row_ids = defaultdict(list)  # type: Dict[str, List[int]]
        self.row_id_to_path = dict()  # type: Dict[int, str]

        self.marked = FileTypeCounter()
        self.photos_size_to_download = self.videos_size_to_download = 0

        # os_stat_device: Set[FileType]
        self._downloading_to = defaultdict(
            list)  # type: DefaultDict[int, Set[FileType]]
    def __init__(
        self,
        menu: bool = False,
        file_type: FileType = None,
        parent: QWidget = None,
        rapidApp=None,
    ) -> None:
        """
        :param menu: whether to render a drop down menu
        :param file_type: whether for photos or videos. Relevant only for menu display.
        """

        super().__init__(parent)
        self.rapidApp = rapidApp
        if rapidApp is not None:
            self.prefs = self.rapidApp.prefs
        else:
            self.prefs = None

        self.storage_space = None  # type: Optional[StorageSpace]

        self.map_action = dict()  # type: Dict[int, QAction]

        if menu:
            pixmap = darkModePixmap(
                path=":/icons/settings.svg",
                size=QSize(100, 100),
                soften_regular_mode_color=True,
            )
            menuIcon = QIcon(pixmap)
            self.file_type = file_type
            self.createActionsAndMenu()
            self.mouse_pos = DestinationDisplayMousePos.normal
            self.tooltip_display_state = DestinationDisplayTooltipState.path
        else:
            menuIcon = None
            self.menu = None
            self.mouse_pos = None
            self.tooltip_display_state = None

        self.deviceDisplay = DeviceDisplay(parent=self,
                                           menuButtonIcon=menuIcon)
        self.deviceDisplay.widthChanged.connect(self.widthChanged)
        size = icon_size()
        self.icon = QIcon(":/icons/folder.svg").pixmap(QSize(
            size, size))  # type: QPixmap
        self.icon = darkModePixmap(self.icon)
        self.display_name = ""
        self.photos_size_to_download = self.videos_size_to_download = 0
        self.files_to_display = None  # type: Optional[DisplayingFilesOfType]
        self.marked = FileTypeCounter()
        self.display_type = None  # type: Optional[DestinationDisplayType]
        self.setSizePolicy(QSizePolicy.MinimumExpanding, QSizePolicy.Fixed)

        # default number of built-in subfolder generation defaults
        self.no_builtin_defaults = 5
        self.max_presets = 5
        # maximum number of menu entries showing name generation presets
        self.max_menu_entries = self.no_builtin_defaults + self.max_presets

        self.sample_rpd_file = None  # type: Optional[Union[Photo, Video]]

        self.os_stat_device = 0  # type: int
        self._downloading_to = defaultdict(
            set)  # type: DefaultDict[int, Set[FileType]]

        self.midPen = paletteMidPen()
        self.frame_width = QApplication.style().pixelMetric(
            QStyle.PM_DefaultFrameWidth)
        self.container_vertical_scrollbar_visible = None
class DestinationDisplay(QWidget):
    """
    Custom widget handling the display of download destinations, not including the file
    system browsing component.

    Serves a dual purpose, depending on whether photos and videos are being downloaded
    to the same file system or not:

    1. Display how much storage space the checked files will use in addition
       to the space used by existing files.

    2. Display the download destination (path), and a local menu to control subfolder
       generation.

    Where photos and videos are being downloaded to the same file system, the storage
    space display is combined into one widget, which appears in its own panel above the
    photo and video destination panels.

    Where photos and videos are being downloaded to different file systems, the combined
    display (above) is invisible, and photo and video panels have the own section in
    which to display their storage space display
    """

    photos = _("Photos")
    videos = _("Videos")
    projected_space_msg = _("Projected storage use after download")

    def __init__(
        self,
        menu: bool = False,
        file_type: FileType = None,
        parent: QWidget = None,
        rapidApp=None,
    ) -> None:
        """
        :param menu: whether to render a drop down menu
        :param file_type: whether for photos or videos. Relevant only for menu display.
        """

        super().__init__(parent)
        self.rapidApp = rapidApp
        if rapidApp is not None:
            self.prefs = self.rapidApp.prefs
        else:
            self.prefs = None

        self.storage_space = None  # type: Optional[StorageSpace]

        self.map_action = dict()  # type: Dict[int, QAction]

        if menu:
            pixmap = darkModePixmap(
                path=":/icons/settings.svg",
                size=QSize(100, 100),
                soften_regular_mode_color=True,
            )
            menuIcon = QIcon(pixmap)
            self.file_type = file_type
            self.createActionsAndMenu()
            self.mouse_pos = DestinationDisplayMousePos.normal
            self.tooltip_display_state = DestinationDisplayTooltipState.path
        else:
            menuIcon = None
            self.menu = None
            self.mouse_pos = None
            self.tooltip_display_state = None

        self.deviceDisplay = DeviceDisplay(parent=self,
                                           menuButtonIcon=menuIcon)
        self.deviceDisplay.widthChanged.connect(self.widthChanged)
        size = icon_size()
        self.icon = QIcon(":/icons/folder.svg").pixmap(QSize(
            size, size))  # type: QPixmap
        self.icon = darkModePixmap(self.icon)
        self.display_name = ""
        self.photos_size_to_download = self.videos_size_to_download = 0
        self.files_to_display = None  # type: Optional[DisplayingFilesOfType]
        self.marked = FileTypeCounter()
        self.display_type = None  # type: Optional[DestinationDisplayType]
        self.setSizePolicy(QSizePolicy.MinimumExpanding, QSizePolicy.Fixed)

        # default number of built-in subfolder generation defaults
        self.no_builtin_defaults = 5
        self.max_presets = 5
        # maximum number of menu entries showing name generation presets
        self.max_menu_entries = self.no_builtin_defaults + self.max_presets

        self.sample_rpd_file = None  # type: Optional[Union[Photo, Video]]

        self.os_stat_device = 0  # type: int
        self._downloading_to = defaultdict(
            set)  # type: DefaultDict[int, Set[FileType]]

        self.midPen = paletteMidPen()
        self.frame_width = QApplication.style().pixelMetric(
            QStyle.PM_DefaultFrameWidth)
        self.container_vertical_scrollbar_visible = None

    @property
    def downloading_to(self) -> DefaultDict[int, Set[FileType]]:
        return self._downloading_to

    @downloading_to.setter
    def downloading_to(self, downloading_to) -> None:
        if downloading_to is not None:
            self._downloading_to = downloading_to
            # TODO determine if this is always needed here
            self.update()

    def createActionsAndMenu(self) -> None:
        self.setMouseTracking(True)
        self.menu = QMenu()

        if self.file_type == FileType.photo:
            defaults = gnc.PHOTO_SUBFOLDER_MENU_DEFAULTS
        else:
            defaults = gnc.VIDEO_SUBFOLDER_MENU_DEFAULTS

        self.subfolder0Act = QAction(make_subfolder_menu_entry(defaults[0]),
                                     self)
        self.subfolder0Act.setCheckable(True)
        self.subfolder0Act.triggered.connect(self.doSubfolder0)
        self.subfolder1Act = QAction(make_subfolder_menu_entry(defaults[1]),
                                     self)
        self.subfolder1Act.setCheckable(True)
        self.subfolder1Act.triggered.connect(self.doSubfolder1)
        self.subfolder2Act = QAction(make_subfolder_menu_entry(defaults[2]),
                                     self)
        self.subfolder2Act.setCheckable(True)
        self.subfolder2Act.triggered.connect(self.doSubfolder2)
        self.subfolder3Act = QAction(make_subfolder_menu_entry(defaults[3]),
                                     self)
        self.subfolder3Act.setCheckable(True)
        self.subfolder3Act.triggered.connect(self.doSubfolder3)
        self.subfolder4Act = QAction(make_subfolder_menu_entry(defaults[4]),
                                     self)
        self.subfolder4Act.setCheckable(True)
        self.subfolder4Act.triggered.connect(self.doSubfolder4)
        self.subfolder5Act = QAction("Preset 0", self)
        self.subfolder5Act.setCheckable(True)
        self.subfolder5Act.triggered.connect(self.doSubfolder5)
        self.subfolder6Act = QAction("Preset 1", self)
        self.subfolder6Act.setCheckable(True)
        self.subfolder6Act.triggered.connect(self.doSubfolder6)
        self.subfolder7Act = QAction("Preset 2", self)
        self.subfolder7Act.setCheckable(True)
        self.subfolder7Act.triggered.connect(self.doSubfolder7)
        self.subfolder8Act = QAction("Preset 3", self)
        self.subfolder8Act.setCheckable(True)
        self.subfolder8Act.triggered.connect(self.doSubfolder8)
        self.subfolder9Act = QAction("Preset 4", self)
        self.subfolder9Act.setCheckable(True)
        self.subfolder9Act.triggered.connect(self.doSubfolder9)
        # Translators: Custom refers to the user choosing a non-default value that
        # they customize themselves
        self.subfolderCustomAct = QAction(_("Custom..."), self)
        self.subfolderCustomAct.setCheckable(True)
        self.subfolderCustomAct.triggered.connect(self.doSubfolderCustom)

        self.subfolderGroup = QActionGroup(self)

        self.subfolderGroup.addAction(self.subfolder0Act)
        self.subfolderGroup.addAction(self.subfolder1Act)
        self.subfolderGroup.addAction(self.subfolder2Act)
        self.subfolderGroup.addAction(self.subfolder3Act)
        self.subfolderGroup.addAction(self.subfolder4Act)
        self.subfolderGroup.addAction(self.subfolder5Act)
        self.subfolderGroup.addAction(self.subfolder6Act)
        self.subfolderGroup.addAction(self.subfolder7Act)
        self.subfolderGroup.addAction(self.subfolder8Act)
        self.subfolderGroup.addAction(self.subfolder9Act)
        self.subfolderGroup.addAction(self.subfolderCustomAct)

        self.menu.addAction(self.subfolder0Act)
        self.menu.addAction(self.subfolder1Act)
        self.menu.addAction(self.subfolder2Act)
        self.menu.addAction(self.subfolder3Act)
        self.menu.addAction(self.subfolder4Act)
        self.menu.addSeparator()
        self.menu.addAction(self.subfolder5Act)
        self.menu.addAction(self.subfolder6Act)
        self.menu.addAction(self.subfolder7Act)
        self.menu.addAction(self.subfolder8Act)
        self.menu.addAction(self.subfolder9Act)
        self.menu.addAction(self.subfolderCustomAct)

        self.map_action[0] = self.subfolder0Act
        self.map_action[1] = self.subfolder1Act
        self.map_action[2] = self.subfolder2Act
        self.map_action[3] = self.subfolder3Act
        self.map_action[4] = self.subfolder4Act
        self.map_action[5] = self.subfolder5Act
        self.map_action[6] = self.subfolder6Act
        self.map_action[7] = self.subfolder7Act
        self.map_action[8] = self.subfolder8Act
        self.map_action[9] = self.subfolder9Act
        self.map_action[-1] = self.subfolderCustomAct

    def presetType(self) -> PresetPrefType:
        if self.file_type == FileType.photo:
            return PresetPrefType.preset_photo_subfolder
        else:
            return PresetPrefType.preset_video_subfolder

    def _cacheCustomPresetValues(self) -> int:
        """
        Get custom photo or video presets, and assign them to class members
        :return: index into the combo of default prefs + custom prefs
        """
        preset_type = self.presetType()
        self.preset_names, self.preset_pref_lists = self.prefs.get_preset(
            preset_type=preset_type)

        if self.file_type == FileType.photo:
            index = self.prefs.photo_subfolder_index(self.preset_pref_lists)
        else:
            index = self.prefs.video_subfolder_index(self.preset_pref_lists)
        return index

    def setupMenuActions(self) -> None:
        index = self._cacheCustomPresetValues()

        action = self.map_action[index]  # type: QAction
        action.setChecked(True)

        # Set visibility of custom presets menu items to match how many we are
        # displaying
        for idx, text in enumerate(self.preset_names[:self.max_presets]):
            action = self.map_action[self.no_builtin_defaults + idx]
            action.setText(text)
            action.setVisible(True)

        for i in range(self.max_presets -
                       min(len(self.preset_names), self.max_presets)):
            idx = len(self.preset_names) + self.no_builtin_defaults + i
            action = self.map_action[idx]
            action.setVisible(False)

    def doSubfolder0(self) -> None:
        self.menuItemChosen(0)

    def doSubfolder1(self) -> None:
        self.menuItemChosen(1)

    def doSubfolder2(self) -> None:
        self.menuItemChosen(2)

    def doSubfolder3(self) -> None:
        self.menuItemChosen(3)

    def doSubfolder4(self) -> None:
        self.menuItemChosen(4)

    def doSubfolder5(self) -> None:
        self.menuItemChosen(5)

    def doSubfolder6(self) -> None:
        self.menuItemChosen(6)

    def doSubfolder7(self) -> None:
        self.menuItemChosen(7)

    def doSubfolder8(self) -> None:
        self.menuItemChosen(8)

    def doSubfolder9(self) -> None:
        self.menuItemChosen(9)

    def doSubfolderCustom(self):
        self.menuItemChosen(-1)

    def menuItemChosen(self, index: int) -> None:
        self.mouse_pos = DestinationDisplayMousePos.normal
        self.update()

        user_pref_list = None

        if index == -1:
            if self.file_type == FileType.photo:
                pref_defn = DICT_SUBFOLDER_L0
                pref_list = self.prefs.photo_subfolder
                generation_type = NameGenerationType.photo_subfolder
            else:
                pref_defn = DICT_VIDEO_SUBFOLDER_L0
                pref_list = self.prefs.video_subfolder
                generation_type = NameGenerationType.video_subfolder

            prefDialog = PrefDialog(
                pref_defn=pref_defn,
                user_pref_list=pref_list,
                generation_type=generation_type,
                prefs=self.prefs,
                sample_rpd_file=self.sample_rpd_file,
                max_entries=self.max_menu_entries,
            )
            if prefDialog.exec():
                user_pref_list = prefDialog.getPrefList()
                if not user_pref_list:
                    user_pref_list = None

        elif index >= self.no_builtin_defaults:
            assert index < self.max_menu_entries
            user_pref_list = self.preset_pref_lists[index -
                                                    self.no_builtin_defaults]

        else:
            if self.file_type == FileType.photo:
                user_pref_list = gnc.PHOTO_SUBFOLDER_MENU_DEFAULTS_CONV[index]
            else:
                user_pref_list = gnc.VIDEO_SUBFOLDER_MENU_DEFAULTS_CONV[index]

        if user_pref_list is not None:
            logging.debug("Updating %s subfolder generation preference value",
                          self.file_type.name)
            if self.file_type == FileType.photo:
                self.prefs.photo_subfolder = user_pref_list
            else:
                self.prefs.video_subfolder = user_pref_list
            self.rapidApp.folder_preview_manager.change_subfolder_structure()

    def setDestination(self, path: str) -> None:
        """
        Set the downloaded destination path
        :param path: valid path
        """

        self.display_name, self.path = get_path_display_name(path)
        try:
            self.os_stat_device = os.stat(path).st_dev
        except FileNotFoundError:
            logging.error(
                "Cannot set download destination display: %s does not exist",
                path)
            self.os_stat_device = 0

        mount = QStorageInfo(path)
        bytes_total, bytes_free = get_mount_size(mount=mount)

        self.storage_space = StorageSpace(bytes_free=bytes_free,
                                          bytes_total=bytes_total,
                                          path=path)

    def setDownloadAttributes(
        self,
        marked: FileTypeCounter,
        photos_size: int,
        videos_size: int,
        files_to_display: DisplayingFilesOfType,
        display_type: DestinationDisplayType,
        merge: bool,
    ) -> None:
        """
        Set the attributes used to generate the visual display of the
        files marked to be downloaded

        :param marked: number and type of files marked for download
        :param photos_size: size in bytes of photos marked for download
        :param videos_size: size in bytes of videos marked for download
        :param files_to_display: whether displaying photos or videos or both
        :param display_type: whether showing only the header (folder only),
         usage only, or both
        :param merge: whether to replace or add to the current values
        """

        if not merge:
            self.marked = marked
            self.photos_size_to_download = photos_size
            self.videos_size_to_download = videos_size
        else:
            self.marked.update(marked)
            self.photos_size_to_download += photos_size
            self.videos_size_to_download += videos_size

        self.files_to_display = files_to_display

        self.display_type = display_type

        if self.display_type != DestinationDisplayType.usage_only:
            self.tool_tip = self.path
        else:
            self.tool_tip = self.projected_space_msg
        self.setToolTip(self.tool_tip)

        self.update()
        self.updateGeometry()

    def sufficientSpaceAvailable(self) -> bool:
        """
        Check to see that there is sufficient space with which to perform a download.

        :return: True or False value if sufficient space. Will always return False if
         the download destination is not yet set.
        """

        if self.storage_space is None:
            return False

        # allow for destinations that don't properly report their size
        if self.storage_space.bytes_total == 0:
            return True

        photos_size_to_download, videos_size_to_download = adjusted_download_size(
            photos_size_to_download=self.photos_size_to_download,
            videos_size_to_download=self.videos_size_to_download,
            os_stat_device=self.os_stat_device,
            downloading_to=self._downloading_to,
        )
        return (photos_size_to_download + videos_size_to_download <
                self.storage_space.bytes_free)

    @pyqtSlot(bool)
    def containerVerticalScrollBar(self, visible: bool) -> None:
        self.container_vertical_scrollbar_visible = visible

    def paintEvent(self, event: QPaintEvent) -> None:
        """
        Render the custom widget
        """

        painter = QStylePainter()
        painter.begin(self)

        x = 0
        y = 0
        width = self.width()

        rect = self.rect()  # type: QRect
        palette = QPalette()
        backgroundColor = palette.base().color()

        if (self.display_type == DestinationDisplayType.usage_only
                and QSplitter().lineWidth()):
            pen = painter.pen()
            painter.setPen(backgroundColor)
            painter.drawLine(rect.topLeft(), rect.topRight())
            painter.setPen(self.midPen)
            painter.drawLine(rect.bottomLeft(), rect.bottomRight())
            painter.drawLine(rect.topLeft(), rect.bottomLeft())
            if (self.container_vertical_scrollbar_visible is None
                    or not self.container_vertical_scrollbar_visible):
                painter.drawLine(rect.topRight(), rect.bottomRight())
            painter.setPen(pen)

            w = QSplitter().lineWidth()
            rect.adjust(w, w, -w, -w)

        painter.fillRect(rect, backgroundColor)

        if self.storage_space is None:
            painter.end()
            return

        highlight_menu = self.mouse_pos == DestinationDisplayMousePos.menu

        if self.display_type != DestinationDisplayType.usage_only:
            # Render the folder icon, folder name, and the menu icon
            self.deviceDisplay.paint_header(
                painter=painter,
                x=x,
                y=y,
                width=width,
                display_name=self.display_name,
                icon=self.icon,
                highlight_menu=highlight_menu,
            )
            y = y + self.deviceDisplay.dc.device_name_height

        if self.display_type != DestinationDisplayType.folder_only:
            # Render the projected storage space
            if self.display_type == DestinationDisplayType.usage_only:
                y += self.deviceDisplay.dc.padding

            photos_size_to_download, videos_size_to_download = adjusted_download_size(
                photos_size_to_download=self.photos_size_to_download,
                videos_size_to_download=self.videos_size_to_download,
                os_stat_device=self.os_stat_device,
                downloading_to=self._downloading_to,
            )

            details = make_body_details(
                bytes_total=self.storage_space.bytes_total,
                bytes_free=self.storage_space.bytes_free,
                files_to_display=self.files_to_display,
                marked=self.marked,
                photos_size_to_download=photos_size_to_download,
                videos_size_to_download=videos_size_to_download,
            )

            self.deviceDisplay.paint_body(painter=painter,
                                          x=x,
                                          y=y,
                                          width=width,
                                          details=details)

        painter.end()

    @pyqtSlot(int)
    def widthChanged(self, width: int) -> None:
        self.updateGeometry()

    def sizeHint(self) -> QSize:
        if self.display_type == DestinationDisplayType.usage_only:
            height = self.deviceDisplay.dc.padding
        else:
            height = 0

        if self.display_type != DestinationDisplayType.usage_only:
            height += self.deviceDisplay.dc.device_name_height
        if self.display_type != DestinationDisplayType.folder_only:
            height += self.deviceDisplay.dc.storage_height

        return QSize(self.deviceDisplay.width(), height)

    def minimumSize(self) -> QSize:
        return self.sizeHint()

    @pyqtSlot(QMouseEvent)
    def mousePressEvent(self, event: QMouseEvent) -> None:
        if self.menu is None:
            return

        iconRect = self.deviceDisplay.menu_button_rect(0, 0, self.width())

        if iconRect.contains(event.pos()):
            if event.button() == Qt.LeftButton:
                menuTopReal = iconRect.bottomLeft()
                x = math.ceil(menuTopReal.x())
                y = math.ceil(menuTopReal.y())
                self.setupMenuActions()
                self.menu.popup(self.mapToGlobal(QPoint(x, y)))

    @pyqtSlot(QMouseEvent)
    def mouseMoveEvent(self, event: QMouseEvent) -> None:
        """
        Sets the tooltip depending on the position of the mouse.
        """

        if self.menu is None:
            # Relevant only for photo and video destination panels, not the combined
            # storage space display.
            return

        if self.display_type == DestinationDisplayType.folders_and_usage:
            # make tooltip different when hovering above storage space compared
            # to when hovering above the destination folder

            headerRect = QRect(0, 0, self.width(),
                               self.deviceDisplay.dc.device_name_height)
            if not headerRect.contains(event.pos()):
                if (self.tooltip_display_state !=
                        DestinationDisplayTooltipState.storage_space):
                    # Display tooltip for storage space
                    self.setToolTip(self.projected_space_msg)
                    self.tooltip_display_state = (
                        DestinationDisplayTooltipState.storage_space)
                    self.update()
                return

        iconRect = self.deviceDisplay.menu_button_rect(0, 0, self.width())
        if iconRect.contains(event.pos()):
            if self.mouse_pos == DestinationDisplayMousePos.normal:
                self.mouse_pos = DestinationDisplayMousePos.menu

                if self.file_type == FileType.photo:
                    self.setToolTip(_("Configure photo subfolder creation"))
                else:
                    self.setToolTip(_("Configure video subfolder creation"))
                self.tooltip_display_state = DestinationDisplayTooltipState.menu
                self.update()

        else:
            if (self.mouse_pos == DestinationDisplayMousePos.menu
                    or self.tooltip_display_state !=
                    DestinationDisplayTooltipState.path):
                self.mouse_pos = DestinationDisplayMousePos.normal
                self.setToolTip(self.tool_tip)
                self.tooltip_display_state = DestinationDisplayTooltipState.path
                self.update()
class BackupDeviceModel(QAbstractListModel):
    """
    Stores 'devices' used for backing up photos and videos.

    Want to display:
    (1) destination on local files systems
    (2) external devices, e.g. external hard drives

    Need to account for when download destination is same file system
    as backup destination.
    """
    def __init__(self, parent) -> None:
        super().__init__(parent)
        self.raidApp = parent.rapidApp
        self.prefs = parent.prefs
        size = icon_size()
        self.removableIcon = QIcon(':icons/drive-removable-media.svg').pixmap(
            size)
        self.folderIcon = QIcon(':/icons/folder.svg').pixmap(size)
        self._initValues()

    def _initValues(self):
        self.rows = RowTracker()  # type: RowTracker
        self.row_id_counter = 0  # type: int
        # {row_id}
        self.headers = set()  # type: Set[int]
        # path: BackupViewRow
        self.backup_devices = dict()  # type: Dict[str, BackupViewRow]
        self.path_to_row_ids = defaultdict(list)  # type: Dict[str, List[int]]
        self.row_id_to_path = dict()  # type: Dict[int, str]

        self.marked = FileTypeCounter()
        self.photos_size_to_download = self.videos_size_to_download = 0

        # os_stat_device: Set[FileType]
        self._downloading_to = defaultdict(
            list)  # type: DefaultDict[int, Set[FileType]]

    @property
    def downloading_to(self):
        return self._downloading_to

    @downloading_to.setter
    def downloading_to(self, downloading_to: DefaultDict[int, Set[FileType]]):
        self._downloading_to = downloading_to
        self.downloadSizeChanged()

    def reset(self) -> None:
        self.beginResetModel()
        self._initValues()
        self.endResetModel()

    def columnCount(self, parent=QModelIndex()):
        return 1

    def rowCount(self, parent=QModelIndex()):
        return max(len(self.rows), 1)

    def insertRows(self, position, rows=2, index=QModelIndex()):
        self.beginInsertRows(QModelIndex(), position, position + rows - 1)
        self.endInsertRows()
        return True

    def removeRows(self, position, rows=2, index=QModelIndex()):
        self.beginRemoveRows(QModelIndex(), position, position + rows - 1)
        self.endRemoveRows()
        return True

    def addBackupVolume(self, mount_details: BackupVolumeDetails) -> None:

        mount = mount_details.mount
        display_name = mount_details.name
        path = mount_details.path
        backup_type = mount_details.backup_type
        os_stat_device = mount_details.os_stat_device

        assert mount is not None
        assert display_name
        assert path
        assert backup_type

        # two rows per device: header row, and detail row
        row = len(self.rows)
        self.insertRows(position=row)
        logging.debug(
            "Adding %s to backup device display with root path %s at rows %s - %s",
            display_name, mount.rootPath(), row, row + 1)

        for row_id in range(self.row_id_counter, self.row_id_counter + 2):
            self.row_id_to_path[row_id] = path
            self.rows[row] = row_id
            row += 1
            self.path_to_row_ids[path].append(row_id)

        header_row_id = self.row_id_counter
        self.headers.add(header_row_id)

        self.row_id_counter += 2

        self.backup_devices[path] = BackupViewRow(
            mount=mount,
            display_name=display_name,
            backup_type=backup_type,
            os_stat_device=os_stat_device)

    def removeBackupVolume(self, path: str) -> None:
        """
        :param path: the value of the volume (mount's path), NOT a
        manually specified path!
        """

        row_ids = self.path_to_row_ids[path]
        header_row_id = row_ids[0]
        row = self.rows.row(header_row_id)
        logging.debug("Removing 2 rows from backup view, starting at row %s",
                      row)
        self.rows.remove_rows(row, 2)
        self.headers.remove(header_row_id)
        del self.path_to_row_ids[path]
        del self.backup_devices[path]
        for row_id in row_ids:
            del self.row_id_to_path[row_id]
        self.removeRows(row, 2)

    def setDownloadAttributes(self, marked: FileTypeCounter, photos_size: int,
                              videos_size: int, merge: bool) -> None:
        """
        Set the attributes used to generate the visual display of the
        files marked to be downloaded

        :param marked: number and type of files marked for download
        :param photos_size: size in bytes of photos marked for download
        :param videos_size: size in bytes of videos marked for download
        :param merge: whether to replace or add to the current values
        """

        if not merge:
            self.marked = marked
            self.photos_size_to_download = photos_size
            self.videos_size_to_download = videos_size
        else:
            self.marked.update(marked)
            self.photos_size_to_download += photos_size
            self.videos_size_to_download += videos_size
        self.downloadSizeChanged()

    def downloadSizeChanged(self) -> None:
        # TODO possibly optimize for photo vs video rows
        for row in range(1, len(self.rows), 2):
            self.dataChanged.emit(self.index(row, 0), self.index(row, 0))

    def _download_size_by_backup_type(
            self, backup_type: BackupLocationType) -> Tuple[int, int]:
        """
        Include photos or videos in download size only if those file types
        are being backed up to this backup device
        :param backup_type: which file types are being backed up to this device
        :return: photos_size_to_download, videos_size_to_download
        """

        photos_size_to_download = videos_size_to_download = 0
        if backup_type != BackupLocationType.videos:
            photos_size_to_download = self.photos_size_to_download
        if backup_type != BackupLocationType.photos:
            videos_size_to_download = self.videos_size_to_download
        return photos_size_to_download, videos_size_to_download

    def data(self, index: QModelIndex, role=Qt.DisplayRole):

        if not index.isValid():
            return None

        row = index.row()

        # check for special case where no backup devices are active
        if len(self.rows) == 0:
            if role == Qt.DisplayRole:
                return ViewRowType.header
            elif role == Roles.device_details:
                if not self.prefs.backup_files:
                    return (_('Backups are not configured'),
                            self.removableIcon)
                elif self.prefs.backup_device_autodetection:
                    return (_('No backup devices detected'),
                            self.removableIcon)
                else:
                    return (_('Valid backup locations not yet specified'),
                            self.folderIcon)

        # at least one device  / location is being used
        if row >= len(self.rows) or row < 0:
            return None
        if row not in self.rows:
            return None

        row_id = self.rows[row]
        path = self.row_id_to_path[row_id]

        if role == Qt.DisplayRole:
            if row_id in self.headers:
                return ViewRowType.header
            else:
                return ViewRowType.content
        else:
            device = self.backup_devices[path]
            mount = device.mount

            if role == Qt.ToolTipRole:
                return path
            elif role == Roles.device_details:
                if self.prefs.backup_device_autodetection:
                    icon = self.removableIcon
                else:
                    icon = self.folderIcon
                return device.display_name, icon
            elif role == Roles.storage:
                photos_size_to_download, videos_size_to_download = \
                    self._download_size_by_backup_type(backup_type=device.backup_type)

                photos_size_to_download, videos_size_to_download = adjusted_download_size(
                    photos_size_to_download=photos_size_to_download,
                    videos_size_to_download=videos_size_to_download,
                    os_stat_device=device.os_stat_device,
                    downloading_to=self._downloading_to)

                bytes_total, bytes_free = get_mount_size(mount=mount)

                return BackupVolumeUse(
                    bytes_total=bytes_total,
                    bytes_free=bytes_free,
                    backup_type=device.backup_type,
                    marked=self.marked,
                    photos_size_to_download=photos_size_to_download,
                    videos_size_to_download=videos_size_to_download)

        return None

    def sufficientSpaceAvailable(self) -> bool:
        """
        Detect if each backup device has sufficient space for backing up, taking
        into accoutn situations where downloads and backups are going to the same
        partition.

        :return: False if any backup device has insufficient space, else True.
         True if there are no backup devices.
        """

        for device in self.backup_devices.values():
            photos_size_to_download, videos_size_to_download = \
                self._download_size_by_backup_type(backup_type=device.backup_type)
            photos_size_to_download, videos_size_to_download = adjusted_download_size(
                photos_size_to_download=photos_size_to_download,
                videos_size_to_download=videos_size_to_download,
                os_stat_device=device.os_stat_device,
                downloading_to=self._downloading_to)

            bytes_total, bytes_free = get_mount_size(mount=device.mount)
            if photos_size_to_download + videos_size_to_download >= bytes_free:
                return False
        return True