// HQApplication.cpp - Implementation of the HQApplication class
 

#include <stdlib.h>
 

// qt includes
#include <qapplication.h>
#include <qpoint.h>
#include <qmenubar.h>
#include <qpopupmenu.h>
#include <qapplication.h>
#include <qfiledialog.h>
#include <qkeycode.h>
#include <qpixmap.h>
#include <qtoolbar.h>
#include <qtoolbutton.h>
#include <qmainwindow.h>
#include <qmessagebox.h> 

// location of the image files for menubar icons
#include "images/tsa.xpm"
#include "images/cone.xpm"
#include "images/cutting.xpm"
#include "images/cylinder.xpm"
#include "images/fittowindow.xpm"
#include "images/folder.xpm"
#include "images/orbit.xpm"
#include "images/page.xpm"
#include "images/pan.xpm"
#include "images/print.xpm"
#include "images/query.xpm"
#include "images/select.xpm"
#include "images/selectwindow.xpm"
#include "images/sphere.xpm"
#include "images/translate.xpm"
#include "images/rotate.xpm"
#include "images/letters.xpm"
#include "images/xy.xpm"
#include "images/xz.xpm"
#include "images/yz.xpm"
#include "images/zoominter.xpm"
#include "images/zoomtowindow.xpm"

// HOOPS/Qt includes
#include "HQApplication.h"
#include "MyHQWidget.h"
#include "HQDeleter.h"
 

// Global HQDeleter object; defined in main.cpp
extern HQDeleter * deleter;
 

// this is used for the global quit
static QApplication * myparent=0;

HQApplication::HQApplication(QApplication * p) : QMainWindow()
{
 // Constuctor: 
 // Should only be called once, on application startup.
 // Subsequent HQApplication objects should be created with the 
 // constructor HQApplication::HQApplication(const char * filename)

 // Set parent widget to input QApplication object 
 myparent = p;

 // Set outer window indentification fields
 QPixmap tsaIcon = QPixmap( (const char**)tsa_xpm );
 this->setIcon(tsaIcon);
 this->setIconText("QTHOOPSVIEW");

 // create the GUI's widget structure
 this->load((const char *)0);

}

HQApplication::HQApplication(const char * filename) : QMainWindow()
{
 // Constructor: 
 // creates a new top level widget and loads the input file
 // connected to the File:New menu item in the menubar

 // Set outer window indentification fields
 QPixmap tsaIcon = QPixmap( (const char**)tsa_xpm );
 this->setIcon(tsaIcon);
 this->setIconText("QTHOOPSVIEW");

 // create the GUI's widget structure and load input file
 this->load(filename);

}

HQApplication::~HQApplication()
{
  // null destructor
}

void HQApplication::myclose()
{
 // This method uses the HQDelete object to schedule the deletion of 
 // the HQApplication "this" pointer for after the Qt Signal/Slot 
 // processor has completed unwinding.

 this->hide();

 deleter->deleteLater(this);
}


void HQApplication::closeEvent( QCloseEvent * )
{
 // catch the X button
 myclose();
}


void HQApplication::newwindow()
{
 // create a new HQApplication object without loading a file
 new HQApplication((const char *)0);
}

void HQApplication::load()
{
 // Prompt user to specify a file to load
 // and create a new window for it they give valid filename

 // prompt user for name of file to load
 QString fn = QFileDialog::getOpenFileName(0,"*.hmf");

 // if valid, create new HQApplication instance
 if (!fn.isEmpty())
  new HQApplication((const char *)fn);
}


void HQApplication::about()
{
 // about dialog
 QMessageBox::information( this, "qthoopsrefapp\n",
  "QtHoopsRefApp v1.0\n"
  "Copyright (C) 1999 Tech Soft 3D\n"
  "\nFor information about the HOOPS 3D Graphics System\n"
  "Send email to info@tsoftamerica.com or visit"
  " www.hoops3d.com");

}


