// @(#)root/eve:$Id$
// Authors: Matevz Tadel & Alja Mrak-Tadel: 2006, 2007

/*************************************************************************
 * Copyright (C) 1995-2007, Rene Brun and Fons Rademakers.               *
 * All rights reserved.                                                  *
 *                                                                       *
 * For the licensing terms see $ROOTSYS/LICENSE.                         *
 * For the list of contributors see $ROOTSYS/README/CREDITS.             *
 *************************************************************************/

#include "TEveTransEditor.h"
#include "TEveTrans.h"
#include "TEveGValuators.h"

#include "TVirtualPad.h"
#include "TMath.h"

#include "TGButton.h"
#include "TGLabel.h"

//______________________________________________________________________________
// TEveTransSubEditor
//
// Sub-editor for TEveTrans class.

ClassImp(TEveTransSubEditor);

//______________________________________________________________________________
TEveTransSubEditor::TEveTransSubEditor(TGWindow* p) :
   TGVerticalFrame (p),
   fTrans          (0),
   fTopHorFrame    (0),
   fUseTrans       (0),
   fEditTrans      (0),
   fEditTransFrame (0),
   fPos            (0),
   fRot            (0),
   fScale          (0),
   fAutoUpdate     (0),
   fUpdate         (0)
{
   // Constructor.

   // --- Top controls

   fTopHorFrame = new TGHorizontalFrame(this);

   fUseTrans  = new TGCheckButton(fTopHorFrame, "UseTrans");
   fTopHorFrame->AddFrame(fUseTrans, new TGLayoutHints(kLHintsLeft, 1,2,0,0));
   fUseTrans->Connect("Toggled(Bool_t)", "TEveTransSubEditor", this, "DoUseTrans()");
   fEditTrans = new TGCheckButton(fTopHorFrame, "EditTrans");
   fTopHorFrame->AddFrame(fEditTrans, new TGLayoutHints(kLHintsLeft, 2,1,0,0));
   fEditTrans->Connect("Toggled(Bool_t)"," TEveTransSubEditor", this, "DoEditTrans()");

   AddFrame(fTopHorFrame, new TGLayoutHints(kLHintsTop, 0,0,2,1));

   // --- Trans edit part

   fEditTransFrame = new TGVerticalFrame(this);

   TGFont *font = gClient->GetFont("-adobe-helvetica-bold-r-*-*-12-*-*-*-*-*-iso8859-1");

   TGHorizontalFrame* hfp = new TGHorizontalFrame(fEditTransFrame);
   TGLabel* labp = new TGLabel(hfp, "Location");
   labp->SetTextFont(font);
   hfp->AddFrame(labp);
   fEditTransFrame->AddFrame(hfp,  new TGLayoutHints(kLHintsTop | kLHintsExpandX, 0,0,2,0));
   fPos = new TEveGTriVecValuator(fEditTransFrame, "Pos", 160, 20);
   fPos->SetNELength(6);
   fPos->Build(kFALSE, "", "", "");
   fPos->SetLimits(-1e5, 1e5, TGNumberFormat::kNESRealThree);
   fPos->GetValuator(0)->SetToolTip("X coordinate");
   fPos->GetValuator(1)->SetToolTip("Y coordinate");
   fPos->GetValuator(2)->SetToolTip("Z coordinate");
   fEditTransFrame->AddFrame(fPos, new TGLayoutHints(kLHintsTop , 0,0,0,0));

   TGHorizontalFrame* hfr = new TGHorizontalFrame(fEditTransFrame);
   TGLabel* labr = new TGLabel(hfr, "Rotation");
   labr->SetTextFont(font);
   hfr->AddFrame(labr);
   fEditTransFrame->AddFrame(hfr, new TGLayoutHints(kLHintsTop | kLHintsExpandX, 0,0,2,0));
   fRot = new TEveGTriVecValuator(fEditTransFrame, "Rot", 160, 20);
   fRot->SetNELength(6);
   fRot->Build(kFALSE, "", "", "");
   fRot->SetLimits(-360, 360, TGNumberFormat::kNESRealOne);
   fRot->GetValuator(0)->SetToolTip("X coordinate");
   fRot->GetValuator(1)->SetToolTip("Y coordinate");
   fRot->GetValuator(2)->SetToolTip("Z coordinate");
   fEditTransFrame->AddFrame(fRot, new TGLayoutHints(kLHintsTop , 0,0, 0, 0));

   TGHorizontalFrame* hfs = new TGHorizontalFrame(fEditTransFrame);
   TGLabel* labs = new TGLabel(hfs, "Scale");
   labs->SetTextFont(font);
   hfs->AddFrame(labs);
   fEditTransFrame->AddFrame(hfs, new TGLayoutHints(kLHintsTop | kLHintsExpandX, 0,0,2,0));
   fScale = new TEveGTriVecValuator(fEditTransFrame, "Scale", 160, 20);
   fScale->SetNELength(6);
   fScale->Build(kFALSE, "", "", "");
   fScale->SetLimits(1e-2, 1e2, TGNumberFormat::kNESRealTwo);
   fScale->GetValuator(0)->SetToolTip("X coordinate");
   fScale->GetValuator(1)->SetToolTip("Y coordinate");
   fScale->GetValuator(2)->SetToolTip("Z coordinate");
   fEditTransFrame->AddFrame(fScale, new TGLayoutHints(kLHintsTop , 0,0,0, 2));

   fPos  ->Connect("ValueSet()", "TEveTransSubEditor", this, "DoTransChanged()");
   fRot  ->Connect("ValueSet()", "TEveTransSubEditor", this, "DoTransChanged()");
   fScale->Connect("ValueSet()", "TEveTransSubEditor", this, "DoTransChanged()");

   {
      TGHorizontalFrame* hframe = new TGHorizontalFrame(fEditTransFrame);
      fAutoUpdate = new TGCheckButton(hframe, "AutoUpdate");
      hframe->AddFrame(fAutoUpdate, new TGLayoutHints(kLHintsLeft, 1,1,1,1));
      fUpdate = new TGTextButton(hframe, "Update");
      hframe->AddFrame(fUpdate, new TGLayoutHints(kLHintsLeft, 0,0,1,1));
      fUpdate->Connect("Clicked()", "TEveTransSubEditor", this, "TransChanged()");

      fEditTransFrame->AddFrame(hframe, new TGLayoutHints(kLHintsTop , 0,0,4,0));
   }

   AddFrame(fEditTransFrame, new TGLayoutHints(kLHintsTop | kLHintsExpandX, 0,0,1,2));
}

