/****************************************************************************
** ui.h extension file, included from the uic-generated form implementation.
**
** If you wish to add, delete or rename functions or slots use
** Qt Designer which will update this file, preserving your code. Create an
** init() function in place of a constructor, and a destroy() function in
** place of a destructor.
*****************************************************************************/
#define AGENDA 1
#define W_OFSET 2
#define NB_DAY 61
//----------------------------------- init() ----------------------------------------------------------
void Wdg_Agenda::init()
{
 m_pCMoteurAgenda = 0;
 QString path_img = G_pCApp->m_PathAppli + "Images" + QDir::separator();
 textLabelPixMap->setPixmap ( QPixmap(path_img +"DMD-Logo-03.png") );
 textLabelPixMap->adjustSize();
#ifdef  AGENDA
 m_GoDatePopup.setPixmap (QPixmap(path_img +"DownExpandArrow.xpm"),    QIconSet::Automatic);
 pushButtonDateDeb->setIconSet (m_GoDatePopup);


 //............. creer le Layout des Variables .............................
 m_pLayout_Agenda      = new QVBoxLayout( 0, 0, 0, "layoutAgenda");
 //............... Y placer une ScrollView .................................
 m_pQScrollView  = new QScrollView(this);
 //............... avec un  CBigShrinker qui recevra les widgets ...........
 m_pCBigShrinker = new CBigShrinker(0,1280,8000);//(vp->viewport());
 m_pQScrollView->addChild(m_pCBigShrinker);
 m_pLayout_Agenda->addWidget(m_pQScrollView );
 //................ se connecter à la base ................................
 //                 (le faire avant ReinitAgendaOnDate() )
 QString localParam, driver, base, user, password,hostname;
 CMoteurBase::Param_UpdateFromDisk( G_pCApp->m_PathAppli + "Manager.ini", localParam );
 if (CGestIni::Param_ReadParam(localParam, "Connexion", "Parametres", &driver, &base, &user, &password, &hostname ) != 0)
    {driver   = "QMYSQL3";
     base     = "DrTuxTest";
     user     = "root";
     password = "";
     hostname = "localhost";
    }

 QString baseCfg;
 CGestIni::Param_UpdateFromDisk(G_pCApp->m_PathAppli + "DataBaseAgenda.cfg" , baseCfg);
 QString errMess;
 m_pCMoteurAgenda = new CMoteurAgenda(driver,        // nom du driver: "QODBC3" "QMYSQL3" "QPSQL7"
                                      base,          // nom de la base: si QODBC3 -> nom de la source de données (userDSN)
                                      user,          // = "root"
                                      password,      // = ""
	                              hostname,
		                      baseCfg,
                                      BASE_AGENDA,
		                      &errMess);
//............................. parametres graphiques de l'agenda ....................................
  void            SetProportionalDaysHeight(int = 15);
  void            SetSpaceBetwenDays(int = 1);
  void            SetMinDaysHeight(int = 15);

QString val;
if (CGestIni::Param_ReadParam(localParam, "Agenda", "Hauteur jour", &val) == 0)
   { m_pCMoteurAgenda->SetDaysHeight(val.toInt());  // 0 proportionnel   <> 0  hauteur fixe de la journée
   }
if (CGestIni::Param_ReadParam(localParam, "Agenda", "Hauteur mini jour", &val) == 0)
   { m_pCMoteurAgenda->SetMinDaysHeight(val.toInt());
   }
if (CGestIni::Param_ReadParam(localParam, "Agenda", "Afficher espace vide", &val) == 0)
   { m_pCMoteurAgenda->SetSpaceBetwenDays(val.toInt());
   }

 //................ Ajouter ce layout aux autres .........................
 GetColorProfils(m_ColorProfils);
 QDate dateDebut(QDate::currentDate());
 mle_debut->setText(dateDebut.toString("ddMMyyyy"));
 ReinitAgendaOnDate(dateDebut);
 m_FirstDate = dateDebut;
 Wdg_AgendaLayout->addLayout( m_pLayout_Agenda, 2, 0 ); // il faut que l'ensemble du dialogue soit en layout pour avoir: FormRubTerrainLayout

#else
 textLabel1->hide();
 mle_debut->hide();
 pushButtonDateDeb->hide();
#endif
}

