// This sourcefile is created by Peter Jodda / peter@jodda.de
// It is under the Peter Jodda license:
// Everyboy may use it on his own risk. This header must stay in the file!
// If you expand this file, please contact me, that I can integrate the
// enhancemants in the original file.
//
// controlbox.c
// einstellungen fure die camera

#include "controlbox.h"
#include <qdialog.h>
#include <qpushbutton.h>
#include <qgrid.h>
#include <qlayout.h>
#include <qlabel.h>
#include <qlineedit.h>
#include <qcheckbox.h>
#include <qcombobox.h>
#include <qfiledialog.h>
#include <qdir.h>
#include <qgroupbox.h>
#include <qhgroupbox.h>
#include <qbuttongroup.h>
#include <qhbuttongroup.h>
#include <qvbuttongroup.h>
#include <stdio.h>

#include "texte.h"

#undef HIGH2_MODE

//-----------------------------------------------------------------------------

static void formatZeit(time_t *zeit, char *string)
{
   struct tm *lokaleZeit;
   
   lokaleZeit = localtime (zeit);
   
   strftime(string,60,"%c",lokaleZeit);
}

//-----------------------------------------------------------------------------

static QFrame *erzeugeEinrueckBox(QWidget *parent,
                                  int titelID,
                                  QBoxLayout *topLayout)
{
   QHGroupBox   *aktuelleGruppe;
   QFrame       *neuesFrame;
   
   
   aktuelleGruppe = new QHGroupBox(TextGet(titelID),parent);
   
   neuesFrame = new QFrame(aktuelleGruppe);
   topLayout->addWidget(aktuelleGruppe);
   
   return neuesFrame;
}                                  

//-----------------------------------------------------------------------------

