chiark / gitweb /
Add "Reload platform" to refresh all the objects on the platform from their files
[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                 for p in self.panelList:
63                         p.Show(False)
64                         self.pluginEnabledPanel.GetSizer().Detach(p)
65                 self.panelList = []
66                 for pluginConfig in self.pluginConfig:
67                         self._buildPluginPanel(pluginConfig)
68
69         def _buildPluginPanel(self, pluginConfig):
70                 plugin = None
71                 for pluginTest in self.pluginList:
72                         if pluginTest['filename'] == pluginConfig['filename']:
73                                 plugin = pluginTest
74                 if plugin is None:
75                         return False
76
77                 pluginPanel = wx.Panel(self.pluginEnabledPanel)
78                 s = wx.GridBagSizer(2, 2)
79                 pluginPanel.SetSizer(s)
80                 title = wx.StaticText(pluginPanel, -1, plugin['name'])
81                 title.SetFont(wx.Font(wx.SystemSettings.GetFont(wx.SYS_ANSI_VAR_FONT).GetPointSize(), wx.FONTFAMILY_DEFAULT, wx.NORMAL, wx.FONTWEIGHT_BOLD))
82                 remButton = wx.Button(pluginPanel, -1, 'X', style=wx.BU_EXACTFIT)
83                 helpButton = wx.Button(pluginPanel, -1, '?', style=wx.BU_EXACTFIT)
84                 s.Add(title, pos=(0,1), span=(1,2), flag=wx.ALIGN_BOTTOM|wx.TOP|wx.LEFT|wx.RIGHT, border=5)
85                 s.Add(helpButton, pos=(0,0), span=(1,1), flag=wx.TOP|wx.LEFT|wx.ALIGN_RIGHT, border=5)
86                 s.Add(remButton, pos=(0,3), span=(1,1), flag=wx.TOP|wx.RIGHT|wx.ALIGN_RIGHT, border=5)
87                 s.Add(wx.StaticLine(pluginPanel), pos=(1,0), span=(1,4), flag=wx.EXPAND|wx.LEFT|wx.RIGHT,border=3)
88                 info = wx.StaticText(pluginPanel, -1, plugin['info'])
89                 info.Wrap(300)
90                 s.Add(info, pos=(2,0), span=(1,4), flag=wx.EXPAND|wx.LEFT|wx.RIGHT,border=3)
91
92                 pluginPanel.paramCtrls = {}
93                 i = 0
94                 for param in plugin['params']:
95                         value = param['default']
96                         if param['name'] in pluginConfig['params']:
97                                 value = pluginConfig['params'][param['name']]
98
99                         ctrl = wx.TextCtrl(pluginPanel, -1, value)
100                         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)
101                         s.Add(ctrl, pos=(3+i,2), span=(1,2), flag=wx.EXPAND|wx.LEFT|wx.RIGHT,border=3)
102
103                         ctrl.Bind(wx.EVT_TEXT, self.OnSettingChange)
104
105                         pluginPanel.paramCtrls[param['name']] = ctrl
106
107                         i += 1
108                 s.Add(wx.StaticLine(pluginPanel), pos=(3+i,0), span=(1,4), flag=wx.EXPAND|wx.LEFT|wx.RIGHT,border=3)
109
110                 self.Bind(wx.EVT_BUTTON, self.OnRem, remButton)
111                 self.Bind(wx.EVT_BUTTON, self.OnHelp, helpButton)
112
113                 s.AddGrowableCol(1)
114                 pluginPanel.SetBackgroundColour(self.GetParent().GetBackgroundColour())
115                 self.pluginEnabledPanel.GetSizer().Add(pluginPanel, flag=wx.EXPAND)
116                 self.pluginEnabledPanel.Layout()
117                 self.pluginEnabledPanel.SetSize((1,1))
118                 self.Layout()
119                 self.pluginEnabledPanel.ScrollChildIntoView(pluginPanel)
120                 self.panelList.append(pluginPanel)
121                 return True
122
123         def OnSettingChange(self, e):
124                 for panel in self.panelList:
125                         idx = self.panelList.index(panel)
126                         for k in panel.paramCtrls.keys():
127                                 self.pluginConfig[idx]['params'][k] = panel.paramCtrls[k].GetValue()
128                 profile.setPluginConfig(self.pluginConfig)
129                 self.callback()
130
131         def OnAdd(self, e):
132                 if self.listbox.GetSelection() < 0:
133                         wx.MessageBox(_("You need to select a plugin before you can add anything."), _("Error: no plugin selected"), wx.OK | wx.ICON_INFORMATION)
134                         return
135                 plugin = self.pluginList[self.listbox.GetSelection()]
136                 newConfig = {'filename': plugin['filename'], 'params': {}}
137                 if not self._buildPluginPanel(newConfig):
138                         return
139                 self.pluginConfig.append(newConfig)
140                 profile.setPluginConfig(self.pluginConfig)
141                 self.callback()
142
143         def OnRem(self, e):
144                 panel = e.GetEventObject().GetParent()
145                 sizer = self.pluginEnabledPanel.GetSizer()
146                 idx = self.panelList.index(panel)
147
148                 panel.Show(False)
149                 for p in self.panelList:
150                         sizer.Detach(p)
151                 self.panelList.pop(idx)
152                 for p in self.panelList:
153                                 sizer.Add(p, flag=wx.EXPAND)
154
155                 self.pluginEnabledPanel.Layout()
156                 self.pluginEnabledPanel.SetSize((1,1))
157                 self.Layout()
158
159                 self.pluginConfig.pop(idx)
160                 profile.setPluginConfig(self.pluginConfig)
161                 self.callback()
162
163         def OnHelp(self, e):
164                 panel = e.GetEventObject().GetParent()
165                 sizer = self.pluginEnabledPanel.GetSizer()
166                 idx = self.panelList.index(panel)
167
168                 fname = self.pluginConfig[idx]['filename'].lower()
169                 fname = fname[0].upper() + fname[1:]
170                 fname = fname[:fname.rfind('.')]
171                 webbrowser.open('http://wiki.ultimaker.com/CuraPlugin:_' + fname)
172
173         def OnGeneralHelp(self, e):
174                 webbrowser.open('http://wiki.ultimaker.com/Category:CuraPlugin')
175
176         def OnOpenPluginLocation(self, e):
177                 explorer.openExplorerPath(profile.getPluginBasePaths()[0])