//----------------------------------------------------------------------------//
//    module: FOptions.cpp                                                    //
//                                                                            //
//    Options dialog: Set those parameters of the PNC2 algorithm, that are    //
//    not tuned within the parameter tuning.                                  //
//                                                                            //
//    copyright (c) 2003 by Lars Haendel                                      //
//    home: www.newty.de                                                      //
//                                                                            //
//    This program is free software and can be used under the terms of the    //
//    GNU licence. See header file for further information and disclaimer.    //
//                                                                            //
//----------------------------------------------------------------------------//


#include <vcl.h>
#pragma hdrstop

#include <Registry.hpp>    // due to:  TRegistry
#include <htmlhelp>        //          HTML help

#include "FOptions.h"
#include "FMain.h"


//----------------------------------------------------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"


//----------------------------------------------------------------------------------------------------------------------
// constructor
TFOptions *FOptions;
__fastcall TFOptions::TFOptions(TComponent* Owner) : TForm(Owner){}


//----------------------------------------------------------------------------------------------------------------------
// initialize controls from project
void __fastcall TFOptions::FormShow(TObject *Sender)
{
   EGroupSize ->Text = prj->Get_N_G_Max();         // max. group size
   EBins      ->Text = prj->Get_N_Bins();          // # bins

   SetNormalizeByRange(prj->NormalizeByRange());   // normalization method
   SetEqualWidth(prj->EqualWidthBinning());        // binning method
   OverlapFacToGUI();

   LaExplanation1->Caption = "Note: Settings are stored within project file and restored from"; // change text
   LaExplanation2->Visible = true;                                                              // show text

   f_SomethingChanged = false;                     // reset change flag
   ActiveControl = BtOk;                           // set focus to button 'Ok'
}


//----------------------------------------------------------------------------------------------------------------------
// write overlap factor from project to GUI
void TFOptions::OverlapFacToGUI()
{
   char szText[256];
   sprintf(szText, "%.*f", PRC_OVERLAP_FAC, prj->GetOverlapFac());
   EOverlapFac->Text = szText;                                    // overlap factor
}


//----------------------------------------------------------------------------------------------------------------------
// set change flag and display note
void TFOptions::SomeThingChanged()
{
   f_SomethingChanged = true;
   LaExplanation1->Caption = "Note: Changes do not have any effect on learned modules!";    // change text
   LaExplanation2->Visible = false;                                                         // hide text
}


//----------------------------------------------------------------------------------------------------------------------
// close dialog
void __fastcall TFOptions::BtOkClick(TObject *Sender)  { Close(); }


//----------------------------------------------------------------------------------------------------------------------
// set parameter N_G_Max in project (where it will be chekced against constraints) and write it back to GUI
void TFOptions::Set_N_G_Max(const int& N_G_Max)
{
   prj->Set_N_G_Max(N_G_Max);                         // set in project

   EGroupSize->Text = prj->Get_N_G_Max();             // re-write text
   EGroupSize->SelectAll();                           // select text

   SomeThingChanged();                                // set change flag and display note
}


//----------------------------------------------------------------------------------------------------------------------
// set parameter N_Bins in project (where it will be chekced against constraints) and write it back to GUI
void TFOptions::Set_N_Bins(const int& N_Bins)
{
   prj->Set_N_Bins(N_Bins);                           // set in project

   EBins->Text = prj->Get_N_Bins();                   // re-write text
   EBins->SelectAll();                                // select text

   SomeThingChanged();                                // set change flag and display note
}


//----------------------------------------------------------------------------------------------------------------------
// set parameter OverlapFac in project (where it will be chekced against constraints) and write it back to GUI
void TFOptions::SetOverlapFac(const float& fac)
{
   prj->SetOverlapFac(fac);                            // set in project

   OverlapFacToGUI();                                  // re-write text
   EOverlapFac->SelectAll();                           // select text

   SomeThingChanged();                                 // set change flag and display note
}


//----------------------------------------------------------------------------------------------------------------------
// set normalize by range
void TFOptions::SetNormalizeByRange(const bool& f_Set)
{
   if(f_Set) RbRange    ->Checked = true;              // set in GUI
   else      RbDeviation->Checked = true;

   // note: will be written to project in OnClick event handler
}


//----------------------------------------------------------------------------------------------------------------------
// set equal width binning flag
void TFOptions::SetEqualWidth(const bool& f_Set)
{
   if(f_Set) RbEqualWidth    ->Checked = true;         // set in GUI
   else      RbEqualFrequency->Checked = true;

   // note: will be written to project in OnClick event handler
}


