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