/****************************************************************************
** 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 TAB_LIBELLE  0
#define TAB_RUBRIQUE 1
#define TAB_URGENCE  2
#define TAB_HTML     3

#define CIM10_DATA_BASE_NAME  "DataSrceODBC-MySQL-CIM10"

//-----------------------------------------------------  init -------------------------------------------
void FormDlgCim10Multi::init()
{setIcon( Theme::getIcon("Cim10All_Icon.png"));
 pushButton_AddToChoix->setPixmap(QPixmap( Theme::getIconNext()
                                  .pixmap(QIconSet::Automatic,TRUE,QIconSet::On)));
 pushButtonRemoveFromChoix->setPixmap(QPixmap( Theme::getIconPrevious()
                                     .pixmap(QIconSet::Automatic,TRUE,QIconSet::On)));
 m_DataBase    = 0;
 tabWidgetCim10->setCurrentPage ( TAB_URGENCE );
 pushButtonMakeHTML->hide();
 //QPixmap image;
 //image.load (path+"CuirMaron.png");
 //textBrowser_Cim10Menu->setSource(path+"TestCIM10UrgMenu.htm");
 //textBrowser_Cim10Pages->setSource(path+"TestCIM10UrgPage0.htm");
 //textBrowser_Cim10Menu->setPaletteBackgroundPixmap(image );
 //textBrowser_Cim10Pages->setPaletteBackgroundPixmap(image );

}
//--------------------------------- Utf8_Query ------------------------------------------------------------
QString FormDlgCim10Multi::Utf8_Query(QSqlQuery &cur, int field)
{QCString     cString  = cur.value(field).toCString ();
 char *ptr             =  cString.data();
 if (CGestIni::IsUtf8( ptr ))
    {return QString::fromUtf8 ( ptr );
    }
 else
    {return QString(ptr) ;
    }

}
//-----------------------------------------------------  setOnglet -------------------------------------------
void FormDlgCim10Multi::setOnglet(int onglet)
{tabWidgetCim10->setCurrentPage (  onglet );
 switch(onglet)
 {case 0: lineEditAutolcatorLibelle->setFocus(); break;
  case 2: lineEditUrgFind1->setFocus();          break;
 }
}
//-----------------------------------------------------  setListCode -------------------------------------------
void FormDlgCim10Multi::setListCode(const QString &str_listCode)
{ QStringList listCode = QStringList::split("@@",str_listCode);   // str += item->text(0) + "::"+item->text(1)+"@@";
  for ( QStringList::Iterator it = listCode.begin(); it != listCode.end(); ++it )
      {QString libelle = *it;
       int p = libelle.find("::");
       if (p != -1)
          { AddItemToListChoix(libelle.left(p), libelle.mid(p+2));
          }
     }
}
//----------------------------------------------------- dataBaseSet -------------------------------------------
void  FormDlgCim10Multi::dataBaseSet(QSqlDatabase  *pQSqlDatabase)
    {
        m_DataBase    = pQSqlDatabase;
        m_DriverName  = m_DataBase->driverName();
        m_BaseName    = m_DataBase->databaseName();
        m_UserName    = m_DataBase->userName();
        m_PassWord    = m_DataBase->password();
        m_HostName    = m_DataBase->hostName();
       if ( ! m_DataBase->isOpen() ) m_DataBase->open();
    }

//-----------------------------------------------------  BaseConnect -------------------------------------------
QSqlDatabase*  FormDlgCim10Multi::BaseConnect(const char* driver,        // nom du driver: "QODBC3" "QMYSQL3" "QPSQL7"
                                              const char* DataBaseName,  // nom de la base: si QODBC3 -> nom de la source de données (userDSN)
                                              const char* user,          // = "root"
                                              const char* password,      // = ""
                                              const char* hostname,      // = "localhost"
                                              QString*    errMess)
    {

        QSqlDatabase *newDB = QSqlDatabase::addDatabase( driver , CIM10_DATA_BASE_NAME);
        if (newDB<=0) return 0;
        newDB->setDatabaseName( DataBaseName );
        newDB->setUserName( user );
        newDB->setPassword( password );
        newDB->setHostName( hostname);
        if ( ! newDB->open() )
           {QString qstr = "";
            qstr += "Failed to open database: " + QString(driver) + "  " + QString(DataBaseName) + "\r\n" +
                     newDB->lastError().driverText() + "\r\n" + newDB->lastError().databaseText();

            if (errMess) *errMess = qstr;
            else          qWarning(qstr);
            return 0;
           }
        m_DriverName  = driver;
        m_BaseName    = DataBaseName;
        m_UserName    = user;
        m_PassWord    = password;
        m_HostName    = hostname;
        m_DataBase    = newDB;
         if ( ! m_DataBase->isOpen() ) m_DataBase->open();
        return newDB;
    }

//-----------------------------------------------------  destroy -------------------------------------------
void FormDlgCim10Multi::destroy()
{if (m_DataBase)
    {if (m_DataBase->isOpen()) m_DataBase->close();
    }
}
//==================================== ONGLET HTML =====================================================================================
//----------------------------------------------- Load_HTML_Files ----------------------------------------------------------------------
void FormDlgCim10Multi::Load_HTML_Files()
{QString path = G_pCApp->m_PathGlossaire  + QDir::separator() + "CIM10Urg"+ QDir::separator();
 textBrowser_Cim10Menu->mimeSourceFactory()->setFilePath( path );
 textBrowser_Cim10Pages->mimeSourceFactory()->setFilePath( path );
 //......... charger la page de menu ..............................
 QString text="";
 CGestIni::Param_UpdateFromDisk(path + "TestCIM10UrgMenu.htm", text);
 textBrowser_Cim10Menu->setText(text);
 //......... charger la premiere page cible ..............................
 Load_HTML_Target_Page( path + "TestCIM10UrgPage0.htm") ;
}

//----------------------------------------------- Load_HTML_Target_Page ----------------------------------------------------------------------
void FormDlgCim10Multi::Load_HTML_Target_Page(const QString &target_page)
{QString text="";
 CGestIni::Param_UpdateFromDisk(target_page, text);
 textBrowser_Cim10Pages->setText(text);

}

//-----------------------------------------------------  textBrowser_Cim10Menu_anchorClicked -------------------------------------------
void FormDlgCim10Multi::textBrowser_Cim10Menu_anchorClicked( const QString &, const QString &link_in )
{QString link(link_in);
 QString path = G_pCApp->m_PathGlossaire + QDir::separator() + "CIM10Urg"+ QDir::separator();

 int pos = link.findRev('#');
 if (pos != -1) link= link.left(pos);
 link.remove(0,1);
 Load_HTML_Target_Page(path + link) ;
}

//-----------------------------------------------------  textBrowser_Cim10Pages_anchorClicked -------------------------------------------
void FormDlgCim10Multi::textBrowser_Cim10Pages_anchorClicked( const QString &, const QString &link_in )
{QString link(link_in);
 link.remove(0,1);
 int pos = link.findRev('|');
 if (pos != -1)
    {new QListViewItem( listViewCim10_Choix,      // inserer dans la liste de choix
                        link.left(pos),            // libelle
                        link.mid(pos+1)            // code CIM10
                      );
    }
}

//-----------------------------------------------------  pushButtonRemoveFromChoix_clicked -------------------------------------------
void FormDlgCim10Multi::pushButtonMakeHTML_clicked()
{QString path = G_pCApp->m_PathGlossaire + QDir::separator() + "CIM10Urg"+ QDir::separator();
 CIM10SrcFileRPU_To_Html(path + "CodesCIM10Urg.txt", path );
 Load_HTML_Files();
}

//-------------------------------------- CIM10SrcFileRPU_To_Html ----------------------------------------------------------------------------
int FormDlgCim10Multi::CIM10SrcFileRPU_To_Html(QString  path_src,  QString &pathDst)
{int i;
 QString text="";
 //................ charger le texte .....................................................
 CGestIni::Param_UpdateFromDisk(path_src, text);
 text.replace(':',',');  // sinon PB lors liens html
 int ok = 0;
 //............. Lire le texte source en memoire.......................................
 char        *txt_src   = (char*)(const char*) text;  // pas beau mais permet de recuperer code deja ecrit
 long        len_file   = text.length();
 char        *txt_end   = txt_src + len_file;
 if (txt_src==0||len_file==0)                                                                       return 0;

 //........................ verifier validité du fichier ...........................
 char *pt          = txt_src;
 char *txt         = txt_src;
 if (len_file>=13)
    {while(pt < txt_end && ok==0)
       {//.... le premier  '\t'  doit etre suivi de "Code CIM10"
        if (*pt=='\t'&&  strncmp(pt+1,"Code CIM10", 10)==0) ok=1;
        pt++;
       }
    }
 if (ok==0) {                                                                                        return 0;}

 //.......... se positionner sur premier code significatif .........................
 //                (en fait sauter la premiere ligne)
 while(pt < txt_end &&  pt[0]!='\r' && pt[0]!='\n') pt++;   // aller en fin de ligne
 while(pt < txt_end && (pt[0]=='\r' || pt[0]=='\n'))pt++;   // passer les fins de ligne
 char *txt_cod = pt;

 //.............. Ecriture du menu frame à gauche ..................................................
 QFile         *fdstMenu = new QFile( pathDst + "TestCIM10UrgMenu.htm");            if (fdstMenu==0) return 0;
 if ( !fdstMenu->open( IO_WriteOnly ) )                                          {delete fdstMenu;   return 0;}

 QString buf_Menu  = "<html>\r\n\r\n<head>\r\n<meta http-equiv = \"Content-Type\"content          = \"text/html; charset=iso-8859-1\">\r\n";
         buf_Menu += "<meta name       = \"GENERATOR\" content=\"DrTux\">\r\n";
         buf_Menu += "<title>MenuCIM10Urg</title>\r\n</head>\r\n";
         buf_Menu += "<body background=\"cuir_marron_clair.gif\" bgcolor=\"#FFFFFF\">\r\n<left>\r\n<ul\r\n>";
 fdstMenu->writeBlock ( buf_Menu, buf_Menu.length() );

 //.............. Parsing 1 du texte source et relever les titres du menu ..........
 //                          car peuvent etre en desordre
 QStringList MenuTitre;
 pt          = txt_cod;
 txt         = txt_cod;
 while(pt < txt_end)
  {if (*pt=='\t')
      {QString str = QString::fromLatin1(txt, pt-txt );
       if (MenuTitre.findIndex(str)==-1)
          {MenuTitre.append(str);
          }
       while(pt < txt_end &&  pt[0]!='\r' && pt[0]!='\n') pt++;   // aller en fin de ligne
       while(pt < txt_end && (pt[0]=='\r' || pt[0]=='\n'))pt++;   // passer les fins de ligne
       txt   = pt;                                                // texte sur nouveau debut de ligne
      }
   else pt++;
  }

 //.............. ecriture des items de menus du frame....................................
 int nb_titres = MenuTitre.count();
 for (i=0; i<nb_titres; i++)         // pour chaque item de menu
     {//........... creer code HTML .....................
      buf_Menu    = "<!-- ....................... ";
      buf_Menu   +=  MenuTitre[i] +" .................................... -->\r\n";
      buf_Menu   +=  "<li><font color=\"#0000FF\" size=\"3\" face=\"Arial\"><U>\r\n";
      buf_Menu   +=  "   <a href=\"#TestCIM10UrgPage"                      +
                     QString::number(i)                                    +
                     ".htm#debut\" target=\"CadrePrincipal\">"             +
                     MenuTitre[i][0].upper() + MenuTitre[i].mid(1).lower() +     // premier caractere en majuscule ,c'est plus jouli
                     "</a> \r\n";
      buf_Menu   +=  "    </U></font>\r\n<br></li>\r\n";
      fdstMenu->writeBlock ( buf_Menu, buf_Menu.length() );
     }
 buf_Menu  ="</ul></left>\r\n</body>\r\n</html>\r\n";
 fdstMenu->writeBlock ( buf_Menu, buf_Menu.length() );
 delete fdstMenu;


 //.............. Parsing 2 du texte source et creation des fichiers de page ..........
 //               pour chaque titre du menu frame creer un fichier page correspondant
 for (i=0; i<nb_titres; i++)
     {QString    name_Page   = "";
                 name_Page   = name_Page  + "TestCIM10UrgPage" + QString::number(i) + ".htm";
      QFile        *fdstPage = new QFile( pathDst + name_Page);
      if (fdstPage && fdstPage->open( IO_WriteOnly ))
         {QString buf_Page  =  "<html>\r\n\r\n<head>\r\n<meta http-equiv = \"Content-Type\" content          = \"text/html; charset=iso-8859-1\">\r\n";
                  buf_Page +=  "<meta name       = \"GENERATOR\" content=\"DrTux\">\r\n";
                  buf_Page +=  "<title>" + name_Page + "</title>\r\n</head>\r\n";
                  buf_Page +=  "<body background=\"cuir_marron_clair.gif\" bgcolor=\"#FFFFFF\">\r\n";
                  buf_Page +=  "\r\n<center>\r\n<a name=\"debut\"> </a>\r\n";
                  buf_Page +=  "<font color=\"#0088FF\" size=\"3\" face=\"Arial\">" + MenuTitre[i].upper() + "</font>\r\n</center>\r\n";
                  buf_Page +=  "<br><left><font color=\"#0000FF\" size=\"2\" face=\"Arial\">\r\n\r\n";

          fdstPage->writeBlock ( buf_Page, buf_Page.length() );
          //............. parsing du texte à la recherche des item correspondant à ce titre .........................
          pt          = txt_cod;
          txt         = txt_cod;
          while(pt < txt_end)
             {QString code, libele;
              if (*pt=='\t')
                 {//.......... relever texte du titre ................
                  if (MenuTitre[i]==QString::fromLatin1(txt, pt-txt ))
                     {//.......... relever texte du code ................
                      pt++;
                      txt  = pt;                                // texte du code
                      while (*pt &&  pt[0]!='\t') pt++;         // aller au prochain '\t' texte du libele;
                      code = QString::fromLatin1(txt, pt-txt ); // noter le code
                      //.......... relever texte du libele ................
                      txt  = pt + 1;                                    // texte du libele
                      while(*pt &&  pt[0]!='\r' && pt[0]!='\n') pt++;   // aller en fin de ligne

                      //........... placer un antislash devant les appostrophes ........
                      //            sinon erreur java !!
                      libele      = QString::fromLatin1(txt, pt-txt ); // noter le libele
                      libele      = libele.left(1).upper() + libele.mid(1).lower();
                      while(*pt && (pt[0]=='\r' || pt[0]=='\n'))pt++;   // passer les fins de ligne

                      //.......... ecrire l'item ..............................................
                      buf_Page  = "<font color=\"#0000FF\" size=\"3\" face=\"Arial\">\r\n   <a href=\"#";
                      buf_Page += libele + "|"+code  + "\" >\r\n";
                      buf_Page += "   "  + libele  +
                                  " [</font><font color=\"#880000\" size=\"2\" face=\"Arial\"> " + code + "</font>]\r\n   </a> <br>\r\n";
                      fdstPage->writeBlock ( buf_Page, buf_Page.length() );
                     }
                  else
                     {while(*pt &&  pt[0]!='\r' && pt[0]!='\n') pt++;   // aller en fin de ligne
                      while(*pt && (pt[0]=='\r' || pt[0]=='\n'))pt++;   // passer les fins de ligne
                     }
                  txt      = pt;
                 }
              else
                 {pt++;
                 }
             } //endwhile(*pt)
          buf_Page  = "</font></left>\r\n\r\n</body>\r\n</html>\r\n";
          fdstPage->writeBlock ( buf_Page, buf_Page.length() );
          delete fdstPage;
         } //endif (fdstPage)
     }// end for (int i=0; i<nb_titres; i++)
 return 1;
}//AfficheBlankSignal



//==================================== ONGLET URGENCES =================================================================
//-----------------------------------------------------  CIM10GetLibellesList -------------------------------------------
long FormDlgCim10Multi::CIM10GetUrgencesList()
{//............. Lire le texte source en memoire.......................................
 QString    path_Text   = G_pCApp->m_PathGlossaire + CHEMIN_CIM10_URG +  "/CodesCIM10Urg.txt";
 QString txt_src;
 CGestIni::Param_UpdateFromDisk(path_Text, txt_src);
 //................. remplir le tableau de CString .......................

 char *pt          = (char*)(const char*)txt_src;
 char *txt         = pt;
 while(*pt)
   {QString code, libele;
    if (*pt=='\t')
       {//.......... relever texte du titre ................
        //*pt  = 0;
        //*pt  ='\t';
        //.......... relever texte du code ................
        ++pt;
        txt  = pt;                         // texte du code
        while (*pt &&  pt[0]!='\t') ++pt;  // aller au prochain '\t' texte du libele;
        *pt  = 0;                          // zero de fin
        code = txt;                        // noter le code
        *pt  ='\t';                        // restituer '\t'
        //.......... relever texte du libele ................
        txt  = pt + 1;                                    // texte du libele
        while(*pt &&  pt[0]!='\r' && pt[0]!='\n') ++pt;   // aller en fin de ligne
        char sav      = *pt; *pt = 0;                     // y placer le zero de fin

        //........... Mettre majuscule en premiere position .........
        //            dans le non qui sera affiché
        char car[2]   = {0,0};
        car[0]        = *txt;
        //_strupr(car);
        libele        = (txt+1);
        libele        = libele.lower ();
        libele.insert(0, car);
        *pt           = sav;                                   // restituer '\r' ou '\n'
        while(*pt && (pt[0]=='\r' || pt[0]=='\n'))++pt;        // passer les fins de ligne
        m_StringList.append(libele + '\t' + code);
       } //endif (*pt=='\t')
     else
       {++pt;
       }
   } //endwhile(*pt)
 return 0;
}


//----------------------------------------------- FiltrerListe -----------------------------------------------
long FormDlgCim10Multi::FiltrerListe( QListView *pQlistView, QString str1, QString str2)
{
 pQlistView->clear();
 //............. deux champs vides.....................
 if (str1 == "" && str2 =="")
    {for ( QStringList::Iterator it = m_StringList.begin(); it != m_StringList.end(); ++it )
         {AddItemToListViewUrg (listViewCim10_Urgences, *it);
         }
    }
 //............. deux champs pleins.....................
 else if (str1 != "" && str2 !="")
    {for ( QStringList::Iterator it = m_StringList.begin(); it != m_StringList.end(); ++it )
         {if ((*it).contains (str1, FALSE) &&  (*it).contains (str2, FALSE))
             {AddItemToListViewUrg (listViewCim10_Urgences, *it);
             }
         }
    }
 //.............  1er champ plein.....................
 else if (str1 != "")
    {for ( QStringList::Iterator it = m_StringList.begin(); it != m_StringList.end(); ++it )
         {if ((*it).contains (str1, FALSE))
             {AddItemToListViewUrg (listViewCim10_Urgences, *it);
             }
         }
    }
 //.............  2ème champ plein.....................
 else
    {for ( QStringList::Iterator it = m_StringList.begin(); it != m_StringList.end(); ++it )
         {if ((*it).contains (str2, FALSE))
             {AddItemToListViewUrg (listViewCim10_Urgences, *it);
             }
         }
    }
 return 0;
}

//----------------------------- AddItemToListViewUrg ----------------------------------------------------------
QListViewItem *FormDlgCim10Multi::AddItemToListViewUrg (QListView *pQlistView, QString itemText)
{int pos = itemText.find('\t');  // rechercher le separateur entre le code et le libelle
 if (pos == -1) return 0;

 QListViewItem *element = new QListViewItem( pQlistView,
                                             itemText.left(pos),   // libelle
                                             itemText.mid(pos+1)   // code
                                           );
 return element ;
}

//-----------------------------------------------------  lineEditUrgFind1_textChanged -------------------------------------------
void FormDlgCim10Multi::lineEditUrgFind1_textChanged( const QString &str )
{QString str2=lineEditUrgFind2->text();
 FiltrerListe(listViewCim10_Urgences, str, str2);
}

//-----------------------------------------------------  lineEditUrgFind2_textChanged -------------------------------------------
void FormDlgCim10Multi::lineEditUrgFind2_textChanged( const QString &str )
{QString str1=lineEditUrgFind1->text();
 FiltrerListe(listViewCim10_Urgences, str1, str);
}

//-----------------------------------------------------  listViewCim10_Urgences_returnPressed -------------------------------------------
void FormDlgCim10Multi::listViewCim10_Urgences_returnPressed( QListViewItem * )
{if (listViewCim10_Urgences->childCount () !=1)  return;   // on accepte return que lorsque un seul élément est affiché
 QListViewItem *qlistViewItem = listViewCim10_Urgences->currentItem ();   // si pas d'element courant cassos
 if (qlistViewItem==0)                           return;

  new QListViewItem( listViewCim10_Choix,      // inserer dans la liste de choix
                     qlistViewItem->text(0),   // libelle
                     qlistViewItem->text(1)    // code CIM10
                   );
 accept();      // ok on se casse avec tout ça
}

void FormDlgCim10Multi::lineEditUrgFind1_returnPressed()
{listViewCim10_Urgences_returnPressed( 0 );

}


void FormDlgCim10Multi::lineEditUrgFind2_returnPressed()
{listViewCim10_Urgences_returnPressed( 0 );

}
//==================================== ONGLET LIBELLES =================================================================

 #define NUM_LIBELLE_FIELD_FR_OMS     5
 #define NUM_LIBELLE_FIELD_SID        1

 #define NUM_MASTER_FIELD_SID         0
 #define NUM_MASTER_FIELD_CODE        1
 #define NUM_MASTER_FIELD_ID1         6
 #define NUM_MASTER_FIELD_ID2         7
 #define NUM_MASTER_FIELD_ID3         8
 #define NUM_MASTER_FIELD_ID4         9
 #define NUM_MASTER_FIELD_ID5         10
 #define NUM_MASTER_FIELD_ID6         11
 #define NUM_MASTER_FIELD_ID7         12

//-----------------------------------------------------  lineEditAutolcatorLibelle_textChanged -------------------------------------------
void FormDlgCim10Multi::lineEditAutolcatorLibelle_textChanged( const QString & )
{CIM10GetLibellesList(listViewCim10_Libelles, lineEditAutolcatorLibelle->text(), lineEditAutolcatorLibelle_2->text(), 0, 0);
}
//-----------------------------------------------------  lineEditAutolcatorLibelle_textChanged -------------------------------------------
void FormDlgCim10Multi::lineEditAutolcatorLibelle_2_textChanged( const QString & )
{CIM10GetLibellesList(listViewCim10_Libelles, lineEditAutolcatorLibelle->text(), lineEditAutolcatorLibelle_2->text(), 0, 0);
}

//-----------------------------------------------------  CIM10GetLibellesList -------------------------------------------
long FormDlgCim10Multi::CIM10GetLibellesList(  QListView *pQlistView,
                                               QString    mot_cle_saisie1,
                                               QString    mot_cle_saisie2,
                                               QLabel    *statutMess, /* = 0  */
                                               QLabel    *errMess     /* = 0  */
                                             )
{//................. Preparer la requete .....................................
  if (m_DataBase==0)
     {if (errMess) errMess->setText(tr("CMoteurBase::CIM10GetLibelleList(): m_DataBase = 0 (erreur lors creation) "));
      return 0;
     }
  if (m_DataBase->isOpen()==FALSE && m_DataBase->open()==FALSE)
     {if (errMess) errMess->setText(tr("CMoteurBase::CIM10GetLibelleList(): Echec de l'ouverture de la base"));
      return 0;
     }
  int     nb = 200;
  int      i = 0;

  QString requete_libelle;
  requete_libelle             = QString("SELECT * FROM ")     + "libelle WHERE ";
  requete_libelle            += QString("valid")              + " ='"              + "1" + "' AND ";

  if (mot_cle_saisie1.length() && mot_cle_saisie2.length())
     {requete_libelle       +=  "(  FR_OMS LIKE'%" + mot_cle_saisie1 + "%" + mot_cle_saisie2 + "%' ";
      requete_libelle       +=  "OR FR_OMS LIKE'%" + mot_cle_saisie2 + "%" + mot_cle_saisie1 + "%')";
     }
  else if (mot_cle_saisie1.length())
     {requete_libelle       +=  "FR_OMS LIKE '%" + mot_cle_saisie1 + "%'";
     }
  else if (mot_cle_saisie2.length())
     {requete_libelle       +=  "FR_OMS LIKE '%" + mot_cle_saisie2 + "%'";
     }
   else
     {requete_libelle        += QString("FR_OMS LIKE 'a%'");
     }

  QSqlQuery query(requete_libelle , m_DataBase );

  if (query.isActive())
     {pQlistView->clear();
      while (query.next()&&i<nb)
         {QListViewItem *element = new QListViewItem( pQlistView,
                                                      Utf8_Query(query, NUM_LIBELLE_FIELD_FR_OMS ),
                                                      " ",
                                                      Utf8_Query(query, NUM_LIBELLE_FIELD_SID    )
                                                    );
          if (element)
             {i++;
             }
         }
     }
  //....................... sortir le message d'erreur si besoin ..........................................
  if (statutMess)
     { QString qstr1;
       qstr1.sprintf(tr("Trouvés : %i sur: %i"), i, nb);
       statutMess->setText(qstr1);
     }
  m_DataBase->close();
  return i;
}



