Commit d16df5b8 authored by eric pellegrini's avatar eric pellegrini
Browse files

Bug fix and improved ergonomy for user definition settings dialog

parent 1420c57e
......@@ -34,7 +34,6 @@ import wx
import wx.aui as wxaui
from MDANSE.Core.Error import Error
from MDANSE.Framework.UserDefinitions.IUserDefinition import IUserDefinition
from MDANSE.Framework.UserDefinitionsStore import UD_STORE
from MDANSE.App.GUI.Framework.Plugins.ComponentPlugin import ComponentPlugin
......@@ -57,7 +56,7 @@ class UserDefinitionViewerPlugin(ComponentPlugin):
ComponentPlugin.__init__(self, parent, size = parent.GetSize(), *args, **kwargs)
self.build_plugins_tree()
def build_panel(self):
self._treePanel = wx.Panel(self, wx.ID_ANY, size=self.GetSize())
......@@ -123,57 +122,84 @@ class UserDefinitionViewerPlugin(ComponentPlugin):
if keycode == wx.WXK_DELETE:
item = self._tree.GetSelection()
itemData = self._tree.GetItemData(item)
itemText = str(self._tree.GetItemText(item))
currentItem = self._tree.GetSelection()
level = self.get_item_level(currentItem)
if itemData is None:
if level > 3:
return
data = itemData.GetData()
if isinstance(data, IUserDefinition):
d = wx.MessageDialog(None, 'Do you really want to delete this definition ?', 'Question', wx.YES_NO|wx.YES_DEFAULT|wx.ICON_QUESTION)
if d.ShowModal() == wx.ID_YES:
UD_STORE.remove_definition(data.target,data.type,itemText)
UD_STORE.save()
self._tree.DeleteAllItems()
self._udTree.clear()
self.build_plugins_tree()
self._info.Clear()
d = wx.MessageDialog(None, 'Do you really want to delete this definition ?', 'Question', wx.YES_NO|wx.YES_DEFAULT|wx.ICON_QUESTION)
if d.ShowModal() == wx.ID_YES:
currentItemName = str(self._tree.GetItemText(currentItem))
if level == 1:
UD_STORE.remove_target(currentItemName)
elif level == 2:
targetItem = self._tree.GetParent(currentItem)
targetItemName = str(self._tree.GetItemText(targetItem))
UD_STORE.remove_section(targetItemName,currentItemName)
elif level == 3:
sectionItem = self._tree.GetParent(currentItem)
sectionItemName = str(self._tree.GetItemText(sectionItem))
targetItem = self._tree.GetParent(sectionItem)
targetItemName = str(self._tree.GetItemText(targetItem))
UD_STORE.remove_definition(targetItemName,sectionItemName,currentItemName)
else:
return
UD_STORE.save()
self._tree.DeleteAllItems()
self._udTree.clear()
self.build_plugins_tree()
self._info.Clear()
def on_try_rename(self, event):
item = self._tree.GetSelection()
itemData = self._tree.GetItemData(item)
if itemData is None:
currentItem = self._tree.GetSelection()
level = self.get_item_level(currentItem)
if level != 3:
event.Veto()
return
data = itemData.GetData()
if not isinstance(data, IUserDefinition):
event.Veto()
def on_rename(self, event):
newtext = self._tree.GetEditControl().GetValue()
item = self._tree.GetSelection()
oldtext = str(self._tree.GetItemText(item))
itemData = self._tree.GetItemData(item)
if itemData is None:
currentItem = self._tree.GetSelection()
currentItemName = str(self._tree.GetItemText(currentItem))
newItemName = self._tree.GetEditControl().GetValue()
if currentItemName == newItemName:
return
data = itemData.GetData()
if isinstance(data, IUserDefinition):
UD_STORE.set_definition(data.target,data.type,newtext,data)
UD_STORE.remove_definition(data.target,data.type,oldtext)
UD_STORE.save()
self.build_plugins_tree()
sectionItem = self._tree.GetItemParent(currentItem)
sectionItemName = str(self._tree.GetItemText(sectionItem))
targetItem = self._tree.GetItemParent(sectionItem)
targetItemName = str(self._tree.GetItemText(targetItem))
currentItemData = self._tree.GetItemData(currentItem)
UD_STORE.set_definition(targetItemName,sectionItemName,newItemName,currentItemData.GetData())
UD_STORE.remove_definition(targetItemName,sectionItemName,currentItemName)
UD_STORE.save()
def close(self):
pass
def plug(self):
self.parent.mgr.GetPane(self).Float().CloseButton(True).BestSize((800, 300))
self.parent.mgr.Update()
def get_item_level(self,item):
parent = self._tree.GetItemParent(item)
if parent == self._tree.GetRootItem():
return 1
else:
return 1 + self.get_item_level(parent)
class UserDefinitionViewerFrame(wx.Frame):
......
......@@ -16,11 +16,7 @@ class UserDefinitionsStore(dict):
Basically, user definitions are used to keep track of definitions made on a given target. The target can
be a file or any kind of object that has to be associated with the user definitions.
This definitions can be selections of atoms, Q vectors definitions, axis definitions ... The
user definitions are loaded when MDANSE starts through a cPickle file that will store these definitions.
They are stored internally as MDANSE.Framework.UserDefinition.IUserDefinition derived objects in a nested
dictionary whose primary key is the target name, secondary key is the category of the user definition and
tertiary key is the name of the user definition.
user definitions are loaded when MDANSE starts through a cPickle file that will store these definitions.
'''
__metaclass__ = Singleton
......@@ -84,6 +80,16 @@ class UserDefinitionsStore(dict):
ud = self[target][section][name]
return ud
def remove_target(self,target):
if self.has_target(target):
del self[target]
def remove_section(self,target,section):
if self.has_section(target, section):
del self[target][section]
def remove_definition(self,target,section,name):
......@@ -100,6 +106,17 @@ class UserDefinitionsStore(dict):
def filter(self,target,section):
return self.get(target,{}).get(section,{}).keys()
def has_target(self,target):
return self.has_key(target)
def has_section(self,target,section):
if not self.has_key(target):
return False
return self[target].has_key(section)
def has_definition(self,target,section,name):
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment