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