//==================================== ONGLET RUBRIQUES =================================================================
//-----------------------------------------------------  CIM10GetLibelleList -------------------------------------------

long FormDlgCim10Multi::CIM10GetRubriquesList( QListView *pQlistView,
                                               QLabel    *statutMess, /* = 0  */
                                               QLabel    *errMess     /* = 0  */
                                             )
{//................. Preparer la requete .....................................
  if (m_DataBase==0)
     {if (errMess) errMess->setText(tr("CMoteurBase::CIM10GetLibelleList(): m_DataBase = 0 (erreur lors création) "));
      return 0;
     }
  if (m_DataBase->isOpen()==FALSE && m_DataBase->open()==FALSE)
     {if (errMess) errMess->setText(tr("CMoteurBase::CIM10GetLibelleList(): Echec de l'ouverture de la base"));
      return 0;
     }
  QString requete_master;
  QString requete_libelle;

  requete_master        = QString("SELECT * FROM ")    + "master" + " WHERE ";
  requete_master       += QString("level")             + " ='"              + "1" + "'";

  QSqlQuery query(requete_master , m_DataBase );

  //................ scanner les enregistrements ................................................
  //                 pour remplir la listview
  int i  = 0;
  int nb = 0;
  //.................. si la requète a un resultat ..............................................
  if (query.isActive())
     {pQlistView->clear();
      while (query.next())
        {QString qsSID          = query.value(NUM_MASTER_FIELD_SID).toString();
         QString qscode         = query.value(NUM_MASTER_FIELD_CODE).toString();
         requete_libelle        = QString("SELECT * FROM ")    + "libelle" + " WHERE ";
         requete_libelle       += QString("SID")               + " ='"              + qsSID + "'";
         //requete_libelle       += QString(" ORDER BY ")        + "FR_OMS";
         QSqlQuery queryLibelle(requete_libelle , m_DataBase );
         if (queryLibelle.isActive() && queryLibelle.next())
            {//............... on rempli la listbox selon son nombre de colonnes .....................
              new QListViewItem( pQlistView,
                                 Utf8_Query(queryLibelle, NUM_LIBELLE_FIELD_FR_OMS ),
                                 qscode, qsSID
                               );
             i++;
            }
         nb++;
        } //end while (pSqlQueryMaster->next())
     } //endif (pSqlQueryMaster &&pSqlQueryMaster->isActive())
  //ListViewPatient->setCurrentItem(0);   // se placer sur le premier enregistrement de la liste
  //....................... sortir le message d'erreur si besoin ..........................................
  if (statutMess)
     { QString qstr1;
       qstr1.sprintf(tr("Trouvés : %i sur: %i"), i, nb);
       statutMess->setText(qstr1);
     }
  m_DataBase->close();
  return i;
}

//-----------------------------------------------------  CIM10GetSubLevelLibelleList -------------------------------------------
long FormDlgCim10Multi::CIM10GetSubLevelLibelleList(  QListViewItem *qlistViewItem,       // pere
                                                      QString        ID_Father,
                                                      QLabel        *statutMess, /* = 0  */
                                                      QLabel        *errMess     /* = 0  */
                                                    )
{ if (qlistViewItem==0)                return 0;
  if (qlistViewItem->childCount ()>0)
     {if (qlistViewItem->isOpen()) qlistViewItem->setOpen (FALSE);
      else                         qlistViewItem->setOpen (TRUE);
      return 0;
     }
  //................. Preparer la requete .....................................
  if (m_DataBase==0)
     {if (errMess) errMess->setText(tr("CMoteurBase::CIM10GetLibelleList(): m_DataBase = 0 (erreur lors création) "));
      return 0;
     }
  if (m_DataBase->isOpen()==FALSE && m_DataBase->open()==FALSE)
     {if (errMess) errMess->setText(tr("CMoteurBase::CIM10GetLibelleList(): Echec de l'ouverture de la base"));
      return 0;
     }
  QString level = "";
  QString id    = "id" + level.setNum (qlistViewItem->depth()+1);
  level.setNum (qlistViewItem->depth()+2);
  QString requete_master;
  QString requete_libelle;

  requete_master        = QString("SELECT * FROM ")    + "master" + " WHERE ";
  requete_master       += QString("level")             + " ='"              + level     + "' AND ";
  requete_master       += id                           + " ='"              + ID_Father + "'";

  QSqlQuery  queryMaster(requete_master , m_DataBase );

  //................ scanner les enregistrements ................................................
  //                 pour remplir la listview
  int i  = 0;
  int nb = 0;
  //.................. si la requète a un resultat ..............................................
  if (queryMaster.isActive())
     {while (queryMaster.next())
        {QString qsSID          = queryMaster.value(NUM_MASTER_FIELD_SID).toString();
         QString qscode         = queryMaster.value(NUM_MASTER_FIELD_CODE).toString();
         requete_libelle        = QString("SELECT * FROM ")    + "libelle" + " WHERE ";
         requete_libelle       += QString("SID")               + " ='"              + qsSID + "'";
         //requete_libelle       += QString(" ORDER BY ")        + "FR_OMS";
         QSqlQuery queryLibelle(requete_libelle , m_DataBase );
         if (queryLibelle.isActive() && queryLibelle.next())
            {new QListViewItem( qlistViewItem,
                                 Utf8_Query(queryLibelle, NUM_LIBELLE_FIELD_FR_OMS ),
                                 qscode, qsSID
                                );
             i++;
            }
         nb++;
        } //end while (pSqlQueryMaster->next())
     } //endif (pSqlQueryMaster &&pSqlQueryMaster->isActive())
  if (qlistViewItem->childCount ()>0) qlistViewItem->setOpen (TRUE);

  //....................... sortir le message d'erreur si besoin ..........................................
  if (statutMess)
     { QString qstr1;
       qstr1.sprintf(tr("Trouvés : %i sur: %i"), i, nb);
       statutMess->setText(qstr1);
     }
  m_DataBase->close();
  return i;
}

