File: directory_tree.py

package info (click to toggle)
dupeguru 4.3.1-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,604 kB
  • sloc: python: 16,846; ansic: 424; makefile: 123
file content (106 lines) | stat: -rw-r--r-- 3,347 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# Created By: Virgil Dupras
# Created On: 2010-02-06
# Copyright 2015 Hardcoded Software (http://www.hardcoded.net)
#
# This software is licensed under the "GPLv3" License as described in the "LICENSE" file,
# which should be included with this package. The terms are also available at
# http://www.gnu.org/licenses/gpl-3.0.html

from hscommon.gui.tree import Tree, Node

from core.directories import DirectoryState
from core.gui.base import DupeGuruGUIObject

STATE_ORDER = [DirectoryState.NORMAL, DirectoryState.REFERENCE, DirectoryState.EXCLUDED]


# Lazily loads children
class DirectoryNode(Node):
    def __init__(self, tree, path, name):
        Node.__init__(self, name)
        self._tree = tree
        self._directory_path = path
        self._loaded = False
        self._state = STATE_ORDER.index(self._tree.app.directories.get_state(path))

    def __len__(self):
        if not self._loaded:
            self._load()
        return Node.__len__(self)

    def _load(self):
        self.clear()
        subpaths = self._tree.app.directories.get_subfolders(self._directory_path)
        for path in subpaths:
            self.append(DirectoryNode(self._tree, path, path.name))
        self._loaded = True

    def update_all_states(self):
        self._state = STATE_ORDER.index(self._tree.app.directories.get_state(self._directory_path))
        for node in self:
            node.update_all_states()

    # The state propery is an index to the combobox
    @property
    def state(self):
        return self._state

    @state.setter
    def state(self, value):
        if value == self._state:
            return
        self._state = value
        state = STATE_ORDER[value]
        self._tree.app.directories.set_state(self._directory_path, state)
        self._tree.update_all_states()


class DirectoryTree(Tree, DupeGuruGUIObject):
    # --- model -> view calls:
    # refresh()
    # refresh_states() # when only states label need to be refreshed
    #
    def __init__(self, app):
        Tree.__init__(self)
        DupeGuruGUIObject.__init__(self, app)

    def _view_updated(self):
        self._refresh()
        self.view.refresh()

    def _refresh(self):
        self.clear()
        for path in self.app.directories:
            self.append(DirectoryNode(self, path, str(path)))

    def add_directory(self, path):
        self.app.add_directory(path)

    def remove_selected(self):
        selected_paths = self.selected_paths
        if not selected_paths:
            return
        to_delete = [path[0] for path in selected_paths if len(path) == 1]
        if to_delete:
            self.app.remove_directories(to_delete)
        else:
            # All selected nodes or on second-or-more level, exclude them.
            nodes = self.selected_nodes
            newstate = DirectoryState.EXCLUDED
            if all(node.state == DirectoryState.EXCLUDED for node in nodes):
                newstate = DirectoryState.NORMAL
            for node in nodes:
                node.state = newstate

    def select_all(self):
        self.selected_nodes = list(self)
        self.view.refresh()

    def update_all_states(self):
        for node in self:
            node.update_all_states()
        self.view.refresh_states()

    # --- Event Handlers
    def directories_changed(self):
        self._view_updated()