/****************************************************************************
** 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.
*****************************************************************************/
//=============================================== IMPLEMENTTION =====================================================
#include <math.h>
#include <stdlib.h>

 // ________________________________________________________________________________
 // MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM

//---------------------------------- init --------------------------------------------------------------
void FormRubIdent::init()
{//........................ les notes sont à priori pas visibles .....................................
 m_IsModifiable = 0;
 m_IconIsNoteShow = Theme::getIconDownArrow();
 m_IconIsNoteHide = Theme::getIconRightArrow();
 m_NoteViewState = FALSE;
 //pushButton_ViewNotes->setIconSet (m_IconIsNoteHide);
 pushButton_ViewNotes->setIconSet (m_IconIsNoteShow);
 //tabWidgetNotes->hide();
 tabWidgetNotes->setCurrentPage(1);
 pushButtonCreate->hide();
 pushButtonSavModif->show();  //void Sig_CarEvent(QEvent * ev);
 pushButtonCancel->show();
 connect( lineEdit_Sexe, SIGNAL( Sig_FocusOut ()),
          this,     SLOT(   OnOutFocuslineEdit_DtNss())
        );
 connect( lineEdit_DtNss, SIGNAL( Sig_FocusOut ()),
          this,     SLOT(   OnOutFocuslineEdit_DtNss())
        );
 connect( lineEditVille, SIGNAL( Sig_FocusOut ()),
          this,     SLOT(   OnOutFocusVille())
        );
 connect( lineEdit_CdPostal, SIGNAL( Sig_FocusOut()),
                   this,     SLOT(   OnOutFocusCodePostal())
                );
 connect( lineEdit_CdPostal, SIGNAL( Sig_CarEvent(QEvent* )),
                   this,     SLOT(   OnCharEventCodePostal(QEvent* ))
                );
 connect( lineEdit_NumSecu, SIGNAL( Sig_CarEvent(QEvent* )),
                  this,     SLOT(   OnCharEventNumSecu(QEvent* ))
                );
 connect( lineEdit_NumSecu, SIGNAL( Sig_FocusIn()),
                   this,   SLOT(   OnInFocusNumSecu())
                );
 connect( lineEdit_NumSecu, SIGNAL( Sig_FocusOut()),
                   this,   SLOT(   OnOutFocusNumSecu())
                );
 m_Contacts_Run     = 0;
 m_CurrentPopupItem = "";
 m_RetPopupItem     = "";
 m_pThemePopup      = 0;
 SetInterfaceOnDroits(G_pCApp->m_Droits);
 lineEdit_NomDossier->setFocus();
}

//----------------------------------------- SetInterfaceOnDroits ------------------------------------------------------------------------
void FormRubIdent::SetInterfaceOnDroits(const QString &droits)
{//....................... droits idv-idc-idg ...................................................................
 if (G_pCApp->IsThisDroitExist(droits,"idv"))
    {groupBox1->show();
     groupBox2->show();
     tabWidgetNotes->show();
     pushButton_ViewNotes->show();
     textLabelNumeroDoss->show();
     textLabel_NumDoss->show();
    }
 else
    {groupBox1->hide();
     groupBox2->hide();
     tabWidgetNotes->hide();
     pushButton_ViewNotes->hide();
     textLabelNumeroDoss->hide();
     textLabel_NumDoss->hide();
    }
 if (G_pCApp->IsThisDroitExist(droits,"idc"))
    {pushButtonSavModif->setEnabled(TRUE);
     pushButtonCreate->setEnabled(TRUE);
     pushButton_AddMedTTT->setEnabled(TRUE);
     pushButton_DelMedTTT->setEnabled(TRUE);
    }
 else
    {pushButtonSavModif->setEnabled(FALSE);
     pushButtonCreate->setEnabled(FALSE);
     pushButton_AddMedTTT->setEnabled(FALSE);
     pushButton_DelMedTTT->setEnabled(FALSE);
    }
}

