#include "TEveViewer.h"
#include "TEveScene.h"
#include "TEveSceneInfo.h"
#include "TEveManager.h"
#include "TEveSelection.h"
#include "TGLFormat.h"
#include "TGLSAViewer.h"
#include "TGLEmbeddedViewer.h"
#include "TGLScenePad.h"
#include "TGLEventHandler.h"
#include "TApplication.h"
#include "TEnv.h"
#include "TSystem.h"
ClassImp(TEveViewer);
Bool_t TEveViewer::fgInitInternal = kFALSE;
Bool_t TEveViewer::fgRecreateGlOnDockOps = kFALSE;
TEveViewer::TEveViewer(const char* n, const char* t) :
TEveWindowFrame(0, n, t),
fGLViewer (0),
fGLViewerFrame (0)
{
SetChildClass(TEveSceneInfo::Class());
fGUIFrame->SetCleanup(kNoCleanup);
if (!fgInitInternal)
{
InitInternal();
}
}
TEveViewer::~TEveViewer()
{
fGLViewer->SetEventHandler(0);
fGLViewerFrame->UnmapWindow();
GetGUICompositeFrame()->RemoveFrame(fGLViewerFrame);
fGLViewerFrame->ReparentWindow(gClient->GetDefaultRoot());
TTimer::SingleShot(150, "TGLViewer", fGLViewer, "Delete()");
}
void TEveViewer::InitInternal()
{
fgRecreateGlOnDockOps = (gVirtualX->SupportsExtension("Apple-WM") == 1);
fgInitInternal = kTRUE;
}
void TEveViewer::PreUndock()
{
TEveWindowFrame::PreUndock();
if (fgRecreateGlOnDockOps)
{
if (fGLViewer->GetGLWidget())
{
fGLViewer->DestroyGLWidget();
}
}
}
void TEveViewer::PostDock()
{
if (fgRecreateGlOnDockOps) {
fGLViewer->CreateGLWidget();
}
TEveWindowFrame::PostDock();
}
const TGPicture* TEveViewer::GetListTreeIcon(Bool_t)
{
return TEveElement::fgListTreeIcons[1];
}
void TEveViewer::SetGLViewer(TGLViewer* viewer, TGFrame* frame)
{
delete fGLViewer;
fGLViewer = viewer;
fGLViewerFrame = frame;
fGLViewer->SetSmartRefresh(kTRUE);
}
TGLSAViewer* TEveViewer::SpawnGLViewer(TGedEditor* ged, Bool_t stereo, Bool_t quad_buf)
{
static const TEveException kEH("TEveViewer::SpawnGLViewer ");
TGCompositeFrame* cf = GetGUICompositeFrame();
TGLFormat *form = 0;
if (stereo && quad_buf)
{
form = new TGLFormat;
form->SetStereo(kTRUE);
}
cf->SetEditable(kTRUE);
TGLSAViewer* v = 0;
try
{
v = new TGLSAViewer(cf, 0, ged, form);
}
catch (std::exception&)
{
Error("SpawnGLViewer", "Insufficient support from the graphics hardware. Aborting.");
gApplication->Terminate(1);
}
cf->SetEditable(kFALSE);
v->ToggleEditObject();
v->DisableCloseMenuEntries();
if (gEnv->GetValue("Eve.Viewer.HideMenus", 1) == 1)
{
v->EnableMenuBarHiding();
}
SetGLViewer(v, v->GetFrame());
if (stereo)
v->SetStereo(kTRUE, quad_buf);
if (fEveFrame == 0)
PreUndock();
return v;
}
TGLEmbeddedViewer* TEveViewer::SpawnGLEmbeddedViewer(TGedEditor* ged, Int_t border)
{
static const TEveException kEH("TEveViewer::SpawnGLEmbeddedViewer ");
TGCompositeFrame* cf = GetGUICompositeFrame();
TGLEmbeddedViewer* v = new TGLEmbeddedViewer(cf, 0, ged, border);
SetGLViewer(v, v->GetFrame());
cf->AddFrame(fGLViewerFrame, new TGLayoutHints(kLHintsNormal | kLHintsExpandX | kLHintsExpandY));
fGLViewerFrame->MapWindow();
if (fEveFrame == 0)
PreUndock();
return v;
}
void TEveViewer::Redraw(Bool_t resetCameras)
{
if (resetCameras) fGLViewer->PostSceneBuildSetup(kTRUE);
fGLViewer->RequestDraw(TGLRnrCtx::kLODHigh);
}
void TEveViewer::SwitchStereo()
{
TGLSAViewer *v = dynamic_cast<TGLSAViewer*>(fGLViewer);
if (!v) {
Warning("SwitchStereo", "Only supported for TGLSAViewer.");
return;
}
v->DestroyGLWidget();
TGLFormat *f = v->GetFormat();
switch_stereo:
f->SetStereo(!f->IsStereo());
v->SetStereo(f->IsStereo());
try
{
v->CreateGLWidget();
}
catch (std::exception&)
{
Error("SwitchStereo", "Insufficient support from the graphics hardware. Reverting.");
goto switch_stereo;
}
}
void TEveViewer::AddScene(TEveScene* scene)
{
static const TEveException eh("TEveViewer::AddScene ");
TGLSceneInfo* glsi = fGLViewer->AddScene(scene->GetGLScene());
if (glsi != 0) {
TEveSceneInfo* si = new TEveSceneInfo(this, scene, glsi);
AddElement(si);
} else {
throw(eh + "scene already in the viewer.");
}
}
void TEveViewer::RemoveElementLocal(TEveElement* el)
{
fGLViewer->RemoveScene(((TEveSceneInfo*)el)->GetGLScene());
}
void TEveViewer::RemoveElementsLocal()
{
fGLViewer->RemoveAllScenes();
}
TObject* TEveViewer::GetEditorObject(const TEveException& eh) const
{
if (!fGLViewer)
throw(eh + "fGLViewer not set.");
return fGLViewer;
}
Bool_t TEveViewer::HandleElementPaste(TEveElement* el)
{
static const TEveException eh("TEveViewer::HandleElementPaste ");
TEveScene* scene = dynamic_cast<TEveScene*>(el);
if (scene != 0) {
AddScene(scene);
return kTRUE;
} else {
Warning(eh.Data(), "class TEveViewer only accepts TEveScene paste argument.");
return kFALSE;
}
}
ClassImp(TEveViewerList);
TEveViewerList::TEveViewerList(const char* n, const char* t) :
TEveElementList(n, t),
fShowTooltip (kTRUE),
fBrightness(0),
fUseLightColorSet(kFALSE)
{
SetChildClass(TEveViewer::Class());
Connect();
}
TEveViewerList::~TEveViewerList()
{
Disconnect();
}
void TEveViewerList::AddElement(TEveElement* el)
{
TEveElementList::AddElement(el);
el->IncParentIgnoreCnt();
}
void TEveViewerList::RemoveElementLocal(TEveElement* el)
{
el->DecParentIgnoreCnt();
TEveElementList::RemoveElementLocal(el);
}
void TEveViewerList::RemoveElementsLocal()
{
for (List_i i=fChildren.begin(); i!=fChildren.end(); ++i)
{
(*i)->DecParentIgnoreCnt();
}
TEveElementList::RemoveElementsLocal();
}
void TEveViewerList::Connect()
{
TQObject::Connect("TGLViewer", "MouseOver(TObject*,UInt_t)",
"TEveViewerList", this, "OnMouseOver(TObject*,UInt_t)");
TQObject::Connect("TGLViewer", "ReMouseOver(TObject*,UInt_t)",
"TEveViewerList", this, "OnReMouseOver(TObject*,UInt_t)");
TQObject::Connect("TGLViewer", "UnMouseOver(TObject*,UInt_t)",
"TEveViewerList", this, "OnUnMouseOver(TObject*,UInt_t)");
TQObject::Connect("TGLViewer", "Clicked(TObject*,UInt_t,UInt_t)",
"TEveViewerList", this, "OnClicked(TObject*,UInt_t,UInt_t)");
TQObject::Connect("TGLViewer", "ReClicked(TObject*,UInt_t,UInt_t)",
"TEveViewerList", this, "OnReClicked(TObject*,UInt_t,UInt_t)");
TQObject::Connect("TGLViewer", "UnClicked(TObject*,UInt_t,UInt_t)",
"TEveViewerList", this, "OnUnClicked(TObject*,UInt_t,UInt_t)");
}
void TEveViewerList::Disconnect()
{
TQObject::Disconnect("TGLViewer", "MouseOver(TObject*,UInt_t)",
this, "OnMouseOver(TObject*,UInt_t)");
TQObject::Disconnect("TGLViewer", "ReMouseOver(TObject*,UInt_t)",
this, "OnReMouseOver(TObject*,UInt_t)");
TQObject::Disconnect("TGLViewer", "UnMouseOver(TObject*,UInt_t)",
this, "OnUnMouseOver(TObject*,UInt_t)");
TQObject::Disconnect("TGLViewer", "Clicked(TObject*,UInt_t,UInt_t)",
this, "OnClicked(TObject*,UInt_t,UInt_t)");
TQObject::Disconnect("TGLViewer", "ReClicked(TObject*,UInt_t,UInt_t)",
this, "OnReClicked(TObject*,UInt_t,UInt_t)");
TQObject::Disconnect("TGLViewer", "UnClicked(TObject*,UInt_t,UInt_t)",
this, "OnUnClicked(TObject*,UInt_t,UInt_t)");
}
void TEveViewerList::RepaintChangedViewers(Bool_t resetCameras, Bool_t dropLogicals)
{
for (List_i i=fChildren.begin(); i!=fChildren.end(); ++i)
{
TGLViewer* glv = ((TEveViewer*)*i)->GetGLViewer();
if (glv->IsChanged())
{
if (resetCameras) glv->PostSceneBuildSetup(kTRUE);
if (dropLogicals) glv->SetSmartRefresh(kFALSE);
glv->RequestDraw(TGLRnrCtx::kLODHigh);
if (dropLogicals) glv->SetSmartRefresh(kTRUE);
}
}
}
void TEveViewerList::RepaintAllViewers(Bool_t resetCameras, Bool_t dropLogicals)
{
for (List_i i=fChildren.begin(); i!=fChildren.end(); ++i)
{
TGLViewer* glv = ((TEveViewer*)*i)->GetGLViewer();
if (resetCameras) glv->PostSceneBuildSetup(kTRUE);
if (dropLogicals) glv->SetSmartRefresh(kFALSE);
glv->RequestDraw(TGLRnrCtx::kLODHigh);
if (dropLogicals) glv->SetSmartRefresh(kTRUE);
}
}
void TEveViewerList::DeleteAnnotations()
{
for (List_i i=fChildren.begin(); i!=fChildren.end(); ++i)
{
TGLViewer* glv = ((TEveViewer*)*i)->GetGLViewer();
glv->DeleteOverlayAnnotations();
}
}
void TEveViewerList::SceneDestructing(TEveScene* scene)
{
for (List_i i=fChildren.begin(); i!=fChildren.end(); ++i)
{
TEveViewer* viewer = (TEveViewer*) *i;
List_i j = viewer->BeginChildren();
while (j != viewer->EndChildren())
{
TEveSceneInfo* sinfo = (TEveSceneInfo*) *j;
++j;
if (sinfo->GetScene() == scene)
viewer->RemoveElement(sinfo);
}
}
}
void TEveViewerList::HandleTooltip()
{
if (fShowTooltip)
{
TGLViewer *glw = dynamic_cast<TGLViewer*>((TQObject*) gTQSender);
TGLEventHandler *glh = (TGLEventHandler*) glw->GetEventHandler();
if (gEve->GetHighlight()->NumChildren() == 1)
{
TString title(gEve->GetHighlight()->FirstChild()->GetHighlightTooltip());
if ( ! title.IsNull())
glh->TriggerTooltip(title);
}
else
{
glh->RemoveTooltip();
}
}
}
void TEveViewerList::OnMouseOver(TObject *obj, UInt_t )
{
TEveElement *el = dynamic_cast<TEveElement*>(obj);
if (el && !el->IsPickable())
el = 0;
void *qsender = gTQSender;
gEve->GetHighlight()->UserPickedElement(el, kFALSE);
gTQSender = qsender;
HandleTooltip();
}
void TEveViewerList::OnReMouseOver(TObject *obj, UInt_t )
{
TEveElement* el = dynamic_cast<TEveElement*>(obj);
if (el && !el->IsPickable())
el = 0;
void *qsender = gTQSender;
gEve->GetHighlight()->UserRePickedElement(el);
gTQSender = qsender;
HandleTooltip();
}
void TEveViewerList::OnUnMouseOver(TObject *obj, UInt_t )
{
TEveElement* el = dynamic_cast<TEveElement*>(obj);
if (el && !el->IsPickable())
el = 0;
void *qsender = gTQSender;
gEve->GetHighlight()->UserUnPickedElement(el);
gTQSender = qsender;
HandleTooltip();
}
void TEveViewerList::OnClicked(TObject *obj, UInt_t , UInt_t state)
{
TEveElement* el = dynamic_cast<TEveElement*>(obj);
if (el && !el->IsPickable())
el = 0;
gEve->GetSelection()->UserPickedElement(el, state & kKeyControlMask);
}
void TEveViewerList::OnReClicked(TObject *obj, UInt_t , UInt_t )
{
TEveElement* el = dynamic_cast<TEveElement*>(obj);
if (el && !el->IsPickable())
el = 0;
gEve->GetSelection()->UserRePickedElement(el);
}
void TEveViewerList::OnUnClicked(TObject *obj, UInt_t , UInt_t )
{
TEveElement* el = dynamic_cast<TEveElement*>(obj);
if (el && !el->IsPickable())
el = 0;
gEve->GetSelection()->UserUnPickedElement(el);
}
void TEveViewerList::SetColorBrightness(Float_t b)
{
TEveUtil::SetColorBrightness(b, 1);
}
void TEveViewerList::SwitchColorSet()
{
fUseLightColorSet = ! fUseLightColorSet;
for (List_i i=fChildren.begin(); i!=fChildren.end(); ++i)
{
TGLViewer* glv = ((TEveViewer*)*i)->GetGLViewer();
if ( fUseLightColorSet)
glv->UseLightColorSet();
else
glv->UseDarkColorSet();
glv->RequestDraw(TGLRnrCtx::kLODHigh);
}
}