ControlBox::ControlBox(QWidget *parent,
                         CameraApplicationClass *theCameraApplication) : 
		QDialog(parent,"Control",TRUE)
{
   QPushButton *ok, *cancel;
   QBoxLayout  *topLayout;
   QBoxLayout  *dialogButtons;
   
   QFrame      *aktuellesFrame;
   int         returnCode;
   int         wert;
   


   cameraApplication = theCameraApplication;

   setCaption(TextGet(TEXT_EINSTELL_TITEL));
   cameraApplication->setWait(true);

   topLayout = new QVBoxLayout( this );

   {
      // camera information
      QGridLayout *grid;
      QBoxLayout  *infoLayout;
      
      aktuellesFrame = erzeugeEinrueckBox(this,TEXT_EINSTELL_INFORMATION,topLayout);
      
      infoLayout   = new QVBoxLayout( aktuellesFrame );
      VersionLabel = new QLabel("Hier die version",aktuellesFrame);
      infoLayout->addWidget(VersionLabel);
  
      grid = new QGridLayout( infoLayout, 2, 2,3);
      
      MemoryLabel = new QLabel("1 MByte",aktuellesFrame);
      BatteryLabel = new QLabel("Full",aktuellesFrame);
      
      grid->addWidget(new QLabel(TextGet(TEXT_EINSTELL_MEMORY),aktuellesFrame),0,0);
      grid->addWidget(MemoryLabel,0,1);
      grid->addWidget(new QLabel(TextGet(TEXT_EINSTELL_BATTERY),aktuellesFrame),1,0);
      grid->addWidget(BatteryLabel,1,1);
   }   

   {
      // clock
      QGridLayout *grid;
      QBoxLayout  *infoLayout;
      
      aktuellesFrame = erzeugeEinrueckBox(this,TEXT_EINSTELL_CLOCK,topLayout);
      
      infoLayout   = new QVBoxLayout( aktuellesFrame );
  
      grid = new QGridLayout( infoLayout, 2, 2,3);
      
      CameraTimeLabel = new QLabel("heute",aktuellesFrame);
      ComputerTimeLabel = new QLabel("morgen",aktuellesFrame);
      
      grid->addWidget(new QLabel(TextGet(TEXT_EINSTELL_CAMTIME),aktuellesFrame),0,0);
      grid->addWidget(CameraTimeLabel,0,1);
      grid->addWidget(new QLabel(TextGet(TEXT_EINSTELL_COMTIME),aktuellesFrame),1,0);
      grid->addWidget(ComputerTimeLabel,1,1);

      ComputerToCameraButton = new QPushButton(TextGet(TEXT_EINSTELL_TIMESET),aktuellesFrame);
      infoLayout->addWidget(ComputerToCameraButton);
   }   
  
   {
      // shutdown zeiten
      QGridLayout *grid;
      
      aktuellesFrame = erzeugeEinrueckBox(this,TEXT_EINSTELL_SHUTDOWN,topLayout);
      
  
      grid = new QGridLayout( aktuellesFrame, 3, 2,3);
      
      CameraSpinbox = new QSpinBox(0,300,1,aktuellesFrame);
      CameraComputerSpinbox = new QSpinBox(0,300,1,aktuellesFrame);
      LCDSpinbox = new QSpinBox(0,300,1,aktuellesFrame);

      
      grid->addWidget(new QLabel(TextGet(TEXT_EINSTELL_SHUTCAM),aktuellesFrame),0,0);
      grid->addWidget(CameraSpinbox,0,1);
      grid->addWidget(new QLabel(TextGet(TEXT_EINSTELL_SHUTCOM),aktuellesFrame),1,0);
      grid->addWidget(CameraComputerSpinbox,1,1);
      grid->addWidget(new QLabel(TextGet(TEXT_EINSTELL_SHUTLCD),aktuellesFrame),2,0);
      grid->addWidget(LCDSpinbox,2,1);

   }   
  
   {
      // quality radio knopf gruppe
      QHButtonGroup *qualitaet;
      
      QualityGroup = qualitaet
                   = new QHButtonGroup(TextGet(TEXT_EINSTELL_QUALITY),this);
      
      QualityStandardButton = new QRadioButton(TextGet(TEXT_EINSTELL_QUAL1),qualitaet,"Standard");
      QualityHighButton     = new QRadioButton(TextGet(TEXT_EINSTELL_QUAL2),qualitaet,"High");
#ifdef HIGH2_MODE      
      QualityHigh2Button    = new QRadioButton(TextGet(TEXT_EINSTELL_QUAL3),qualitaet,"High2");
#endif      
      // qualitaet ist 1 based
      qualitaet->insert(QualityStandardButton,1);
      qualitaet->insert(QualityHighButton,2);
#ifdef HIGH2_MODE      
      qualitaet->insert(QualityHigh2Button,3);
#endif
      
      topLayout->addWidget(qualitaet);        
   }
  
   {
      // LCD helligkeit
      QBoxLayout     *infoLayout;
      
      aktuellesFrame = erzeugeEinrueckBox(this,TEXT_EINSTELL_LCD,topLayout);
      
      infoLayout   = new QHBoxLayout( aktuellesFrame );
      infoLayout->addWidget(new QLabel(TextGet(TEXT_EINSTELL_DUNKEL),aktuellesFrame));
      
      BrightnessSlider = new QSlider(1,7,
                                     1,
                                     4,
                                     QSlider::Horizontal,
                                     aktuellesFrame);
      infoLayout->addWidget(BrightnessSlider);
      
      infoLayout->addWidget(new QLabel(TextGet(TEXT_EINSTELL_HELL),aktuellesFrame));
  
      
     }   
   {
      // blitz radio knopf gruppe
      QVButtonGroup *blitz;
      
      FlashGroup = blitz
                 = new QVButtonGroup(TextGet(TEXT_EINSTELL_BLITZ),this);
      
      FlashAutomaticButton = new QRadioButton(TextGet(TEXT_EINSTELL_BLITZAUTO),blitz);
      FlashForceButton     = new QRadioButton(TextGet(TEXT_EINSTELL_BLITZIMMER),blitz);
      FlashOffButton       = new QRadioButton(TextGet(TEXT_EINSTELL_BLITZAUS),blitz);
      FlashRedEyeButton    = new QRadioButton(TextGet(TEXT_EINSTELL_BLITZRED),blitz);
      
      // blitz ist 0 based
      blitz->insert(FlashAutomaticButton,0);
      blitz->insert(FlashForceButton,1);
      blitz->insert(FlashOffButton,2);
      blitz->insert(FlashRedEyeButton,3);
      
      topLayout->addWidget(blitz);        
   }


   {
      // linse radio knopf gruppe
      QHButtonGroup *linse;
      
      LensGroup = linse 
                = new QHButtonGroup(TextGet(TEXT_EINSTELL_LINSE),this);
      
      LensNormalButton = new QRadioButton(TextGet(TEXT_EINSTELL_LINSENORM),linse,"Normal");
      LensMacroButton  = new QRadioButton(TextGet(TEXT_EINSTELL_LINSEMACRO) ,linse,"Macro");
      
      // linse ist 1 based
      linse->insert(LensNormalButton,1);
      linse->insert(LensMacroButton,2);
      
      topLayout->addWidget(linse);        
   }
   
   // jetzt die werte von der kamera holen und setzen
   returnCode = cameraApplication->cameraReal->GetOptions(CameraOptionAll,
                         optionen);
   if(returnCode ==RC_OK)
   {
      time_t computerZeit,unterschied;   

      char string[CAMERA_STRING_MAX];
      sprintf(string,"%s %s",optionen.ID,optionen.Type);
      VersionLabel->setText(string);
      
      sprintf(string,"%d",optionen.Memory);
      MemoryLabel->setText(string);
      
      if(optionen.Battery ==0)
      {
          BatteryLabel->setText(TextGet(TEXT_EINSTELL_TRAFO));
      }
      else
      {
         sprintf(string,"%d %%",optionen.Battery);
         BatteryLabel->setText(string);
      }

      formatZeit(&(optionen.Clock),string);
      CameraTimeLabel->setText(string); 
      time(&computerZeit);
      formatZeit(&computerZeit,string);
      ComputerTimeLabel->setText(string);

      if(computerZeit > optionen.Clock)
         unterschied= computerZeit - optionen.Clock;   
      else
         unterschied= optionen.Clock - computerZeit;   
      if(unterschied > 60)
         ComputerToCameraButton -> setEnabled(true);
      else
         ComputerToCameraButton -> setEnabled(false);
   
      CameraSpinbox        ->setValue(optionen.ShutdownCamera);
      CameraComputerSpinbox->setValue(optionen.ShutdownComputer);
      LCDSpinbox           ->setValue(optionen.ShutdownLCD);
   
      if(optionen.Quality ==1)
         QualityStandardButton->setEnabled(true);
      else if(optionen.Quality ==2)
         QualityHighButton->setEnabled(true);
#ifdef HIGH2_MODE      
      else if(optionen.Quality ==3)
         QualityHigh2Button->setEnabled(true);
#endif
      QualityGroup->setButton(optionen.Quality);         

      BrightnessSlider->setValue(optionen.Brightness);

      if(optionen.Flash==0)
         FlashAutomaticButton->setEnabled(true);
      else if(optionen.Flash==1)
         FlashForceButton->setEnabled(true);
      else if(optionen.Flash==2)
         FlashOffButton->setEnabled(true);
      else if(optionen.Flash==3)
         FlashRedEyeButton->setEnabled(true);
      FlashGroup->setButton(optionen.Flash );         

      if(optionen.Lens == 2)
      {
         LensNormalButton->setEnabled(true);
         wert =1;
      }
      else if(optionen.Lens == 1)
      {
         LensMacroButton->setEnabled(true);
         wert = 2;
      }
      LensGroup->setButton(wert);         

   }
   else
   {
      // noch ein paar dummy-werte eintragen
   }


   // --- the cancel and ok buttons
   dialogButtons = new QHBoxLayout( topLayout);
   ok = new QPushButton( TextGet(TEXT_OK), this );
   ok->setGeometry( 10,10, 100,30 );
   connect( ok, SIGNAL(clicked()), SLOT(OkPressed()) );

   cancel = new QPushButton( TextGet(TEXT_CANCEL), this );
   cancel->setGeometry( 10,60, 100,30 );
   connect( cancel, SIGNAL(clicked()), SLOT(reject()) );

   dialogButtons->addWidget(ok);   
   dialogButtons->addWidget(cancel);   

   // den computer nach camera button verbinden
   connect(ComputerToCameraButton,
           SIGNAL(clicked()),
           this,
           SLOT(ComputerToCameraPressed()) );

   cameraApplication->setWait(false);

}

