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