Accueil > > > MiniChat/Client/mainwindow.cpp
QL-CHATROOM V 1.0
MiniChat/Client/mainwindow.cpp
Informations sur ce code source
Qualité ChatRoom v 1.0
Une application de chat, genre salon de chat avec un administrateur.
Programmer par C++, bibliothèque Qt architecture client/serveur.
Les sockets sont échangés entre les clients et le serveur par le protocole réseau T
Fichier : MiniChat/Client/mainwindow.cpp
Nombre de lignes : 372 lignes
Afficher ce fichier en plein écran
- /*
- Qualité ChatRoom a ChatRoom.
- Copyright (C) 2012 KHABALI Anas
-
- This program is free software: you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation, either version 3 of the License, or
- (at your option) any later version.
-
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with this program. If not, see <http://www.gnu.org/licenses/>.
-
- Contacte:
- Email: anas.khabali@gmail.com
- phone: +212676747779
- site : http://http://khabalisoft.e-monsite.com/
- */
-
- #include "MainWindow.h"
- #include "statut.h"
- #include <QRegExp>
- #include <QMessageBox>
- #include <QFont>
- #include <QTime>
- #include <QColor>
- #include <QColorDialog>
-
- MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
- {
- setupUi(this);
- setFixedSize(240,420);
- setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
- stackedWidget->setCurrentWidget(loginPage);
- socket = new QTcpSocket(this);
- messagePrivee->setVisible(false);
- AdminPanel->setVisible(false);
- ip->setText("localhost");
- pseudo->setFocus();
- bold = false;
- italic = false;
- underlined = false;
- avatar="0";
- statut = "0";
- connectComposant();
- rgb.append("0"); rgb.append("0"); rgb.append("0");
- }
-
- void MainWindow::connectComposant(){
- connect(login, SIGNAL(clicked()), this, SLOT(on_loginButton_clicked()));
- connect(sayButton, SIGNAL(clicked()), this, SLOT(on_sayButton_clicked()));
- connect(sayPButton, SIGNAL(clicked()), this, SLOT(on_sayPButton_clicked()));
- connect(msgPriveeButton, SIGNAL(toggled(bool)), this, SLOT(on_msgPButton_toggled(bool)));
- connect(Bloquer, SIGNAL(clicked()), this, SLOT(on_bloquerButton_clicked()));
- connect(debloquer, SIGNAL(clicked()), this, SLOT(on_debloquerButton_clicked()));
- connect(colorButton,SIGNAL(clicked()),this,SLOT(on_couleur_clicked()));
- connect(boldButton, SIGNAL(toggled(bool)),this,SLOT(on_bold_toggled(bool)));
- connect(italicButton, SIGNAL(toggled(bool)),this,SLOT(on_italic_toggled(bool)));
- connect(underlinedButton, SIGNAL(toggled(bool)),this,SLOT(on_underlined_toggled(bool)));
-
-
- connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead()));
- connect(socket, SIGNAL(connected()), this, SLOT(connected()));
- connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(error()));
-
- connect(actionDeconexion, SIGNAL(triggered()), this, SLOT(closeConnection()));
- connect(actionQuit, SIGNAL(triggered()), this, SLOT(close()));
- connect(actionChanger_avatare, SIGNAL(triggered()), this, SLOT(on_avatareButton_clicked()));
- connect(actionChanger_statut, SIGNAL(triggered()), this, SLOT(on_actionStatut_triggred()));
- connect(actionApropos,SIGNAL(triggered()),this,SLOT(apropos()));
- connect(actionApropos_du_Qt, SIGNAL(triggered()),this,SLOT(apropos_Qt()));
- }
-
- void MainWindow::on_actionStatut_triggred(){
- if(stackedWidget->currentWidget() == chatPage){
- Statut *fen = new Statut(this) ;
- if( fen->exec()== QDialog::Accepted ){
- QVariant s = fen->comboBox->currentIndex();
- statut = s.toString();
- socket->write(QString("/statutSwitch:"+ pseudo->text()+"-"+avatar+"-"+statut+"\n").toUtf8());
- }
- }
- }
-
- void MainWindow::on_loginButton_clicked()
- {
- if(pseudo->text().isEmpty()){
- QMessageBox::information(this,"Pseudo vide","Le <b>Pseudo</b> est obligatoire !");
- return;
- }
- socket->connectToHost(ip->text(), 4200);
- }
-
- void MainWindow::connected()
- {
- stackedWidget->setCurrentWidget(chatPage);
- if (isAdmin()) AdminPanel->setVisible(true);
-
- setFixedSize(600,400);
- setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
- bienvenu->setText("Bienvenu <i>"+pseudo->text()+ "</i>");
-
- if(avatar.isEmpty()) avatar="0";
- QVariant s = statutComboBox->currentIndex();
- statut = s.toString();
-
- socket->write(QString("/me:"+ pseudo->text() +"-"+ avatar +"-"+statut+"\n").toUtf8());
- }
-
- void MainWindow::on_avatareButton_clicked()
- {
- list = new ListeAvatare(this);
- if( list->exec() == QDialog::Accepted){
- QVariant i = list->listWidget->currentRow();
- avatar= i.toString();
- }
- if(stackedWidget->currentWidget() == chatPage){
- QVariant s = statutComboBox->currentIndex();
- statut = s.toString();
- socket->write(QString("/avatarSwitch:"+ pseudo->text()+"-"+avatar +"-"+statut+"\n").toUtf8());
- }
- }
-
- void MainWindow::on_sayButton_clicked()
- {
- QString message = sayLineEdit->text().trimmed();
- if(!message.isEmpty() && !isCommande()){
- if(bold) message+= "-B";
- else message+="-S";
-
- if(italic) message+= "-I";
- else message+="-S";
-
- if(underlined) message+= "-U";
- else message+="-S";
-
- message+=+"-"+rgb.join("-");
-
- socket->write(QString(message+"\n").toUtf8());
- }
- sayLineEdit->clear();
- sayLineEdit->setFocus();
- }
-
- bool MainWindow::selectedUser()
- {
- if(!usrList->currentIndex().isValid()){
- QMessageBox::information(this,"Erreur destination","Selectionez un contacte de la liste !");
- return false;
- }
- QStringList usr = usrList->currentItem()->text().split(" ");
- if(QString(usr[0]).compare(pseudo->text()) == 0)
- {
- QMessageBox::information(this,"Erreur destination","C'est vous !");
- return false;
- }
- return true;
- }
-
- void MainWindow::on_sayPButton_clicked()
- {
- if(!sayPLineEdit->text().isEmpty() && selectedUser())
- {
- QStringList usr = usrList->currentItem()->text().split(" ");
- socket->write(QString("/msgP:"+usr[0]+"-"+sayPLineEdit->text().trimmed()+" \n").toUtf8());
- sayPLineEdit->clear();
- sayPLineEdit->setFocus();
- }
- }
-
- void MainWindow::on_bloquerButton_clicked()
- {
- if(selectedUser()){
- QStringList usr = usrList->currentItem()->text().split(" ");
- socket->write(QString("/bloquer:"+usr[0]+"\n").toUtf8());
- }
- }
-
- void MainWindow::on_debloquerButton_clicked()
- {
- if(selectedUser()){
- QStringList usr = usrList->currentItem()->text().split(" ");
- socket->write(QString("/debloquer:"+usr[0]+ "\n").toUtf8());
-
- }
- }
-
- void MainWindow::readyRead()
- {
- while(socket->canReadLine())
- {
- QString line = QString::fromUtf8(socket->readLine()).trimmed();
-
- QRegExp messageRegex("^([^:]+):(.*)$");
- QRegExp usersRegex("^/users:(.*)$");
- QRegExp msgPRegex("^/msgP:(.*)$");
- QRegExp bloquerRegex("^/bloquer:(.*)$");
- QRegExp debloquerRegex("^/debloquer:(.*)$");
- QRegExp serveurRegex("^Serveur:(.*)$");
-
- if(bloquerRegex.indexIn(line) != -1)
- {
- QString usr = bloquerRegex.cap(1);
-
- if(QString(pseudo->text()).compare(usr) == 0){
- sayLineEdit->setText("vous etes bloqué");
- centralWidget()->setEnabled(false);
- }
- screen->append(QTime::currentTime().toString("<u><font color=blue>hh:mm:ss</u></font>")
- +"<I><font color=red><b> Admin:</b> "+usr+" est bloqué !</font></I>");
- }
- else if(serveurRegex.indexIn(line) != -1){
- QString msg = serveurRegex.cap(1);
- screen->append(QTime::currentTime().toString("<u><font color=blue>hh:mm:ss</u></font>")+
- "<I><font color=green> Serveur : "
- +msg+"</font></I>");
- }
-
- else if(debloquerRegex.indexIn(line) != -1){
- QString usr = debloquerRegex.cap(1);
- if(pseudo->text().compare(usr) == 0){
- sayLineEdit->setText("vous etes debloqué");
- centralWidget()->setEnabled(true);
- }
- screen->append(QTime::currentTime().toString("<u><font color=blue>hh:mm:ss</u></font>")+
- "<I><font color=green><b> Admin:</b> "+usr+" est débloqué !</font></I>");
-
- }
- else if(usersRegex.indexIn(line) != -1)
- {
- usrList->clear();
- QStringList users = usersRegex.cap(1).split(",");
- QString s= "Disponible";
- foreach(QString user, users){
- QStringList info = user.split("-");
-
- if(QString("0").compare(info[2])==0) s= "";
- else if(QString("1").compare(info[2])==0) s= " (Occupé)";
- else if(QString("2").compare(info[2])==0) s= " (Hors Ligne)";
-
- new QListWidgetItem(QPixmap(":/img/avatare/"+info[1]+".png"),info[0]+s, usrList);
- }
- }
- else if(msgPRegex.indexIn(line) != -1)
- {
- QStringList msgP = msgPRegex.cap(1).split("-");
- messagePrivee->setVisible(true);
- setFixedSize(600,600);
- screenP->append( "<b>" + msgP[0] + " : </b>" + msgP[1] );
- }
- else if (messageRegex.indexIn(line) != -1)
- {
- QString user = messageRegex.cap(1);
- QStringList info = messageRegex.cap(2).split("-");
- QString msg = QTime::currentTime().toString("<font color=blue ><u>hh:mm:ss<u></font>")
- +" <b>"+user+"</b> : ";
- if(QString(info[1]).compare("B") == 0) msg+="<B>";
- if (QString(info[2]).compare("I") == 0) msg+="<I>";
- if (QString(info[3]).compare("U") == 0) msg+="<U>";
- QColor c(QString(info[4]).toInt(),QString(info[5]).toInt(),QString(info[6]).toInt());
- msg+="<font color="+c.name()+" >"+info[0]+"</font>";
-
- if(QString(info[1]).compare("B") == 0) msg+="</B>";
- if (QString(info[2]).compare("I") == 0) msg+="</I>";
- if (QString(info[3]).compare("U") == 0) msg+="</U>";
-
- screen->append(msg);
- }
- }
- }
-
- void MainWindow::closeConnection()
- {
- socket->close();
- stackedWidget->setCurrentWidget(loginPage);
- setFixedSize(240,420);
- screen->clear();
- screenP->clear();
- }
-
- void MainWindow::on_msgPButton_toggled(bool a)
- {
- if(selectedUser())
- {
- messagePrivee->setVisible(a);
- if(a) setFixedSize( 600,600);
- else setFixedSize(600,400);
- }
- }
-
- void MainWindow::on_bold_toggled(bool b){
- bold = b;
- }
-
- void MainWindow::on_italic_toggled(bool b){
- italic = b;
- }
-
- void MainWindow::on_underlined_toggled(bool b){
- underlined = b;
- }
-
- void MainWindow::error()
- {
- QMessageBox::critical(this, "Erreur", socket->errorString());
- closeConnection();
- }
-
- void MainWindow::apropos()
- {
- QMessageBox::about(this, tr("Apropos Ql-ChatRoom"),
- tr("<h2>Ql-Chat v 0.0</h2>"
- "<p>Qualité ChatRoom a ChatRoom.<br/>"
- "Copyright (C) 2012 KHABALI Anas<br/>"
- "<img src=:/img/agplv3-88x31.png /><br/>"
- "This program is free software: you can redistribute it and/or modify<br/>"
- "it under the terms of the GNU General Public License as published by<br/>"
- "the Free Software Foundation, either version 3 of the License, or<br/>"
- "(at your option) any later version.<br/><br/>"
- "This program is distributed in the hope that it will be useful,<br/>"
- "but WITHOUT ANY WARRANTY; without even the implied warranty of<br/>"
- "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the<br/>"
- "GNU General Public License for more details.<br/>"
- "You should have received a copy of the GNU General Public License<br/>"
- "along with this program. If not, see <a href=\"http://www.gnu.org/licenses/\" >www.gnu.org/licenses/</a>.<br/><br/>"
- "Programmed By:<br/>"
- "<i>KHABALI ANAS</i><br/>"
- "A C++ <i>FrameWork Qt</i> Project at the university.<br/>"
- "Master Qualité du logiciel.<br/>"
- "Université Ibn Tofail, Maroc.<br/>"
- "<a href=\"http://khabalisoft.e-monsite.com/ \">Site Web</a>"
- ));
- }
-
- void MainWindow::apropos_Qt()
- {
- QMessageBox::aboutQt(this,"Apropos Qt");
- }
-
- bool MainWindow::isAdmin()
- {
- if(ip->text().compare("localhost")== 0 || ip->text().compare("127.0.0.1") == 0)
- return true;
-
- return false;
- }
- void MainWindow::on_couleur_clicked()
- {
- QColor coul = QColorDialog::getColor(Qt::black, this);
- QVariant i = coul.red();
- rgb[0] = i.toString();
- i = coul.green();
- rgb[1]= i.toString();
- i = coul.blue();
- rgb[2]= i.toString();
- }
-
- bool MainWindow::isCommande()
- {
- QString msg= sayLineEdit->text();
- if( msg.compare("/clear") == 0)
- {
- screen->clear();
- return true;
- }
- return false;
- }
|