/******************************************************************************/

//______________________________________________________________________________
void TEveTransSubEditor::SetModel(TEveTrans* t)
{
   // Set model object.

   fTrans = t;

   fUseTrans ->SetState(fTrans->fUseTrans  ? kButtonDown : kButtonUp);
   fEditTrans->SetState(fTrans->fEditTrans ? kButtonDown : kButtonUp);
   if (fTrans->fEditTrans)
   {
      for (Int_t i=0; i<3; ++i)
      {
         fRot  ->GetValuator(i)->GetEntry()->SetState(fTrans->GetEditRotation());
         fScale->GetValuator(i)->GetEntry()->SetState(fTrans->GetEditScale());
      }
      fEditTransFrame->MapWindow();
   }
   else
   {
      fEditTransFrame->UnmapWindow();
   }

   ((TGMainFrame*)fEditTransFrame->GetMainFrame())->Layout();

   fPos->SetValues(fTrans->ArrT());
   Float_t a[3];
   fTrans->GetRotAngles(a);
   a[0] *= TMath::RadToDeg();
   a[1] *= TMath::RadToDeg();
   a[2] *= TMath::RadToDeg();
   fRot->SetValues(a);
   Double_t x, y, z;
   fTrans->GetScale(x, y, z);
   fScale->SetValues(x, y, z);
}

//______________________________________________________________________________
void TEveTransSubEditor::SetTransFromData()
{
   // Set model object from widget data.

   Double_t v[3];
   fTrans->UnitTrans();
   fRot->GetValues(v);
   fTrans->SetRotByAngles(v[0]*TMath::DegToRad(), v[1]*TMath::DegToRad(), v[2]*TMath::DegToRad());
   fPos->GetValues(v);
   fTrans->SetPos(v);
   fScale->GetValues(v);
   fTrans->Scale(v[0], v[1], v[2]);
}

