chiark / gitweb /
Copyright message needs to be after the __future__ imports.
[cura.git] / Cura / gui / app.py
1 from __future__ import absolute_import
2 __copyright__ = "Copyright (C) 2013 David Braam - Released under terms of the AGPLv3 License"
3
4 import sys
5 import os
6 import platform
7 import shutil
8 import glob
9 import warnings
10
11 #Only import the _core to save import time
12 import wx._core
13
14 class CuraApp(wx.App):
15         def __init__(self, files):
16                 if platform.system() == "Windows" and not 'PYCHARM_HOSTED' in os.environ:
17                         super(CuraApp, self).__init__(redirect = True, filename = 'output.txt')
18                 else:
19                         super(CuraApp, self).__init__(redirect = False)
20
21                 self.mainWindow = None
22                 self.splash = None
23                 self.loadFiles = files
24
25                 if sys.platform.startswith('darwin'):
26                         #Do not show a splashscreen on OSX, as by Apple guidelines
27                         self.afterSplashCallback()
28                 else:
29                         from Cura.gui import splashScreen
30                         self.splash = splashScreen.splashScreen(self.afterSplashCallback)
31
32         def MacOpenFile(self, path):
33                 try:
34                         self.mainWindow.OnDropFiles([path])
35                 except Exception as e:
36                         warnings.warn("File at {p} cannot be read: {e}".format(p=path, e=str(e)))
37
38         def afterSplashCallback(self):
39                 #These imports take most of the time and thus should be done after showing the splashscreen
40                 import webbrowser
41                 from Cura.gui import mainWindow
42                 from Cura.gui import configWizard
43                 from Cura.util import profile
44                 from Cura.util import resources
45                 from Cura.util import version
46
47                 #If we haven't run it before, run the configuration wizard.
48                 if profile.getPreference('machine_type') == 'unknown':
49                         if platform.system() == "Windows":
50                                 exampleFile = os.path.normpath(os.path.join(resources.resourceBasePath, 'example', 'UltimakerRobot_support.stl'))
51                         else:
52                                 #Check if we need to copy our examples
53                                 exampleFile = os.path.expanduser('~/CuraExamples/UltimakerRobot_support.stl')
54                                 if not os.path.isfile(exampleFile):
55                                         try:
56                                                 os.makedirs(os.path.dirname(exampleFile))
57                                         except:
58                                                 pass
59                                         for filename in glob.glob(os.path.normpath(os.path.join(resources.resourceBasePath, 'example', '*.*'))):
60                                                 shutil.copy(filename, os.path.join(os.path.dirname(exampleFile), os.path.basename(filename)))
61                         self.loadFiles = [exampleFile]
62                         if self.splash is not None:
63                                 self.splash.Show(False)
64                         configWizard.configWizard()
65
66                 if profile.getPreference('check_for_updates') == 'True':
67                         newVersion = version.checkForNewerVersion()
68                         if newVersion is not None:
69                                 if self.splash is not None:
70                                         self.splash.Show(False)
71                                 if wx.MessageBox('A new version of Cura is available, would you like to download?', 'New version available', wx.YES_NO | wx.ICON_INFORMATION) == wx.YES:
72                                         webbrowser.open(newVersion)
73                                         return
74                 self.mainWindow = mainWindow.mainWindow()
75                 if self.splash is not None:
76                         self.splash.Show(False)
77                 self.mainWindow.Show()
78                 self.mainWindow.OnDropFiles(self.loadFiles)
79
80                 setFullScreenCapable(self.mainWindow)
81
82 if platform.system() == "Darwin":
83         import ctypes, objc
84         _objc = ctypes.PyDLL(objc._objc.__file__)
85
86         # PyObject *PyObjCObject_New(id objc_object, int flags, int retain)
87         _objc.PyObjCObject_New.restype = ctypes.py_object
88         _objc.PyObjCObject_New.argtypes = [ctypes.c_void_p, ctypes.c_int, ctypes.c_int]
89
90         def setFullScreenCapable(frame):
91                 frameobj = _objc.PyObjCObject_New(frame.GetHandle(), 0, 1)
92
93                 NSWindowCollectionBehaviorFullScreenPrimary = 1<<7
94                 window = frameobj.window()
95                 newBehavior = window.collectionBehavior() | NSWindowCollectionBehaviorFullScreenPrimary
96                 window.setCollectionBehavior_(newBehavior)
97
98 else:
99         def setFullScreenCapable(frame):
100                 pass