source: applications/Area51/trunk/Lib/area51Lib/interface/document.py @ 406

Revision 406, 4.0 KB checked in by tal, 4 years ago (diff)
External change monitoring.
Line 
1import objc
2from AppKit import *
3from defconAppKit.windows.progressWindow import ProgressWindow
4from defcon import Font
5from area51Lib.interface.documentWindow import Area51DocumentWindowController
6
7
8class Area51Document(NSDocument):
9
10    def readFromURL_ofType_error_(self, url, tp):
11        """
12        NSDocument method that is called when
13        a new document is being read from a path.
14        In this case, the _loadFont method is
15        called and the result is returned.
16        """
17        # read a font
18        result = self._loadFont(url)
19        self._needExternalChangeCheck = False
20        return (result, None)
21
22    def _loadFont(self, url):
23        """
24        Private method that hadles loading the
25        UFO at path. This returns a boolean
26        indicating if it was successful or not.
27        """
28        path = url.path()
29        # start a progress window
30        self.loadingProgress = ProgressWindow("Opening...")
31        # try to open
32        try:
33            self.font = Font(path)
34        # if there is a problem, close the progress
35        # window and return False
36        except:
37            self.loadingProgress.close()
38            del self.loadingProgress
39            return False
40        # success. return True.
41        return True
42
43    def writeSafelyToURL_ofType_forSaveOperation_error_(self, url, tp, op):
44        """
45        NSDocument that is called when the user
46        wants to save the font. This lets the
47        font object do the work.
48        """
49        path = url.path()
50        progress = None
51        # save as. toss up progress.
52        if op == NSSaveAsOperation:
53            progress = self.vanillaWindowController.startProgress("Saving...")
54        # if the font path is not the same as the url
55        # and this is not a save as operation, then the
56        # user has moved the file in the finder. handle
57        # this by setting the path in the font.
58        else:
59            if self.font.path != path:
60                self.font.path = path
61        # now save
62        self.font.save(path)
63        # close progress
64        if progress:
65            progress.close()
66        return (True, None)
67
68    def makeWindowControllers(self):
69        """
70        NSDocument method that will be called when it
71        is time to make the interface for the document.
72        """
73        window = self.vanillaWindowController = Area51DocumentWindowController(self.font)
74        self.loadingProgress.close()
75        del self.loadingProgress
76        windowController = window.w.getNSWindowController()
77        self.addWindowController_(windowController)
78
79    def setFileURL_(self, url):
80        """
81        NSDocument method that will be called when
82        the user changes the file name outside
83        of this application, ie in the Finder.
84        """
85        super(Area51Document, self).setFileURL_(url)
86        if hasattr(self, "vanillaWindowController"):
87            self.vanillaWindowController.filePathChangedByUser()
88
89    def validateUserInterfaceItem_(self, item):
90        """
91        This method validates menu items. We only care
92        about the "generate font" menu item, so this
93        only deals with that.
94        """
95        if item.action() != "compileOTF:":
96            return super(Area51Document, self).validateUserInterfaceItem_(item)
97        # only enable the item if the FDK has been found
98        return NSApp().delegate().haveFDK()
99
100    # ------------------------
101    # External Changes Support
102    # ------------------------
103
104    def setNeedsExternalChangeCheck_(self, value):
105        self._needExternalChangeCheck = value
106
107    def getNeedsExternalChangeCheck(self):
108        return self._needExternalChangeCheck
109
110    # -----------------
111    # Inspector Support
112    # -----------------
113
114    def getSelectedGlyph(self):
115        return self.vanillaWindowController.getSelectedGlyph()
116
117    # -----------------
118    # Menu Item Methods
119    # -----------------
120
121    def compileOTF_(self, sender):
122        """
123        This method will be called when the user selects the
124        "Generate Font..." menu item.
125        """
126        self.vanillaWindowController.compileOTF()
127
Note: See TracBrowser for help on using the repository browser.