root/gaphor/tags/gaphor-0.9.2/gaphor/plugin.py

Revision 1135, 3.4 kB (checked in by arjanmol, 2 years ago)

cleanup

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 # vim:sw=4:et
2 """This module provides everything needed to create a plugin.
3
4 resource - Get/set application wide resources
5 import_plugin - The save way to import other plugins into your plugin.
6
7 Classes to construct Actions:
8     Action
9     CheckAction (Checkbutton Action)
10     RadioAction (RadioButton Action, should set the group attribute)
11     ObjectAction (this is not an action, but it contains some code to make
12                   instances of Actions behave like Action classes)
13
14 Each action is initialized. The window containing the action can be accessed
15 by the 'window' property.
16 """
17
18 import sys
19 import os.path
20 from gaphor import resource
21
22 from gaphor.misc.action import Action as _Action
23 from gaphor.misc.action import CheckAction as _CheckAction
24 from gaphor.misc.action import RadioAction as _RadioAction
25 from gaphor.misc.action import ObjectAction
26 from gaphor.i18n import _
27
28 import gtk
29
30 def import_plugin(name):
31     """
32     A normal 'import gaphor._plugins.<name>' doesn't work.
33     Use this function instead.
34     """
35     from gaphor.pluginmanager import MODULENS
36     mod = sys.modules[MODULENS + name]
37     return mod
38
39 class _ActionMixIn(object):
40     """
41     Handle initialization of actions in a way that the main window
42     can properly initialize the action.
43     """
44
45     def init(self, window):
46         self._window = window
47
48     def get_window(self):
49         return self._window
50
51     window = property(get_window)
52
53 class Action(_Action, _ActionMixIn): pass
54 class CheckAction(_CheckAction, _ActionMixIn): pass
55 class RadioAction(_RadioAction, _ActionMixIn): pass
56
57 class DiagramExportAction(Action):
58     """
59     Diagram export action allows to save a diagram into filename.
60     Deriving classes should::
61         - implement save method
62         - define title attribute
63         - define file extension
64     """
65     title = None # gtk file chooser title
66     ext   = None # file extension, like .svg
67
68     def update(self):
69         tab = self.get_window().get_current_diagram_tab()
70         self.sensitive = tab and True or False
71
72
73     def execute(self):
74         filename = (self.get_window().get_current_diagram().name or 'export') + self.ext
75         filesel = gtk.FileChooserDialog(title = self.title,
76             action = gtk.FILE_CHOOSER_ACTION_SAVE,
77             buttons = (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, gtk.STOCK_SAVE, gtk.RESPONSE_OK))
78         filesel.set_current_name(filename)
79
80         save = False
81         while True:
82             response = filesel.run()
83             filename = filesel.get_filename()
84
85             if response == gtk.RESPONSE_OK:
86                 if os.path.exists(filename):
87                     dialog = gtk.MessageDialog(filesel,
88                         gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,
89                         gtk.MESSAGE_QUESTION, gtk.BUTTONS_YES_NO,
90                         _("The file %s already exists. Do you want to replace it with the file you are exporting to?") % filename)
91                     answer = dialog.run()
92                     dialog.destroy()
93                     if answer == gtk.RESPONSE_YES:
94                         save = True
95                         break
96                 else:
97                     save = True
98                     break
99             else:
100                 break
101
102         if save and filename:
103             self.save(filename)
104         filesel.destroy()
105
106
107     def save(self, filename):
108         raise NotImplementedError, 'save method should be implemented'
109
110
111 del _Action, _CheckAction, _RadioAction, _ActionMixIn
112
Note: See TracBrowser for help on using the browser.