chiark / gitweb /
Add uppercase STL and HEX to file dialog filters for linux/MacOS
[cura.git] / Cura / skeinforge_application / skeinforge_plugins / profile.py
1 """
2 This page is in the table of contents.
3 Profile is a script to set the craft types setting for the skeinforge chain.
4
5 Profile presents the user with a choice of the craft types in the profile_plugins folder.  The chosen craft type is used to determine the craft type profile for the skeinforge chain.  The default craft type is extrusion.
6
7 The setting is the selection.  If you hit 'Save and Close' the selection will be saved, if you hit 'Cancel' the selection will not be saved.
8
9 To change the profile setting, in a shell in the profile folder type:
10 > python profile.py
11
12 """
13
14 from __future__ import absolute_import
15 #Init has to be imported first because it has code to workaround the python bug where relative imports don't work if the module is imported as a main module.
16 import __init__
17
18 from fabmetheus_utilities import archive
19 from fabmetheus_utilities import euclidean
20 from fabmetheus_utilities import settings
21 from skeinforge_application.skeinforge_utilities import skeinforge_profile
22 import os
23
24
25 __author__ = 'Enrique Perez (perez_enrique@yahoo.com)'
26 __date__ = '$Date: 2008/21/04 $'
27 __license__ = 'GNU Affero General Public License http://www.gnu.org/licenses/agpl.html'
28
29
30 def addSubmenus( craftTypeName, menu, pluginFileName, pluginPath, profileRadioVar ):
31         "Add a tool plugin menu."
32         submenu = settings.Tkinter.Menu( menu, tearoff = 0 )
33         menu.add_cascade( label = pluginFileName.capitalize(), menu = submenu )
34         settings.ToolDialog().addPluginToMenu( submenu, pluginPath )
35         submenu.add_separator()
36         pluginModule = skeinforge_profile.getCraftTypePluginModule( pluginFileName )
37         profilePluginSettings = settings.getReadRepository( pluginModule.getNewRepository() )
38         isSelected = ( craftTypeName == pluginFileName )
39         for profileName in profilePluginSettings.profileList.value:
40                 value = isSelected and profileName == profilePluginSettings.profileListbox.value
41                 ProfileMenuRadio( pluginFileName, submenu, profileName, profileRadioVar, value )
42
43 def addToMenu( master, menu, repository, window ):
44         "Add a tool plugin menu."
45         ProfileMenuSaveListener( menu, window )
46
47 def addToProfileMenu( menu ):
48         "Add a profile menu."
49         settings.ToolDialog().addPluginToMenu(menu, archive.getUntilDot(archive.getSkeinforgePluginsPath('profile.py')))
50         menu.add_separator()
51         directoryPath = skeinforge_profile.getPluginsDirectoryPath()
52         pluginFileNames = skeinforge_profile.getPluginFileNames()
53         craftTypeName = skeinforge_profile.getCraftTypeName()
54         profileRadioVar = settings.Tkinter.StringVar()
55         for pluginFileName in pluginFileNames:
56                 addSubmenus( craftTypeName, menu, pluginFileName, os.path.join( directoryPath, pluginFileName ), profileRadioVar )
57
58 def getNewRepository():
59         'Get new repository.'
60         return skeinforge_profile.ProfileRepository()
61
62
63 class ProfileMenuRadio:
64         "A class to display a profile menu radio button."
65         def __init__( self, profilePluginFileName, menu, name, radioVar, value ):
66                 "Create a profile menu radio."
67                 self.activate = False
68                 self.menu = menu
69                 self.name = name
70                 self.profileJoinName = profilePluginFileName + '.& /' + name
71                 self.profilePluginFileName = profilePluginFileName
72                 self.radioVar = radioVar
73                 menu.add_radiobutton( label = name.replace('_', ' '), command = self.clickRadio, value = self.profileJoinName, variable = self.radioVar )
74                 self.menuLength = menu.index( settings.Tkinter.END )
75                 if value:
76                         self.radioVar.set( self.profileJoinName )
77                         self.menu.invoke( self.menuLength )
78                 self.activate = True
79
80         def clickRadio(self):
81                 "Workaround for Tkinter bug, invoke and set the value when clicked."
82                 if not self.activate:
83                         return
84                 self.radioVar.set( self.profileJoinName )
85                 pluginModule = skeinforge_profile.getCraftTypePluginModule( self.profilePluginFileName )
86                 profilePluginSettings = settings.getReadRepository( pluginModule.getNewRepository() )
87                 profilePluginSettings.profileListbox.value = self.name
88                 settings.writeSettings( profilePluginSettings )
89                 profileSettings = skeinforge_profile.getReadProfileRepository()
90                 plugins = profileSettings.craftRadios
91                 for plugin in plugins:
92                         plugin.value = ( plugin.name == self.profilePluginFileName )
93                 settings.writeSettings( profileSettings )
94                 skeinforge_profile.updateProfileSaveListeners()
95
96
97 class ProfileMenuSaveListener:
98         "A class to update a profile menu."
99         def __init__( self, menu, window ):
100                 "Set the menu."
101                 self.menu = menu
102                 addToProfileMenu( menu )
103                 euclidean.addElementToListDictionaryIfNotThere( self, window, settings.globalProfileSaveListenerListTable )
104
105         def save(self):
106                 "Profile has been saved and profile menu should be updated."
107                 settings.deleteMenuItems( self.menu )
108                 addToProfileMenu( self.menu )
109
110
111 def main():
112         "Display the profile dialog."
113         settings.startMainLoopFromConstructor(getNewRepository())
114
115 if __name__ == "__main__":
116         main()