/******************************************************************************/

//______________________________________________________________________________
void TEveTransSubEditor::UseTrans()
{
   // Emit "UseTrans()" signal.

   Emit("UseTrans()");
}

//______________________________________________________________________________
void TEveTransSubEditor::TransChanged()
{
   // Set transformation values from widget and emit "TransChanged()" signal.

   SetTransFromData();
   Emit("TransChanged()");
}

/******************************************************************************/

//______________________________________________________________________________
void TEveTransSubEditor::DoUseTrans()
{
   // Slot for UseTrans.

   fTrans->SetUseTrans(fUseTrans->IsOn());
   UseTrans();
}

//______________________________________________________________________________
void TEveTransSubEditor::DoEditTrans()
{
   // Slot for EditTrans.

   fTrans->SetEditTrans(fEditTrans->IsOn());
   TransChanged();
}

//______________________________________________________________________________
void TEveTransSubEditor::DoTransChanged()
{
   // Slot for TransChanged.

   if (fAutoUpdate->IsOn())
      TransChanged();
}


//______________________________________________________________________________
// TEveTransEditor
//
// Editor for TEveTrans class.

ClassImp(TEveTransEditor);

//______________________________________________________________________________
TEveTransEditor::TEveTransEditor(const TGWindow *p, Int_t width, Int_t height,
                                 UInt_t options, Pixel_t back) :
   TGedFrame(p, width, height, options | kVerticalFrame, back),
   fM (0),
   fSE(0)
{
   // Constructor.

   MakeTitle("TEveTrans");

   fSE = new TEveTransSubEditor(this);
   AddFrame(fSE, new TGLayoutHints(kLHintsTop, 2, 0, 2, 2));
   fSE->Connect("UseTrans()",     "TEveTransEditor", this, "Update()");
   fSE->Connect("TransChanged()", "TEveTransEditor", this, "Update()");
}

/******************************************************************************/

