=begin
/****************************************************************************
** $Id: qt/examples/glpixmap/globjwin.cpp   2.2.2   edited 2000-08-31 $
**
** Copyright (C) 1992-2000 Trolltech AS.  All rights reserved.
**
** This file is part of an example program for Qt.  This example
** program may be used, distributed and modified without limitation.
**
*****************************************************************************/

/****************************************************************************
**
** Implementation of GLObjectWindow widget class
**
****************************************************************************/
=end

#include <qpushbutton.h>
#include <qslider.h>
#include <qlayout.h>
#include <qframe.h>
#include <qlabel.h>
#include <qmenubar.h>
#include <qpopupmenu.h>
#include <qapplication.h>
#include <qkeycode.h>
#include <qpixmap.h>
#include <qpainter.h>
#include "globjwin.h"
#include "glbox.h"
require "qt2"
include Qt2
require "glbox.rb"

class GLObjectWindow < QWidget

def initialize( parent=nil ,  name="" )
    super( parent, name )

    #// Create a menu
    @file = QPopupMenu.new()
    @file.setCheckable( TRUE )
    @file.insertItem( "Render Pixmap", self,
		      "makePixmap()" )
    @file.insertItem( "Render Pixmap Hidden", self,
		      "makePixmapHidden()" )
    @file.insertSeparator()
    @fixMenuItemId = @file.insertItem( "Use Fixed Pixmap Size", self,
				      "useFixedPixmapSize()" )
    @file.insertSeparator()
    @insertMenuItemId = @file.insertItem( "Insert Pixmap Here", self,
					 "makePixmapForMenu()" )
    @file.insertSeparator()
    @file.insertItem( "Exit",  QApplication::qApp, "quit()", CTRL+Key_Q )

    #// Create a menu bar
    m = QMenuBar.new( self )
    m.setSeparator( QMenuBar::InWindowsStyle )
    m.insertItem("&File", @file )

    #// Create nice frames to put around the OpenGL widgets
    f1 = QFrame.new( self, "frame1" )
    f1.setFrameStyle( QFrame::Sunken | QFrame::Panel )
    f1.setLineWidth( 2 )

    #// Create an OpenGL widget
    @c1 = GLBox.new( f1, "glbox1")

    #// Create a label that can display the pixmap
    @lb = QLabel.new("", self, "pixlabel" )
    @lb.setFrameStyle( QFrame::Sunken | QFrame::Panel )
    @lb.setLineWidth( 2 )
    @lb.setAlignment( AlignCenter )
    @lb.setMargin( 0 )

    #// Create the three sliders one for each rotation axis
    @x = QSlider.new ( 0, 360, 60, 0, QSlider::Vertical, self, "xsl" )
    @x.setTickmarks( QSlider::Left )
    Qt2::connect( @x, QSIGNAL("valueChanged(int)"), @c1, "setXRotation(int)" )

    @y = QSlider.new ( 0, 360, 60, 0, QSlider::Vertical, self, "ysl" )
    @y.setTickmarks( QSlider::Left )
    Qt2::connect( @y, QSIGNAL("valueChanged(int)"), @c1, "setYRotation(int)" )

    @z = QSlider.new ( 0, 360, 60, 0, QSlider::Vertical, self, "zsl" )
    @z.setTickmarks( QSlider::Left )
    Qt2::connect( @z, QSIGNAL("valueChanged(int)"), @c1, "setZRotation(int)" )


    #// Now that we have all the widgets, put them into a nice layout

    #// Put the sliders on top of each other
    vlayout = QVBoxLayout.new( 20, "vlayout")
    vlayout.addWidget( @x )
    vlayout.addWidget( @y )
    vlayout.addWidget( @z )

    #// Put the GL widget inside the frame
    flayout1 = QHBoxLayout.new( f1, 2, 2, "flayout1")
    flayout1.addWidget( @c1, 1 )

    #// Top level layout, puts the sliders to the left of the frame/GL widget
    hlayout = QHBoxLayout.new( self, 20, 20, "hlayout")
    hlayout.setMenuBar( m )
    hlayout.addLayout( vlayout )
    hlayout.addWidget( f1, 1 )
    hlayout.addWidget( @lb, 1 )
    #initialize instanse variable
    @pmSz = QSize.new(-1,-1)

end



def makePixmap()

    #// This is the easiest way to render a pixmap, and sufficient unless one
    #// has special needs

    #// Make a pixmap to to be rendered by the gl widget
    #pm = QPixmap.new(nil)

    #// Render the pixmap, with either c1's size or the fixed size pmSz
    if ( @pmSz.nil? )
	@pm = @c1.renderPixmap( @pmSz.width(), @pmSz.height() )
    else 
	@pm = @c1.renderPixmap()
   end

    if ( !@pm.isNull() )
	#// Present the pixmap to the user
	drawOnPixmap( @pm )
	@lb.setPixmap( @pm )
    else
	@lb.setText( "Failed to render Pixmap." )
    end
end


def makePixmapHidden()

    #// Make a QGLWidget to draw the pixmap. This widget will not be shown.
    w = GLBox.new( self, "temporary glwidget", @c1 )

    success = FALSE
    @pm = QPixmap.new(nil)

    if ( w.isValid() ) 
	#// Set the current rotation
	w.copyRotation( @c1 )

	#// Determine wanted pixmap size
	sz = @pmSz.isValid() ? @pmSz : @c1.size()

	#// Make our hidden glwidget render the pixmap
	@pm = w.renderPixmap( sz.width(), sz.height() )

	if ( !@pm.isNull() )
	    success = TRUE
             end
    end

    if ( success ) 
	#// Present the pixmap to the user
	drawOnPixmap( @pm )
	@lb.setPixmap( @pm )
    else 
	@lb.setText( "Failed to render Pixmap." )
    end
    deleteQObj( w )
end


def drawOnPixmap( pm )

    #// Draw some text on the pixmap to differentiate it from the GL window

    if ( pm.isNull() ) 
	print("Cannot draw on null pixmap")
	return
    else 
	p = QPainter.new( pm )
             p.setFont( QFont.new( "Helvetica", 18 ) )
	p.setPen( Qt::white )
	p.drawText( pm.rect(), AlignCenter, "This is a Pixmap" )
             p=nil
             GC.start
    end
end



def useFixedPixmapSize()

    if ( @pmSz.isValid() )
	@pmSz = QSize.new()
	@file.setItemChecked( @fixMenuItemId, FALSE )
    else
	@pmSz = QSize.new( 200, 200 )
	@file.setItemChecked( @fixMenuItemId, TRUE )
    end
end


def makePixmapForMenu()

    @pm = @c1.renderPixmap( 32, 32 )
    if ( !@pm.isNull() )
	@file.changeItem(@insertMenuItemId , @pm, "Insert Pixmap Here")
    end
end
end