//-----------------------------------------------------  listViewCim10_rubriques_clicked -------------------------------------------
void FormDlgCim10Multi::listViewCim10_rubriques_clicked( QListViewItem * qlistViewItem )
{if (qlistViewItem==0) return;
 CIM10GetSubLevelLibelleList(qlistViewItem,       // pere
                             qlistViewItem->text(2),
                             0, /* = 0  */
                             0 /* = 0  */
                            );
}

//-----------------------------------------------------  tabWidgetCim10_currentChanged -------------------------------------------
void FormDlgCim10Multi::tabWidgetCim10_currentChanged( QWidget * pQWidget)
{pushButtonMakeHTML->hide();
 switch(tabWidgetCim10->indexOf ( pQWidget))
    {case TAB_LIBELLE:
          if (listViewCim10_Libelles->childCount ()<=0)
             {CIM10GetLibellesList(listViewCim10_Libelles, QString(""), QString(""), 0, 0);
             }
          break;
     case TAB_RUBRIQUE:
          if (listViewCim10_rubriques->childCount ()<=0)
             {CIM10GetRubriquesList( listViewCim10_rubriques,0,0);
              lineEditAutolcatorLibelle->setFocus ();
             }
          break;
     case TAB_URGENCE:
          if (m_StringList.isEmpty())
             {CIM10GetUrgencesList();
              FiltrerListe(listViewCim10_Urgences, QString("") , QString(""));  // tout afficher
              lineEditUrgFind1->setFocus ();
             }
          break;
     case TAB_HTML:
         pushButtonMakeHTML->show();
         if (textBrowser_Cim10Menu->text().length()==0 )Load_HTML_Files();
         break;
    }
}


