Bug fixed: If you are logged in and result is sent in result dialog. If you want...
[speedfreak] / Client / mainwindow.cpp
index 8dbfc79..9d9984d 100644 (file)
@@ -14,6 +14,7 @@
 #include <QUrl>
 #include <QSettings>
 #include <QDebug>
+#include "usersettings.h"
 
 MainWindow::MainWindow(QWidget *parent) :
     QMainWindow(parent),
@@ -25,32 +26,24 @@ MainWindow::MainWindow(QWidget *parent) :
     QCoreApplication::setOrganizationDomain("fudeco.com");
     QCoreApplication::setApplicationName("Speed Freak");
 
-    routeDialog = new RouteDialog;
-    connect(routeDialog,SIGNAL(sendroute()),this,SLOT(clientSendRoute()));
-
-    routeSaveDialog = new RouteSaveDialog;
+    helpDialog = NULL;
+    accstart = NULL;
+    routeSaveDialog = NULL;
+    topResultDialog = NULL;
 
     settingsDialog = new SettingsDialog;
     connect(settingsDialog,SIGNAL(sendregistration()),this,SLOT(clientRegUserToServer()));
     connect(settingsDialog,SIGNAL(userNameChanged()),this,SLOT(clientUserLogin()));
-
-    topResultDialog = new TopResultDialog;
-    connect(topResultDialog, SIGNAL(refreshCategoryList()), this, SLOT(clientRequestCategoryList()));
-    connect(topResultDialog, SIGNAL(refreshTopList(int)), this, SLOT(clientRequestTopList(int)));
+    connect(settingsDialog, SIGNAL(logout()), this, SLOT(setUsernameToMainPanel()));
 
     httpClient = new HttpClient(this);
     connect(httpClient->myXmlreader, SIGNAL(receivedCategoryList()), this, SLOT(setCategoryCompoBox()));
-    connect(httpClient->myXmlreader, SIGNAL(receivedTop10List()), this, SLOT(showTop10()));
-
-    resultDialog = new ResultDialog;
-    connect(resultDialog, SIGNAL(sendresult()), this, SLOT(clientSendResult()));
-
-    accstart = NULL;
-
-    creditsDialog = new CreditsDialog;
+    connect(httpClient->myXmlreader, SIGNAL(receivedTop10List()), this, SLOT(showTop10()));    
 
     welcomeDialog = new WelcomeDialog;
-    welcomeDialog->show();
+    //welcomeDialog->show();
+
+    this->setUsernameToMainPanel();
 
     //Button settings
     ui->pushButtonAccelerate->setAutoFillBackground(true);
@@ -70,11 +63,27 @@ MainWindow::MainWindow(QWidget *parent) :
 MainWindow::~MainWindow()
 {
     delete ui;
-    delete routeSaveDialog;
-    delete routeDialog;
+
+    if(routeSaveDialog)
+        delete routeSaveDialog;
 
     if(accstart)
         delete accstart;
+
+    if(topResultDialog)
+        delete topResultDialog;
+
+    if(settingsDialog)
+        delete settingsDialog;
+
+    if(welcomeDialog)
+        delete welcomeDialog;
+
+    if(httpClient)
+        delete httpClient;
+
+    if(helpDialog)
+        delete helpDialog;
 }
 
 void MainWindow::changeEvent(QEvent *e)
@@ -102,7 +111,11 @@ void MainWindow::on_pushButtonWWW_clicked()
   */
 void MainWindow::on_pushButtonCredits_clicked()
 {
-    creditsDialog->show();
+    if(!helpDialog)
+        helpDialog = new HelpDialog;
+
+    connect(helpDialog, SIGNAL(rejected()), this, SLOT(killDialog()));
+    helpDialog->show();
 }
 
 /**
@@ -110,6 +123,11 @@ void MainWindow::on_pushButtonCredits_clicked()
   */
 void MainWindow::on_pushButtonRoute_clicked()
 {
+    if(!routeSaveDialog)
+        routeSaveDialog = new RouteSaveDialog;
+
+    connect(routeSaveDialog, SIGNAL(sendroute()), this, SLOT(clientSendRoute()));
+    connect(routeSaveDialog, SIGNAL(rejected()), this, SLOT(killDialog()));
     routeSaveDialog->show();
 }
 
@@ -128,6 +146,9 @@ void MainWindow::on_pushButtonAccelerate_clicked()
 {
     if(!accstart)
         accstart = new accelerationstart(this);
+
+    connect(accstart, SIGNAL(sendresult(QString, double)), this, SLOT(clientSendResult(QString, double)));
+    connect(accstart, SIGNAL(rejected()), this, SLOT(killDialog()));
     accstart->show();
 }
 
@@ -136,6 +157,13 @@ void MainWindow::on_pushButtonAccelerate_clicked()
   */
 void MainWindow::on_pushButtonResults_clicked()
 {
+    if (!topResultDialog)
+        topResultDialog = new TopResultDialog;
+
+    clientRequestCategoryList();
+    connect(topResultDialog, SIGNAL(refreshCategoryList()), this, SLOT(clientRequestCategoryList()));
+    connect(topResultDialog, SIGNAL(refreshTopList(int)), this, SLOT(clientRequestTopList(int)));
+    connect(topResultDialog, SIGNAL(rejected()), this, SLOT(killDialog()));
     topResultDialog->show();
 }
 