//----------------------------------------------------------------------------------------------------------------------
// evaluate changes to group size control
void __fastcall TFOptions::BtGroupSizeUpClick(TObject*)        // arrow up clicked
{
   Set_N_G_Max(prj->Get_N_G_Max()+10);         // set in project and re-write (will be checked against constraints)
   ActiveControl = EGroupSize;                 // give focus back
}

void __fastcall TFOptions::BtGroupSizeDownClick(TObject*)      // arrow down clicked
{
   Set_N_G_Max(prj->Get_N_G_Max()-10);         // set in project and re-write (will be checked against constraints)
   ActiveControl = EGroupSize;                 // give focus back
}

void __fastcall TFOptions::EGroupSizeKeyDown(TObject*, WORD &Key, TShiftState)
{
  if(Key==VK_RETURN)
      EGroupSizeExit(this);                                       // enter pressed -> update
   else
   {                                                              // else: alter repetition count
      if(Key==VK_END)      Set_N_G_Max(MAX_N_G_MAX);              // 'End'        pressed -> set maximal possible
      if(Key==VK_HOME)     Set_N_G_Max(MIN_N_G_MAX);              // 'Pos 1'      pressed -> set minimal possible
      if(Key==VK_UP)       Set_N_G_Max(prj->Get_N_G_Max()+1);     // 'Arrow Up'   pressed -> increment
      if(Key==VK_DOWN)     Set_N_G_Max(prj->Get_N_G_Max()-1);     // 'Arrow Down' pressed -> decrement
      if(Key==VK_PRIOR)    Set_N_G_Max(prj->Get_N_G_Max()+10);    // 'Page Up'    pressed -> increment by 10
      if(Key==VK_NEXT)     Set_N_G_Max(prj->Get_N_G_Max()-10);    // 'Page Down'  pressed -> decrement by 10
   }
}
void __fastcall TFOptions::EGroupSizeExit(TObject *Sender)
{
   Set_N_G_Max(atof(EGroupSize->Text.c_str()));     // set (will be checked)
}


//----------------------------------------------------------------------------------------------------------------------
// evaluate changes to number of bins control
void __fastcall TFOptions::BtBinsUpClick(TObject*)
{
   Set_N_Bins(prj->Get_N_Bins()+1);          // set in project and re-write (will be checked against constraints)
   ActiveControl = EBins;                    // give focus back
}


void __fastcall TFOptions::BtBinsDownClick(TObject*)
{
   Set_N_Bins(prj->Get_N_Bins()-1);          // set in project and re-write (will be checked against constraints)
   ActiveControl = EBins;                    // give focus back
}

void __fastcall TFOptions::EBinsKeyDown(TObject*, WORD &Key, TShiftState)
{
  if(Key==VK_RETURN)
      EBinsExit(this);                                         // enter pressed -> update
   else
   {                                                           // else: alter repetition count
      if(Key==VK_END)      Set_N_Bins(MAX_N_BINS);             // 'End'        pressed -> set maximal possible
      if(Key==VK_HOME)     Set_N_Bins(MIN_N_BINS);             // 'Pos 1'      pressed -> set minimal possible
      if(Key==VK_UP)       Set_N_Bins(prj->Get_N_Bins()+1);    // 'Arrow Up'   pressed -> increment
      if(Key==VK_DOWN)     Set_N_Bins(prj->Get_N_Bins()-1);    // 'Arrow Down' pressed -> decrement
      if(Key==VK_PRIOR)    Set_N_Bins(prj->Get_N_Bins()+5);    // 'Page Up'    pressed -> increment by 5
      if(Key==VK_NEXT)     Set_N_Bins(prj->Get_N_Bins()-5);    // 'Page Down'  pressed -> decrement by 5
   }
}
void __fastcall TFOptions::EBinsExit(TObject*)
{
   Set_N_Bins(atof(EBins->Text.c_str()));     // set (will be checked)
}



//----------------------------------------------------------------------------------------------------------------------
// evaluate changes to overlap factor controls
void __fastcall TFOptions::BtOverlapFacUpClick(TObject*)
{
   SetOverlapFac(prj->GetOverlapFac()+0.1);        // set in project and re-write (will be checked against constraints)
   ActiveControl = EOverlapFac;                    // give focus back
}

void __fastcall TFOptions::BtOverlapFacDownClick(TObject*)
{
   SetOverlapFac(prj->GetOverlapFac()-0.1);        // set in project and re-write (will be checked against constraints)
   ActiveControl = EOverlapFac;                    // give focus back
}


