1 __copyright__ = "Copyright (C) 2013 David Braam - Released under terms of the AGPLv3 License"
11 #Only try to import the _core to save import time
17 class CuraApp(wx.App):
18 def __init__(self, files):
19 if platform.system() == "Windows" and not 'PYCHARM_HOSTED' in os.environ:
20 from Cura.util import profile
21 super(CuraApp, self).__init__(redirect=True, filename=os.path.join(profile.getBasePath(), 'output_log.txt'))
23 super(CuraApp, self).__init__(redirect=False)
25 self.mainWindow = None
27 self.loadFiles = files
29 self.Bind(wx.EVT_ACTIVATE_APP, self.OnActivate)
31 if sys.platform.startswith('win'):
32 #Check for an already running instance, if another instance is running load files in there
33 from Cura.util import version
34 from ctypes import windll
39 portNr = 0xCA00 + sum(map(ord, version.getVersion(False)))
42 other_hwnd = windll.user32.FindWindowA(None, ctypes.c_char_p('Cura - ' + version.getVersion()))
44 sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
45 sock.sendto('\0'.join(files), ("127.0.0.1", portNr))
47 windll.user32.SetForegroundWindow(other_hwnd)
52 socketListener = threading.Thread(target=self.Win32SocketListener, args=(portNr,))
53 socketListener.daemon = True
54 socketListener.start()
56 if sys.platform.startswith('darwin'):
57 #Do not show a splashscreen on OSX, as by Apple guidelines
58 self.afterSplashCallback()
60 from Cura.gui import splashScreen
61 self.splash = splashScreen.splashScreen(self.afterSplashCallback)
63 def MacOpenFile(self, path):
65 self.mainWindow.OnDropFiles([path])
66 except Exception as e:
67 warnings.warn("File at {p} cannot be read: {e}".format(p=path, e=str(e)))
69 def MacReopenApp(self, event):
70 self.GetTopWindow().Raise()
72 def MacHideApp(self, event):
73 self.GetTopWindow().Show(False)
78 def MacPrintFile(self, file_path):
81 def OnActivate(self, e):
83 self.GetTopWindow().Raise()
86 def Win32SocketListener(self, port):
89 sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
90 sock.bind(("127.0.0.1", port))
92 data, addr = sock.recvfrom(2048)
94 wx.CallAfter(self.mainWindow.OnDropFiles, data.split('\0'))
95 except Exception as e:
96 warnings.warn("File at {p} cannot be read: {e}".format(p=data, e=str(e)))
100 def afterSplashCallback(self):
101 #These imports take most of the time and thus should be done after showing the splashscreen
103 from Cura.gui import mainWindow
104 from Cura.gui import configWizard
105 from Cura.gui import newVersionDialog
106 from Cura.util import profile
107 from Cura.util import resources
108 from Cura.util import version
110 resources.setupLocalization(profile.getPreference('language')) # it's important to set up localization at very beginning to install _
112 #If we do not have preferences yet, try to load it from a previous Cura install
113 if profile.getMachineSetting('machine_type') == 'unknown':
115 otherCuraInstalls = profile.getAlternativeBasePaths()
116 for path in otherCuraInstalls[::-1]:
118 print 'Loading old settings from %s' % (path)
119 profile.loadPreferences(os.path.join(path, 'preferences.ini'))
120 profile.loadProfile(os.path.join(path, 'current_profile.ini'))
124 print traceback.print_exc()
127 print traceback.print_exc()
129 #If we haven't run it before, run the configuration wizard.
130 if profile.getMachineSetting('machine_type') == 'unknown':
131 #Check if we need to copy our examples
132 exampleFile = os.path.normpath(os.path.join(resources.resourceBasePath, 'example', 'UltimakerRobot_support.stl'))
134 self.loadFiles = [exampleFile]
135 if self.splash is not None:
136 self.splash.Show(False)
137 configWizard.ConfigWizard()
139 if profile.getPreference('check_for_updates') == 'True':
140 newVersion = version.checkForNewerVersion()
141 if newVersion is not None:
142 if self.splash is not None:
143 self.splash.Show(False)
144 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:
145 webbrowser.open(newVersion)
147 if profile.getMachineSetting('machine_name') == '':
149 self.mainWindow = mainWindow.mainWindow()
150 if self.splash is not None:
151 self.splash.Show(False)
152 self.SetTopWindow(self.mainWindow)
153 self.mainWindow.Show()
154 self.mainWindow.OnDropFiles(self.loadFiles)
155 if profile.getPreference('last_run_version') != version.getVersion(False):
156 profile.putPreference('last_run_version', version.getVersion(False))
157 newVersionDialog.newVersionDialog().Show()
159 setFullScreenCapable(self.mainWindow)
161 if sys.platform.startswith('darwin'):
162 wx.CallAfter(self.StupidMacOSWorkaround)
164 def StupidMacOSWorkaround(self):
166 On MacOS for some magical reason opening new frames does not work until you opened a new modal dialog and closed it.
167 If we do this from software, then, as if by magic, the bug which prevents opening extra frames is gone.
169 dlg = wx.Dialog(None)
170 wx.PostEvent(dlg, wx.CommandEvent(wx.EVT_CLOSE.typeId))
174 if platform.system() == "Darwin": #Mac magic. Dragons live here. THis sets full screen options.
177 _objc = ctypes.PyDLL(objc._objc.__file__)
179 # PyObject *PyObjCObject_New(id objc_object, int flags, int retain)
180 _objc.PyObjCObject_New.restype = ctypes.py_object
181 _objc.PyObjCObject_New.argtypes = [ctypes.c_void_p, ctypes.c_int, ctypes.c_int]
183 def setFullScreenCapable(frame):
184 frameobj = _objc.PyObjCObject_New(frame.GetHandle(), 0, 1)
186 NSWindowCollectionBehaviorFullScreenPrimary = 1 << 7
187 window = frameobj.window()
188 newBehavior = window.collectionBehavior() | NSWindowCollectionBehaviorFullScreenPrimary
189 window.setCollectionBehavior_(newBehavior)
191 def setFullScreenCapable(frame):
195 def setFullScreenCapable(frame):