//-----------------------------------------------------------------------------
void ControlBox::ComputerToCameraPressed()
{
   CameraRealOptionGetClass *camera;
   time_t                   aktuelleZeit,computerZeit,unterschied;
   char                     string[255];
   int                      returnCode;
   
   
   aktuelleZeit = time(&aktuelleZeit);
   computerZeit = aktuelleZeit;

   
   cameraApplication->setWait(true);

   // zeit holen, dabei gleichzeitig setzen
   returnCode = cameraApplication->cameraReal->GetTime(&aktuelleZeit,TRUE);

   cameraApplication->setWait(false);


   if(returnCode != RC_OK)
   {
      return;
   }  

   
   formatZeit(&aktuelleZeit,string);
   CameraTimeLabel->setText(string); 

   formatZeit(&computerZeit,string);
   ComputerTimeLabel->setText(string);

   
   
   // das setzen kann nicht geklappt haben, also werte neu setzen
   if(computerZeit > aktuelleZeit)
      unterschied= computerZeit - aktuelleZeit;   
   else
      unterschied= aktuelleZeit - computerZeit;   
   if(unterschied > 60)
      ComputerToCameraButton -> setEnabled(true);
   else
      ComputerToCameraButton -> setEnabled(false);

}

//-----------------------------------------------------------------------------

