<commit_before>// $Header$

#include "TPCLoaderEditor.h"
#include <Alieve/TPCLoader.h>
#include <Reve/RGTopFrame.h>
#include <Reve/RGValuators.h>

#include <TSystem.h>
#include <TVirtualPad.h>
#include <TColor.h>

#include <TGLabel.h>
#include <TGButton.h>
#include <TGTextEntry.h>
#include <TGNumberEntry.h>
#include <TGFileDialog.h>
#include <TGToolTip.h>

using namespace Reve;
using namespace Alieve;

//______________________________________________________________________
// TPCLoaderEditor
//

ClassImp(TPCLoaderEditor)

TPCLoaderEditor::TPCLoaderEditor(const TGWindow *p, Int_t id, Int_t width, Int_t height,
				 UInt_t options, Pixel_t back) :
  TGedFrame(p, id, width, height, options | kVerticalFrame, back)
{
  fM = 0;
  MakeTitle("TPCLoader");

  Int_t labelW = 42;
  {
    TGHorizontalFrame* f = new TGHorizontalFrame(this);
    TGHorizontalFrame* g = new TGHorizontalFrame(f, labelW, 0, kFixedWidth);
    TGLabel* l = new TGLabel(g, "File: ");
    g->AddFrame(l, new TGLayoutHints(kLHintsLeft, 0,0,4,0));
    f->AddFrame(g);
    fFile = new TGTextEntry(f);
    fFile->SetWidth(140);
    f->AddFrame(fFile);
    fFile->Connect("DoubleClicked()",
		   "Alieve::TPCLoaderEditor", this, "FileSelect()");
    fFile->Connect("TextChanged(const char *)",
		   "Alieve::TPCLoaderEditor", this, "FileChanged()");
    fOpenFile = new TGTextButton(f, "Open");
    f->AddFrame(fOpenFile);
    fOpenFile->Connect("Clicked()",
		       "Alieve::TPCLoaderEditor", this, "DoOpen()");
    AddFrame(f);
  }

  fEvent = new RGValuator(this, "Event:", 110, 0);
  fEvent->SetShowSlider(kFALSE);
  fEvent->SetLabelWidth(labelW);
  fEvent->SetNELength(6);
  fEvent->Build();
  fEvent->SetLimits(0, 1000);
  fEvent->SetToolTip("Current event number");
  fEvent->Connect("ValueSet(Double_t)",
		  "Alieve::TPCLoaderEditor", this, "DoEvent()");
  // Reuse Event for DoubleSR button
  fDoubleSR = new TGCheckButton(fEvent, "Double SR");
  fDoubleSR->SetToolTipText("Double sampling rate");
  fEvent->AddFrame(fDoubleSR, new TGLayoutHints(kLHintsLeft, 12, 0, 1, 0));
  fDoubleSR->Connect("Toggled(Bool_t)",
                     "Alieve::TPCLoaderEditor", this, "DoDoubleSR()");
  AddFrame(fEvent);

  fUpdateSectors = new TGTextButton(this, "Update Sectors");
  AddFrame(fUpdateSectors, new TGLayoutHints(kLHintsExpandX, 8,8,8,0));
  fUpdateSectors->Connect("Clicked()",
		     "Alieve::TPCLoaderEditor", this, "DoUpdateSectors()");
  {
    TGHorizontalFrame* f = new TGHorizontalFrame(this);
    fCreateSectors3D = new TGTextButton(f, "Create 3D");
    f->AddFrame(fCreateSectors3D, new TGLayoutHints(kLHintsExpandX, 0,4,0,0));
    fCreateSectors3D->Connect("Clicked()",
		     "Alieve::TPCLoaderEditor", this, "DoCreateSectors3D()");
    fDeleteSectors3D = new TGTextButton(f, "Delete 3D");
    f->AddFrame(fDeleteSectors3D, new TGLayoutHints(kLHintsExpandX, 4,0,0,0));
    fDeleteSectors3D->Connect("Clicked()",
		     "Alieve::TPCLoaderEditor", this, "DoDeleteSectors3D()");
    AddFrame(f, new TGLayoutHints(kLHintsExpandX, 8,8,8,0));
  }

  // Register the editor.
  TClass *cl = TPCLoader::Class();
  TGedElement *ge = new TGedElement;
  ge->fGedFrame = this;
  ge->fCanvas = 0;
  cl->GetEditorList()->Add(ge);
}

TPCLoaderEditor::~TPCLoaderEditor()
{}

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

void TPCLoaderEditor::SetModel(TVirtualPad* pad, TObject* obj, Int_t /*event*/)
{
  fModel = 0;
  fPad   = 0;

  if (!obj || !obj->InheritsFrom(TPCLoader::Class()) || obj->InheritsFrom(TVirtualPad::Class())) {
    SetActive(kFALSE);
    return;
  }

  fModel = obj;
  fPad   = pad;

  fM = dynamic_cast<TPCLoader*>(fModel);

  // !!!! order changed, need TGTextEntry::SetText NO BLOODY EMIT.
  fFile->SetToolTipText(gSystem->DirName(fM->fFile));
  fFile->SetText(gSystem->BaseName(fM->fFile));
  fEvent->SetValue(fM->fEvent);
  fEvent->SetEnabled(fM->fEvent >= 0);
  fDoubleSR->SetState(fM->fDoubleSR  ? kButtonDown : kButtonUp);

  SetActive();
}

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

namespace {
const char *tpcfiletypes[] = {
   "Root files",  "*.root",
   "All files",   "*.*",
    0,               0
};
}

void TPCLoaderEditor::FileSelect()
{
  TString dname = gSystem->DirName (fM->fFile);
  TString fname = gSystem->BaseName(fM->fFile);
  TGFileInfo fi;
  fi.fIniDir    = const_cast<char*>(dname.Data());
  fi.fFilename  = const_cast<char*>(fname.Data());
  fi.fFileTypes = tpcfiletypes;

  new TGFileDialog(fClient->GetRoot(), gReve, kFDOpen, &fi);
  if (!fi.fFilename)
    return;

  fFile->SetToolTipText(gSystem->DirName (fi.fFilename));
  fFile->SetText       (gSystem->BaseName(fi.fFilename));
}

void TPCLoaderEditor::FileChanged()
{
  fM->fFile = Form("%s/%s", fFile->GetToolTip()->GetText()->Data(),
		   fFile->GetText());
}

