// @(#)root/eve:$Id$
// Author: Dmytro Kovalskyi, 28.2.2008

/*************************************************************************
 * 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 "TEveParamList.h"

// Cleanup these includes:
#include "TGLabel.h"
#include "TGButton.h"
#include "TGNumberEntry.h"
#include "TGColorSelect.h"
#include "TGDoubleSlider.h"

#include "TEveGValuators.h"
#include "TGNumberEntry.h"
#include "TGedEditor.h"

//==============================================================================
//==============================================================================
// TEveParamList
//==============================================================================

//______________________________________________________________________________
//
// Collection of named parameters.

ClassImp(TEveParamList);

//______________________________________________________________________________
TEveParamList::TEveParamList(const char* n, const char* t, Bool_t doColor) :
   TNamed(n, t),
   fColor(0)
{
   // Constructor.

   if (doColor) SetMainColorPtr(&fColor);
}

//______________________________________________________________________________
TEveParamList::FloatConfig_t TEveParamList::GetFloatParameter(const TString& name)
{
   // Get config-struct for float parameter 'name'.

   static const TEveException eh("TEveParamList::GetFloatParameter ");

   for (FloatConfigVec_ci itr = fFloatParameters.begin(); itr != fFloatParameters.end(); ++itr)
      if (itr->fName.CompareTo(name)==0 ) return *itr;
   Error(eh, "parameter not found.");
   return FloatConfig_t();
}

//______________________________________________________________________________
TEveParamList::IntConfig_t TEveParamList::GetIntParameter(const TString& name)
{
   // Get config-struct for int parameter 'name'.

   static const TEveException eh("TEveParamList::GetIntParameter ");

   for (IntConfigVec_ci itr = fIntParameters.begin(); itr != fIntParameters.end(); ++itr)
      if (itr->fName.CompareTo(name) == 0) return *itr;
   Error(eh, "parameter not found.");
   return IntConfig_t();
}

//______________________________________________________________________________
Bool_t TEveParamList::GetBoolParameter(const TString& name)
{
   // Get value for bool parameter 'name'.

   static const TEveException eh("TEveParamList::GetBoolParameter ");

   for (BoolConfigVec_ci itr = fBoolParameters.begin(); itr != fBoolParameters.end(); ++itr)
      if ( itr->fName.CompareTo(name)==0 ) return itr->fValue;
   Error(eh, "parameter not found.");
   return kFALSE;
}

//______________________________________________________________________________
void TEveParamList::ParamChanged(const char* name)
{
   // Emit ParamChanged() signal.

   Emit("ParamChanged(char*)", name);
}

//==============================================================================
//==============================================================================
// TEveParamListEditor
//==============================================================================

//______________________________________________________________________________
// GUI editor for TEveParamList.
//
// Slot methods from this object do not call Update, instead they call
// their model's ParamChanged(const char* name) function which emits a
// corresponding signal.
//
// This helps in handling of parameter changes as they are probably
// related to displayed objects in a more complicated way. Further,
// the TGCheckButton::HandleButton() emits more signal after the
// Clicked() signal and if model is reset in the editor, its contents
// is removed. This results in a crash.

ClassImp(TEveParamListEditor);

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

   MakeTitle("TEveParamList");
}

//______________________________________________________________________________
void TEveParamListEditor::InitModel(TObject* obj)
{
   // Initialize widgets when a new object is selected.

   fM = dynamic_cast<TEveParamList*>(obj);

   if (fParamFrame) {
      fParamFrame->UnmapWindow();
      RemoveFrame(fParamFrame);
      fParamFrame->DestroyWindow();
      delete fParamFrame;
   }
   fParamFrame = new TGVerticalFrame(this);
   AddFrame(fParamFrame);

   // integers
   fIntParameters.clear();
   for (UInt_t i = 0; i < fM->fIntParameters.size(); ++i)
   {
      TGCompositeFrame* frame = new TGHorizontalFrame(fParamFrame);

      // number entry widget
      TGNumberEntry* widget = new TGNumberEntry
         (frame, fM->fIntParameters[i].fValue,
          5,                                 // number of digits
          i,                                 // widget ID
          TGNumberFormat::kNESInteger,       // style
          TGNumberFormat::kNEAAnyNumber,     // input value filter
          TGNumberFormat::kNELLimitMinMax,   // specify limits
          fM->fIntParameters[i].fMin,        // min value
          fM->fIntParameters[i].fMax);       // max value
      frame->AddFrame(widget, new TGLayoutHints(kLHintsLeft|kLHintsCenterY, 2,8,2,2));
      widget->Connect("ValueSet(Long_t)", "TEveParamListEditor", this, "DoIntUpdate()");
      fIntParameters.push_back(widget);

      // label
      frame->AddFrame(new TGLabel(frame,fM->fIntParameters[i].fName.Data()),
                      new TGLayoutHints(kLHintsLeft|kLHintsCenterY));

      fParamFrame->AddFrame(frame, new TGLayoutHints(kLHintsTop));
   }


   // floats
   fFloatParameters.clear();
   for (UInt_t i = 0; i < fM->fFloatParameters.size(); ++i)
   {
      TGCompositeFrame* frame = new TGHorizontalFrame(fParamFrame);

      // number entry widget
      TGNumberEntry* widget = new TGNumberEntry
         (frame, fM->fFloatParameters[i].fValue,
          5,                                // number of digits
          i,                                // widget ID
          TGNumberFormat::kNESReal,         // style
          TGNumberFormat::kNEAAnyNumber,    // input value filter
          TGNumberFormat::kNELLimitMinMax,  // specify limits
          fM->fFloatParameters[i].fMin,     // min value
          fM->fFloatParameters[i].fMax);    // max value
      frame->AddFrame(widget, new TGLayoutHints(kLHintsLeft|kLHintsCenterY, 2,8,2,2));
      widget->Connect("ValueSet(Long_t)", "TEveParamListEditor", this, "DoFloatUpdate()");
      fFloatParameters.push_back( widget );

      // label
      frame->AddFrame(new TGLabel(frame,fM->fFloatParameters[i].fName.Data()),
                      new TGLayoutHints(kLHintsLeft|kLHintsCenterY) );

      fParamFrame->AddFrame(frame, new TGLayoutHints(kLHintsTop));
   }

   // boolean
   fBoolParameters.clear();
   for (UInt_t i = 0; i < fM->fBoolParameters.size(); ++i)
   {
      TGCheckButton* widget = new TGCheckButton(fParamFrame,
                                                fM->fBoolParameters[i].fName.Data(),
                                                i);
      widget->Connect("Clicked()", "TEveParamListEditor", this, "DoBoolUpdate()");
      fBoolParameters.push_back(widget);

      fParamFrame->AddFrame(widget, new TGLayoutHints(kLHintsTop,2,0,1,1));
   }
   MapSubwindows();
}

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

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

   InitModel(obj);

   for (UInt_t i = 0; i < fIntParameters.size(); ++i)
      fIntParameters[i]->GetNumberEntry()->SetIntNumber(fM->fIntParameters[i].fValue);

   for (UInt_t i = 0; i < fFloatParameters.size(); ++i)
      fFloatParameters[i]->GetNumberEntry()->SetNumber(fM->fFloatParameters[i].fValue);

   for (UInt_t i = 0; i < fBoolParameters.size(); ++i)
      fBoolParameters[i]->SetState( fM->fBoolParameters[i].fValue ? kButtonDown : kButtonUp);
}

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

//______________________________________________________________________________
void TEveParamListEditor::DoIntUpdate()
{
   // Slot for integer parameter update.

   TGNumberEntry *widget = (TGNumberEntry*) gTQSender;
   Int_t id = widget->WidgetId();
   if (id < 0 || id >= (int) fM->fIntParameters.size()) return;
   fM->fIntParameters[id].fValue = widget->GetNumberEntry()->GetIntNumber();

   fM->ParamChanged(fM->fIntParameters[id].fName);
   gTQSender = (void*) widget;
}

//______________________________________________________________________________
void TEveParamListEditor::DoFloatUpdate()
{
   // Slot for float parameter update.

   TGNumberEntry *widget = (TGNumberEntry*) gTQSender;
   Int_t id = widget->WidgetId();
   if (id < 0 || id >= (int) fM->fFloatParameters.size()) return;
   fM->fFloatParameters[id].fValue = widget->GetNumberEntry()->GetNumber();

   fM->ParamChanged(fM->fFloatParameters[id].fName);
   gTQSender = (void*) widget;
}

//______________________________________________________________________________
void TEveParamListEditor::DoBoolUpdate()
{
   // Slot for bool parameter update.

   TGCheckButton *widget = (TGCheckButton*) gTQSender;
   Int_t id = widget->WidgetId();
   if (id < 0 || id >= (int) fM->fBoolParameters.size()) return;
   fM->fBoolParameters[id].fValue = widget->IsOn();

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