void ControlBox::OkPressed()
{
   int returnCode;
   int wert;
   int geaenderteEinstellungen = 0;
   
   if(CameraSpinbox->value() != optionen.ShutdownCamera)
   {
      optionen.ShutdownCamera = CameraSpinbox->value();
      geaenderteEinstellungen |= CameraOptionShutdownCamera;
   }

   if(CameraComputerSpinbox->value() != optionen.ShutdownComputer)
   {
      optionen.ShutdownComputer = CameraComputerSpinbox->value();
      geaenderteEinstellungen |= CameraOptionShutdownComputer;
   }

   if(LCDSpinbox->value() != optionen.ShutdownLCD)
   {
      optionen.ShutdownLCD = LCDSpinbox->value();
      geaenderteEinstellungen |= CameraOptionShutdownLCD;
   }

   wert =1;
   if(QualityStandardButton->isChecked())
      wert =1;
   if(QualityHighButton->isChecked())
      wert =2;
#ifdef HIGH2_MODE      
   if(QualityHigh2Button->isChecked())
      wert =3;
#endif
   if(wert != optionen.Quality)
   {
      optionen.Quality = wert;
      geaenderteEinstellungen |= CameraOptionQuality;
   }
   
   if(BrightnessSlider->value() != optionen.Brightness)
   {
      optionen.Brightness = BrightnessSlider->value();
      geaenderteEinstellungen |= CameraOptionBrightness;
   }
   
   
   wert =0;
   if(FlashAutomaticButton->isChecked())
      wert =0;
   if(FlashForceButton->isChecked())
      wert =1;
   if(FlashOffButton->isChecked())
      wert =2;
   if(FlashRedEyeButton->isChecked())
      wert =3;
   if(wert != optionen.Flash)
   {
      optionen.Flash = wert;
      geaenderteEinstellungen |= CameraOptionFlash;
   }

   wert =1;
   if(LensNormalButton->isChecked())
      wert =2;
   if(LensMacroButton->isChecked())
      wert =1;
   if(wert != optionen.Lens)
   {
      optionen.Lens = wert;
      geaenderteEinstellungen |= CameraOptionLens;
      
   }
   // die werte zuruecksetzen, aber nur bei aenderungen
   if(geaenderteEinstellungen != 0)
   {
      cameraApplication->setWait(true);
      returnCode = cameraApplication->cameraReal->SetOptions(geaenderteEinstellungen,
                                                 optionen);
      cameraApplication->setWait(false);
   }
   
   // box jetzt wegblenden
   accept();

}

//-----------------------------------------------------------------------------
