"""
Part of the tagit module.
A copy of the license is provided with the project.
Author: Matthias Baumgartner, 2016
"""
# IMPORTS
# INNER-MODULE IMPORTS
from ...bindings import Binding
from ..controller import DataController
# EXPORTS
__all__ = ('CSidebox_DB_Management', )
## CODE ##
[docs]class CSidebox_DB_Management(DataController):
"""A set of functions to manage the model.
"""
def __init__(self, widget, model, settings, parent=None):
super(CSidebox_DB_Management, self).__init__(widget, model, settings, parent)
# Keyboard bindings
self.get_root().bind(on_keyboard=self.on_keyboard)
def __del__(self):
# Release bindings
self.get_root().unbind(on_keyboard=self.on_keyboard)
[docs] def on_keyboard(self, wx, evt):
"""Handle keypresses."""
if Binding.check(evt, self.settings.trace('bindings', 'sidebox', 'db_management', 'save_as', Binding.simple(115, Binding.CTRL, Binding.SHIFT))): # Ctrl + Shift + s
self.request_save_as()
return True
elif Binding.check(evt, self.settings.trace('bindings', 'sidebox', 'db_management', 'save', Binding.simple(115, (Binding.CTRL, Binding.SHIFT)))): # Ctrl + s
self.request_save()
return True
return False
def _load_callback(self, path):
"""Load the database from *path*."""
try:
self.model.load(path)
self.parent.dispatch('on_images_change')
self.parent.dispatch('status', '[color=#FF0000][b]Database: [/b][/color] Loaded')
except Exception, e:
self.widget.error('The file cannot be loaded ({0})'.format(e.message))
def _save_callback(self, cont=None, path=None):
"""Save the database to *path*.
*path* can be None, which saves it in-place.
"""
try:
self.model.save(path)
self.parent.dispatch('status', '[color=#FF0000][b]Database: [/b][/color] Saved')
if cont is not None:
cont()
except Exception, e:
self.widget.error('The database cannot be saved ({0})'.format(e.message))
def _index_callback(self, path):
"""Index a *path*."""
try:
print "Indexing", path
self.parent.dispatch('status', '[color=#FF0000][b]Database: [/b][/color] Indexing images...')
self.model.index_dir(path, recursive=True, sync_method=self.model.SYNC_SET, insert_only=True)
self.parent.dispatch('on_images_change')
self.parent.dispatch('status', '[color=#FF0000][b]Database: [/b][/color] Indexing finished')
except Exception, e:
self.widget.error('Error while indexing ({0})'.format(e.message))
def _do_load(self):
"""Load a file. Opens a file picker dialogue."""
self.widget.load_file_dialogue(self._load_callback)
def _do_save_as(self, cont=None):
"""Save a copy."""
self.widget.save_file_dialogue(lambda path: self._save_callback(cont, path))
def _do_save(self, cont=None):
"""Save in-place if possible, save copy otherwise."""
if not self.model.has_file_connection():
self._do_save_as(cont)
else:
self._save_callback(path=None, cont=cont)
def _do_new(self):
"""Load a new in-memory database."""
self.model.load(None)
self.parent.dispatch('status', '[color=#FF0000][b]Database: [/b][/color] Created in-memory')
self.parent.dispatch('on_images_change')
[docs] def request_load(self):
"""On load button click."""
if self.model.has_changes():
text='The database has unsaved changes. Do you want to save them?'
self.widget.confirm_dialogue(text, lambda: self._do_save(self._do_load), self._do_load)
else:
self._do_load()
[docs] def request_new(self):
"""On new button click."""
if self.model.has_changes():
text='The database has unsaved changes. Do you want to save them?'
self.widget.confirm_dialogue(text, lambda: self._do_save(self._do_new), self._do_new)
else:
self._do_new()
[docs] def request_save(self):
"""On save button click."""
self._do_save()
[docs] def request_save_as(self):
"""On save as button click."""
self._do_save_as()
[docs] def request_reload(self):
"""On reload button click."""
if self.model.has_changes():
def rollback():
self.model.rollback()
self.parent.dispatch('on_images_change')
self.parent.dispatch('status', '[color=#FF0000][b]Database: [/b][/color] Changes reverted')
text = 'Undo all changes?'
self.widget.confirm_dialogue(text, rollback)
[docs] def request_index(self):
"""On index button click."""
self.widget.load_dir_dialogue(self._index_callback)
## EOF ##