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
......@@ -123,18 +122,33 @@ 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()
if itemData is None:
level = self.get_item_level(currentItem)
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)
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()
......@@ -142,31 +156,34 @@ class UserDefinitionViewerPlugin(ComponentPlugin):
self._info.Clear()
def on_try_rename(self, event):
item = self._tree.GetSelection()
itemData = self._tree.GetItemData(item)
if itemData is None:
event.Veto()
return
data = itemData.GetData()
if not isinstance(data, IUserDefinition):
currentItem = self._tree.GetSelection()
level = self.get_item_level(currentItem)
if level != 3:
event.Veto()
return
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)
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()
self.build_plugins_tree()
def close(self):
pass
......@@ -175,6 +192,15 @@ class UserDefinitionViewerPlugin(ComponentPlugin):
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):
......
......@@ -17,10 +17,6 @@ class UserDefinitionsStore(dict):
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.
'''
__metaclass__ = Singleton
......@@ -85,6 +81,16 @@ class UserDefinitionsStore(dict):
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):
if self.has_definition(target, section, name):
......@@ -101,6 +107,17 @@ class UserDefinitionsStore(dict):
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):
if not self.has_key(target):
......
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