//----------------------------------------- OnOutFocuslineEdit_DtNss --------------------------------------------------
/*! \brief ACTION: calcule l'age entre deux dates de début et de fin.
// ENTREE: QDate *pDebut: pointe sur une QDate de debut (exemple date de naissance)
//         QDate *pFin:   pointe sur une QDate de  fin (peut etre zero alors il sera pris la date en cours)
//         bool bTexte:   TRUE   alors la date sera exprimée comme: 4 ans 6 mois 3 jours
//                        FALSE  alors la date sera exprimée comme un chiffre
//         int bAbrege:   2  et alors seules les années seront sorties sous la forme: "0" ou "35" etc ...
//                        1  et alors seules les années seront sorties sous la forme: "0" ou "35" etc ...
//                        0  et alors les sorties seront detaillées avec si age<3 avec les mois et jours:
// QString CMoteurBase::CalculeDiffAge(QDate *pDebut, QDate *pFin, bool bTexte, int bAbrege)
*/
void FormRubIdent::OnOutFocuslineEdit_DtNss()
{ // `HistN0_Date`  >=  "2001-05-19T11:09:40" AND `HistN0_Date`  <=  "2001-05-20T11:09:41";
 QString resolvToken("");
 QString        dtNss      = lineEdit_DtNss->text().mid(4)+lineEdit_DtNss->text().mid(2,2)+lineEdit_DtNss->text().mid(0,2);
 QDate        qDteNss(lineEdit_DtNss->text().mid(4,4).toInt(),
                      lineEdit_DtNss->text().mid(2,2).toInt(),
                      lineEdit_DtNss->text().mid(0,2).toInt()
                     );
 QString sAge = m_pCMoteurBase->CalculeDiffAge(&qDteNss, 0, TRUE, 0);
 //textLabel_Age->setText(sAge);
 int i = 0; while (sAge[i]>='0' && sAge[i]<='9') ++i; sAge.truncate(i);
 int              age      = sAge.toInt();

 if (age < 15)          resolvToken="L'enfant";
 if (age >= 15 && age <= 21)
    {QString sexe = lineEdit_Sexe->text();
     if (sexe=="F")     resolvToken="Mademoiselle";
     else               resolvToken="Monsieur";
    }
 if (age>21)
    {QString sexe = lineEdit_Sexe->text();
     if (sexe=="F")     resolvToken="Madame";
     else               resolvToken="Monsieur";
    }
 lineEdit_Titre->setText(resolvToken);
}

//--------------------------------- SetModifiableState -----------------------------------------------------------------------
void  FormRubIdent::SetModifiableState(int state)
{m_IsModifiable = state;
 if (m_IsModifiable)
    {pushButtonSavModif->show();
     pushButton_AddMedTTT->show();
     pushButton_DelMedTTT->show();
    }
 else
    {pushButtonSavModif->hide();
     pushButton_AddMedTTT->hide();
     pushButton_DelMedTTT->hide();
    }
}

//--------------------------------- OnInFocusNumSecu -----------------------------------------------------------------------
void  FormRubIdent::OnInFocusNumSecu()
{QString numsecu = lineEdit_NumSecu->text();  //18105m_IsModifiable = 0;

 if (numsecu.length()==0)
    {if (lineEdit_Sexe->text()!="F") numsecu += "1";
     else                            numsecu += "2";
     QString dtnss = lineEdit_DtNss->text();
     if (dtnss.length()==8)
         {numsecu += dtnss.mid(6);
          numsecu += dtnss.mid(2,2);
         }
     lineEdit_NumSecu->setText(numsecu);
    }
}

//--------------------------------- OnOutFocusNumSecu -------------------------------------------------------------
void  FormRubIdent::OnOutFocusNumSecu()
{QString numsecu = lineEdit_NumSecu->text();  //18105
 if (numsecu.length()==13)
    {lineEdit_NssClef->setText(ComputeClef_Secu(numsecu));
    }
 else
    {lineEdit_NssClef->clear();
    }

 QString leftNumSecu("");
 if (lineEdit_Sexe->text()!="F") leftNumSecu += "1";
 else                            leftNumSecu += "2";
 QString dtnss = lineEdit_DtNss->text();
 if (dtnss.length()==8)
    {leftNumSecu += dtnss.mid(6);
     leftNumSecu += dtnss.mid(2,2);
    }
 if (numsecu.length()>=5 && leftNumSecu.length()>=5 && leftNumSecu != numsecu.left(5))
    {checkBox_IsNotPatient->setChecked (TRUE) ;   // l'assuré n'est pas le patient
     lineEdit_NomAssure->setEnabled(TRUE);
     lineEdit_PrenomAssure->setEnabled(TRUE);
     if (lineEdit_NomAssure->text()=="")     lineEdit_NomAssure->setText(lineEdit_NomDossier->text());
     if (numsecu.length()==13)
        {lineEdit_NomAssure->setFocus();
         lineEdit_NomAssure->setCursorPosition (-1);
        }
    }
 else
    {checkBox_IsNotPatient->setChecked (FALSE) ;   // l'assuré est le patient
     lineEdit_NomAssure->setEnabled(FALSE);
     lineEdit_PrenomAssure->setEnabled(FALSE);
    }
}

