#!/usr/bin/env python
# -*- coding: iso-8859-1 -*-
#
#-------------------------------------------------------------------------------
#
#     This file is part of the Code_Saturne User Interface, element of the
#     Code_Saturne CFD tool.
#
#     Copyright (C) 1998-2007 EDF S.A., France
#
#     contact: saturne-support@edf.fr
#
#     The Code_Saturne User Interface 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 2 of
#     the License, or (at your option) any later version.
#
#     The Code_Saturne User Interface 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 the Code_Saturne Kernel; if not, write to the
#     Free Software Foundation, Inc.,
#     51 Franklin St, Fifth Floor,
#     Boston, MA  02110-1301  USA
#
#-------------------------------------------------------------------------------

"""
Parse command line arguments and launch the GUI.
"""

#-------------------------------------------------------------------------------
# Library modules import
#-------------------------------------------------------------------------------

import os, sys, time

#-------------------------------------------------------------------------------
# Third-party modules
#-------------------------------------------------------------------------------

try:
    from PyQt4.QtCore import *
    from PyQt4.QtGui  import *
except ImportError:
    print "\n  Error: Unable to import PyQt4.QtCore or PyQt4.QtGui modules."
    print "  Please check your PyQt4 installation.\n" 
    sys.exit(0)

#-------------------------------------------------------------------------------
# Application modules import
#-------------------------------------------------------------------------------

from Base.Common import *
from Base.CommandLine import usage, process_cmd_line

#-------------------------------------------------------------------------------
# If the user just wants help message, print them before importing any modules
#-------------------------------------------------------------------------------


if ('-h' in sys.argv[1:]) or ('--help' in sys.argv[1:]):
    print usage()
    sys.exit(0)

if ('-v' in sys.argv[1:]) or ('--version' in sys.argv[1:]):
    print 'SaturneGUI %s' % VERSION
    sys.exit(0)


#-------------------------------------------------------------------------------
# Start point of the Graphical User Interface
#-------------------------------------------------------------------------------


def startGUI():
    """
    Start Qt and a session of the application.
    """
    import Base.MainView

    case, spl, matisse, batch_window, batch_file, tree_window, read_only \
       = process_cmd_line(sys.argv[1:])

    spl = False

    app = QApplication(sys.argv)
    app.setOrganizationName("EDF R&D")
    app.setOrganizationDomain("www.code_saturne.org")
    app.setApplicationName("Code_Saturne GUI")
    #app.setWindowIcon(QIcon(":/icon.png"))
    app.connect(app, SIGNAL("lastWindowClosed()"), app, SLOT("quit()"))

    if spl:
        app.setOverrideCursor(QCursor(Qt.WaitCursor))
        path = os.path.dirname(os.path.abspath(__file__))
        pixmap = QPixmap('%s/logocs.png' % path)
        splash = QSplashScreen(pixmap, Qt.WindowStaysOnTopHint)
        splash.show()
        app.processEvents()
        QTimer.singleShot(2000, splash.hide)

    main = Base.MainView.MainView(cmd_case = case,
                                  cmd_matisse = matisse,
                                  cmd_batch_window = batch_window,
                                  cmd_batch_file = batch_file,
                                  cmd_tree_window = tree_window,
                                  cmd_read_only = read_only)

    try:
        main.show()
        if spl:
            app.processEvents()
            app.restoreOverrideCursor()
    except:
        print "\n  Unable to display a Qt window."
        print "  Please check your display environment.\n" 
        sys.exit(0)

    sys.exit(app.exec_())


if __name__ == '__main__':
    startGUI()

#-------------------------------------------------------------------------------
# End
#-------------------------------------------------------------------------------