@@ -144,7 +172,8 @@ void MainWindow::on_pushButtonResults_clicked()
   */
 void MainWindow::clientRequestCategoryList()
 {
-    httpClient->requestCategories();
+    if(httpClient)
+        httpClient->requestCategories();
 }
 
 /**
@@ -152,8 +181,13 @@ void MainWindow::clientRequestCategoryList()
   */
 void MainWindow::clientRequestTopList(int index)
 {
-    QString limit = QString::number(topResultDialog->getLimitNr());
-    httpClient->requestTopList(httpClient->myXmlreader->myCategoryList->getRecentCategory(index), limit);
+    QString limit;
+
+    if(topResultDialog && httpClient->myXmlreader->myCategoryList)
+    {
+        limit = QString::number(topResultDialog->getLimitNr());
+        httpClient->requestTopList(httpClient->myXmlreader->myCategoryList->getRecentCategory(index), limit);
+    }
 }
 
 /**
@@ -162,7 +196,8 @@ void MainWindow::clientRequestTopList(int index)
   */
 void MainWindow::setCategoryCompoBox()
 {
-    topResultDialog->setCompoBoxCategories(httpClient->myXmlreader->myCategoryList->getCategoryList());
+    if(topResultDialog && httpClient->myXmlreader->myCategoryList)
+        topResultDialog->setCompoBoxCategories(httpClient->myXmlreader->myCategoryList->getCategoryList());
 }
 
 /**
@@ -171,8 +206,13 @@ void MainWindow::setCategoryCompoBox()
   */
 void MainWindow::showTop10()
 {
-    int ind = topResultDialog->getRecentCategoryIndex();
-    setListViewTopList(httpClient->myXmlreader->myCategoryList->getRecentCategory(ind), topResultDialog->getLimitNr());
+    int ind;
+
+    if(topResultDialog && httpClient->myXmlreader->myCategoryList && topResultDialog)
+    {
+        ind = topResultDialog->getRecentCategoryIndex();
+        setListViewTopList(httpClient->myXmlreader->myCategoryList->getRecentCategory(ind), topResultDialog->getLimitNr());
+    }
 }
 
 /**
@@ -183,39 +223,102 @@ void MainWindow::showTop10()
 void MainWindow::setListViewTopList(QString category, int size)
 {
     QString topList;
-    topList.append(httpClient->myXmlreader->myCategoryList->getTopList(category, size));
-    topResultDialog->showTopList(topList);
+
+    if(httpClient->myXmlreader->myCategoryList && topResultDialog)
+    {
+        topList.append(httpClient->myXmlreader->myCategoryList->getTopList(category, size));
+        topResultDialog->showTopList(topList);
+    }
 }
 
+/**
+  * This function register user to server.
+  */
 void MainWindow::clientRegUserToServer()
 {
-    httpClient->requestRegistration();
+    if(httpClient)
+        httpClient->requestRegistration();
 }
 
 /**
-  * This function performs login to server
+  * This function performs login to server.
   */
 void MainWindow::clientUserLogin()
 {
-    httpClient->checkLogin();
+    
+    if(httpClient)
+    {
+       connect(httpClient, SIGNAL(loginOK()), this, SLOT(setUsernameToMainPanel()));
+        httpClient->checkLogin();
+    }
 }
 
 /**
-  * This function send route to server
+  * This function send route data to server.
   */
 void MainWindow::clientSendRoute()
 {
-    httpClient->sendRouteXml();
+    if(httpClient)
+        httpClient->sendRouteXml();
 }
 
 /**
-  * This function send acceleration data to server
+  * This function send acceleration data to server.
   */
-void MainWindow::clientSendResult()
+void MainWindow::clientSendResult(QString category, double result)
 {
-    qDebug() << "_clientSendResult";
+    qDebug() << "__clientSendResult";
     if(accstart) {
         qDebug() << "_clientSendResult, calling server";
-        httpClient->sendResultXml(accstart->getMeasureCategory(), resultDialog->getResult());
+        if(httpClient)
+            httpClient->sendResultXml(category, result);
+    }
+}
+/**
+  * This slot function called when ever dialog finished.
+  */
+void MainWindow::killDialog()
+{
+    if(topResultDialog)
+    {
+        qDebug() << "__MW kill: topResultDialog";
+        delete topResultDialog;
+        topResultDialog = NULL;
+    }
+    if(routeSaveDialog)
+    {
+        qDebug() << "__MW kill: routeSaveDialog";
+        //delete routeSaveDialog;
+        //routeSaveDialog = NULL;
+    }
+    if(accstart)
+    {
+        qDebug() << "__MW kill: accstart";
+        delete accstart;
+        accstart = NULL;
+    }
+    if(welcomeDialog)
+    {
+        qDebug() << "__MW kill: welcomeDialog";
+        delete welcomeDialog;
+        welcomeDialog = NULL;
+    }
+    if(helpDialog)
+    {
+        qDebug() << "__MW kill: helpDialog";
+        delete helpDialog;
+        helpDialog = NULL;
+    }
+}
+
+void MainWindow::setUsernameToMainPanel()
+{
+    if (loginSaved())
+    {
+        this->setWindowTitle("SpeedFreak - " + settingsDialog->getUserName());
+    }
+    else
+    {
+        this->setWindowTitle("SpeedFreak - Not logged");
     }
 }