//--------------------------------- checkBox_IsNotPatient_clicked -------------------------------------------------------------
void FormRubIdent::checkBox_IsNotPatient_clicked()
{if (checkBox_IsNotPatient->isChecked())
    {lineEdit_NomAssure->setEnabled(TRUE);
     lineEdit_PrenomAssure->setEnabled(TRUE);
     if (lineEdit_NumSecu->text().length()==13) lineEdit_NomAssure->setFocus();
     if (lineEdit_NomAssure->text()=="")        lineEdit_NomAssure->setText(lineEdit_NomDossier->text());
     //if (lineEdit_PrenomAssure->text()=="")  lineEdit_PrenomAssure->setText(lineEdit_PremomDossier->text());
    }
else
    {lineEdit_NomAssure->setEnabled(FALSE);
     lineEdit_PrenomAssure->setEnabled(FALSE);
     lineEdit_NomAssure->setText("");
     lineEdit_PrenomAssure->setText("");
    }
}

//--------------------------------- OnOutFocusVille -----------------------------------------------------------------------
void  FormRubIdent::OnOutFocusVille()
{if (m_pCMoteurBase==0) return;
 QString codePostal("");
 QString ville = lineEditVille->text().stripWhiteSpace();
 //.......... si pas de ville saisie on tente avec le code postal .........................
 if (ville.length()==0)
    {OnOutFocusCodePostal();
     return;
    }
 //............... chercher liste de codepostaux pouvant correspondre à cette ville .............
 QStringList codePostalList;
 m_pCMoteurBase->GetCodePostalFromVille( ville.replace("SAINT ","ST "),  codePostalList);
 if (codePostalList.findIndex ( lineEdit_CdPostal->text()+" "+lineEditVille->text() ) != -1 ) return;

 int nb = codePostalList.count();
 if (nb==1)
    {codePostal = codePostalList[0];
    }
 else if (nb>1 && nb<35 )
    {
     codePostal = DoPopupList(codePostalList, lineEditVille->x(), lineEditVille->y());
    }
 //............ separer la ville du code postal ...................................................
 int pos = codePostal.find(' ');
 if (pos != -1)
    {ville      = codePostal.mid(pos+1);
     codePostal = codePostal.left(pos);
    }
 if (//lineEdit_CdPostal->text().length()<5           ||                               // si code postal incomplet
     (codePostal.length()!=0 /*&& lineEdit_CdPostal->text() != codePostal */)            // ou non conforme avec celui de la ville
    )
    {lineEdit_CdPostal->setText(codePostal );                                        // placer celui de la ville
     lineEditVille->setText(ville);
     return;
    }

 //.............. si pas de code postal trouvé pour cette ville tenter de placer la ville du code postal .............
 if (codePostal.length()==0 )
    {ville = m_pCMoteurBase->GetVilleFromCodePostal(lineEdit_CdPostal->text());
     if (ville.length()!=0) lineEditVille->setText(ville);
    }
}

//--------------------------------- OnOutFocusCodePostal -------------------------------------------------------------
void  FormRubIdent::OnOutFocusCodePostal()
{if (m_pCMoteurBase==0) return;
 QStringList villeList;
 if (lineEdit_CdPostal->text().length()==5)
    {m_pCMoteurBase->GetVilleFromCodePostal(lineEdit_CdPostal->text(), villeList);
     int nb = villeList.count();
     if (nb==1)
        {lineEditVille->setText(villeList[0]);
        }
     else if (nb>1 && nb<35 )
        {if (villeList.findIndex ( lineEditVille->text()) == -1 )  // si la ville ne correspond pas au code postal
            {QString ville = DoPopupList(villeList, lineEdit_CdPostal->x(), lineEdit_CdPostal->y());
             if (ville.length()>0) lineEditVille->setText(ville);
            }
        }
    }
 else
    {lineEditVille->clear();
    }
}

//--------------------------------- DoPopupList ----------------------------------------------------------------------
QString FormRubIdent::DoPopupList(QStringList &list, int x ,int y)
{m_CurrentPopupItem = "";
 m_RetPopupItem     = "";
 if (m_pThemePopup) return m_RetPopupItem;
 m_pThemePopup      = new ThemePopup(0, "MyPopupMenu" );
 if (m_pThemePopup==0)       return m_RetPopupItem;
 //............................... chercher une zone de liste deroulante (bleue) .....................
 for ( QStringList::Iterator it =list.begin(); it != list.end(); ++it )
    {m_pThemePopup->insertItem( *(it),    this, SLOT( GetPopupItem()),  CTRL+Key_L );
    }
 //................. connecter le menu .................................................................
 connect( m_pThemePopup, SIGNAL( activated( int)),
          this,          SLOT(OnMenuActivated( int))
        );

 m_pThemePopup->exec(mapToGlobal (QPoint ( x, y)));
 delete  m_pThemePopup;
 m_pThemePopup = 0;
 return  m_RetPopupItem;
}

//--------------------------------- OnMenuActivated  -------------------------------------------------------------
void FormRubIdent::OnMenuActivated (int id)
{if (id != -1 && m_pThemePopup)
    {m_CurrentPopupItem = m_pThemePopup->text(id);
    }
}

