#!/usr/bin/env python

#------------------------------------------------------------------------------
#
#   The Python translation of SoQt-1.2.0/test-code/components/tripleview.cpp
#
#-- ORIGINAL COPYRIGHT NOTICE -------------------------------------------------
#
#   This file is part of the Coin 3D visualization library.
#   Copyright (C) 1998-2004 by Systems in Motion.  All rights reserved.
#
#   This library is free software; you can redistribute it and/or
#   modify it under the terms of the GNU General Public License
#   ("GPL") version 2 as published by the Free Software Foundation.
#   See the file LICENSE.GPL at the root directory of this source
#   distribution for additional information about the GNU GPL.
#
#   For using Coin with software that can not be combined with the GNU
#   GPL, and for taking advantage of the additional benefits of our
#   support services, please contact Systems in Motion about acquiring
#   a Coin Professional Edition License.
#
#   See <URL:http://www.coin3d.org/> for more information.
#
#   Systems in Motion, Teknobyen, Abels Gate 5, 7030 Trondheim, NORWAY.
#   <URL:http://www.sim.no/>.
#
#------------------------------------------------------------------------------
#
#   This is just a simple test application to check that we can have
#   SoQtComponent derived objects within other widgets.
#
#   It also demonstrates having multiple views on a scene from multiple
#   SoQtRenderArea instances.
#
#   Note that this example doesn't work correctly with SoQt + TGS'
#   Inventor for some reason. Looks like a TGS' Inventor bug to me. See
#   also Bugzilla #20.
#
#   mortene@sim.no
#
#______________________________________________________________________________

from iv import *
from qt import *
import math
import sys

start = SbTime.getTimeOfDay()

# Timer callback function will rotate the scene according to the
# current time.
def timer_callback(scenerotate, sensor):
    timediff = SbTime.getTimeOfDay() - start

    rotx = SbRotation(SbVec3f(1, 0, 0), 0.5 * timediff.getValue())
    roty = SbRotation(SbVec3f(0, 1, 0), timediff.getValue())
    rotz = SbRotation(SbVec3f(0, 0, 1), 1.5 * timediff.getValue())
    assert(isinstance(scenerotate, SoRotation))
    scenerotate.rotation.setValue(rotx * roty * rotz)

# timer_callback()


# Make a Qt renderarea as a child widget of viewparent, adding the
# scene under common and a camera with the given orientation.
def add_view(viewparent, common, cameraorientation):
    root = SoSeparator()

    camera = SoPerspectiveCamera()
    camera.orientation.setValue(cameraorientation)
    root.addChild(camera)
  
    root.addChild(common)
  
    area = SoQtRenderArea(viewparent)
    area.setSceneGraph(root)
  
    camera.viewAll(root, area.getViewportRegion())

# add_view()


def main():
    # Initialize system.

    app = QApplication(sys.argv)
    parent = QWidget()
    app.setMainWidget(parent)
    SoQt.init(parent)

    parent.setMinimumSize(300, 200)

    # Set up the Qt widget layout data.

    hlayout = QHBoxLayout(parent)

    view0 = QGroupBox(parent)
    hlayout.addWidget(view0, 0.66)

    vlayout = QVBoxLayout()
    hlayout.addLayout(vlayout, 0.33)

    view1 = QGroupBox(parent)
    vlayout.addWidget(view1, 0.50)
    view2 = QGroupBox(parent)
    vlayout.addWidget(view2, 0.50)

    # Construct the common part of the scenegraph.

    commonroot = SoGroup()
    light = SoDirectionalLight()
    light.direction.setValue(-0.5, -0.5, -0.8)
    commonroot.addChild(light)
    scenerotate = SoRotation()
    commonroot.addChild(scenerotate)

    if len(sys.argv) == 2:
        myInput = SoInput()
        myInput.openFile(sys.argv[1])
        fileroot = SoDB.readAll(myInput)
        if not fileroot:
            sys.exit(1)
        commonroot.addChild(fileroot)
    else:
        mat = SoMaterial()
        mat.diffuseColor.setValue(1, 1, 0)
        commonroot.addChild(mat)

        cube = SoCube()
        commonroot.addChild(cube)
        
        mat = SoMaterial()
        mat.diffuseColor.setValue(0, 0, 1)
        commonroot.addChild(mat)

        trans = SoTranslation()
        trans.translation.setValue(0, 0, 1)
        commonroot.addChild(trans)

        sphere = SoSphere()
        sphere.radius.setValue(0.5)
        commonroot.addChild(sphere)

    # Add the 3 renderareas.

    add_view(view0, commonroot, SbRotation(SbVec3f(0, 0, 1), 0))
    add_view(view1, commonroot, SbRotation(SbVec3f(0, 1, 0), math.pi / 2))
    add_view(view2, commonroot, SbRotation(SbVec3f(1, 0, 0), -math.pi / 2))

    # Set up a timer callback to do a simple animation.

    ts = SoTimerSensor(timer_callback, scenerotate)
    ts.setInterval(SbTime(0.02)) # max 50 fps
    ts.schedule()
    
    # Map window and start event loop.
    
    SoQt.show(parent)
    SoQt.mainLoop()

# main()

if __name__ == '__main__':
    main()

# Local Variables: ***
# mode: python ***
# End: ***