void HQApplication::load(const char * filename)
{
 // Create the GUI's Qt widget structure

 // Create an instance of class MyHQWidget
 MyHQWidget * c = new MyHQWidget( this, "MyHQWidget", filename);
 c->setMinimumSize( 200, 200 ); 
 this->setCentralWidget( c ); 

 // Create file menu
 QPopupMenu *file = new QPopupMenu(); 
 file->insertItem( "New", this, SLOT(newwindow()), CTRL+Key_N );
 file->insertItem( "Open", this, SLOT(load()), CTRL+Key_O );
 file->insertItem( "Load", c, SLOT(OnLoad()), CTRL+Key_L ); 
 file->insertItem( "Clear", c, SLOT(OnClear()), CTRL+Key_Z ); 
 file->insertSeparator();
 file->insertItem( "Print", c, SLOT(OnPrint()), CTRL+Key_P ); 
 file->insertItem( "Save", c, SLOT(OnSaveFileAs()), CTRL+Key_S ); 
 file->insertSeparator();
 file->insertItem( "Close", this, SLOT(myclose()), CTRL+Key_C ); 
 file->insertSeparator();
 file->insertItem( "Exit", myparent, SLOT(quit()), CTRL+Key_Q );

 // Create help menu
 QPopupMenu *help = new QPopupMenu(); 
    help->insertItem( "About", this , SLOT(about()) );

 // get the menu bar and create its Widgets
 QMenuBar * menu =  this->menuBar();
 menu->setSeparator( QMenuBar::InWindowsStyle );
 menu->insertItem("&File", file );
 menu->insertItem("&Manipulate", c->GetManipulateMenu() );
 menu->insertItem("&Create", c->GetCreateMenu() );
 menu->insertItem("&Selection", c->GetSelectionMenu() );
 menu->insertItem("&View", c->GetViewMenu() );
 menu->insertItem("&Help", help );

 // Create a tool bar
 tools = new QToolBar( "ops", this, this );

 // Create QToolButtons for toolbar and connect them 
 // to appropriate SLOTS

 // new
 QPixmap pageIcon = QPixmap( (const char**)page_xpm );
 new QToolButton (pageIcon, "New Window", 0,this, 
        SLOT(newwindow()), tools,"new window");

 // open
 QPixmap openIcon = QPixmap( (const char**)folder_xpm );
 new QToolButton (openIcon, "Open File", 0,this, 
        SLOT(load()), tools,"open file in new window");

 // print
 QPixmap printIcon = QPixmap( (const char**)print_xpm );
 new QToolButton (printIcon, "Print", 0, c, 
        SLOT(OnPrint()), tools,"print");

 tools->addSeparator ();

 // QToolButtons connected to HOOPS/MVO View object methods

 // xy - Set Camera to look down z axix
 QPixmap xyIcon = QPixmap( (const char**)xy_xpm );
 new QToolButton (xyIcon, "XY", 0, c, 
        SLOT(OnXyPlane()), tools,"xy");

 // xz - Set Camera to look down y axis
 QPixmap xzIcon = QPixmap( (const char**)xz_xpm );
 new QToolButton (xzIcon, "XZ", 0, c, 
        SLOT(OnXzPlane()), tools,"xz");

 // yz - Set camera to look down x axis
 QPixmap yzIcon = QPixmap( (const char**)yz_xpm );
 new QToolButton (yzIcon, "YZ", 0, c, 
        SLOT(OnYzPlane()), tools,"yz");

 tools->addSeparator ();
 

 // Invoke HOOPS/MVO View method FitWorld
 QPixmap zoomextentsIcon = QPixmap( (const char**) fittowindow_xpm );
 new QToolButton (zoomextentsIcon, "Zoom to Extents", 0, c,
        SLOT(OnZoomToExtents()), tools,"zoom extents");

 // Set the HOOPS/MVO View's current Operator to HOpCameraZoomBox
 QPixmap zoomwindowIcon = QPixmap( (const char**)zoomtowindow_xpm);
 new QToolButton (zoomwindowIcon, "Zoom to Window", 0, c, 
        SLOT(OnZoomToWindow()), tools,"zoom window");

 tools->addSeparator ();
 

 // Set the HOOPS/MVO View's current Operator to HOpCameraOrbit
 QPixmap orbitIcon = QPixmap( (const char**)orbit_xpm );
 new QToolButton (orbitIcon, "Orbit", 0, c, 
        SLOT(OnOrbit()), tools,"orbit");

 // Set the HOOPS/MVO View's current Operator to HOpCameraZoom
 QPixmap zoomIcon = QPixmap( (const char**)zoominter_xpm );
 new QToolButton (zoomIcon, "Zoom", 0, c, 
        SLOT(OnZoom()), tools,"zoom");

 // set the HOOPS/MVO View's current Operator to HOpCameraPan 
 QPixmap panIcon = QPixmap( (const char**)pan_xpm );
 new QToolButton (panIcon, "Pan", 0, c, SLOT(OnPan()), tools,"pan");

 tools->addSeparator ();

 // Set the HOOPS/MVO View's current Operator to HOpSelectAperture
 QPixmap selectIcon = QPixmap( (const char**)select_xpm );
 new QToolButton (selectIcon, "Select by Single Click", 0, c, 
        SLOT(OnApertureSelect()), tools,"select");

 // Set the HOOPS/MVO View's Current Operator to HOpSelectBox
 QPixmap selectwindowIcon = QPixmap( (const char**)selectwindow_xpm );
 new QToolButton (selectwindowIcon, "Select by Window", 0, c, ]
        SLOT(OnWindowSelect()), tools,"select window");

 tools->addSeparator ();

 // Set the HOOPS/MVO View's Current Operator to HOpCreateSphere
 QPixmap sphereIcon = QPixmap( (const char**)sphere_xpm );
 new QToolButton (sphereIcon, "Create Sphere", 0, c, 
        SLOT(OnCreateSphere()), tools,"sphere");

 // Set the HOOPS/MVO View's Current Operator to HOpCreateCone
 QPixmap coneIcon = QPixmap( (const char**)cone_xpm );
 new QToolButton (coneIcon, "Create Cone", 0, c, 
        SLOT(OnCreateCone()), tools,"cone");

 // Set the HOOPS/MVO View's Current Operator to HOpCreateCylinder
 QPixmap cylinderIcon = QPixmap( (const char**)cylinder_xpm );
 new QToolButton (cylinderIcon, "Create Cylinder", 0, c, 
        SLOT(OnCreateCylinder()), tools,"cylinder");

 // Set the HOOPS/MVO View's Current Operator to HOpCreateCuttingPlane
 QPixmap cuttingIcon = QPixmap( (const char**)cutting_xpm );
 new QToolButton (cuttingIcon, "Create Cutting Plane", 0, c, 
        SLOT(OnCreateCuttingPlane()), tools,"cutting plane");

 tools->addSeparator ();

 // Set the HOOPS/MVO View's Current Operator to HOpObjectTranslate
 QPixmap translateIcon = QPixmap( (const char**)translate_xpm );
 new QToolButton (translateIcon, "Translate", 0, c, 
        SLOT(OnTranslate()), tools,"translate");

 // Set the HOOPS/MVO View's Current Operator to HOpObjectRotate
 QPixmap rotateIcon = QPixmap( (const char**)rotate_xpm );
 new QToolButton (rotateIcon, "Rotate", 0, c, 
        SLOT(OnRotate()), tools,"rotate");

 tools->addSeparator ();

 // Set the HOOPS/MVO View's Current Operator to HOpAnnotateModel
 QPixmap lettersIcon = QPixmap( (const char**)letters_xpm );
 new QToolButton (lettersIcon, "Annotate Model", 0, c, 
        SLOT(OnAnnotateModel()), tools,"annotate");

 tools->addSeparator ();

 // Set the HOOPS/MVO View's Current Operator to HOpQuery
 QPixmap queryIcon = QPixmap( (const char**)query_xpm );
 new QToolButton (queryIcon, "Query", 0, c, 
        SLOT(OnQuery()), tools,"query");

 // All done creating widgets in the toolbar, now add it to the 
 // HQApplication object
 this->addToolBar( tools, "tools", QMainWindow::Top);

 this->show();

}