//--------------------------------- GetPopupItem  -------------------------------------------------------------
void FormRubIdent::GetPopupItem ()
{m_RetPopupItem  = m_CurrentPopupItem;
}

//--------------------------------- OnCharEventCodePostal -------------------------------------------------------------
void  FormRubIdent::OnCharEventCodePostal(QEvent* )
{OnOutFocusCodePostal();
}

//--------------------------------- OnCharEventNumSecu-------------------------------------------------------------
void  FormRubIdent::OnCharEventNumSecu(QEvent * )
{OnOutFocusNumSecu();
}

/*--------------------------------- ComputeClef_Secu ----------------------------*/
QString FormRubIdent::ComputeClef_Secu(const char *txt)
{if (strlen(txt) !=13) return QString("  ");
 QString result (QString::number( 97 - fmod(atof(txt) , 97)));
 if (result.length()<2) result.prepend('0');
 return result;
}

//--------------------------------- SetData-------------------------------------------------------------
void FormRubIdent::SetData()
{
 if (m_pCMoteurBase==0) return;
 pushButtonCreate->hide();
 if (m_IsModifiable) pushButtonSavModif->show();
 pushButtonCancel->show();
 if (DataToFormulaire (m_ID_Doss))
    {lineEdit_NomDossier->setText(m_DossNom);        // avec Nom    deja connu recupere dans la liste
     lineEdit_PremomDossier->setText(m_DossPrenom);  // avec Prenom deja connu recupere dans la liste
    }
 else
    {pushButtonSavModif->hide();
     ClearDialog();
    }
}
//--------------------------------- SetData-------------------------------------------------------------
void FormRubIdent::SetData(int flag)
{FormRubIdent::SetData();
 if (flag & NEW_IDENTITE)
    {ClearDialog();
     textLabel_NumDoss->setText(m_pCMoteurBase->GUID_Create());
     pushButtonCreate->show();
     pushButtonSavModif->hide();
     pushButtonCancel->show();
    }
}

//--------------------------------- ClearDialog -----------------------------------------------------------------
void FormRubIdent::ClearDialog()
{    lineEdit_NomAssure->clear();
     lineEdit_NomDeNss->clear();
     lineEdit_PrenomAssure->clear();
     lineEdit_Titre->clear();
     textEdit_Adresse->clear();
     lineEdit_Tel1->clear();
     lineEdit_Tel2->clear();
     lineEdit_Tel3->clear();
     lineEdit_CdPostal->clear();
     lineEditVille->clear();
     lineEdit_Sexe->clear();
     textLabel_NumDoss->clear();
     lineEdit_Email->clear();
     lineEdit_NomDossier->clear();
     lineEdit_PremomDossier->clear();
     lineEdit_Profession->clear();
     QDateTime  dt ;
     dt.setTime_t(0);
     //....................... trucs speciaux ........................................
     QDate qDteNss =  dt.date () ;
     lineEdit_DtNss->setText(qDteNss.toString(QString("ddMMyyyy")));
     lineEdit_NumSecu->clear();
     lineEdit_NssClef->clear();
     textEdit_Note->clear();
     listView_Praticiens->clear();
}