//----------------------------------- SetUser ----------------------------------------------------------
void Wdg_Agenda::SetUser(const QString &user)
{m_pCMoteurAgenda->m_UserName = user;
 ReinitAgendaOnDate(m_FirstDate);
}
//----------------------------------- GetDate ----------------------------------------------------------
QDate Wdg_Agenda::GetDate()
{return m_FirstDate;
}

//----------------------------------- destroy ----------------------------------------------------------
void Wdg_Agenda::destroy()
{if (m_pCMoteurAgenda==0) return;
 delete m_pCMoteurAgenda;
 m_pCMoteurAgenda = 0;
}
/*
//---------------------------- OnButtonDeleteClickedPtr ------------------------------------------------
void Wdg_Agenda::OnButtonSavePtr(const char*, void *ptr)
{if (ptr==0) return;
// emit Sign_RendezVousDeleteClicked(this);
}
*/
//---------------------------------------- GetColorProfils -------------------------------------------
void Wdg_Agenda::GetColorProfils(MAP_COLOR &colorProfils)
{m_pCMoteurAgenda->COL_Get_List(colorProfils);

 /*
 QString localParam;
 CMoteurBase::Param_UpdateFromDisk(GlobalPathAppli + "Manager.ini", localParam);
 colorProfils.clear();
 CMoteurBase::GotoDebug();
 QString     section  = "";
 QString     var_name = "";
 QString     val1;
 char        *pt = (char*)(const char*) localParam;
 while((pt = CMoteurBase::Param_GotoNextSection(pt, 0, &section)) && *pt)
    {//.................................. ATCD ........................................................
     if (section=="AGENDA_COLOR_PROFIL")
        {//  [AGENDA_COLOR_PROFIL]
         //             Consultation=#f3f972
         //             Bloc = #6370ff
         //             Bloc = #6370ff
         //             Libre = #27d60c
         //             Pansement = #c66c05
         //

	 while (*pt && *pt != '[') // tant que pas au debut d'une nouvelle section
                {val1     = "";
		 var_name = "";
		 pt       = CMoteurBase::Param_ExtraireNextValeurs(pt, var_name, &val1 );
                 if (var_name.length()) colorProfils[var_name] = val1;
                }
	 section="";
        }
    } // end while((pt=m_pCMoteurBase->Param_GotoNextSection(pt, 0, &section)) && *pt)
 */
}



//----------------------------------- ReinitAgendaOnDate -------------------------------------------
void Wdg_Agenda::ReinitAgendaOnDate(QDate dateDeb)
{
 if (m_pCBigShrinker != 0) delete m_pCBigShrinker;
 m_pCBigShrinker = new CBigShrinker(0,1280,4000);
 m_pQScrollView->addChild(m_pCBigShrinker);
 m_Wdg_AgendaDayList.clear();
 dateDeb = dateDeb.addDays (-8 );
 int    i  = 0;
 int    y  = 2;
 Wdg_AgendaDay *pWdg_AgendaDay;
 for (i=0;  i<61; ++i)
     {   pWdg_AgendaDay = new Wdg_AgendaDay(m_pCBigShrinker);
         m_Wdg_AgendaDayList.append (pWdg_AgendaDay);
         pWdg_AgendaDay->move ( W_OFSET, y );
         pWdg_AgendaDay->initDialog(dateDeb, &m_ColorProfils, m_pCMoteurAgenda);
         y += pWdg_AgendaDay->GetHeight();
         connect( pWdg_AgendaDay->m_ButtonDate, SIGNAL( Sign_ButtonClickedPtr (const char*, void *)  ),
                  this ,                        SLOT(   OnButtonDateClickedPtr(const char*, void *)  )
                );
         connect( pWdg_AgendaDay, SIGNAL( Sign_DayChanged(Wdg_AgendaDay* )  ),
                  this ,          SLOT  ( DayChanged(Wdg_AgendaDay* )  )
                );
         connect( pWdg_AgendaDay, SIGNAL( Sign_RendezVousAccederClicked(Wdg_RendezVous*)  ),
                  this ,           SLOT(   OnRendezVousAccederClicked(Wdg_RendezVous*)  )
                );
         connect( pWdg_AgendaDay, SIGNAL( Sign_RendezVousSaveClicked(Wdg_AgendaDay*)  ),
                  this ,           SLOT(   OnRendezVousSaveClicked(Wdg_AgendaDay*)  )
                );
        if (dateDeb==QDate::currentDate() & CMoteurBase::IsThisDroitExist(G_pCApp->m_Droits, "agv")) pWdg_AgendaDay->ExpandDialog();
        dateDeb = dateDeb.addDays (1);
     }
 m_pCBigShrinker->resize(1200, y);
 m_pCBigShrinker->show();
 m_pQScrollView->show();
 ReArrangeAgenda();
}