//-----------------------------------------------------  listViewCim10_Libelles_doubleClicked -------------------------------------------
void FormDlgCim10Multi::listViewCim10_Libelles_doubleClicked( QListViewItem *qlistViewItem )
{QWidget * pQWidget = tabWidgetCim10->currentPage();
 if (pQWidget==0) return;
 GetDlgListCode(tabWidgetCim10->indexOf ( pQWidget), qlistViewItem);
 if (m_Code!= "")
    {AddItemToListChoix(m_Libelle, m_Code);
    }
 accept();
}


//-----------------------------------------------------  listViewCim10_rubriques_doubleClicked -------------------------------------------
void FormDlgCim10Multi::listViewCim10_rubriques_doubleClicked( QListViewItem *qlistViewItem )
{QWidget * pQWidget = tabWidgetCim10->currentPage();
 if (pQWidget==0) return;
 GetDlgListCode(tabWidgetCim10->indexOf ( pQWidget), qlistViewItem);
 if (m_Code!= "")
    {AddItemToListChoix(m_Libelle, m_Code);
    }
 accept();
}


//-----------------------------------------------------  listViewCim10_Urgences_doubleClicked -------------------------------------------
void FormDlgCim10Multi::listViewCim10_Urgences_doubleClicked( QListViewItem *qlistViewItem )
{QWidget * pQWidget = tabWidgetCim10->currentPage();
 if (pQWidget==0) return;
 GetDlgListCode(tabWidgetCim10->indexOf ( pQWidget), qlistViewItem);
 if (m_Code!= "")
    {AddItemToListChoix(m_Libelle, m_Code);
    }
 accept();
}