//--------------------------------- DataToFormulaire -------------------------------------------------------------
int FormRubIdent::DataToFormulaire( QString &refToPrimkeyDoss )
{/*
   QString      m_DOSS_IDENT_GUID;        // GUID du dossier auquel l'enregistrement fait reference
   QString      m_DOSS_IDENT_NSS ;        // date de naissance
   QString      m_DOSS_IDENT_SEX;         // sexe
   QString      m_DOSS_IDENT_JFNOM;       // nom de jeune fille
   QString      m_DOSS_IDENT_NB_ENF;      // nombre d'enfants
   QString      m_DOSS_IDENT_RUE;         // adresse
   QString      m_DOSS_IDENT_CODE_POST;   // Code postal
   QString      m_DOSS_IDENT_VILLE;       // Ville
   QString      m_DOSS_IDENT_CEDEX;       // cedex
   QString      m_DOSS_IDENT_PAYS;        // pays
   QString      m_DOSS_IDENT_TEL1;        // Telephone 1
   QString      m_DOSS_IDENT_TEL2;        // Telephone 2
   QString      m_DOSS_IDENT_TEL3;        // Telephone 3
   QString      m_DOSS_IDENT_TEL1_TYP;    // Telephone 1  type (parent amis etc...)
   QString      m_DOSS_IDENT_TEL2_TYP;    // Telephone 2
   QString      m_DOSS_IDENT_TEL3_TYP;    // Telephone 3
   QString      m_DOSS_IDENT_NUMSS;       // Numéro de secu
   QString      m_DOSS_IDENT_EMAIL;       // Numéro de secu
   QString      m_DOSS_IDENT_IS_ASS;      // le patient est il l'assuré
   QString      m_DOSS_IDENT_NOM_ASS;     // nom de l'assuré
   QString      m_DOSS_IDENT_PRENOM_ASS;  // prénom de l'assuré
   QString      m_DOSS_IDENT_PROF;        // profession
   QString      m_DOSS_IDENT_TITRE;       // Titre( Dr Mr Md Pr ...)
   QString      m_DOSS_IDENT_REF_PK;      // reference à la primary key: DOSS_INDEX_PRIM_KEY de la table: DOSS_INDEX
*/
 //............................... recuperer les notes ............................................
 QString data;
 m_pCMoteurBase->PatientNoteGetData(refToPrimkeyDoss ,  data);
 textEdit_Note->setText(data);
 m_pCMoteurBase->PatientIntervenantsGetData(refToPrimkeyDoss , listView_Praticiens);
 //............... chaines d'erreurs  ..................................................
 if  (m_pCMoteurBase->m_DataBase->isOpen()==FALSE && m_pCMoteurBase->m_DataBase->open()==FALSE) return 0;

  //............... crer un curseur SQL ..................................................
    QString requete  = QString( "SELECT ");
            requete += m_pCMoteurBase->m_DOSS_IDENT_NOM_ASS          + ", " +       // 0
                       m_pCMoteurBase->m_DOSS_IDENT_PRENOM_ASS       + ", " +       // 1
                       m_pCMoteurBase->m_DOSS_IDENT_RUE              + ", " +       // 2
                       m_pCMoteurBase->m_DOSS_IDENT_TEL1             + ", " +       // 3
                       m_pCMoteurBase->m_DOSS_IDENT_TEL2             + ", " +       // 4
                       m_pCMoteurBase->m_DOSS_IDENT_TEL3             + ", " +       // 5
                       m_pCMoteurBase->m_DOSS_IDENT_EMAIL            + ", " +       // 6
                       m_pCMoteurBase->m_DOSS_IDENT_CODE_POST        + ", " +       // 7
                       m_pCMoteurBase->m_DOSS_IDENT_VILLE            + ", " +       // 8
                       m_pCMoteurBase->m_DOSS_IDENT_SEX              + ", " +       // 9
                       m_pCMoteurBase->m_DOSS_IDENT_GUID             + ", " +       // 10
                       m_pCMoteurBase->m_DOSS_IDENT_JFNOM            + ", " +       // 11
                       m_pCMoteurBase->m_DOSS_IDENT_TITRE            + ", " +       // 12
                       m_pCMoteurBase->m_DOSS_IDENT_PROF             + ", " +       // 13
                       m_pCMoteurBase->m_DOSS_IDENT_NSS              + ", " +       // 14
                       m_pCMoteurBase->m_DOSS_IDENT_NUMSS            + ", " +       // 15
                       m_pCMoteurBase->m_DOSS_IDENT_IS_ASS           + " FROM "  +  // 16
                       m_pCMoteurBase->m_DOSS_IDENT_TBL_NAME         + " WHERE " +
                       m_pCMoteurBase->m_DOSS_IDENT_REF_PK           + " = '"    + refToPrimkeyDoss + "';";

    QSqlQuery query( requete  , m_pCMoteurBase->m_DataBase );                     // ouvrir un curseur sur la table
    if ( query.isActive() && query.next())
       {lineEdit_NomAssure->setText(     CGestIni::Utf8_Query(query , 0)      );
        lineEdit_PrenomAssure->setText(  CGestIni::Utf8_Query(query , 1)      );

        textEdit_Adresse->setText(       CGestIni::Utf8_Query(query , 2)      );
        lineEdit_Tel1->setText(          ToNum(query.value( 3).toString())    );
        lineEdit_Tel2->setText(          ToNum(query.value( 4).toString())    );
        lineEdit_Tel3->setText(          ToNum(query.value( 5).toString())    );
        lineEdit_Email->setText(         CGestIni::Utf8_Query(query , 6)      );
        lineEdit_CdPostal->setText(      ToNum(query.value( 7).toString())    );
        lineEditVille->setText(          CGestIni::Utf8_Query(query , 8)      );
        lineEdit_Sexe->setText(          CGestIni::Utf8_Query(query , 9)      );
        textLabel_NumDoss->setText(      CGestIni::Utf8_Query(query , 10)     );
        lineEdit_NomDeNss->setText(      CGestIni::Utf8_Query(query , 11)     );
        lineEdit_Titre->setText(         CGestIni::Utf8_Query(query , 12)     );
        lineEdit_Profession->setText(    CGestIni::Utf8_Query(query , 13)     );

        //....................... trucs speciaux ........................................
        QDate qDteNss =                 query.value( 14).toDate();
        lineEdit_DtNss->setText(qDteNss.toString(QString("ddMMyyyy")));
        QString   nss =                 query.value( 15).toString();
       lineEdit_NumSecu->setText( nss.left(13)  );
       lineEdit_NssClef->setText( nss.mid(13)   );
       if (query.value( 16).toString()=="1")
          {checkBox_IsNotPatient->setChecked (FALSE) ;  // l'assuré n'est  le patient
           lineEdit_NomAssure->setEnabled(FALSE);
           lineEdit_PrenomAssure->setEnabled(FALSE);
          }
       else
         {checkBox_IsNotPatient->setChecked (TRUE) ;   // l'assuré n'est pas le patient
          lineEdit_NomAssure->setEnabled(TRUE);
          lineEdit_PrenomAssure->setEnabled(TRUE);
         }

       m_pCMoteurBase->m_DataBase->close();
      return 1;
    }
else
    {
     m_pCMoteurBase->OutSQL_error(query);
     m_pCMoteurBase->m_DataBase->close();
     return 0;
    }
}

