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


//----------------------------------------------- init -----------------------------------------------
void FormDlgActesClassants::init()
{//............. Lire le texte source en memoire...........................................
 QString    path_Text   = G_pCApp->m_PathGlossaire + CHEMIN_CIM10_URG + QDir::separator() + "CodesActesUrg.txt";
 char        *txt_src   = CGenTools::LoadDataInBuffer(path_Text);
 if (txt_src==0)                                        return ;
 char *pt          = txt_src;
 char *txt         = txt_src;
 listView_ActesClassants->clear();
 while(*pt)
   {QString code, libele;
    if (*pt=='\t')
       {//.......... relever texte du libele ..............................................
        *pt    = 0;
        libele = txt;
        *pt    ='\t';
        //.......... relever texte du code ................................................
        pt++;
        txt         = pt;                                  // texte du code
        while(*pt &&  pt[0]!='\r' && pt[0]!='\n') pt++;    // aller en fin de ligne
        char sav    = *pt; *pt = 0;
        code        = txt;                                 // noter le code
        *pt         = sav;                                 // restituer '\r' ou '\n'
        while(*pt && (pt[0]=='\r' || pt[0]=='\n'))pt++;    // passer les fins de ligne
        //.......... ajouter l'item à la liste ............................................
        m_StringList.append(libele + '\t' + code);
        txt = pt;
       } //endif (*pt=='\t')
     else
       {pt++;
       }
   } //endwhile(*pt)
 delete[]txt_src;
 FiltrerListe( listView_ActesClassants, QString (""), QString (""));
}

//----------------------------------------------- FiltrerListe -----------------------------------------------
long FormDlgActesClassants::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 (listView_ActesClassants, *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 (listView_ActesClassants, *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 (listView_ActesClassants, *it);
             }
         }
    }
 //.............  2ème champ plein......................
 else
    {for ( QStringList::Iterator it = m_StringList.begin(); it != m_StringList.end(); ++it )
         {if ((*it).contains (str2, FALSE))
             {AddItemToListViewUrg (listView_ActesClassants, *it);
             }
         }
    }
 return 0;
}

//----------------------------- AddItemToListViewUrg ----------------------------------------------------------
QListViewItem *FormDlgActesClassants::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 ;
}

//-----------------------------------------------------  lineEditActesClassantsFind1_textChanged -------------------------------------------
void FormDlgActesClassants::lineEditActesClassantsFind1_textChanged( const QString &str )
{QString str2=lineEditActesClassantsFind2->text();
 FiltrerListe(listView_ActesClassants, str, str2);
}

//-----------------------------------------------------  lineEditActesClassantsFind2_textChanged -------------------------------------------
void FormDlgActesClassants::lineEditActesClassantsFind2_textChanged( const QString &str )
{QString str1=lineEditActesClassantsFind1->text();
 FiltrerListe(listView_ActesClassants, str1, str);
}

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

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

//-----------------------------------------------------  lineEditActesClassantsFind1_returnPressed -------------------------------------------
void FormDlgActesClassants::lineEditActesClassantsFind1_returnPressed()
{listView_ActesClassants_returnPressed(0);
 accept();      // ok on se casse avec tout ça
}

//-----------------------------------------------------  lineEditActesClassantsFind2_returnPressed -------------------------------------------
void FormDlgActesClassants::lineEditActesClassantsFind2_returnPressed()
{listView_ActesClassants_returnPressed(0);
 accept();      // ok on se casse avec tout ça
}

//-----------------------------------------------------  ButtAjouter -------------------------------------------
void FormDlgActesClassants::ButtAjouter(int nb)
{   if (listView_ActesClassants->childCount () ==-1)     return;   // si liste vide cassos
    QListViewItem *qlistViewItem = listView_ActesClassants->currentItem();
    if (qlistViewItem==0)                                return;   // si pas d'element courant

    QListViewItem *find = listView_Choix->findItem( qlistViewItem->text(0), 0 );
    if (find != 0) delete find;                                    // si existe deja l'effacer

    int nb_actes = listView_Choix->childCount ();                  // si maximum atteint cassos
    if (nb_actes>10)
       {//AfxMessageBox("Maximum possible de 10 actes atteint !!!!");
        return;
       }
    QListViewItem element = new QListViewItem( listView_Choix,                                        // inserer dans la liste de choix
                                               qlistViewItem->text(0),                                // libelle
                                               qlistViewItem->text(1)+ " X " + QString::number(nb)    // code UG
	                                      );
}

//-----------------------------------------------------  pushButton_AddToChoix_1_clicked -------------------------------------------
void FormDlgActesClassants::pushButton_AddToChoix_1_clicked()
{ButtAjouter(1);
}
//-----------------------------------------------------  pushButton_AddToChoix_2_clicked -------------------------------------------
void FormDlgActesClassants::pushButton_AddToChoix_2_clicked()
{ButtAjouter(2);
}
//-----------------------------------------------------  pushButton_AddToChoix_3_clicked -------------------------------------------
void FormDlgActesClassants::pushButton_AddToChoix_3_clicked()
{ButtAjouter(3);
}
//-----------------------------------------------------  pushButton_AddToChoix_4_clicked -------------------------------------------
void FormDlgActesClassants::pushButton_AddToChoix_4_clicked()
{ButtAjouter(4);
}
//-----------------------------------------------------  pushButton_AddToChoix_5_clicked -------------------------------------------
void FormDlgActesClassants::pushButton_AddToChoix_5_clicked()
{ButtAjouter(5);
}
//-----------------------------------------------------  pushButton_AddToChoix_6_clicked -------------------------------------------
void FormDlgActesClassants::pushButton_AddToChoix_6_clicked()
{ButtAjouter(6);
}
//-----------------------------------------------------  pushButton_AddToChoix_7_clicked -------------------------------------------
void FormDlgActesClassants::pushButton_AddToChoix_7_clicked()
{ButtAjouter(7);
}
//-----------------------------------------------------  pushButton_AddToChoix_8_clicked -------------------------------------------
void FormDlgActesClassants::pushButton_AddToChoix_8_clicked()
{ButtAjouter(8);
}
//-----------------------------------------------------  pushButton_AddToChoix_9_clicked -------------------------------------------
void FormDlgActesClassants::pushButton_AddToChoix_9_clicked()
{ButtAjouter(9);
}
//-----------------------------------------------------  pushButton_AddToChoix_10_clicked -------------------------------------------
void FormDlgActesClassants::pushButton_AddToChoix_10_clicked()
{ButtAjouter(10);
}

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

//-----------------------------------------------------  pushButtonOK_clicked -------------------------------------------
void FormDlgActesClassants::pushButtonOK_clicked()
{accept();
}

//-----------------------------------------------------  listView_ActesClassants_clicked -------------------------------------------
void FormDlgActesClassants::listView_ActesClassants_clicked( QListViewItem * )
{ButtAjouter(1);
}

//-----------------------------------------------------  listView_ActesClassants_doubleClicked -------------------------------------------
void FormDlgActesClassants::listView_ActesClassants_doubleClicked( QListViewItem * )
{//ButtAjouter(1);
 accept();
}

//-----------------------------------------------------  listView_Choix_clicked -------------------------------------------
void FormDlgActesClassants::listView_Choix_clicked( QListViewItem * qlistViewItem)
{if (qlistViewItem==0) return;
 QListViewItem *find = listView_ActesClassants->findItem( qlistViewItem->text(0), 0 );
 if (find == 0) return;
 listView_ActesClassants->setCurrentItem ( find );
 listView_ActesClassants->ensureItemVisible ( find );
}

