Superviseur - Afficheur 1.0
Ce logiciel correspond à l'afficheur de la suite des trois logiciels composant le superviseur
|
00001 #include "afficheur.h" 00002 #include "ui_afficheur.h" 00003 #include "connexiondialog.h" 00004 00005 00007 00014 Afficheur::Afficheur(QWidget *parent) : 00015 QMainWindow(parent), 00016 ui(new Ui::Afficheur) 00017 { 00018 ui->setupUi(this); 00019 00020 this->collSocket = new CollecteurSocket(); 00021 /*QThread *th = new QThread(); 00022 this->collSocket->moveToThread(th); 00023 th->start();*/ 00024 00025 this->connexionInfos = new QLabel(tr("Déconnecté")); 00026 ui->statusBar->addWidget(this->connexionInfos); 00027 00028 ui->listeMachines->header()->setResizeMode(QHeaderView::ResizeToContents); 00029 00030 this->zoomFacteur = 1.0; 00031 00032 connect(this->collSocket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(afficherErreur(QAbstractSocket::SocketError))); 00033 connect(ui->actionConnexion, SIGNAL(triggered(bool)), this, SLOT(connexion(bool))); 00034 connect(ui->actionA_propos_de_Qt, SIGNAL(triggered()), qApp, SLOT(aboutQt())); 00035 connect(this->collSocket, SIGNAL(nouvellesDonnees(QString)), this, SLOT(ajouterDonnees(QString))); 00036 connect(this->collSocket, SIGNAL(connected()), this, SLOT(connexionEtablie())); 00037 connect(this->collSocket, SIGNAL(disconnected()), this, SLOT(deconnecte())); 00038 00039 remplirEcran(); 00040 creerOutilTailleZoom(); 00041 00042 this->scene = new QGraphicsScene(this); 00043 this->scene->setItemIndexMethod(QGraphicsScene::BspTreeIndex); 00044 this->scene->setSceneRect(0, 0, 380, 262); 00045 ui->graphicsView->setScene(this->scene); 00046 //ui->graphicsView->setRenderHint(QPainter::Antialiasing); 00047 00048 this->startTimer(1000); 00049 } 00050 00051 00053 00056 Afficheur::~Afficheur() 00057 { 00058 delete ui; 00059 //delete this->scene; 00060 delete this->collSocket; 00061 //delete this->connexionInfos; 00062 } 00063 00064 00066 00070 void Afficheur::changeEvent(QEvent *e) 00071 { 00072 QMainWindow::changeEvent(e); 00073 switch (e->type()) { 00074 case QEvent::LanguageChange: 00075 ui->retranslateUi(this); 00076 break; 00077 default: 00078 break; 00079 } 00080 } 00081 00082 00084 00087 void Afficheur::timerEvent(QTimerEvent *){ 00088 int timeout = 30; 00089 HyperviseurGraphique* hypGraph; 00090 QHashIterator<QString, HyperviseurGraphique*> li(this->hyperviseursGraph); 00091 00092 while(li.hasNext()){ 00093 li.next(); 00094 hypGraph = li.value(); 00095 if(hypGraph->derniereReception().secsTo(QTime::currentTime()) > timeout){ 00096 this->statusBar()->showMessage(tr("Aucune donnée reçue de l'hyperviseur '%1' depuis %2 secondes. Suppression.").arg(hypGraph->name()).arg(timeout), 15000); 00097 this->hyperviseursGraph.remove(li.key()); 00098 delete this->hyperviseursTree.value(li.key()); 00099 this->hyperviseursTree.remove(li.key()); 00100 if(this->hyperviseursAffiches.contains(hypGraph)){ 00101 this->hyperviseursAffiches.removeAt(this->hyperviseursAffiches.indexOf(hypGraph)); 00102 } 00103 delete hypGraph; 00104 }else if(ui->actionAfficher_les_machines_en_bonne_santee->isChecked() || hypGraph->etatDeSante() > 70){ 00105 if(!this->hyperviseursAffiches.contains(hypGraph)) this->hyperviseursAffiches.append(hypGraph); 00106 }else{ 00107 if(this->hyperviseursAffiches.contains(hypGraph)){ 00108 this->hyperviseursAffiches.removeAt(this->hyperviseursAffiches.indexOf(hypGraph)); 00109 this->scene->removeItem(hypGraph); 00110 } 00111 } 00112 } 00113 00114 int i; 00115 int largeur = 0; 00116 int hauteur = 0; 00117 int nextHauteur = 0; 00118 for(i=0; i < this->hyperviseursAffiches.size(); i++){ 00119 hypGraph = this->hyperviseursAffiches.at(i); 00120 if(!this->scene->items().contains(hypGraph)) this->scene->addItem(hypGraph); 00121 if(largeur+hypGraph->boundingRect().width() > ui->graphicsView->size().width()){ 00122 largeur = 0; 00123 hauteur = nextHauteur+20; 00124 nextHauteur = 0; 00125 } 00126 nextHauteur = qMax(nextHauteur, hauteur+(int)hypGraph->boundingRect().height()); 00127 hypGraph->setPos(largeur, hauteur); 00128 largeur += hypGraph->boundingRect().width()+20; 00129 } 00130 this->scene->setSceneRect(this->scene->itemsBoundingRect()); 00131 } 00132 00133 00135 00137 void Afficheur::remplirEcran(){ 00138 /*QRect geom = qApp->desktop()->availableGeometry(); 00139 geom.setHeight( geom.height() - ( this->frameGeometry().width() - this->geometry().width() )/2 ); 00140 geom.setY( this->geometry().y() - this->y() ); 00141 geom.setWidth( geom.width() - ( this->frameGeometry().width() - this->geometry().width() )/2 ); 00142 geom.setX( this->geometry().x() - this->x() ); 00143 this->setGeometry(geom);*/ 00144 this->setWindowState( Qt::WindowMaximized /*Qt::WindowFullScreen*/); 00145 } 00146 00147 00149 00154 void Afficheur::redimmensionner(int pourcent){ 00155 ui->graphicsView->scale(1/this->zoomFacteur, 1/this->zoomFacteur); 00156 this->zoomFacteur = (qreal)pourcent/100.0; 00157 ui->graphicsView->scale(this->zoomFacteur, this->zoomFacteur); 00158 this->zoomValeur->setText(QString::number(pourcent)+"%"); 00159 } 00160 00161 00163 00169 void Afficheur::connexion(bool fermer){ 00170 if(!fermer && this->collSocket->state() == QAbstractSocket::ConnectedState){ 00171 this->collSocket->disconnectFromHost(); 00172 }else if(fermer){ 00173 ConnexionDialog dialog(this); 00174 if(dialog.exec() == QDialog::Accepted){ 00175 this->connexionInfos->setText(tr("Connexion en cours ...")); 00176 this->collSocket->connexion(dialog.hote(), dialog.port()); 00177 this->hoteCourant = dialog.hote(); 00178 this->portCourant = dialog.port(); 00179 } 00180 } 00181 } 00182 00183 00185 00187 void Afficheur::connexionEtablie(){ 00188 this->connexionInfos->setText(tr("Connecté")); 00189 ui->actionConnexion->setChecked(true); 00190 //this->collSocket->setSocketOption(QAbstractSocket::LowDelayOption, 1); 00191 } 00192 00193 00195 00197 void Afficheur::deconnecte(){ 00198 this->connexionInfos->setText(tr("Déconnecté")); 00199 ui->actionConnexion->setChecked(false); 00200 } 00201 00202 00204 00210 void Afficheur::afficherErreur(QAbstractSocket::SocketError socketError, QString message){ 00211 switch(socketError){ 00212 case QAbstractSocket::RemoteHostClosedError: 00213 if(QMessageBox::information(this, tr("Connexion fermée"), 00214 tr("Le collecteur à fermé la connexion.")+"\n") 00215 //+tr("Souhaitez-vous vous reconnecter ?"), QMessageBox::Yes | QMessageBox::No) 00216 == QMessageBox::Yes) 00217 { 00218 this->collSocket->connexion(this->hoteCourant, this->portCourant); 00219 } 00220 00221 break; 00222 case QAbstractSocket::HostNotFoundError: 00223 QMessageBox::information(this, tr("Hôte non trouvé"), tr("Le collecteur n'est pas accessible.")); 00224 break; 00225 case QAbstractSocket::ConnectionRefusedError: 00226 QMessageBox::information(this, tr("Connexion refusée"), 00227 tr("Le collecteur à refusé la connexion.")+"\n" 00228 +tr("Vérifiez l'adresse, le port et le pare-feu.")); 00229 break; 00230 default: 00231 QMessageBox::information(this, tr("Erreur"), 00232 tr("Une erreur est survenue: %1.").arg(this->collSocket->errorString())); 00233 } 00234 if(this->collSocket->state() != QAbstractSocket::ConnectedState) this->deconnecte(); 00235 00236 } 00237 00238 00240 00245 void Afficheur::changerDonnees(QVariantMap donnees, QTreeWidgetItem *item){ 00246 int i; 00247 QStringList liste; 00248 liste << donnees.value("name").toString(); 00249 liste << QString::number(donnees.value("cpu", QVariant("")).toDouble(), 'f', 2); 00250 liste << QString::number(donnees.value("ram", QVariant("")).toDouble(), 'f', 2); 00251 liste << QString::number(donnees.value("disk", QVariant("")).toDouble(), 'f', 2); 00252 liste << QString::number(donnees.value("cputemp", QVariant("")).toDouble(), 'f', 2); 00253 liste << QString::number(donnees.value("mbtemp", QVariant("")).toDouble(), 'f', 2); 00254 00255 for(i=0; i < liste.size(); i++){ 00256 item->setText(i, liste.at(i)); 00257 } 00258 } 00259 00260 00262 00268 void Afficheur::changerDonneesVMs(QList<QVariantMap>donnees, QTreeWidgetItem *item){ 00269 int i; 00270 bool find; 00271 QVariantMap vmInfos; 00272 QListIterator<QVariantMap> li(donnees); 00273 00274 while(li.hasNext()){ 00275 find = false; 00276 vmInfos = li.next(); 00277 for(i=0; i < item->childCount(); i++){ 00278 if(vmInfos.value("name", QVariant("")) == item->child(i)->text(0) && !vmInfos.value("name", QVariant("")).isNull()){ 00279 find = true; 00280 break; 00281 } 00282 } 00283 if(!find){ 00284 QTreeWidgetItem *itemVM = new QTreeWidgetItem(item); 00285 itemVM->setText(0, vmInfos.value("name").toString()); 00286 } 00287 } 00288 00289 for(i=0; i < item->childCount(); i++){ 00290 find = false; 00291 li.toFront(); 00292 while(li.hasNext()){ 00293 vmInfos = li.next(); 00294 if(vmInfos.value("name", QVariant("")) == item->child(i)->text(0) && !vmInfos.value("name", QVariant("")).isNull()){ 00295 item->child(i)->setText(1, QString::number(vmInfos.value("cpu", QVariant(0.0)).toDouble(), 'f', 2)); 00296 item->child(i)->setText(2, QString::number((float)(vmInfos.value("ram", QVariant(0)).toInt())/1024.0)+"/"+QString::number((float)(vmInfos.value("maxram", QVariant(0)).toInt()/1024.0))); 00297 find = true; 00298 break; 00299 } 00300 } 00301 if(!find){ 00302 item->removeChild(item->child(i)); 00303 } 00304 } 00305 } 00306 00307 00309 00316 void Afficheur::ajouterDonnees(QString donnees){ 00317 /*int i; 00318 for(i=0; i<donnees.size(); i++){ 00319 qDebug() << donnees.at(i); 00320 }*/ 00321 00322 //QString str = "{\"name\": \"Fedora14\", \"ram\": 50.82372863817628, \"cpu\": 52.162033877474634, \"time\": 1307446856.860979, \"disk\": 0.0, \"vms\": [{\"state\": 5, \"cpu\": 0.0, \"name\": \"1-Fedora-14-64bits\", \"uuid\": \"87c63557-2a88-d1ee-b38b-9d7211f0256d\"}, {\"state\": 5, \"cpu\": 0.0, \"name\": \"4-Fedora-14-64bits\", \"uuid\": \"59d1f532-fa78-512b-23f7-879426212cc5\"}, {\"state\": 5, \"cpu\": 0.0, \"name\": \"3-Fedora-14-64bits\", \"uuid\": \"da4f4e7e-838a-11e0-80a4-0f6687be54e0\"}, {\"state\": 5, \"cpu\": 0.0, \"name\": \"6-Noyau-Machine-Hote\", \"uuid\": \"541eae91-29f7-d17f-ab36-9a735b1e2805\"}, {\"state\": 5, \"cpu\": 0.0, \"name\": \"5-Fedora-14-64bits\", \"uuid\": \"534cc0d2-37e1-7a06-f999-2544934651be\"}, {\"state\": 1, \"cpu\": 5.41171442050386, \"name\": \"2-Fedora-14-64bits\", \"uuid\": \"6cdbd6d2-266d-c568-bc78-7aaab689c0e5\"}]}"; 00323 bool ok = false; 00324 QJson::Parser parser; 00325 QVariantMap donneesP = parser.parse(donnees.toAscii(), &ok).toMap(); 00326 if(!ok){ 00327 qDebug() << tr("Une erreur est survenue dans le parsing de la chaine JSON."); 00328 return; 00329 } 00330 QString nom = donneesP.value("name", "").toString(); 00331 00332 QVariant vms = donneesP.take("vms"); 00333 QVariantList vmsListe = vms.toList(); 00334 QList<QVariantMap> donneesPVMs; 00335 QListIterator<QVariant> li(vmsListe); 00336 while(li.hasNext()) donneesPVMs.append(li.next().toMap()); 00337 00338 if(hyperviseursGraph.contains(nom) && !nom.isEmpty()){ 00339 HyperviseurGraphique *hypGraph = hyperviseursGraph.value(nom); 00340 hypGraph->changerDonnees(donneesP, donneesPVMs); 00341 00342 QTreeWidgetItem *item = hyperviseursTree.value(nom); 00343 this->changerDonnees(donneesP, item); 00344 this->changerDonneesVMs(donneesPVMs, item); 00345 00346 }else if(!nom.isEmpty()){ 00347 HyperviseurGraphique *hypGraph = new HyperviseurGraphique(donneesP, donneesPVMs); hypGraph->setPos(0,0); 00348 hyperviseursGraph.insert(nom, hypGraph); 00349 00350 QTreeWidgetItem *item = new QTreeWidgetItem(); 00351 this->changerDonnees(donneesP, item); 00352 this->changerDonneesVMs(donneesPVMs, item); 00353 ui->listeMachines->addTopLevelItem(item); 00354 hyperviseursTree.insert(nom, item); 00355 00356 this->statusBar()->showMessage(tr("Nouvel hyperviseur : '%1'").arg(nom), 15000); 00357 } 00358 00359 } 00360 00361 00363 00365 void Afficheur::creerOutilTailleZoom(){ 00366 QLabel *tailleTxt; 00367 QLabel *zoomTxt; 00368 00369 tailleTxt = new QLabel(" "+tr("Taille")+": ", ui->centralWidget); 00370 tailleTxt->setObjectName("tailleTxt"); 00371 00372 ui->optionsToolBar->addSeparator(); 00373 ui->optionsToolBar->addWidget(tailleTxt); 00374 00375 this->taille = new QSlider(ui->centralWidget); 00376 this->taille->setObjectName(QString::fromUtf8("taille")); 00377 this->taille->setMinimumSize(QSize(150, 0)); 00378 this->taille->setMaximumSize(QSize(180, 16777215)); 00379 this->taille->setOrientation(Qt::Horizontal); 00380 this->taille->setMinimum(25); 00381 this->taille->setMaximum(200); 00382 this->taille->setValue(100); 00383 this->taille->setEnabled(false); 00384 00385 this->tailleValeur = new QLabel(QString::number(this->taille->value())+"%", ui->centralWidget); 00386 ui->optionsToolBar->addWidget(this->tailleValeur); 00387 00388 ui->optionsToolBar->addWidget(this->taille); 00389 ui->optionsToolBar->addWidget(new QLabel(" ", ui->centralWidget)); 00390 ui->optionsToolBar->addSeparator(); 00391 00392 zoomTxt = new QLabel(" "+tr("Zoom")+": ", ui->centralWidget); 00393 zoomTxt->setObjectName("zoomTxt"); 00394 00395 ui->optionsToolBar->addWidget(zoomTxt); 00396 00397 this->zoom = new QSlider(ui->centralWidget); 00398 this->zoom->setObjectName(QString::fromUtf8("zoom")); 00399 this->zoom->setMinimumSize(QSize(150, 0)); 00400 this->zoom->setMaximumSize(QSize(180, 16777215)); 00401 this->zoom->setOrientation(Qt::Horizontal); 00402 this->zoom->setMinimum(50); 00403 this->zoom->setMaximum(200); 00404 this->zoom->setValue(100); 00405 00406 this->zoomValeur = new QLabel(QString::number(this->zoom->value())+"%", ui->centralWidget); 00407 ui->optionsToolBar->addWidget(this->zoomValeur); 00408 00409 ui->optionsToolBar->addWidget(this->zoom); 00410 00411 connect(this->zoom, SIGNAL(valueChanged(int)), this, SLOT(redimmensionner(int))); 00412 00413 } 00414 00415 00416