//--------------------------------- ToNum ---------------------------------------------------------------------------------
QString FormRubIdent::ToNum(QString in)
{for(uint i=0; i<in.length(); )
    {if (in.at(i)<'0' || in.at(i)>'9') in.remove(i,1);
     else ++i;
    }
 return in;
}

//--------------------------------- pushButtonCreate_clicked -------------------------------------------------------------
void FormRubIdent::pushButtonCreate_clicked()
{if (! G_pCApp->IsThisDroitExist(G_pCApp->m_Droits,"idc") ) return;
 QString nom       = lineEdit_NomDossier->text().stripWhiteSpace () ;
 QString prenom    = lineEdit_PremomDossier->text().stripWhiteSpace () ;
 QString numGUID   = textLabel_NumDoss->text();
 if (nom == "") nom = tr("ANONYME");


 PtrListIdentFieldData list_field;    //  créer la liste: nom champs <-> valeurs
 list_field.setAutoDelete(TRUE);
 SetFieldListData(list_field);


 m_ID_Doss = m_pCMoteurBase->DossIdentIndexCreate(nom, prenom, numGUID,  &list_field );
 pushButtonCreate->hide();
 if (m_IsModifiable) pushButtonSavModif->show();
 pushButtonCancel->show();
 if (m_ID_Doss != "")
    {//.......................... ecrire la note ............................................
     nom    = nom.upper();
     prenom = prenom.upper();
     m_pCMoteurBase->PatientNoteWrite(m_ID_Doss ,numGUID,  textEdit_Note->text());
     //...................... ecrire les intervenants ................................................................
     QListViewItemIterator it( listView_Praticiens );
     while ( it.current() )
           {QListViewItem *item = it.current();
            ++it;
            m_pCMoteurBase->PatientIntervenantsWrite( m_ID_Doss , numGUID,  item->text(3));
           }
     //.......................... faire part au Dr Tux du changement d'identité ............................................
     emit  Sign_PatientListMustBeUpdated(numGUID,        m_ID_Doss,
                                         nom,            prenom
                                        );
     emit  Sign_IdentChange(numGUID,        m_ID_Doss,
                            nom,            prenom
                           );
    }
 emit Sign_Ident_KillMe();
}

