chiark / gitweb /
abf89c7149c83147c60f534d4a33c4b755b79561
[cura.git] / Cura / gui / pluginPanel.py
1 from __future__ import absolute_import
2 __copyright__ = "Copyright (C) 2013 David Braam - Released under terms of the AGPLv3 License"
3
4 import wx
5 import webbrowser
6 from wx.lib import scrolledpanel
7
8 from Cura.util import profile
9 from Cura.util import explorer
10
11 class pluginPanel(wx.Panel):
12         def __init__(self, parent, callback):
13                 wx.Panel.__init__(self, parent,-1)
14                 #Plugin page
15                 self.pluginList = profile.getPluginList()
16                 self.callback = callback
17
18                 sizer = wx.GridBagSizer(2, 2)
19                 self.SetSizer(sizer)
20                 
21                 effectStringList = []
22                 for effect in self.pluginList:
23                         effectStringList.append(effect['name'])
24                 
25                 self.listbox = wx.ListBox(self, -1, choices=effectStringList)
26                 title = wx.StaticText(self, -1, "Plugins:")
27                 title.SetFont(wx.Font(wx.SystemSettings.GetFont(wx.SYS_ANSI_VAR_FONT).GetPointSize(), wx.FONTFAMILY_DEFAULT, wx.NORMAL, wx.FONTWEIGHT_BOLD))
28                 helpButton = wx.Button(self, -1, '?', style=wx.BU_EXACTFIT)
29                 addButton = wx.Button(self, -1, 'V', style=wx.BU_EXACTFIT)
30                 openPluginLocationButton = wx.Button(self, -1, 'Open plugin location')
31                 sb = wx.StaticBox(self, label="Enabled plugins")
32                 boxsizer = wx.StaticBoxSizer(sb, wx.VERTICAL)
33                 self.pluginEnabledPanel = scrolledpanel.ScrolledPanel(self)
34                 self.pluginEnabledPanel.SetupScrolling(False, True)
35                 
36                 sizer.Add(title, (0,0), border=10, flag=wx.ALIGN_CENTER_VERTICAL|wx.LEFT|wx.TOP)
37                 sizer.Add(helpButton, (0,1), border=10, flag=wx.ALIGN_RIGHT|wx.RIGHT|wx.TOP)
38                 sizer.Add(self.listbox, (1,0), span=(2,2), border=10, flag=wx.EXPAND|wx.LEFT|wx.RIGHT)
39                 sizer.Add(addButton, (3,0), span=(1,2), border=5, flag=wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_BOTTOM)
40                 sizer.Add(boxsizer, (4,0), span=(4,2), border=10, flag=wx.EXPAND|wx.LEFT|wx.RIGHT)
41                 sizer.Add(openPluginLocationButton, (8, 0), border=10, flag=wx.LEFT|wx.BOTTOM)
42                 boxsizer.Add(self.pluginEnabledPanel, 1, flag=wx.EXPAND)
43                 
44                 sizer.AddGrowableCol(0)
45                 sizer.AddGrowableRow(1) # Plugins list box
46                 sizer.AddGrowableRow(4) # Enabled plugins
47                 sizer.AddGrowableRow(5) # Enabled plugins
48                 sizer.AddGrowableRow(6) # Enabled plugins
49                 
50                 sizer = wx.BoxSizer(wx.VERTICAL)
51                 self.pluginEnabledPanel.SetSizer(sizer)
52                 
53                 self.Bind(wx.EVT_BUTTON, self.OnAdd, addButton)
54                 self.Bind(wx.EVT_BUTTON, self.OnGeneralHelp, helpButton)
55                 self.Bind(wx.EVT_BUTTON, self.OnOpenPluginLocation, openPluginLocationButton)
56                 self.listbox.Bind(wx.EVT_LEFT_DCLICK, self.OnAdd)
57                 self.panelList = []
58                 self.updateProfileToControls()
59         
60         def updateProfileToControls(self):
61                 self.pluginConfig = profile.getPluginConfig()
62                 print 'pluginPanel::updateProfileToControls', self.pluginConfig
63                 for p in self.panelList:
64                         p.Show(False)
65                         self.pluginEnabledPanel.GetSizer().Detach(p)
66                 self.panelList = []
67                 for pluginConfig in self.pluginConfig:
68                         self._buildPluginPanel(pluginConfig)
69         
70         def _buildPluginPanel(self, pluginConfig):
71                 plugin = None
72                 for pluginTest in self.pluginList:
73                         if pluginTest['filename'] == pluginConfig['filename']:
74                                 plugin = pluginTest
75                 if plugin is None:
76                         return False
77                 
78                 pluginPanel = wx.Panel(self.pluginEnabledPanel)
79                 s = wx.GridBagSizer(2, 2)
80                 pluginPanel.SetSizer(s)
81                 title = wx.StaticText(pluginPanel, -1, plugin['name'])
82                 title.SetFont(wx.Font(wx.SystemSettings.GetFont(wx.SYS_ANSI_VAR_FONT).GetPointSize(), wx.FONTFAMILY_DEFAULT, wx.NORMAL, wx.FONTWEIGHT_BOLD))
83                 remButton = wx.Button(pluginPanel, -1, 'X', style=wx.BU_EXACTFIT)
84                 helpButton = wx.Button(pluginPanel, -1, '?', style=wx.BU_EXACTFIT)
85                 s.Add(title, pos=(0,1), span=(1,2), flag=wx.ALIGN_BOTTOM|wx.TOP|wx.LEFT|wx.RIGHT, border=5)
86                 s.Add(helpButton, pos=(0,0), span=(1,1), flag=wx.TOP|wx.LEFT|wx.ALIGN_RIGHT, border=5)
87                 s.Add(remButton, pos=(0,3), span=(1,1), flag=wx.TOP|wx.RIGHT|wx.ALIGN_RIGHT, border=5)
88                 s.Add(wx.StaticLine(pluginPanel), pos=(1,0), span=(1,4), flag=wx.EXPAND|wx.LEFT|wx.RIGHT,border=3)
89                 info = wx.StaticText(pluginPanel, -1, plugin['info'])
90                 info.Wrap(300)
91                 s.Add(info, pos=(2,0), span=(1,4), flag=wx.EXPAND|wx.LEFT|wx.RIGHT,border=3)
92                 
93                 pluginPanel.paramCtrls = {}
94                 i = 0
95                 for param in plugin['params']:
96                         value = param['default']
97                         if param['name'] in pluginConfig['params']:
98                                 value = pluginConfig['params'][param['name']]
99                         
100                         ctrl = wx.TextCtrl(pluginPanel, -1, value)
101                         s.Add(wx.StaticText(pluginPanel, -1, param['description']), pos=(3+i,0), span=(1,2), flag=wx.LEFT|wx.RIGHT|wx.ALIGN_CENTER_VERTICAL,border=3)
102                         s.Add(ctrl, pos=(3+i,2), span=(1,2), flag=wx.EXPAND|wx.LEFT|wx.RIGHT,border=3)
103
104                         ctrl.Bind(wx.EVT_TEXT, self.OnSettingChange)
105                         
106                         pluginPanel.paramCtrls[param['name']] = ctrl
107                         
108                         i += 1
109                 s.Add(wx.StaticLine(pluginPanel), pos=(3+i,0), span=(1,4), flag=wx.EXPAND|wx.LEFT|wx.RIGHT,border=3)
110
111                 self.Bind(wx.EVT_BUTTON, self.OnRem, remButton)
112                 self.Bind(wx.EVT_BUTTON, self.OnHelp, helpButton)
113
114                 s.AddGrowableCol(1)
115                 pluginPanel.SetBackgroundColour(self.GetParent().GetBackgroundColour())
116                 self.pluginEnabledPanel.GetSizer().Add(pluginPanel, flag=wx.EXPAND)
117                 self.pluginEnabledPanel.Layout()
118                 self.pluginEnabledPanel.SetSize((1,1))
119                 self.Layout()
120                 self.pluginEnabledPanel.ScrollChildIntoView(pluginPanel)
121                 self.panelList.append(pluginPanel)
122                 return True
123         
124         def OnSettingChange(self, e):
125                 print 'pluginPanel::OnSettingChange'
126                 for panel in self.panelList:
127                         idx = self.panelList.index(panel)
128                         for k in panel.paramCtrls.keys():
129                                 self.pluginConfig[idx]['params'][k] = panel.paramCtrls[k].GetValue()
130                 profile.setPluginConfig(self.pluginConfig)
131                 self.callback()
132         
133         def OnAdd(self, e):
134                 if self.listbox.GetSelection() < 0:
135                         wx.MessageBox('You need to select a plugin before you can add anything.', 'Error: no plugin selected', wx.OK | wx.ICON_INFORMATION)
136                         return
137                 plugin = self.pluginList[self.listbox.GetSelection()]
138                 newConfig = {'filename': plugin['filename'], 'params': {}}
139                 if not self._buildPluginPanel(newConfig):
140                         return
141                 self.pluginConfig.append(newConfig)
142                 profile.setPluginConfig(self.pluginConfig)
143                 self.callback()
144
145         def OnRem(self, e):
146                 panel = e.GetEventObject().GetParent()
147                 sizer = self.pluginEnabledPanel.GetSizer()
148                 idx = self.panelList.index(panel)
149                 
150                 panel.Show(False)
151                 for p in self.panelList:
152                         sizer.Detach(p)
153                 self.panelList.pop(idx)
154                 for p in self.panelList:
155                                 sizer.Add(p, flag=wx.EXPAND)
156
157                 self.pluginEnabledPanel.Layout()
158                 self.pluginEnabledPanel.SetSize((1,1))
159                 self.Layout()
160
161                 self.pluginConfig.pop(idx)
162                 profile.setPluginConfig(self.pluginConfig)
163                 self.callback()
164
165         def OnHelp(self, e):
166                 panel = e.GetEventObject().GetParent()
167                 sizer = self.pluginEnabledPanel.GetSizer()
168                 idx = self.panelList.index(panel)
169                 
170                 fname = self.pluginConfig[idx]['filename'].lower()
171                 fname = fname[0].upper() + fname[1:]
172                 fname = fname[:fname.rfind('.')]
173                 webbrowser.open('http://wiki.ultimaker.com/CuraPlugin:_' + fname)
174         
175         def OnGeneralHelp(self, e):
176                 webbrowser.open('http://wiki.ultimaker.com/Category:CuraPlugin')
177         
178         def OnOpenPluginLocation(self, e):
179                 explorer.openExplorerPath(profile.getPluginBasePaths()[0])