2010-02-07 14:26:50 +00:00
|
|
|
# Created By: Virgil Dupras
|
|
|
|
# Created On: 2010-02-06
|
2013-04-28 14:35:51 +00:00
|
|
|
# Copyright 2013 Hardcoded Software (http://www.hardcoded.net)
|
2010-02-07 14:26:50 +00:00
|
|
|
#
|
2010-09-30 10:17:41 +00:00
|
|
|
# This software is licensed under the "BSD" License as described in the "LICENSE" file,
|
2010-02-07 14:26:50 +00:00
|
|
|
# which should be included with this package. The terms are also available at
|
2010-09-30 10:17:41 +00:00
|
|
|
# http://www.hardcoded.net/licenses/bsd_license
|
2010-02-07 14:26:50 +00:00
|
|
|
|
2010-11-24 15:12:10 +00:00
|
|
|
from hscommon.gui.tree import Tree, Node
|
2010-02-07 14:26:50 +00:00
|
|
|
|
2011-04-12 11:22:29 +00:00
|
|
|
from ..directories import DirectoryState
|
2012-03-13 18:27:08 +00:00
|
|
|
from .base import DupeGuruGUIObject
|
2010-02-07 14:26:50 +00:00
|
|
|
|
2011-04-12 11:22:29 +00:00
|
|
|
STATE_ORDER = [DirectoryState.Normal, DirectoryState.Reference, DirectoryState.Excluded]
|
2010-02-07 14:26:50 +00:00
|
|
|
|
|
|
|
# Lazily loads children
|
|
|
|
class DirectoryNode(Node):
|
2011-03-16 08:31:16 +00:00
|
|
|
def __init__(self, tree, path, name):
|
2010-02-07 14:26:50 +00:00
|
|
|
Node.__init__(self, name)
|
2011-03-16 08:31:16 +00:00
|
|
|
self._tree = tree
|
2010-02-07 14:26:50 +00:00
|
|
|
self._directory_path = path
|
|
|
|
self._loaded = False
|
2011-03-16 08:31:16 +00:00
|
|
|
self._state = STATE_ORDER.index(self._tree.app.directories.get_state(path))
|
2010-02-07 14:26:50 +00:00
|
|
|
|
2010-02-07 15:00:58 +00:00
|
|
|
def __len__(self):
|
|
|
|
if not self._loaded:
|
|
|
|
self._load()
|
|
|
|
return Node.__len__(self)
|
|
|
|
|
2010-02-07 14:26:50 +00:00
|
|
|
def _load(self):
|
|
|
|
self.clear()
|
2011-03-16 08:31:16 +00:00
|
|
|
subpaths = self._tree.app.directories.get_subfolders(self._directory_path)
|
2010-02-07 14:26:50 +00:00
|
|
|
for path in subpaths:
|
2011-03-16 08:31:16 +00:00
|
|
|
self.append(DirectoryNode(self._tree, path, path[-1]))
|
2010-02-07 14:26:50 +00:00
|
|
|
self._loaded = True
|
|
|
|
|
2011-07-12 16:51:11 +00:00
|
|
|
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()
|
|
|
|
|
2010-02-07 14:26:50 +00:00
|
|
|
# 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]
|
2011-03-16 08:31:16 +00:00
|
|
|
self._tree.app.directories.set_state(self._directory_path, state)
|
2011-07-12 16:51:11 +00:00
|
|
|
self._tree.update_all_states()
|
2010-02-07 14:26:50 +00:00
|
|
|
|
|
|
|
|
2012-03-13 18:27:08 +00:00
|
|
|
class DirectoryTree(Tree, DupeGuruGUIObject):
|
2011-07-12 16:51:11 +00:00
|
|
|
#--- model -> view calls:
|
|
|
|
# refresh()
|
|
|
|
# refresh_states() # when only states label need to be refreshed
|
|
|
|
#
|
2012-01-13 21:14:06 +00:00
|
|
|
def __init__(self, app):
|
2010-02-07 14:26:50 +00:00
|
|
|
Tree.__init__(self)
|
2012-03-13 18:27:08 +00:00
|
|
|
DupeGuruGUIObject.__init__(self, app)
|
2010-02-12 14:39:29 +00:00
|
|
|
|
2012-03-13 18:27:08 +00:00
|
|
|
def _view_updated(self):
|
2010-02-07 14:26:50 +00:00
|
|
|
self._refresh()
|
|
|
|
self.view.refresh()
|
|
|
|
|
|
|
|
def _refresh(self):
|
|
|
|
self.clear()
|
|
|
|
for path in self.app.directories:
|
2011-03-16 08:31:16 +00:00
|
|
|
self.append(DirectoryNode(self, path, str(path)))
|
2010-02-07 14:26:50 +00:00
|
|
|
|
|
|
|
def add_directory(self, path):
|
|
|
|
self.app.add_directory(path)
|
|
|
|
|
2011-11-28 19:21:10 +00:00
|
|
|
def remove_selected(self):
|
2011-11-28 20:25:18 +00:00
|
|
|
selected_paths = self.selected_paths
|
|
|
|
if not selected_paths:
|
2011-11-28 19:52:48 +00:00
|
|
|
return
|
2011-11-28 20:25:18 +00:00
|
|
|
to_delete = [path[0] for path in selected_paths if len(path) == 1]
|
|
|
|
if to_delete:
|
|
|
|
self.app.remove_directories(to_delete)
|
2011-11-28 19:52:48 +00:00
|
|
|
else:
|
2011-11-28 20:25:18 +00:00
|
|
|
# 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
|
2011-11-28 19:21:10 +00:00
|
|
|
|
2011-07-12 16:51:11 +00:00
|
|
|
def update_all_states(self):
|
|
|
|
for node in self:
|
|
|
|
node.update_all_states()
|
|
|
|
self.view.refresh_states()
|
|
|
|
|
2010-02-07 14:26:50 +00:00
|
|
|
#--- Event Handlers
|
|
|
|
def directories_changed(self):
|
|
|
|
self._refresh()
|
|
|
|
self.view.refresh()
|
|
|
|
|