//--------------------------------- pushButtonSavModif_clicked -------------------------------------------------------------
void FormRubIdent::pushButtonSavModif_clicked()
{if (m_IsModifiable==0) return;
 if (! G_pCApp->IsThisDroitExist(G_pCApp->m_Droits,"idc") ) return;
 QString nom       = lineEdit_NomDossier->text().stripWhiteSpace () ;
 QString prenom    = lineEdit_PremomDossier->text().stripWhiteSpace () ;
 QString numGUID   = textLabel_NumDoss->text();
 QString str       = "";

 if (nom == "")
    {nom = tr("ANONYME");
     lineEdit_NomDossier->setText(nom);
    }

 // typedef QPtrList<CIdentFieldData> PtrListIdentFieldData
 PtrListIdentFieldData list_field;   //  créer la liste: nom champs <-> valeurs
 list_field.setAutoDelete(TRUE);
 SetFieldListData(list_field);

 nom    = nom.upper();
 prenom = prenom.upper();
 m_pCMoteurBase->DossIdentIndexUpdate(m_ID_Doss, nom, prenom, &list_field );
 m_pCMoteurBase->PatientNoteWrite(m_ID_Doss ,numGUID,  textEdit_Note->text());
 emit  Sign_IdentModified(numGUID,        m_ID_Doss,
                          nom,            prenom
                         );
 emit Sign_Ident_KillMe();
}
//--------------------------------- SetFieldListData ------------------------------------------------------------------------
void FormRubIdent::SetFieldListData(PtrListIdentFieldData &list_field)
{list_field.append( new CIdentFieldData(m_pCMoteurBase->m_DOSS_IDENT_NOM_ASS,    lineEdit_NomAssure->text().stripWhiteSpace(),          FTN_MAJ_STRING));
 list_field.append( new CIdentFieldData(m_pCMoteurBase->m_DOSS_IDENT_PRENOM_ASS, lineEdit_PrenomAssure->text().stripWhiteSpace(),       FTN_MAJ_STRING));
 list_field.append( new CIdentFieldData(m_pCMoteurBase->m_DOSS_IDENT_JFNOM,      lineEdit_NomDeNss->text().stripWhiteSpace(),           FTN_MAJ_STRING));
 list_field.append( new CIdentFieldData(m_pCMoteurBase->m_DOSS_IDENT_TITRE,      lineEdit_Titre->text().stripWhiteSpace(),              FTN_STRING));
 list_field.append( new CIdentFieldData(m_pCMoteurBase->m_DOSS_IDENT_RUE,        textEdit_Adresse->text(),            FTN_STRING));
 list_field.append( new CIdentFieldData(m_pCMoteurBase->m_DOSS_IDENT_TEL1,       lineEdit_Tel1->text(),               FTN_MAJ_STRING));
 list_field.append( new CIdentFieldData(m_pCMoteurBase->m_DOSS_IDENT_TEL2,       lineEdit_Tel2->text(),               FTN_MAJ_STRING));
 list_field.append( new CIdentFieldData(m_pCMoteurBase->m_DOSS_IDENT_TEL3,       lineEdit_Tel3->text(),               FTN_MAJ_STRING));
 list_field.append( new CIdentFieldData(m_pCMoteurBase->m_DOSS_IDENT_EMAIL,      lineEdit_Email->text(),              FTN_STRING));
 list_field.append( new CIdentFieldData(m_pCMoteurBase->m_DOSS_IDENT_CODE_POST,  lineEdit_CdPostal->text(),           FTN_INTEGER));
 list_field.append( new CIdentFieldData(m_pCMoteurBase->m_DOSS_IDENT_VILLE,      lineEditVille->text(),               FTN_MAJ_STRING));
 list_field.append( new CIdentFieldData(m_pCMoteurBase->m_DOSS_IDENT_SEX,        lineEdit_Sexe->text(),               FTN_MAJ_STRING));
 list_field.append( new CIdentFieldData(m_pCMoteurBase->m_DOSS_IDENT_NUMSS,      lineEdit_NumSecu->text()+
                                                                                 lineEdit_NssClef->text(),            FTN_NUM_STRING));
 list_field.append( new CIdentFieldData(m_pCMoteurBase->m_DOSS_IDENT_NSS,        lineEdit_DtNss->text(),              FTN_DATETIME));
 list_field.append( new CIdentFieldData(m_pCMoteurBase->m_DOSS_IDENT_IS_ASS,     QString::number(!checkBox_IsNotPatient->isChecked()),  FTN_INTEGER));
 list_field.append( new CIdentFieldData(m_pCMoteurBase->m_DOSS_IDENT_PROF,       lineEdit_Profession->text(),         FTN_MAJ_STRING));
}

//--------------------------------- pushButtonCancel_clicked -------------------------------------------------------------
void FormRubIdent::pushButtonCancel_clicked()
{emit Sign_Ident_KillMe();
}

