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