/****************************************************************************
** 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.
*****************************************************************************/
void Form2::init()
{
    //fichier fQuestion
    FILE * fQuestion;
    fQuestion=fopen("phrases","r");// fichier qui contient les phrases
    QTextIStream ficQuestion(fQuestion);//prend le fichier pour le lire
    QString question;
     noQuestion=0;
     while (!ficQuestion.atEnd())
     {
	 question=ficQuestion.readLine();// lit la ligne jusqu'au prochain
	 listQuestion.append(question); //ajout de nouvelle question
     }
     afficherQuestion();
     fclose(fQuestion);
}


void Form2::afficherQuestion()
{
   suite->setEnabled(false); //bloquage du bouton suite
    QString ligneQuestion=listQuestion[noQuestion];
    QStringList lChamps=QStringList::split(":",ligneQuestion,true);// coupage de chaque élément de la liste
    phrase1->setText(lChamps[0]); //1er champs de la phrase
    phrase3->setText(lChamps[1]);// 2ème champs de la phrase
    QStringList lLibBoutons=QStringList::split(",",lChamps[2]);
    bouton1->setText(lLibBoutons[0]);
    bouton2->setText(lLibBoutons[1]);
    bouton3->setText(lLibBoutons[2]);		
    bouton4->setText(lLibBoutons[3]);
    bouton5->setText(lLibBoutons[4]);
    bouton6->setText(lLibBoutons[5]);
    bouton7->setText(lLibBoutons[6]);
    bouton8->setText(lLibBoutons[7]);
    bouton9->setText(lLibBoutons[8]);
    bouton10->setText(lLibBoutons[9]);
    bouton11->setText(lLibBoutons[10]);
    bouton12->setText(lLibBoutons[11]);
    noRepCorrecte_ch=lChamps[3];
   
}

void Form2::cliquer()
{
    //récupération du bouton cliquer et affichage dans phrase 2
    QPushButton * bt = (QPushButton *) sender(); //pointeur sur un bouton
    QString motRecuperer= bt-> text();
    phrase2 -> setText(motRecuperer);
    if (motRecuperer==noRepCorrecte_ch)
    {
	//coloration de la réponse en vert si réponse juste	
	phrase2->setPaletteBackgroundColor(QColor(0,255,0));
	reussite->setText("Bravo tu es trés fort(e) !");
	suite->setEnabled(true); 
    }
    else
    {
	phrase2->setPaletteBackgroundColor(QColor(255,0,0));
	reussite->setText(" Retente ta chance !!");
	suite->setEnabled(false); 
    }
}

void Form2::phraseSuivante()
{
    reussite->setText("");
    phrase2->setText("?");
    phrase2->setPaletteBackgroundColor(QColor(188,160,208));
    noQuestion++;
    suite->setEnabled(false); 
    if (noQuestion<listQuestion.count())
    {
	afficherQuestion();
    }
   else
    {
	//affichage d'un message
	reussite->setText("Bravo, l'exercice a bien été réussi!!");
	//désactivation du bouton suivant
	suite->setEnabled(false);
    }
}