//----------------------------------- OnRendezVousSaveClicked -------------------------------------------
void Wdg_Agenda::OnRendezVousSaveClicked(Wdg_AgendaDay * /*pWdg_AgendaDay */)
{//.............. sauver dernier utilisateur ....................................
 /*
 CMoteurBase::Param_UpdateToDisk(GlobalPathAppli + "Manager.ini", localParam);
 QString var   = "Modèles";

 switch(pWdg_RendezVous->m_Date.dayOfWeek())   // Monday=1..Sunday=7
   {case 1: var+= tr( "Lundi");
   }
 textLabel_Titre->setText( str_date );
 if (str_date.left(3) == "sam")
 CMoteurBase::Param_WriteParam( &m_LocalParam, "Agenda", "Modeles", Global_UserName);
 CMoteurBase::Param_UpdateToDisk(GlobalPathAppli + "Manager.ini", localParam);
*/
}

//----------------------------------- OnRendezVousAccederClicked -------------------------------------------
void Wdg_Agenda::OnRendezVousAccederClicked(Wdg_RendezVous *pWdg_RendezVous)
{emit Sign_RendezVousAccederClicked(pWdg_RendezVous);
}

//----------------------------------- DayChanged -------------------------------------------
void Wdg_Agenda::DayChanged(Wdg_AgendaDay* pWdg_AgendaDay)
{ReArrangeAgenda();
}

//----------------------------------- ReArrangeAgenda -------------------------------------------
void Wdg_Agenda::ReArrangeAgenda()
{int    y  = 2;
 Wdg_AgendaDay *pWdg_AgendaDay;
 for ( pWdg_AgendaDay = m_Wdg_AgendaDayList.first(); pWdg_AgendaDay; pWdg_AgendaDay = m_Wdg_AgendaDayList.next() )
     {pWdg_AgendaDay->move ( W_OFSET, y );
      y += pWdg_AgendaDay->GetHeight();
     }
 m_pCBigShrinker->resize(1200, y);
}

//----------------------------------- OnButtonDateClickedPtr() ----------------------------------
void Wdg_Agenda::OnButtonDateClickedPtr(const char*, void *ptr)
{Wdg_AgendaDay *pWdg_AgendaDay = (Wdg_AgendaDay *) ptr;
 if (pWdg_AgendaDay==0) return;
 pWdg_AgendaDay->ExpandDialog();
 ReArrangeAgenda();
}

//----------------------------------- pushButtonDateDeb_clicked() ----------------------------------
void Wdg_Agenda::pushButtonDateDeb_clicked()
{Dlg_Calendar          *dlg  = new Dlg_Calendar(this, "Calendar_Dial", TRUE, WStyle_Customize | WStyle_NormalBorder | WStyle_Title |WStyle_MinMax | WStyle_SysMenu);
 if (dlg ==0)                                                   return;
 dlg->setCaption(tr("Date de début"));
 dlg->move ( QCursor::pos().x(), QCursor::pos().y() );
 QString text = mle_debut->text();
 int jour, mois, annee;
 jour  = text.mid(0,2).toInt();
 mois  = text.mid(2,2).toInt();
 annee = text.mid(4,4).toInt();
 if (QDate::isValid ( annee, mois, jour ) )
    {dlg->setDate(QDate(annee, mois, jour));
    }
 if (dlg->exec()== QDialog::Accepted )
    {mle_debut->setText(dlg->getDate().toString("ddMMyyyy"));
     m_FirstDate = dlg->getDate();
     ReinitAgendaOnDate(m_FirstDate);
    }
 delete dlg;
}