//-----------------------------------------------------  GetDlgListCode -------------------------------------------
QListViewItem *FormDlgCim10Multi::GetDlgListCode(int tab_index, QListViewItem *qlistViewItem)
{m_Code="";
 switch(tab_index)
    {case TAB_LIBELLE:
          if (qlistViewItem==0) qlistViewItem = listViewCim10_Libelles->currentItem ();
          if (qlistViewItem==0) return 0;
          m_Libelle = qlistViewItem->text(0);
          m_Code    = SID_to_CIM10(qlistViewItem->text(2), 0 );
          break;
     case TAB_RUBRIQUE:
          if (qlistViewItem==0) qlistViewItem = listViewCim10_rubriques->currentItem ();
          if (qlistViewItem==0) return 0;
          m_Code    = qlistViewItem->text(1);
          m_Libelle = qlistViewItem->text(0);
          break;
     case TAB_URGENCE:
          if (qlistViewItem==0) qlistViewItem = listViewCim10_Urgences->currentItem ();
          if (qlistViewItem==0) return 0;
          m_Code    = qlistViewItem->text(1);
          m_Libelle = qlistViewItem->text(0);
          break;
    }
 return qlistViewItem;
}

//----------------------------------------------------- AddItemToListChoix --------------------------------------
void FormDlgCim10Multi::AddItemToListChoix(const QString &libelle, const QString &code)
{QListViewItem element = new QListViewItem( listViewCim10_Choix,
                                            libelle,
                                            code
                                           );

}

