chiark / gitweb /
Merge branch 'SteamEngine' of github.com:daid/Cura into SteamEngine
[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 do not have preferences yet, try to load it from a previous Cura install
48                 if profile.getPreference('machine_type') == 'unknown':
49                         try:
50                                 otherCuraInstalls = profile.getAlternativeBasePaths()
51                                 otherCuraInstalls.sort()
52                                 profile.loadPreferences(os.path.join(otherCuraInstalls[-1], 'preferences.ini'))
53                                 profile.loadProfile(os.path.join(otherCuraInstalls[-1], 'current_profile.ini'))
54                         except:
55                                 print sys.exc_info()
56
57                 #If we haven't run it before, run the configuration wizard.
58                 if profile.getPreference('machine_type') == 'unknown':
59                         if platform.system() == "Windows":
60                                 exampleFile = os.path.normpath(os.path.join(resources.resourceBasePath, 'example', 'UltimakerRobot_support.stl'))
61                         else:
62                                 #Check if we need to copy our examples
63                                 exampleFile = os.path.expanduser('~/CuraExamples/UltimakerRobot_support.stl')
64                                 if not os.path.isfile(exampleFile):
65                                         try:
66                                                 os.makedirs(os.path.dirname(exampleFile))
67                                         except:
68                                                 pass
69                                         for filename in glob.glob(os.path.normpath(os.path.join(resources.resourceBasePath, 'example', '*.*'))):
70                                                 shutil.copy(filename, os.path.join(os.path.dirname(exampleFile), os.path.basename(filename)))
71                         self.loadFiles = [exampleFile]
72                         if self.splash is not None:
73                                 self.splash.Show(False)
74                         configWizard.configWizard()
75
76                 if profile.getPreference('check_for_updates') == 'True':
77                         newVersion = version.checkForNewerVersion()
78                         if newVersion is not None:
79                                 if self.splash is not None:
80                                         self.splash.Show(False)
81                                 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:
82                                         webbrowser.open(newVersion)
83                                         return
84                 self.mainWindow = mainWindow.mainWindow()
85                 if self.splash is not None:
86                         self.splash.Show(False)
87                 self.mainWindow.Show()
88                 self.mainWindow.OnDropFiles(self.loadFiles)
89
90                 setFullScreenCapable(self.mainWindow)
91
92 if platform.system() == "Darwin":
93         import ctypes, objc
94         _objc = ctypes.PyDLL(objc._objc.__file__)
95
96         # PyObject *PyObjCObject_New(id objc_object, int flags, int retain)
97         _objc.PyObjCObject_New.restype = ctypes.py_object
98         _objc.PyObjCObject_New.argtypes = [ctypes.c_void_p, ctypes.c_int, ctypes.c_int]
99
100         def setFullScreenCapable(frame):
101                 frameobj = _objc.PyObjCObject_New(frame.GetHandle(), 0, 1)
102
103                 NSWindowCollectionBehaviorFullScreenPrimary = 1<<7
104                 window = frameobj.window()
105                 newBehavior = window.collectionBehavior() | NSWindowCollectionBehaviorFullScreenPrimary
106                 window.setCollectionBehavior_(newBehavior)
107
108 else:
109         def setFullScreenCapable(frame):
110                 pass