1 from __future__ import absolute_import
2 __copyright__ = "Copyright (C) 2013 David Braam - Released under terms of the AGPLv3 License"
11 #Only import the _core to save import time
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')
20 super(CuraApp, self).__init__(redirect=False)
22 self.mainWindow = None
24 self.loadFiles = files
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
34 portNr = 0xCA00 + sum(map(ord, version.getVersion(False)))
37 other_hwnd = windll.user32.FindWindowA(None, ctypes.c_char_p('Cura - ' + version.getVersion()))
39 sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
40 sock.sendto('\0'.join(files), ("127.0.0.1", portNr))
42 windll.user32.SetForegroundWindow(other_hwnd)
47 socketListener = threading.Thread(target=self.Win32SocketListener, args=(portNr,))
48 socketListener.daemon = True
49 socketListener.start()
51 if sys.platform.startswith('darwin'):
52 #Do not show a splashscreen on OSX, as by Apple guidelines
53 self.afterSplashCallback()
55 from Cura.gui import splashScreen
56 self.splash = splashScreen.splashScreen(self.afterSplashCallback)
58 def MacOpenFile(self, path):
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)))
64 def Win32SocketListener(self, port):
66 sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
67 sock.bind(("127.0.0.1", port))
69 data, addr = sock.recvfrom(2048)
70 self.mainWindow.OnDropFiles(data.split('\0'))
72 def afterSplashCallback(self):
73 #These imports take most of the time and thus should be done after showing the splashscreen
75 from Cura.gui import mainWindow
76 from Cura.gui import configWizard
77 from Cura.gui import newVersionDialog
78 from Cura.util import profile
79 from Cura.util import resources
80 from Cura.util import version
82 resources.setupLocalization(profile.getPreference('language')) # it's important to set up localization at very beginning to install _
84 #If we do not have preferences yet, try to load it from a previous Cura install
85 if profile.getMachineSetting('machine_type') == 'unknown':
87 otherCuraInstalls = profile.getAlternativeBasePaths()
88 otherCuraInstalls.sort()
89 if len(otherCuraInstalls) > 0:
90 profile.loadPreferences(os.path.join(otherCuraInstalls[-1], 'preferences.ini'))
91 profile.loadProfile(os.path.join(otherCuraInstalls[-1], 'current_profile.ini'))
94 print traceback.print_exc()
96 #If we haven't run it before, run the configuration wizard.
97 if profile.getMachineSetting('machine_type') == 'unknown':
98 if platform.system() == "Windows":
99 exampleFile = os.path.normpath(os.path.join(resources.resourceBasePath, 'example', 'UltimakerRobot_support.stl'))
101 #Check if we need to copy our examples
102 exampleFile = os.path.expanduser('~/CuraExamples/UltimakerRobot_support.stl')
103 if not os.path.isfile(exampleFile):
105 os.makedirs(os.path.dirname(exampleFile))
108 for filename in glob.glob(os.path.normpath(os.path.join(resources.resourceBasePath, 'example', '*.*'))):
109 shutil.copy(filename, os.path.join(os.path.dirname(exampleFile), os.path.basename(filename)))
110 self.loadFiles = [exampleFile]
111 if self.splash is not None:
112 self.splash.Show(False)
113 configWizard.configWizard()
115 if profile.getPreference('check_for_updates') == 'True':
116 newVersion = version.checkForNewerVersion()
117 if newVersion is not None:
118 if self.splash is not None:
119 self.splash.Show(False)
120 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:
121 webbrowser.open(newVersion)
123 if profile.getMachineSetting('machine_name') == '':
125 self.mainWindow = mainWindow.mainWindow()
126 if self.splash is not None:
127 self.splash.Show(False)
128 self.mainWindow.Show()
129 self.mainWindow.OnDropFiles(self.loadFiles)
130 if profile.getPreference('last_run_version') != version.getVersion(False):
131 profile.putPreference('last_run_version', version.getVersion(False))
132 newVersionDialog.newVersionDialog().Show()
134 setFullScreenCapable(self.mainWindow)
136 if platform.system() == "Darwin":
139 _objc = ctypes.PyDLL(objc._objc.__file__)
141 # PyObject *PyObjCObject_New(id objc_object, int flags, int retain)
142 _objc.PyObjCObject_New.restype = ctypes.py_object
143 _objc.PyObjCObject_New.argtypes = [ctypes.c_void_p, ctypes.c_int, ctypes.c_int]
145 def setFullScreenCapable(frame):
146 frameobj = _objc.PyObjCObject_New(frame.GetHandle(), 0, 1)
148 NSWindowCollectionBehaviorFullScreenPrimary = 1 << 7
149 window = frameobj.window()
150 newBehavior = window.collectionBehavior() | NSWindowCollectionBehaviorFullScreenPrimary
151 window.setCollectionBehavior_(newBehavior)
153 def setFullScreenCapable(frame):
157 def setFullScreenCapable(frame):