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