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