//______________________________________________________________________________
void TEveTransEditor::SetModel(TObject* obj)
{
   // Set model object.

   fM = dynamic_cast<TEveTrans*>(obj);
   fSE->SetModel(fM);
}
 TEveTransEditor.cxx:1
 TEveTransEditor.cxx:2
 TEveTransEditor.cxx:3
 TEveTransEditor.cxx:4
 TEveTransEditor.cxx:5
 TEveTransEditor.cxx:6
 TEveTransEditor.cxx:7
 TEveTransEditor.cxx:8
 TEveTransEditor.cxx:9
 TEveTransEditor.cxx:10
 TEveTransEditor.cxx:11
 TEveTransEditor.cxx:12
 TEveTransEditor.cxx:13
 TEveTransEditor.cxx:14
 TEveTransEditor.cxx:15
 TEveTransEditor.cxx:16
 TEveTransEditor.cxx:17
 TEveTransEditor.cxx:18
 TEveTransEditor.cxx:19
 TEveTransEditor.cxx:20
 TEveTransEditor.cxx:21
 TEveTransEditor.cxx:22
 TEveTransEditor.cxx:23
 TEveTransEditor.cxx:24
 TEveTransEditor.cxx:25
 TEveTransEditor.cxx:26
 TEveTransEditor.cxx:27
 TEveTransEditor.cxx:28
 TEveTransEditor.cxx:29
 TEveTransEditor.cxx:30
 TEveTransEditor.cxx:31
 TEveTransEditor.cxx:32
 TEveTransEditor.cxx:33
 TEveTransEditor.cxx:34
 TEveTransEditor.cxx:35
 TEveTransEditor.cxx:36
 TEveTransEditor.cxx:37
 TEveTransEditor.cxx:38
 TEveTransEditor.cxx:39
 TEveTransEditor.cxx:40
 TEveTransEditor.cxx:41
 TEveTransEditor.cxx:42
 TEveTransEditor.cxx:43
 TEveTransEditor.cxx:44
 TEveTransEditor.cxx:45
 TEveTransEditor.cxx:46
 TEveTransEditor.cxx:47
 TEveTransEditor.cxx:48
 TEveTransEditor.cxx:49
 TEveTransEditor.cxx:50
 TEveTransEditor.cxx:51
 TEveTransEditor.cxx:52
 TEveTransEditor.cxx:53
 TEveTransEditor.cxx:54
 TEveTransEditor.cxx:55
 TEveTransEditor.cxx:56
 TEveTransEditor.cxx:57
 TEveTransEditor.cxx:58
 TEveTransEditor.cxx:59
 TEveTransEditor.cxx:60
 TEveTransEditor.cxx:61
 TEveTransEditor.cxx:62
 TEveTransEditor.cxx:63
 TEveTransEditor.cxx:64
 TEveTransEditor.cxx:65
 TEveTransEditor.cxx:66
 TEveTransEditor.cxx:67
 TEveTransEditor.cxx:68
 TEveTransEditor.cxx:69
 TEveTransEditor.cxx:70
 TEveTransEditor.cxx:71
 TEveTransEditor.cxx:72
 TEveTransEditor.cxx:73
 TEveTransEditor.cxx:74
 TEveTransEditor.cxx:75
 TEveTransEditor.cxx:76
 TEveTransEditor.cxx:77
 TEveTransEditor.cxx:78
 TEveTransEditor.cxx:79
 TEveTransEditor.cxx:80
 TEveTransEditor.cxx:81
 TEveTransEditor.cxx:82
 TEveTransEditor.cxx:83
 TEveTransEditor.cxx:84
 TEveTransEditor.cxx:85
 TEveTransEditor.cxx:86
 TEveTransEditor.cxx:87
 TEveTransEditor.cxx:88
 TEveTransEditor.cxx:89
 TEveTransEditor.cxx:90
 TEveTransEditor.cxx:91
 TEveTransEditor.cxx:92
 TEveTransEditor.cxx:93
 TEveTransEditor.cxx:94
 TEveTransEditor.cxx:95
 TEveTransEditor.cxx:96
 TEveTransEditor.cxx:97
 TEveTransEditor.cxx:98
 TEveTransEditor.cxx:99
 TEveTransEditor.cxx:100
 TEveTransEditor.cxx:101
 TEveTransEditor.cxx:102
 TEveTransEditor.cxx:103
 TEveTransEditor.cxx:104
 TEveTransEditor.cxx:105
 TEveTransEditor.cxx:106
 TEveTransEditor.cxx:107
 TEveTransEditor.cxx:108
 TEveTransEditor.cxx:109
 TEveTransEditor.cxx:110
 TEveTransEditor.cxx:111
 TEveTransEditor.cxx:112
 TEveTransEditor.cxx:113
 TEveTransEditor.cxx:114
 TEveTransEditor.cxx:115
 TEveTransEditor.cxx:116
 TEveTransEditor.cxx:117
 TEveTransEditor.cxx:118
 TEveTransEditor.cxx:119
 TEveTransEditor.cxx:120
 TEveTransEditor.cxx:121
 TEveTransEditor.cxx:122
 TEveTransEditor.cxx:123
 TEveTransEditor.cxx:124
 TEveTransEditor.cxx:125
 TEveTransEditor.cxx:126
 TEveTransEditor.cxx:127
 TEveTransEditor.cxx:128
 TEveTransEditor.cxx:129
 TEveTransEditor.cxx:130
 TEveTransEditor.cxx:131
 TEveTransEditor.cxx:132
 TEveTransEditor.cxx:133
 TEveTransEditor.cxx:134
 TEveTransEditor.cxx:135
 TEveTransEditor.cxx:136
 TEveTransEditor.cxx:137
 TEveTransEditor.cxx:138
 TEveTransEditor.cxx:139
 TEveTransEditor.cxx:140
 TEveTransEditor.cxx:141
 TEveTransEditor.cxx:142
 TEveTransEditor.cxx:143
 TEveTransEditor.cxx:144
 TEveTransEditor.cxx:145
 TEveTransEditor.cxx:146
 TEveTransEditor.cxx:147
 TEveTransEditor.cxx:148
 TEveTransEditor.cxx:149
 TEveTransEditor.cxx:150
 TEveTransEditor.cxx:151
 TEveTransEditor.cxx:152
 TEveTransEditor.cxx:153
 TEveTransEditor.cxx:154
 TEveTransEditor.cxx:155
 TEveTransEditor.cxx:156
 TEveTransEditor.cxx:157
 TEveTransEditor.cxx:158
 TEveTransEditor.cxx:159
 TEveTransEditor.cxx:160
 TEveTransEditor.cxx:161
 TEveTransEditor.cxx:162
 TEveTransEditor.cxx:163
 TEveTransEditor.cxx:164
 TEveTransEditor.cxx:165
 TEveTransEditor.cxx:166
 TEveTransEditor.cxx:167
 TEveTransEditor.cxx:168
 TEveTransEditor.cxx:169
 TEveTransEditor.cxx:170
 TEveTransEditor.cxx:171
 TEveTransEditor.cxx:172
 TEveTransEditor.cxx:173
 TEveTransEditor.cxx:174
 TEveTransEditor.cxx:175
 TEveTransEditor.cxx:176
 TEveTransEditor.cxx:177
 TEveTransEditor.cxx:178
 TEveTransEditor.cxx:179
 TEveTransEditor.cxx:180
 TEveTransEditor.cxx:181
 TEveTransEditor.cxx:182
 TEveTransEditor.cxx:183
 TEveTransEditor.cxx:184
 TEveTransEditor.cxx:185
 TEveTransEditor.cxx:186
 TEveTransEditor.cxx:187
 TEveTransEditor.cxx:188
 TEveTransEditor.cxx:189
 TEveTransEditor.cxx:190
 TEveTransEditor.cxx:191
 TEveTransEditor.cxx:192
 TEveTransEditor.cxx:193
 TEveTransEditor.cxx:194
 TEveTransEditor.cxx:195
 TEveTransEditor.cxx:196
 TEveTransEditor.cxx:197
 TEveTransEditor.cxx:198
 TEveTransEditor.cxx:199
 TEveTransEditor.cxx:200
 TEveTransEditor.cxx:201
 TEveTransEditor.cxx:202
 TEveTransEditor.cxx:203
 TEveTransEditor.cxx:204
 TEveTransEditor.cxx:205
 TEveTransEditor.cxx:206
 TEveTransEditor.cxx:207
 TEveTransEditor.cxx:208
 TEveTransEditor.cxx:209
 TEveTransEditor.cxx:210
 TEveTransEditor.cxx:211
 TEveTransEditor.cxx:212
 TEveTransEditor.cxx:213
 TEveTransEditor.cxx:214
 TEveTransEditor.cxx:215
 TEveTransEditor.cxx:216
 TEveTransEditor.cxx:217
 TEveTransEditor.cxx:218
 TEveTransEditor.cxx:219
 TEveTransEditor.cxx:220
 TEveTransEditor.cxx:221
 TEveTransEditor.cxx:222
 TEveTransEditor.cxx:223
 TEveTransEditor.cxx:224
 TEveTransEditor.cxx:225
 TEveTransEditor.cxx:226
 TEveTransEditor.cxx:227
 TEveTransEditor.cxx:228
 TEveTransEditor.cxx:229
 TEveTransEditor.cxx:230
 TEveTransEditor.cxx:231
 TEveTransEditor.cxx:232
 TEveTransEditor.cxx:233
 TEveTransEditor.cxx:234
 TEveTransEditor.cxx:235
 TEveTransEditor.cxx:236
 TEveTransEditor.cxx:237
 TEveTransEditor.cxx:238
 TEveTransEditor.cxx:239
 TEveTransEditor.cxx:240
 TEveTransEditor.cxx:241
 TEveTransEditor.cxx:242
 TEveTransEditor.cxx:243
 TEveTransEditor.cxx:244
 TEveTransEditor.cxx:245
 TEveTransEditor.cxx:246
 TEveTransEditor.cxx:247
 TEveTransEditor.cxx:248
 TEveTransEditor.cxx:249
 TEveTransEditor.cxx:250
 TEveTransEditor.cxx:251
 TEveTransEditor.cxx:252
 TEveTransEditor.cxx:253
 TEveTransEditor.cxx:254
 TEveTransEditor.cxx:255
 TEveTransEditor.cxx:256
 TEveTransEditor.cxx:257
 TEveTransEditor.cxx:258
 TEveTransEditor.cxx:259
 TEveTransEditor.cxx:260