void __fastcall TFOptions::EOverlapFacKeyDown(TObject*, WORD &Key, TShiftState)
{
 if(Key==VK_RETURN)
      EOverlapFacExit(this);                                   // enter pressed -> update
   else
   {                                                           // else: alter repetition count
      if(Key==VK_END)      SetOverlapFac(MAX_OVERLAP_FAC);              // 'End'        pressed -> set maximal possible
      if(Key==VK_HOME)     SetOverlapFac(MIN_OVERLAP_FAC);              // 'Pos 1'      pressed -> set minimal possible
      if(Key==VK_UP)       SetOverlapFac(prj->GetOverlapFac()+0.1);     // 'Arrow Up'   pressed -> increment
      if(Key==VK_DOWN)     SetOverlapFac(prj->GetOverlapFac()-0.1);     // 'Arrow Down' pressed -> decrement
      if(Key==VK_PRIOR)    SetOverlapFac(prj->GetOverlapFac()+0.5);     // 'Page Up'    pressed -> increment by 0.5
      if(Key==VK_NEXT)     SetOverlapFac(prj->GetOverlapFac()-0.5);     // 'Page Down'  pressed -> decrement by 0.5
   }
}
void __fastcall TFOptions::EOverlapFacExit(TObject *Sender)
{
   SetOverlapFac(atof(EOverlapFac->Text.c_str()));
}


//----------------------------------------------------------------------------------------------------------------------
// restore settings from hard coded defaults
void __fastcall TFOptions::BtDefaultsClick(TObject *Sender)
{
   // set in project and re-write to GUI
   Set_N_G_Max(DEF_N_G_MAX);                                            // N_G_Max
   SetOverlapFac(DEF_OVERLAP_FAC);                                      // overlap fac
   Set_N_Bins(DEF_N_BINS);                                              // N_Bins
   SetNormalizeByRange(DEF_NORMALIZE_BY_RANGE);                         // normalize by range flag
   SetEqualWidth(DEF_EQUAL_WIDTH_BINNING);                              // equal width binning flag
}


//----------------------------------------------------------------------------------------------------------------------
// read default values from registry and set them
void __fastcall TFOptions::BtLoadClick(TObject*)
{
   // a) open key creating it if it does not exist
   TRegistry* registry = new TRegistry;
   registry->OpenKey(SZ_REGISTRY_KEY, true);

   // b) read from registry and set in project and re-write to GUI
   Set_N_G_Max  (registry->ReadInteger(SZ_N_G_MAX));                    // N_G_Max
   Set_N_Bins   (registry->ReadInteger(SZ_N_BINS));                     // N_Bins
   SetOverlapFac(registry->ReadFloat(SZ_OVERLAP_FAC));                  // overlap factor
   SetNormalizeByRange(registry->ReadBool(SZ_NORMALIZE_BY_RANGE));      // normalize by range flag
   SetEqualWidth(registry->ReadBool(SZ_EQUAL_WIDTH_BINNING));           // equal width binning flag

   // c) close key
   registry->CloseKey();
   delete registry;
}



//----------------------------------------------------------------------------------------------------------------------
// write current settings as default values to registry
void __fastcall TFOptions::BtSaveClick(TObject *Sender)
{
   // a) open key creating it if it does not exist
   TRegistry* registry = new TRegistry;
   registry->OpenKey(SZ_REGISTRY_KEY, true);

   // b) write current project settings to registry
   registry->WriteInteger(SZ_N_G_MAX, prj->Get_N_G_Max());                 // write N_G_Max
   registry->WriteInteger(SZ_N_BINS, prj->Get_N_Bins());                   // write N_Bins
   registry->WriteFloat(SZ_OVERLAP_FAC, prj->GetOverlapFac());             // write overlap factor
   registry->WriteBool(SZ_EQUAL_WIDTH_BINNING, prj->EqualWidthBinning());  // write flag equal width binning
   registry->WriteBool(SZ_NORMALIZE_BY_RANGE, prj->NormalizeByRange());    // write flag normalize by range

   // c) close key
   registry->CloseKey();
   delete registry;
}


//----------------------------------------------------------------------------------------------------------------------
// binning normalization method
void __fastcall TFOptions::RbRangeClick         (TObject*) { prj->NormalizeByRange()  = true;  SomeThingChanged(); }
void __fastcall TFOptions::RbDeviationClick     (TObject*) { prj->NormalizeByRange()  = false; SomeThingChanged(); }
void __fastcall TFOptions::RbEqualWidthClick    (TObject*) { prj->EqualWidthBinning() = true;  SomeThingChanged(); }
void __fastcall TFOptions::RbEqualFrequencyClick(TObject*) { prj->EqualWidthBinning() = false; SomeThingChanged(); }


//----------------------------------------------------------------------------------------------------------------------
// close all help windows
void __fastcall TFOptions::FormClose(TObject*, TCloseAction&){ /* HtmlHelp(0, 0, HH_CLOSE_ALL, 0); */}