void TPCLoaderEditor::DoOpen()
{
  fM->OpenFile();
  SetModel(fPad, fModel, 0);<commit_msg>Use StrDup to feed TGFileInfo for brain-dead interface of TGFileDialog.<commit_after>// $Header$

#include "TPCLoaderEditor.h"
#include <Alieve/TPCLoader.h>
#include <Reve/RGTopFrame.h>
#include <Reve/RGValuators.h>

#include <TSystem.h>
#include <TVirtualPad.h>
#include <TColor.h>

#include <TGLabel.h>
#include <TGButton.h>
#include <TGTextEntry.h>
#include <TGNumberEntry.h>
#include <TGFileDialog.h>
#include <TGToolTip.h>

using namespace Reve;
using namespace Alieve;

//______________________________________________________________________
// TPCLoaderEditor
//

ClassImp(TPCLoaderEditor)

TPCLoaderEditor::TPCLoaderEditor(const TGWindow *p, Int_t id, Int_t width, Int_t height,
				 UInt_t options, Pixel_t back) :
  TGedFrame(p, id, width, height, options | kVerticalFrame, back)
{
  fM = 0;
  MakeTitle("TPCLoader");

  Int_t labelW = 42;
  {
    TGHorizontalFrame* f = new TGHorizontalFrame(this);
    TGHorizontalFrame* g = new TGHorizontalFrame(f, labelW, 0, kFixedWidth);
    TGLabel* l = new TGLabel(g, "File: ");
    g->AddFrame(l, new TGLayoutHints(kLHintsLeft, 0,0,4,0));
    f->AddFrame(g);
    fFile = new TGTextEntry(f);
    fFile->SetWidth(140);
    f->AddFrame(fFile);
    fFile->Connect("DoubleClicked()",
		   "Alieve::TPCLoaderEditor", this, "FileSelect()");
    fFile->Connect("TextChanged(const char *)",
		   "Alieve::TPCLoaderEditor", this, "FileChanged()");
    fOpenFile = new TGTextButton(f, "Open");
    f->AddFrame(fOpenFile);
    fOpenFile->Connect("Clicked()",
		       "Alieve::TPCLoaderEditor", this, "DoOpen()");
    AddFrame(f);
  }

  fEvent = new RGValuator(this, "Event:", 110, 0);
  fEvent->SetShowSlider(kFALSE);
  fEvent->SetLabelWidth(labelW);
  fEvent->SetNELength(6);
  fEvent->Build();
  fEvent->SetLimits(0, 1000);
  fEvent->SetToolTip("Current event number");
  fEvent->Connect("ValueSet(Double_t)",
		  "Alieve::TPCLoaderEditor", this, "DoEvent()");
  // Reuse Event for DoubleSR button
  fDoubleSR = new TGCheckButton(fEvent, "Double SR");
  fDoubleSR->SetToolTipText("Double sampling rate");
  fEvent->AddFrame(fDoubleSR, new TGLayoutHints(kLHintsLeft, 12, 0, 1, 0));
  fDoubleSR->Connect("Toggled(Bool_t)",
                     "Alieve::TPCLoaderEditor", this, "DoDoubleSR()");
  AddFrame(fEvent);

  fUpdateSectors = new TGTextButton(this, "Update Sectors");
  AddFrame(fUpdateSectors, new TGLayoutHints(kLHintsExpandX, 8,8,8,0));
  fUpdateSectors->Connect("Clicked()",
		     "Alieve::TPCLoaderEditor", this, "DoUpdateSectors()");
  {
    TGHorizontalFrame* f = new TGHorizontalFrame(this);
    fCreateSectors3D = new TGTextButton(f, "Create 3D");
    f->AddFrame(fCreateSectors3D, new TGLayoutHints(kLHintsExpandX, 0,4,0,0));
    fCreateSectors3D->Connect("Clicked()",
		     "Alieve::TPCLoaderEditor", this, "DoCreateSectors3D()");
    fDeleteSectors3D = new TGTextButton(f, "Delete 3D");
    f->AddFrame(fDeleteSectors3D, new TGLayoutHints(kLHintsExpandX, 4,0,0,0));
    fDeleteSectors3D->Connect("Clicked()",
		     "Alieve::TPCLoaderEditor", this, "DoDeleteSectors3D()");
    AddFrame(f, new TGLayoutHints(kLHintsExpandX, 8,8,8,0));
  }

  // Register the editor.
  TClass *cl = TPCLoader::Class();
  TGedElement *ge = new TGedElement;
  ge->fGedFrame = this;
  ge->fCanvas = 0;
  cl->GetEditorList()->Add(ge);
}

TPCLoaderEditor::~TPCLoaderEditor()
{}

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

void TPCLoaderEditor::SetModel(TVirtualPad* pad, TObject* obj, Int_t /*event*/)
{
  fModel = 0;
  fPad   = 0;

  if (!obj || !obj->InheritsFrom(TPCLoader::Class()) || obj->InheritsFrom(TVirtualPad::Class())) {
    SetActive(kFALSE);
    return;
  }

  fModel = obj;
  fPad   = pad;

  fM = dynamic_cast<TPCLoader*>(fModel);

  // !!!! order changed, need TGTextEntry::SetText NO BLOODY EMIT.
  fFile->SetToolTipText(gSystem->DirName(fM->fFile));
  fFile->SetText(gSystem->BaseName(fM->fFile));
  fEvent->SetValue(fM->fEvent);
  fEvent->SetEnabled(fM->fEvent >= 0);
  fDoubleSR->SetState(fM->fDoubleSR  ? kButtonDown : kButtonUp);

  SetActive();
}

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

namespace {
const char *tpcfiletypes[] = {
   "Root files",  "*.root",
   "All files",   "*.*",
    0,               0
};
}

void TPCLoaderEditor::FileSelect()
{
  TGFileInfo fi;
  fi.fIniDir    = StrDup(gSystem->DirName (fM->fFile));
  fi.fFilename  = StrDup(gSystem->BaseName(fM->fFile));
  fi.fFileTypes = tpcfiletypes;

  new TGFileDialog(fClient->GetRoot(), gReve, kFDOpen, &fi);
  if (!fi.fFilename)
    return;

  fFile->SetToolTipText(gSystem->DirName (fi.fFilename));
  fFile->SetText       (gSystem->BaseName(fi.fFilename));
}

void TPCLoaderEditor::FileChanged()
{
  fM->fFile = Form("%s/%s", fFile->GetToolTip()->GetText()->Data(),
		   fFile->GetText());
}

void TPCLoaderEditor::DoOpen()
{
  fM->OpenFile();
  SetModel(fPad, fModel, 0);
{'fraction_non_alphanumeric': 0.20479258960934354, 'fraction_numerical': 0.011880789367700362, 'mean_word_length': 4.926610978520286, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 145, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7033247', 'n_tokens_mistral': 3844, 'n_tokens_neox': 3639, 'n_words': 612}