#!/usr/bin/env python
#-*- coding:utf-8 -*-

"""
This file is part of OpenSesame.

OpenSesame is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

OpenSesame is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with OpenSesame.  If not, see <http://www.gnu.org/licenses/>.
"""

if __name__ == "__main__":

	# First, load a minimum number of modules and show an empty app window. This
	# gives the user the feeling of a snappy response.
	import sys
	from PyQt4 import QtGui
	app = QtGui.QApplication(sys.argv)
	from libqtopensesame.qtopensesame import qtopensesame
	opensesame = qtopensesame()
	opensesame.show()
	app.processEvents()

	# Now that the window is shown, load the remaining modules and resume the
	# GUI initialization.
	from libopensesame.misc import change_working_dir
	change_working_dir()			
	opensesame.resume_init()
	from PyQt4 import QtCore	
	QtCore.QObject.connect(app, QtCore.SIGNAL("quit()"), opensesame.close)	

	# Open an experiment if it has been specified as a command line argument and
	# suppress the new wizard in that case.
	import os.path	
	if len(sys.argv) >= 2 and os.path.isfile(sys.argv[1]):
		new_wizard = False
		opensesame.open_file(path=sys.argv[1])
	else:
		new_wizard = True

	# Run the app (the startup method depends on command line parameters)
	if opensesame.options.run:
		opensesame.run_experiment()		
	elif opensesame.options.run_in_window:
		opensesame.run_experiment(False)
	else:	
		opensesame.check_update(always=False)
		opensesame.show_random_tip(always=False)
		if new_wizard:
			opensesame.start_new_wizard()
		else:
			opensesame.open_general_tab()
			
	opensesame.restore_window_state()
	opensesame.refresh()	
	opensesame.show()
			
	# Exit using the application exit status
	sys.exit(app.exec_())

