// 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.
//

#include "settingsbox.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 "texte.h"


#include <stdio.h>

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

SettingsBox::SettingsBox(QWidget *parent,
                         CameraApplicationClass *theCameraApplication) : 
		QDialog(parent,"Settings",TRUE)
{
   QPushButton *ok, *cancel;
   QGridLayout *grid;
   QBoxLayout  *topLayout;
   QBoxLayout  *dialogButtons;

   cameraApplication = theCameraApplication;

   // titel setzen
   setCaption(TextGet(TEXT_SETTINGS));

   topLayout = new QVBoxLayout( this, 2 );

   grid = new QGridLayout( topLayout, 5, 3, 10 );
   grid->addWidget(new QLabel(TextGet(TEXT_PREFIX),this),0,0);
   grid->addWidget(new QLabel(TextGet(TEXT_DIRECTORY),this),1,0);
   grid->addWidget(new QLabel(TextGet(TEXT_SERIAL_DEVICE),this),2,0);
   grid->addWidget(new QLabel(TextGet(TEXT_SERIAL_SPEED),this),3,0);
   grid->addWidget(new QLabel(TextGet(TEXT_SETTINGS_TIME),this),4,0);
   
   // edit eingabe felder   
   prefix          = new QLineEdit(cameraApplication->LoadPrefix,this);
   actualDirectory = new QLineEdit(cameraApplication->LoadDirectory,this);
   device          = new QLineEdit(cameraApplication->deviceName,this);
   
   
   grid->addWidget(prefix,0,1);
   grid->addWidget(actualDirectory,1,1);
   grid->addWidget(device,2,1);
   
   // geschwindigkeit combo box
   speeds = new QComboBox(this);
   speeds->insertItem(TextGet(TEXT_SPEED_9600),0);
   speeds->insertItem(TextGet(TEXT_SPEED_19200),1);
   speeds->insertItem(TextGet(TEXT_SPEED_38400),2);
   speeds->insertItem(TextGet(TEXT_SPEED_57600),3);
   speeds->insertItem(TextGet(TEXT_SPEED_115200),4);
   switch(cameraApplication->Geschwindigkeit)
   {
      case 9600:
         speeds->setCurrentItem(0);
         break;
      case 19200:
         speeds->setCurrentItem(1);
         break;
      case 38400:
         speeds->setCurrentItem(2);
         break;
      case 57600:
         speeds->setCurrentItem(3);
         break;
      case 115200:
         speeds->setCurrentItem(4);
         break;
   }
   
   grid->addWidget(speeds,3,1);

   // set time checkbox
   setEveryTime = new  QCheckBox(TextGet(TEXT_SETTINGS_SETCLOCK),this);
   if( cameraApplication->AutoSetUhr )
   {
      setEveryTime->setChecked(true);  
   }
   else
   {
      setEveryTime->setChecked(false);  
   }
   grid->addWidget(setEveryTime,4,1);

   SelectButton = new QPushButton(TextGet(TEXT_SELECT),this);
   grid->addWidget(SelectButton ,1,2);
   
   DetectButton = new QPushButton(TextGet(TEXT_AUTODETECT),this);
   grid->addWidget(DetectButton,2,2);

   // --- 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);   

   // select und detect knoepfe verbinden
   connect(SelectButton,SIGNAL(clicked()),this,SLOT(selectDirectory()) );
   connect(DetectButton,SIGNAL(clicked()),this,SLOT(AutodetectPressed()) );

}

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

void SettingsBox::selectDirectory()
{
   QFileDialog *fileDialog;
   QString     returnPath;

   // file dialog öffnen
   fileDialog = new QFileDialog(this,"fileDialog",TRUE);  
   fileDialog->setCaption(TextGet(TEXT_FILEDIALOG_TITEL));
   fileDialog->setDir(actualDirectory->text()) ;
   fileDialog->setMode ( QFileDialog::Directory);
   
   // ausführen lassen
   fileDialog->exec();
   
   // wert uebernehmen
   returnPath= fileDialog->selectedFile();
   actualDirectory->setText(returnPath);
   
   // dialog loeschen
   delete fileDialog;
   
}

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

void SettingsBox::OkPressed()
{
   int geschwindigkeitItem;
  
   // die strings zurückkopieren
   strcpy( cameraApplication->LoadPrefix    , prefix->text().latin1());
   strcpy( cameraApplication->LoadDirectory , actualDirectory->text().latin1());
   strcpy( cameraApplication->deviceName    , device->text().latin1());
   
   // geschwindigkeit ermitteln
   geschwindigkeitItem = speeds->currentItem();
   switch(geschwindigkeitItem)
   {
      case 0:
         cameraApplication->Geschwindigkeit =9600;
         break;
      case 1:
         cameraApplication->Geschwindigkeit =19200;
         break;
      case 2:
         cameraApplication->Geschwindigkeit =38400;
         break;
      case 3:
         cameraApplication->Geschwindigkeit =57600;
         break;
      case 4:
         cameraApplication->Geschwindigkeit =115200;
         break;

   }
   
   
   // Automatisches Uhr Setzen abfragen
   if(setEveryTime->isChecked())
   {
      cameraApplication->AutoSetUhr = 1;
   }
   else
   {
      cameraApplication->AutoSetUhr = 0;
   }
   

   // und box beenden
   accept();
}

//-----------------------------------------------------------------------------
static int deviceNameCount=4;
static char deviceNameStrings[20][20] = 
{
               "/dev/ttyS0",
               "/dev/ttyS1",
               "/dev/ttyS2",
               "/dev/ttyS3",
};

void SettingsBox::AutodetectPressed()
{
   int returnCode,i; 
   
   // warte cursor einschalten
   cameraApplication->setWait(true);
   
   // alle devices durchtesten
   for(i=0;i<deviceNameCount;i++)
   {
   
      // testen, ob sich unter dem device etwas meldet
      returnCode = cameraApplication->cameraReal->IsCameraDevice(deviceNameStrings[i]);
      if(returnCode == RC_OK)
      {
         // gefunden
         strcpy(cameraApplication->deviceName,deviceNameStrings[i]);
         device->setText(deviceNameStrings[i]);

         cameraApplication->setWait(false);

         return;

      }
   } // for i

   // wartecursor aus
   cameraApplication->setWait(false);


   // nichts gefunden
   device->setText(TextGet(TEXT_NOTHING_DETECTED));   
   
}
