chiark / gitweb /
Change how the engine is interfaced from the python code. Put the GCode viewer in...
[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
15 class CuraApp(wx.App):
16         def __init__(self, files):
17                 if platform.system() == "Windows" and not 'PYCHARM_HOSTED' in os.environ:
18                         super(CuraApp, self).__init__(redirect=True, filename='output.txt')
19                 else:
20                         super(CuraApp, self).__init__(redirect=False)
21
22                 self.mainWindow = None
23                 self.splash = None
24                 self.loadFiles = files
25
26                 if sys.platform.startswith('win'):
27                         #Check for an already running instance, if another instance is running load files in there
28                         from Cura.util import version
29                         from ctypes import windll
30                         import ctypes
31                         import socket
32                         import threading
33
34                         portNr = 0xCA00 + sum(map(ord, version.getVersion(False)))
35                         if len(files) > 0:
36                                 try:
37                                         other_hwnd = windll.user32.FindWindowA(None, ctypes.c_char_p('Cura - ' + version.getVersion()))
38                                         if other_hwnd != 0:
39                                                 sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
40                                                 sock.sendto('\0'.join(files), ("127.0.0.1", portNr))
41
42                                                 windll.user32.SetForegroundWindow(other_hwnd)
43                                                 return
44                                 except:
45                                         pass
46
47                         socketListener = threading.Thread(target=self.Win32SocketListener, args=(portNr,))
48                         socketListener.daemon = True
49                         socketListener.start()
50
51                 if sys.platform.startswith('darwin'):
52                         #Do not show a splashscreen on OSX, as by Apple guidelines
53                         self.afterSplashCallback()
54                 else:
55                         from Cura.gui import splashScreen
56                         self.splash = splashScreen.splashScreen(self.afterSplashCallback)
57
58         def MacOpenFile(self, path):
59                 try:
60                         self.mainWindow.OnDropFiles([path])
61                 except Exception as e:
62                         warnings.warn("File at {p} cannot be read: {e}".format(p=path, e=str(e)))
63
64         def Win32SocketListener(self, port):
65                 import socket
66                 try:
67                         sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
68                         sock.bind(("127.0.0.1", port))
69                         while True:
70                                 data, addr = sock.recvfrom(2048)
71                                 self.mainWindow.OnDropFiles(data.split('\0'))
72                 except:
73                         pass
74
75         def afterSplashCallback(self):
76                 #These imports take most of the time and thus should be done after showing the splashscreen
77                 import webbrowser
78                 from Cura.gui import mainWindow
79                 from Cura.gui import configWizard
80                 from Cura.gui import newVersionDialog
81                 from Cura.util import profile
82                 from Cura.util import resources
83                 from Cura.util import version
84
85                 resources.setupLocalization(profile.getPreference('language'))  # it's important to set up localization at very beginning to install _
86
87                 #If we do not have preferences yet, try to load it from a previous Cura install
88                 if profile.getMachineSetting('machine_type') == 'unknown':
89                         try:
90                                 otherCuraInstalls = profile.getAlternativeBasePaths()
91                                 otherCuraInstalls.sort()
92                                 if len(otherCuraInstalls) > 0:
93                                         profile.loadPreferences(os.path.join(otherCuraInstalls[-1], 'preferences.ini'))
94                                         profile.loadProfile(os.path.join(otherCuraInstalls[-1], 'current_profile.ini'))
95                         except:
96                                 import traceback
97                                 print traceback.print_exc()
98
99                 #If we haven't run it before, run the configuration wizard.
100                 if profile.getMachineSetting('machine_type') == 'unknown':
101                         if platform.system() == "Windows":
102                                 exampleFile = os.path.normpath(os.path.join(resources.resourceBasePath, 'example', 'UltimakerRobot_support.stl'))
103                         else:
104                                 #Check if we need to copy our examples
105                                 exampleFile = os.path.expanduser('~/CuraExamples/UltimakerRobot_support.stl')
106                                 if not os.path.isfile(exampleFile):
107                                         try:
108                                                 os.makedirs(os.path.dirname(exampleFile))
109                                         except:
110                                                 pass
111                                         for filename in glob.glob(os.path.normpath(os.path.join(resources.resourceBasePath, 'example', '*.*'))):
112                                                 shutil.copy(filename, os.path.join(os.path.dirname(exampleFile), os.path.basename(filename)))
113                         self.loadFiles = [exampleFile]
114                         if self.splash is not None:
115                                 self.splash.Show(False)
116                         configWizard.configWizard()
117
118                 if profile.getPreference('check_for_updates') == 'True':
119                         newVersion = version.checkForNewerVersion()
120                         if newVersion is not None:
121                                 if self.splash is not None:
122                                         self.splash.Show(False)
123                                 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:
124                                         webbrowser.open(newVersion)
125                                         return
126                 if profile.getMachineSetting('machine_name') == '':
127                         return
128                 self.mainWindow = mainWindow.mainWindow()
129                 if self.splash is not None:
130                         self.splash.Show(False)
131                 self.mainWindow.Show()
132                 self.mainWindow.OnDropFiles(self.loadFiles)
133                 if profile.getPreference('last_run_version') != version.getVersion(False):
134                         profile.putPreference('last_run_version', version.getVersion(False))
135                         newVersionDialog.newVersionDialog().Show()
136
137                 setFullScreenCapable(self.mainWindow)
138
139 if platform.system() == "Darwin":
140         try:
141                 import ctypes, objc
142                 _objc = ctypes.PyDLL(objc._objc.__file__)
143
144                 # PyObject *PyObjCObject_New(id objc_object, int flags, int retain)
145                 _objc.PyObjCObject_New.restype = ctypes.py_object
146                 _objc.PyObjCObject_New.argtypes = [ctypes.c_void_p, ctypes.c_int, ctypes.c_int]
147
148                 def setFullScreenCapable(frame):
149                         frameobj = _objc.PyObjCObject_New(frame.GetHandle(), 0, 1)
150
151                         NSWindowCollectionBehaviorFullScreenPrimary = 1 << 7
152                         window = frameobj.window()
153                         newBehavior = window.collectionBehavior() | NSWindowCollectionBehaviorFullScreenPrimary
154                         window.setCollectionBehavior_(newBehavior)
155         except:
156                 def setFullScreenCapable(frame):
157                         pass
158
159 else:
160         def setFullScreenCapable(frame):
161                 pass