//-----------------------------------------------------  SID_to_CIM10 -------------------------------------------
QString FormDlgCim10Multi::SID_to_CIM10(QString qsSID,  QLabel *errMess )
{  QString qscode("");
  //................. verifier validité de la base .....................................
  if (m_DataBase==0)
     {if (errMess) errMess->setText(tr("CMoteurBase::CIM10GetLibelleList(): m_DataBase = 0 (erreur lors création) "));
      return qscode;
     }
  if (m_DataBase->isOpen()==FALSE && m_DataBase->open()==FALSE)
     {if (errMess) errMess->setText(tr("CMoteurBase::CIM10GetLibelleList(): Echec de l'ouverture de la base"));
      return qscode;
     }
  //.................. preparer la requete .....................................................
  QString requete_master;
  requete_master        = QString("SELECT * FROM master WHERE SID = '") + qsSID + "'";

  QSqlQuery* pSqlQueryMaster = new QSqlQuery(requete_master , m_DataBase );
  //.................. si la requète a un resultat ..............................................
  if (pSqlQueryMaster && pSqlQueryMaster->isActive() && pSqlQueryMaster->next())
     {qscode         = pSqlQueryMaster->value(NUM_MASTER_FIELD_CODE).toString();
     } //endif (pSqlQueryMaster &&pSqlQueryMaster->isActive())
  if (pSqlQueryMaster) delete  pSqlQueryMaster;
  m_DataBase->close();
  return qscode;
}

//-----------------------------------------------------  pushButton_AddToChoix_clicked -------------------------------------------
void FormDlgCim10Multi::pushButton_AddToChoix_clicked()
{QWidget * pQWidget = tabWidgetCim10->currentPage();
 if (pQWidget==0)              return;
 GetDlgListCode(tabWidgetCim10->indexOf ( pQWidget), 0);
 if (m_Code!= "")
    {AddItemToListChoix(m_Libelle, m_Code);
    }
}


//-----------------------------------------------------  pushButtonRemoveFromChoix_clicked -------------------------------------------
void FormDlgCim10Multi::pushButtonRemoveFromChoix_clicked()
{QListViewItem *element = listViewCim10_Choix->currentItem ();
 if (element == 0) return;
 delete element;
}

