// MyHQWidget.cpp - Implementation of the HOOPS/Qt class MyHQWidget
//
// More about this class

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

// qt includes
#include <qfiledialog.h>
#include <qlabel.h> 
#include <qmessagebox.h>
#include <qpopupmenu.h> 
#include <qcursor.h> 
#include <qslider.h>
#include <qlayout.h>
 

// hoops_mvo includes
#include "HDB.h"
#include "HBaseView.h"
#include "HBaseModel.h"
#include "HOpCameraOrbit.h"

// MyHQWidget includes
#include "HMySelectionSet.h"

// the qt/hoops base class
#include "MyHQWidget.h"

// Hoops/3DGS include
#include "hc.h"

// this is setup in main
extern HDB * m_pHDB;
 

MyHQWidget::MyHQWidget(QWidget* parent, const char* name , const char * filename)
    : HQWidget( parent, name )
{

 CreateMenus();

 // Create and initialize HOOPS/MVO Model and View objects
 m_pHBaseModel = new HBaseModel(m_pHDB);
 m_pHBaseModel->Init();

 // Initialize View object to null ; gets created in MyHQWidget::Init
 m_pHView = 0;

 // if called with a file name we load it 
 // otherwise open an empty view
 if(filename)
  m_pHBaseModel->Read(filename);

 // enable MouseMoveEvents 
 setMouseTracking(true);

 // enable key events
 setEnabled(true);
 setFocusPolicy(QWidget::StrongFocus);

}
 

MyHQWidget::~MyHQWidget()
{
 // Destructor 

 // Clean up memory
 if(m_pHBaseModel)   delete m_pHBaseModel;
 if(m_pHView)        delete m_pHView;

 // etc.
}
 

void MyHQWidget::SetupView()
{

 // set initial HOOPS/3DGS segment tree attributes for the 
 // HOOPS/MVO View Object

 m_pHView->FitWorld();  // fit the camera to the scene extents
 m_pHView->RenderGouraud();  // set the render mode to gouraud

 // configure view segment 
 HC_Open_Segment_By_Key(m_pHView->GetViewKey());
  HC_Set_Color_By_Index("windows", 0);
  HC_Set_Selectability("everything = off");
 HC_Close_Segment();

 // Configure scene/model segment
 HC_Open_Segment_By_Key(m_pHView->GetSceneKey());
  HC_Set_Color_By_Index("faces", 2);
  HC_Set_Color_By_Index("text, lights", 1);
  HC_Set_Color_By_Index("edges, lines", 1);
  HC_Set_Color_By_Index("markers", 1);
  HC_Set_Rendering_Options
        ("no color interpolation, no color index interpolation");
  HC_Set_Visibility 
        ("lights = (faces = on, edges = off, markers = off), markers = off, faces=on, edges=off, lines=on, text = on");

  HC_Set_Selectability("everything = off, faces = on");
  HC_Set_Text_Font("transforms = off");
 HC_Close_Segment();

 // configure segment for temporary construction geometry
 HC_Open_Segment_By_Key (m_pHView->GetConstructionKey());
   HC_Set_Heuristics("quick moves");
   HC_Set_Visibility("faces = off, edges = on, lines = on");
 HC_Close_Segment();
 

 // configure windowspace segment for quickmoves
 HC_Open_Segment_By_Key(m_pHView->GetWindowspaceKey());
  HC_Set_Color_By_Index ("geometry", 3);
  HC_Set_Color_By_Index ("window contrast", 1);
  HC_Set_Color_By_Index ("windows", 1);

  HC_Set_Visibility("markers=on");
  HC_Set_Color("markers = green, lines = green");
  HC_Set_Marker_Symbol("+");
  HC_Set_Selectability("off");
 HC_Close_Segment(); 

}
 

void MyHQWidget::Init()
{
 // setup our HOOPS/MVO Base View, Selection object and current operator
 // This must be called after MyHQWidget's constructor has executed

 // create and initialize MVO HBaseView object
 m_pHView = new HBaseView(m_pHBaseModel, NULL, NULL, NULL, 
        GetWindowId(),  GetColorMap());
 m_pHView->Init();

 // create our app-specific MVO HSelection object and initialize
 m_pHView->SetSelection( new HMySelectionSet(m_pHView));
 (m_pHView->GetSelection())->Init();

 // Set up the HOOPS/MVO View's HOOPS/3DGS Attributes
 SetupView();
 
 // Set View's current Operator
 m_pHView->SetCurrentOperator(new HOpCameraOrbit(m_pHView));
 
 

 // Call the Views Update Method - initiates HOOPS/3DGS Update_Display 
 m_pHView->Update();

}
 

void MyHQWidget::load(const char * filename)
{
 // load a file into our view 
 
 // Get rid of old model
 m_pHBaseModel->Flush();

 // Clean up the View
 HC_Open_Segment_By_Key(m_pHView->GetSceneKey());
  HC_Flush_Contents (".", "geometry");
 HC_Close_Segment();
 

 // Read the file
 m_pHBaseModel->Read(filename);

 // Set up the HOOPS/MVO View's HOOPS/3DGS attributes
 SetupView();

 // Set View's current operator
 m_pHView->SetCurrentOperator(new HOpCameraOrbit(m_pHView));

 // Call the Views Update Method - initiates HOOPS/3DGS Update_Display 
 m_pHView->Update();

}
 

void MyHQWidget::OnLoad()
{
 // open a load file dialog

 QString fn = QFileDialog::getOpenFileName(0,"*.hmf",this);
 if (!fn.isEmpty())
  load(fn);
}
 

void MyHQWidget::OnOrbit() 
{
 // Set MVO View Object current Operator to HOpCameraOrbit 
 
 if (m_pHView->GetCurrentOperator())
 delete m_pHView->GetCurrentOperator();

 m_pHView->SetCurrentOperator(new HOpCameraOrbit(m_pHView)); 

}
 
 
 

void MyHQWidget::OnRightButtonDown()
{
 // Create and display a Qt Popup Menu

 QPopupMenu * s = new QPopupMenu();
 bool sl=(((HMySelectionSet*)m_pHView->GetSelection())->GetSelectLevel() == HMySelectionSet::SegmentSelectLevel);

 QPopupMenu * v = new QPopupMenu();
 v->setItemChecked(v->insertItem("Edges", this ,
        SLOT(OnToolsVisibilityEdges())), m_edges );

  // And so on....

}
 
 
 

void MyHQWidget::CreateMenus()
{
 // create the Qt Popup menus for the top level widget

 manipulate_menu = new QPopupMenu();
 orbit_m = manipulate_menu->insertItem( "Orbit Camera",  this,
        SLOT(OnOrbit()) );

  // And so on..

 // add Signal/Slot connection for updating menus prior to showing
 manipulate_menu->connect(manipulate_menu, SIGNAL(aboutToShow()), this , 
        SLOT(UpdateMenus()));
}

void MyHQWidget::UpdateMenus()
{
 // Update the Qt Popup menu state prior to displaying

 // Find name of current Operator on HOOPS/MVO View object
 char m_operator[256];
 m_pHView->GetCurrentOperator()->GetName(m_operator);

 manipulate_menu->setItemChecked( orbit_m, false);

  // And so on..

}