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

The user definition can now be viewed from the user definition widget

parent 201e2d38
......@@ -32,10 +32,8 @@ Created on Mar 30, 2015
import wx
from MDANSE.Externals.pubsub import pub
from MDANSE.GUI import DATA_CONTROLLER
from MDANSE.Framework.Plugins.IPlugin import IPlugin, plugin_parent
from MDANSE.Framework.Plugins.IPlugin import IPlugin
def get_data_plugin(window):
......@@ -62,21 +60,13 @@ class DataPlugin(IPlugin):
self._datakey = datakey
self._dataProxy = DATA_CONTROLLER[self._datakey]
self._mgr.Bind(wx.EVT_CHILD_FOCUS, self.on_changing_pane)
self._currentWindow = self
def build_panel(self):
pass
def plug(self, standalone=False):
pass
@property
def currentWindow(self):
return self._currentWindow
@property
def datakey(self):
return self._datakey
......@@ -93,14 +83,4 @@ class DataPlugin(IPlugin):
@property
def dataplugin(self):
return self
def on_changing_pane(self, event):
window = plugin_parent(event.GetWindow())
if window is None:
return
self._currentWindow = window
pub.sendMessage('msg_set_plugins_tree', plugin=window)
\ No newline at end of file
......@@ -91,11 +91,15 @@ class IPlugin(wx.Panel):
wx.Panel.__init__(self, parent, *args, **kwargs)
self._parent = parent
self._currentWindow = self
self.SetDropTarget(PluginDropTarget(self))
self.build_dialog()
self._mgr.Bind(wx.EVT_CHILD_FOCUS, self.on_changing_pane)
self._mgr.Bind(aui.EVT_AUI_PANE_CLOSE, self.on_close_pane)
@abc.abstractmethod
......@@ -123,11 +127,12 @@ class IPlugin(wx.Panel):
return False
return self.is_parent(window.Parent)
@property
def currentWindow(self):
def close(self):
pass
return self._currentWindow
def on_close_pane(self, event):
d = wx.MessageDialog(None, 'Closing this plugin will also close all the other ones you plugged in in so far. Do you really want to close ?', 'Question', wx.YES_NO|wx.YES_DEFAULT|wx.ICON_QUESTION)
......@@ -135,9 +140,6 @@ class IPlugin(wx.Panel):
return
window = event.GetPane().window
# if isinstance(window,IPlugin):
# window.close_children()
self._mgr.DetachPane(window)
window.Destroy()
......@@ -145,6 +147,8 @@ class IPlugin(wx.Panel):
self.SetFocus()
self._currentWindow = self
pub.sendMessage('msg_set_plugins_tree', plugin=self)
def build_dialog(self):
......@@ -165,17 +169,7 @@ class IPlugin(wx.Panel):
plugin = REGISTRY["plugin"].get(pluginName,None)
if plugin is None:
return
# Get the list of ancestors of the plugin that will host the plugin to be dropped in.
ancestors = IPlugin.get_ancestors(plugin)
# klasses = tuple([REGISTRY['plugin'][anc] for anc in plugin.ancestor])
# if not issubclass(self.__class__,klasses):
# self.parent.drop(pluginName)
# return
if not plugin.ancestor[0] in ancestors:
return
plugin = plugin(self)
self._mgr.AddPane(plugin, aui.AuiPaneInfo().Caption(getattr(plugin, "label", pluginName)))
......@@ -186,14 +180,15 @@ class IPlugin(wx.Panel):
plugin.SetFocus()
@staticmethod
def get_ancestors(plugin):
if plugin.ancestor == ['empty_data']:
return [plugin.type]
def on_changing_pane(self, event):
parentPlugin = REGISTRY['plugin'][plugin.ancestor[0]]
window = plugin_parent(event.GetWindow())
if window is None:
return
return [plugin.type] + IPlugin.get_ancestors(parentPlugin)
self._currentWindow = window
pub.sendMessage('msg_set_plugins_tree', plugin=window)
\ No newline at end of file
......@@ -31,72 +31,66 @@ Created on Apr 14, 2015
'''
import wx
import wx.aui as wxaui
from MDANSE.Core.Error import Error
from MDANSE import LOGGER
from MDANSE.Externals.pubsub import pub
from MDANSE.Framework.UserDefinitionsStore import UD_STORE
from MDANSE.Framework.Plugins.ComponentPlugin import ComponentPlugin
class UserDefinitionViewerPluginError(Error):
pass
class UserDefinitionViewerPlugin(ComponentPlugin):
type = "user_definition_viewer"
label = "User Definition Viewer"
ancestor = ['empty_data']
class UserDefinitionViewerFrame(wx.Frame):
def __init__(self, parent, *args, **kwargs):
def __init__(self, parent, title="User Definition Viewer", ud=None):
wx.Frame.__init__(self, parent, wx.ID_ANY, size = (800,400), title = title, style=wx.DEFAULT_DIALOG_STYLE|wx.MINIMIZE_BOX|wx.MAXIMIZE_BOX|wx.RESIZE_BORDER)
self._udTree = {}
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())
treeSizer = wx.BoxSizer(wx.VERTICAL)
self._tree = wx.TreeCtrl(self._treePanel, wx.ID_ANY, style=wx.TR_DEFAULT_STYLE|wx.TR_HIDE_ROOT|wx.TR_EDIT_LABELS)
mainPanel = wx.Panel(self, wx.ID_ANY, size=self.GetSize())
treeSizer.Add( self._tree, 1, wx.ALL|wx.EXPAND, 5)
self._treePanel.SetSizer(treeSizer)
self._infoPanel = wx.Panel(self, wx.ID_ANY, size=self.GetSize())
infoSizer = wx.BoxSizer(wx.HORIZONTAL)
self._info = wx.TextCtrl(self._infoPanel, wx.ID_ANY, style=wx.TE_MULTILINE|wx.TE_READONLY)
self._tree = wx.TreeCtrl(mainPanel, wx.ID_ANY, style=wx.TR_DEFAULT_STYLE|wx.TR_HIDE_ROOT|wx.TR_EDIT_LABELS)
infoSizer.Add(self._info, 1, wx.ALL|wx.EXPAND, 5)
self._infoPanel.SetSizer(infoSizer)
self._mgr.AddPane(self._infoPanel, wxaui.AuiPaneInfo().Center().Dock().CaptionVisible(False).CloseButton(False))
self._mgr.AddPane(self._treePanel, wxaui.AuiPaneInfo().Left().Dock().CaptionVisible(False).CloseButton(False))
self._mgr.Update()
self._root = self._tree.AddRoot("root")
self.set_plugins_tree(self._root, UD_STORE)
self._info = wx.TextCtrl(mainPanel, wx.ID_ANY, style=wx.TE_MULTILINE|wx.TE_READONLY)
self._save = wx.Button(mainPanel, wx.ID_ANY, label="Save user definitions")
mainSizer = wx.BoxSizer(wx.VERTICAL)
infoSizer = wx.BoxSizer(wx.HORIZONTAL)
infoSizer.Add(self._tree, 1, wx.ALL|wx.EXPAND, 5)
infoSizer.Add(self._info, 2, wx.ALL|wx.EXPAND, 5)
mainSizer.Add(infoSizer,1,wx.ALL|wx.EXPAND,5)
mainSizer.Add(self._save,0,wx.ALL|wx.EXPAND,5)
mainPanel.SetSizer(mainSizer)
self.Bind(wx.EVT_TREE_SEL_CHANGED, self.on_show_info )
self.Bind(wx.EVT_TREE_KEY_DOWN, self.on_delete_from_key, self._tree)
self.Bind(wx.EVT_TREE_END_LABEL_EDIT, self.on_rename, self._tree)
self.Bind(wx.EVT_TREE_BEGIN_LABEL_EDIT, self.on_try_rename, self._tree)
def build_plugins_tree(self):
self._root = self._tree.AddRoot("root")
self.set_plugins_tree(self._root, UD_STORE)
self.Bind(wx.EVT_BUTTON, self.on_save_ud, self._save)
self.Bind(wx.EVT_CLOSE, self.on_quit)
if ud is not None:
self.expand_ud(ud)
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)
def set_plugins_tree(self, node, data):
for k, v in data.items():
dataItem = wx.TreeItemData(v)
subnode = self._tree.AppendItem(node, str(k), data=dataItem)
......@@ -104,10 +98,40 @@ class UserDefinitionViewerPlugin(ComponentPlugin):
if isinstance(v, dict):
self.set_plugins_tree(subnode, v)
def find_ud(self,baseitem,itemNames):
if not itemNames:
return baseitem
name = itemNames.pop(0)
item, cookie = self._tree.GetFirstChild(baseitem)
while item.IsOk():
if self._tree.GetItemText(item) == name:
baseitem = item
return self.find_ud(baseitem,itemNames)
item, cookie = self._tree.GetNextChild(baseitem,cookie)
def on_show_info(self, event):
return None
def expand_ud(self,ud):
item = self.find_ud(self._root, ud)
if item is None:
return
self._tree.SelectItem(item,True)
while item != self._root:
item = self._tree.GetItemParent(item)
self._tree.Expand(item)
ItemData = self._tree.GetItemData(event.GetItem())
def on_show_info(self, event=None):
ItemData = self._tree.GetItemData(self._tree.GetSelection())
if ItemData is None:
return
......@@ -115,7 +139,7 @@ class UserDefinitionViewerPlugin(ComponentPlugin):
containerStr = str(ItemData.GetData())
self._info.SetValue(containerStr)
def on_delete_from_key(self, event):
keycode = event.GetKeyCode()
......@@ -130,31 +154,39 @@ class UserDefinitionViewerPlugin(ComponentPlugin):
return
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()
if d.ShowModal() == wx.ID_NO:
return
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
self._tree.DeleteAllItems()
self._udTree.clear()
self.build_plugins_tree()
self._info.Clear()
pub.sendMessage("msg_set_ud")
def on_save_ud(self,event):
UD_STORE.save()
LOGGER('User definitions successfully saved.','info',['console'])
def on_try_rename(self, event):
currentItem = self._tree.GetSelection()
......@@ -163,7 +195,7 @@ class UserDefinitionViewerPlugin(ComponentPlugin):
if level != 3:
event.Veto()
return
def on_rename(self, event):
currentItem = self._tree.GetSelection()
......@@ -183,61 +215,15 @@ class UserDefinitionViewerPlugin(ComponentPlugin):
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):
def __init__(self, parent, title="User Definition Viewer"):
wx.Frame.__init__(self, parent, wx.ID_ANY, size = (800,400), title = title, style=wx.DEFAULT_DIALOG_STYLE|wx.MINIMIZE_BOX|wx.MAXIMIZE_BOX|wx.RESIZE_BORDER)
self.build_dialog()
def build_dialog(self):
mainPanel = wx.Panel(self, wx.ID_ANY, size = self.GetSize())
mainSizer = wx.BoxSizer(wx.VERTICAL)
self._userDefinitionViewerPlugin = UserDefinitionViewerPlugin(mainPanel, wx.ID_ANY)
mainSizer.Add(self._userDefinitionViewerPlugin, 1, wx.ALL|wx.EXPAND)
mainPanel.SetSizer(mainSizer)
mainSizer.Fit(mainPanel)
mainPanel.Layout()
self.Bind(wx.EVT_CLOSE, self.on_quit)
def on_quit(self, event):
d = wx.MessageDialog(None,
'Do you really want to quit ?',
'Question',
wx.YES_NO|wx.YES_DEFAULT|wx.ICON_QUESTION)
d = wx.MessageDialog(None,'Do you really want to quit ?','Question',wx.YES_NO|wx.YES_DEFAULT|wx.ICON_QUESTION)
if d.ShowModal() == wx.ID_YES:
self.Destroy()
if __name__ == "__main__":
app = wx.App(False)
f = UserDefinitionViewerFrame(None)
f = UserDefinitionViewerFrame(None,ud=['protein_in_periodic_universe.nc','atom_selection',"sfdfdfsd"])
f.Show()
app.MainLoop()
\ No newline at end of file
......@@ -49,7 +49,7 @@ class UserDefinitionsStore(dict):
else:
self.update(UD)
f.close()
def save(self):
'''
Save the user definitions.
......
......@@ -44,7 +44,7 @@ class PartialChargesPlugin(UserDefinitionsPlugin):
type = 'partial_charges'
label = "Partial charges settings"
label = "Partial charges"
ancestor = ["molecular_viewer"]
......
......@@ -155,7 +155,7 @@ class QVectorsPlugin(UserDefinitionsPlugin):
type = 'q_vectors'
label = "Q vectors settings"
label = "Q vectors"
ancestor = ["molecular_viewer"]
......
......@@ -48,27 +48,38 @@ class UserDefinitionWidget(IWidget):
__metaclass__ = abc.ABCMeta
type = None
def initialize(self):
self._filename = None
self._basename = None
def add_widgets(self):
sizer = wx.BoxSizer(wx.HORIZONTAL)
self._availableUDs = wx.Choice(self._widgetPanel, wx.ID_ANY,style=wx.CB_SORT)
self._newUD = wx.Button(self._widgetPanel, wx.ID_ANY, label="New")
viewUD = wx.Button(self._widgetPanel, wx.ID_ANY, label="View selected definition")
newUD = wx.Button(self._widgetPanel, wx.ID_ANY, label="New definition")
sizer.Add(self._availableUDs, 1, wx.ALL|wx.EXPAND, 5)
sizer.Add(self._newUD, 0, wx.ALL|wx.EXPAND, 5)
sizer.Add(viewUD, 0, wx.ALL|wx.EXPAND, 5)
sizer.Add(newUD, 0, wx.ALL|wx.EXPAND, 5)
pub.subscribe(self.msg_save_definition, "msg_save_definition")
pub.subscribe(self.msg_set_ud, "msg_set_ud")
self.Bind(wx.EVT_BUTTON, self.on_new_user_definition, self._newUD)
self.Bind(wx.EVT_BUTTON, self.on_view_user_definition, viewUD)
self.Bind(wx.EVT_BUTTON, self.on_new_user_definition, newUD)
return sizer
def on_view_user_definition(self,event):
ud = self._availableUDs.GetStringSelection()
if not ud:
LOGGER("Please select a user definition","error",["dialog"])
return
from MDANSE.Framework.Plugins.UserDefinitionViewerPlugin import UserDefinitionViewerFrame
f = UserDefinitionViewerFrame(self,ud=[self._basename,self.type,ud])
f.Show()
def on_new_user_definition(self,event):
dlg = UserDefinitionsDialog(self,self._trajectory,self.type)
......@@ -87,21 +98,17 @@ class UserDefinitionWidget(IWidget):
self._basename = os.path.basename(self._filename)
self.msg_set_ud()
uds = UD_STORE.filter(self._basename, self.type)
self._availableUDs.SetItems(uds)
def msg_save_definition(self, message):
filename, section, name = message
if section is not self.type:
return
def msg_set_ud(self):
if filename != self._basename:
return
uds = UD_STORE.filter(self._basename, self.type)
self._availableUDs.Append(name)
self._availableUDs.SetItems(uds)
class UserDefinitionsDialog(wx.Dialog):
......@@ -152,7 +159,7 @@ class UserDefinitionsPlugin(ComponentPlugin):
actionsSizer = wx.StaticBoxSizer(sb, wx.HORIZONTAL)
self._udName = wx.TextCtrl(udPanel, wx.ID_ANY, style = wx.TE_PROCESS_ENTER)
saveButton = wx.Button(udPanel, wx.ID_ANY, label="Save")
saveButton = wx.Button(udPanel, wx.ID_ANY, label="Set")
actionsSizer.Add(self._udName, 1, wx.ALL|wx.EXPAND, 5)
actionsSizer.Add(saveButton, 0, wx.ALL, 5)
......@@ -161,9 +168,9 @@ class UserDefinitionsPlugin(ComponentPlugin):
self._mainPanel.GetSizer().Add(udPanel,0,wx.EXPAND|wx.ALL,5)
self.Bind(wx.EVT_BUTTON, self.on_save, saveButton)
self.Bind(wx.EVT_BUTTON, self.on_set_ud, saveButton)
def on_save(self, event):
def on_set_ud(self, event):
name = str(self._udName.GetValue().strip())
......@@ -180,8 +187,7 @@ class UserDefinitionsPlugin(ComponentPlugin):
return
UD_STORE.set_definition(self._target,self.type,name,value)
UD_STORE.save()
pub.sendMessage("msg_save_definition", message=(self._target,self.type,name))
pub.sendMessage("msg_set_ud")
LOGGER('User definition %r successfully saved.' % name,'info',['console'])
LOGGER('User definition %r successfully set.' % name,'info',['console'])
......@@ -321,7 +321,7 @@ or directly to the MDANSE mailing list:
self.load_data(dataType,filename)
LOGGER("Data %r successfully loaded" % filename, "info")
LOGGER("Data %r successfully loaded." % filename, "info", ["console"])
def on_open_user_definitions(self,event):