//--------------------------------- listView_Praticiens_doubleClicked -------------------------------------------------------------
void FormRubIdent::listView_Praticiens_doubleClicked( QListViewItem *pQListViewItem )
{QString idInterv = "";
 if (pQListViewItem) idInterv = pQListViewItem->text(3);
 exeAnnuaire(idInterv);
}
//--------------------------------- pushButton_AddMedTTT_clicked -------------------------------------------------------------
void FormRubIdent::pushButton_AddMedTTT_clicked()
{QString idInterv = "";
 //............ recuperer ID praticien en cours .......................................
 QListViewItem *pQListViewItem = listView_Praticiens-> selectedItem ();
 if (pQListViewItem) idInterv = pQListViewItem->text(3);
 exeAnnuaire( idInterv );

}
//--------------------------------- exeAnnuaire -------------------------------------------------------------
void FormRubIdent::exeAnnuaire(QString idInterv )
{/*
 QString pathExe, pathExch;
 if (CMoteurBase::Param_ReadParam(G_pCApp->m_DrTuxParam, "Gestion des contacts", "PathExe",     &pathExe) !=0)             return;         // path editeur de texte non défini
 if (CMoteurBase::Param_ReadParam(G_pCApp->m_DrTuxParam, "Repertoire Temporaire", "Repertoire", &pathExch)!=0)             return;         // path editeur de texte non défini
 if ( QDir::isRelativePath ( pathExch ) ) pathExch = QDir::cleanDirPath (pathExch.prepend(G_pCApp->m_PathAppli) );
 //if ( QDir::isRelativePath (pathExe ) )   pathExe  = QDir::cleanDirPath (pathExe.prepend(G_pCApp->m_PathAppli) );
 */
 if (! G_pCApp->IsThisDroitExist(G_pCApp->m_Droits,"idc") ) return;
 QString pathExe  = m_pCMoteurBase->Construct_Name_Exe("personnes",QFileInfo (qApp->argv()[0]).dirPath (true));
 QString pathExch;


 // path editeur de texte non défini
 if (CMoteurBase::Param_ReadParam(G_pCApp->m_DrTuxParam, "Repertoire Temporaire", "Repertoire", &pathExch)!=0)             return;         // path editeur de texte non défini
 // Adaptation à la plateforme
 pathExch = m_pCMoteurBase->Construct_PathBin_Module("Manager",QFileInfo (qApp->argv()[0]).dirPath (true)) + pathExch;

 QProcess*  proc = new QProcess( this );
 if (proc==0) return;
 QString exchFile = QDir::cleanDirPath (pathExch + QDir::separator() +  "GetPersonnne" + m_NumGUID + ".ech");


 QFile file( exchFile );
 if (file.exists( exchFile )) file.remove();

 proc->addArgument(pathExe);
 proc->addArgument(exchFile );
 proc->addArgument( "Record");
 proc->addArgument( idInterv);
 // long tempo = 0;
 if (m_Contacts_Run==FALSE && proc->start() )
    {m_Contacts_Run = TRUE;
     QTimer timer;               // pour etre sur qu'un evenement se produise
     timer.start(100, FALSE);    // sinon boucle sans fin dans    processEvents ( QEventLoop::WaitForMore )
     qApp->processEvents ();
     while(proc->isRunning () )
           {QApplication::eventLoop()->processEvents ( QEventLoop::WaitForMore );
           }
     m_Contacts_Run = FALSE;
     //......... recuperer fichier d'echange .............
     if ( !file.open( IO_ReadOnly ) ) return;
     file.close();
     //QTextStream ts( &file );
     //QString ret = ts.read();    // 281|ANTOINE|Christine|Viscéral
     QString ret="";
     CGestIni::Param_UpdateFromDisk(exchFile, ret);
     QString id, spec,nom, prenom;
     int deb =  0;
     int end = ret.find('|');
     id      = ret.mid(deb, end-deb);
     deb     = end + 1;
     end     = ret.find('|', deb);
     nom     = ret.mid(deb, end-deb);
     deb     = end + 1;
     end     = ret.find('|', deb);
     prenom  = ret.mid(deb, end-deb);
     deb     = end + 1;
     spec    = ret.mid(deb);
     QString numGUID   = textLabel_NumDoss->text();
     if (pushButtonCreate->isShown()) new QListViewItem( listView_Praticiens, spec, nom, prenom, id );
     else if (m_pCMoteurBase->PatientIntervenantsWrite( m_ID_Doss , numGUID,  id) != 0)
        {new QListViewItem( listView_Praticiens, spec, nom, prenom, id );
        }
    }
 if (file.exists( exchFile )) file.remove();
 delete proc;
}


//--------------------------------- pushButton_DelMedTTT_clicked -------------------------------------------------------------
void FormRubIdent::pushButton_DelMedTTT_clicked()
{if (! G_pCApp->IsThisDroitExist(G_pCApp->m_Droits,"idc") ) return;
 QListViewItem* pQListViewItem = listView_Praticiens->currentItem ();
 if (pQListViewItem==0) return;
 if ( !(pushButtonCreate->isShown()) ) m_pCMoteurBase->PatientIntervenantsDelete( pQListViewItem->text(3));
 delete pQListViewItem;
}


//--------------------------------- pushButton_ViewNotes_clicked -------------------------------------------------------------
void FormRubIdent::pushButton_ViewNotes_clicked()
{if (m_NoteViewState == FALSE)
    { m_NoteViewState = TRUE;
      pushButton_ViewNotes->setIconSet (m_IconIsNoteShow);
      tabWidgetNotes->show();
    }
 else
    {m_NoteViewState = FALSE;
     pushButton_ViewNotes->setIconSet (m_IconIsNoteHide);
     tabWidgetNotes->hide();
    }
}


//////////////////////////////////////////////////////////////////////////////
/////////// Synchronisation de la note avec Dock_Menu ////////////////////////
//////////////////////////////////////////////////////////////////////////////

void FormRubIdent::synchronizeNoteWith( const char* txt)
{ textEdit_Note->setText( txt );
}


void FormRubIdent::textEdit_Note_textChanged()
{ emit Sign_NoteToBeSynchronized( 1, textEdit_Note->text() );
}
