Display top performer in acceleration 0-100 category. Read and parsing results.xml...
authorToni Jussila <toni.jussila@gmail.com>
Fri, 5 Mar 2010 09:06:30 +0000 (11:06 +0200)
committerToni Jussila <toni.jussila@gmail.com>
Fri, 5 Mar 2010 09:06:30 +0000 (11:06 +0200)
Client/UI.pro
Client/carmainwindow.cpp
Client/carmainwindow.h
Client/carmainwindow.ui
Client/readxmlfile.xml [new file with mode: 0644]
Client/results.xml [new file with mode: 0755]
Client/xmlreader.cpp
Client/xmlreader.h

index 97de2a2..dfacc94 100644 (file)
@@ -11,15 +11,15 @@ SOURCES += main.cpp \
     stringlistmodel.cpp \
     measuredialog.cpp \
     calculate.cpp \
-    accelerometer.cpp
-
+    accelerometer.cpp \
+    xmlreader.cpp
 HEADERS += carmainwindow.h \
     resultdialog.h \
     stringlistmodel.h \
     measuredialog.h \
     accelerometer.h \
-    calculate.h
-
+    calculate.h \
+    xmlreader.h
 FORMS += carmainwindow.ui \
     resultdialog.ui \
     measuredialog.ui
index b8af9fe..b730a69 100644 (file)
@@ -1,9 +1,4 @@
 #include "carmainwindow.h"
-#include "ui_carmainwindow.h"
-#include "stringlistmodel.h"
-#include <QStandardItemModel>
-#include <QStringList>
-#include <QString>
 
 /**
   *Constructor of this class.
@@ -14,10 +9,10 @@ CarMainWindow::CarMainWindow(QWidget *parent):QMainWindow(parent), ui(new Ui::Ca
     ui->setupUi(this);
     result = new ResultDialog();
     measure = new MeasureDialog();
+    xmlreader = new XmlReader();
 
     initUnitCompoBox();
     initSpeedListView();
-    initCategoryCompoBox();
 }
 
 /**
@@ -28,6 +23,7 @@ CarMainWindow::~CarMainWindow()
     delete ui;
     delete result;
     delete measure;
+    delete xmlreader;
 }
 
 /**
@@ -47,8 +43,7 @@ void CarMainWindow::changeEvent(QEvent *e)
 }
 
 /**
-  *This slot function is called when ever list view is update.
-  *@param QModelIndex index.
+  *This slot function is called when ever list view is update. Start-tab view.
   */
 void CarMainWindow::on_listView_clicked(QModelIndex index)
 {
@@ -62,7 +57,7 @@ void CarMainWindow::on_listView_clicked(QModelIndex index)
 }
 
 /**
-  *This slot function is called when ever auto start button clicked.
+  *This slot function is called when ever auto start button clicked. Start-tab view.
   */
 void CarMainWindow::on_autoStartButton_clicked()
 {
@@ -80,7 +75,7 @@ void CarMainWindow::on_autoStartButton_clicked()
 }
 
 /**
-  *This slot function is called when ever list view is update.
+  *This slot function is called when ever list view is update. Start-tab view.
   *@param QString unit.
   */
 void CarMainWindow::updateUnitCompoBox(QString unit)
@@ -89,16 +84,16 @@ void CarMainWindow::updateUnitCompoBox(QString unit)
 }
 
 /**
-  *This function is used to init unit combobox.
+  *This function is used to init unit combobox. Start-tab view.
   */
 void CarMainWindow::initUnitCompoBox()
 {
-    units << "km/h" << "km" << "h" << "m" << "min" << "mil" << "in" << "ft" << "yrd";
+    units << "km/h" << "km" << "h" << "m" << "min" << "Mile" << "Mph" << "in" << "ft" << "yrd";
     ui->unitComboBox->addItems(units);
 }
 
 /**
-  *This function is used to set items to unit combobox.
+  *This function is used to set items to unit combobox. Start-tab view.
   *@param QStringlist numbers
   */
 void CarMainWindow::setUnitCompoBox(QStringList units)
@@ -107,18 +102,17 @@ void CarMainWindow::setUnitCompoBox(QStringList units)
 }
 
 /**
-  *This function is used to init speed listview.
+  *This function is used to init speed listview. Start-tab view.
   */
 void CarMainWindow::initSpeedListView()
 {
-    numbers << "0-100 km/h" << "0-1/4 mil" << "0-50 km" << "50-100 mil" << "0-100 m" << "0-50 ft" << "0-50 yrd" << "0-500 in";
+    numbers << "0-40 km/h" << "0-1/4 Mile" << "0-1/8 Mile" << "0-50 km" << "50-100 Mile" << "0-60 Mph" << "0-100 m" << "0-50 ft" << "0-50 yrd" << "0-500 in";
     QAbstractItemModel *model = new StringListModel(numbers);
     ui->listView->setModel(model);
 }
 
 /**
-<<<<<<< HEAD:Client/carmainwindow.cpp
-  *This function is used to set items to speed listview.
+  *This function is used to set items to speed listview. Start-tab view.
   *@param QStringlist numbers
   */
 void CarMainWindow::setSpeedListView(QStringList numbers)
@@ -128,36 +122,37 @@ void CarMainWindow::setSpeedListView(QStringList numbers)
 }
 
 /**
-  *This function is used to init category combobox.
-  */
-void CarMainWindow::initCategoryCompoBox()
-{
-    categories << "Top 10 1/4 mile" << "Top 10 0-100 km/h" << "Top 10 car";
-    ui->comboBoxTopCategory->addItems(categories);
-}
-
-/**
-  *This function is used to set items to category combobox.
-  *@param QStringlist categories
+  *This function is used to set items to category combobox. Top-tab view.
+  *@param
   */
-void CarMainWindow::setCategoryCompoBox(QStringList categories)
+void CarMainWindow::setCategoryCompoBox()
 {
-    ui->comboBoxTopCategory->addItems(categories);
+    ui->comboBoxTopCategory->addItems(xmlreader->getTop10List());
 }
 
 /**
-  *This slot function is called when ever categories combobox is update.
+  *This function is used to set items to labelTopList. Top-tab view.
   *@param QString category
   */
-void CarMainWindow::on_comboBoxTopCategory_activated(QString category)
+void CarMainWindow::setListViewTopList(QString category)
 {
-    //TODO: get top list
+    QString topList;
 
-    QStringList topList;
-    topList << "1. Pertti 7,5s" << "2. Ville 10,2s";
+    if (category == "acceleration-0-100")
+    {
+        topList.append(xmlreader->getTop10AccelerationList());
+    }
+
+    else if (category == "Speed")
+    {
+        topList.append(xmlreader->getTop10SpeedList());
+    }
 
-    QAbstractItemModel *model = new StringListModel(topList);
-    ui->listViewTopList->setModel(model);
+    else if (category == "G-force")
+    {
+        topList.append(xmlreader->getTop10GforceList());
+    }
+    ui->labelTopList->setText(topList);
 }
 
 /**
@@ -168,3 +163,20 @@ void CarMainWindow::openResultView()
     // Show result dialog.
     result->show();
 }
+
+/**
+  *This slot function is called when ever refresh button clicked. Top-tab view.
+  */
+void CarMainWindow::on_buttonTopRefresh_clicked()
+{
+    setCategoryCompoBox();
+}
+
+/**
+  *This slot function is called when ever category combobox current index changed. Top-tab view.
+  *@param QString category
+  */
+void CarMainWindow::on_comboBoxTopCategory_currentIndexChanged(QString category)
+{
+    setListViewTopList(category);
+}
index 198bb30..8071d08 100644 (file)
@@ -1,17 +1,16 @@
 #ifndef CARMAINWINDOW_H
 #define CARMAINWINDOW_H
 
-/**
-  *This class
-  *@author Toni Jussila
-  *@version 0.0.1
-  */
-
 #include <QMainWindow>
+#include <QStandardItemModel>
 #include <QModelIndex>
 #include <QStringList>
+#include <QString>
 #include "resultdialog.h"
 #include "measuredialog.h"
+#include "xmlreader.h"
+#include "ui_carmainwindow.h"
+#include "stringlistmodel.h"
 
 namespace Ui {
     class CarMainWindow;
@@ -23,9 +22,10 @@ public:
     CarMainWindow(QWidget *parent = 0);
     ~CarMainWindow();
 
-    void setUnitCompoBox(QStringList units);
-    void setSpeedListView(QStringList numbers);
-    void setCategoryCompoBox(QStringList categories);
+    void setUnitCompoBox(QStringList units);    //Start-tab
+    void setSpeedListView(QStringList numbers); //Start-tab
+    void setListViewTopList(QString category);  //Top-tab
+    void setCategoryCompoBox(); //Top-tab
 
 protected:
     void changeEvent(QEvent *e);
@@ -34,22 +34,24 @@ private:
     Ui::CarMainWindow *ui;
     ResultDialog *result;
     MeasureDialog *measure;
+    XmlReader *xmlreader;
 
-    void initUnitCompoBox();
-    void initSpeedListView();
-    void initCategoryCompoBox();
+    void initUnitCompoBox();    //Start-tab
+    void initSpeedListView();   //Start-tab
 
 private:
-    QStringList numbers;
-    QStringList units;
-    QStringList categories;
+    QStringList numbers; //Start-tab
+    QStringList units;  //Start-tab
+    QStringList categories; //Top-tab
 
 private slots:
-    void on_comboBoxTopCategory_activated(QString );
-    void on_autoStartButton_clicked();
-    void on_listView_clicked(QModelIndex index);
-    void updateUnitCompoBox(QString unit);
+    void on_comboBoxTopCategory_currentIndexChanged(QString category); //Top-tab
+    void on_listView_clicked(QModelIndex index); //Start-tab
+    void updateUnitCompoBox(QString unit);  //Start-tab
     void openResultView();
+    void on_buttonTopRefresh_clicked(); //Top-tab: button
+    void on_autoStartButton_clicked();  //Start-tab: button
+
 };
 
 #endif // CARMAINWINDOW_H
index af62383..f9ec648 100644 (file)
@@ -24,7 +24,7 @@
      </rect>
     </property>
     <property name="currentIndex">
-     <number>1</number>
+     <number>0</number>
     </property>
     <widget class="QWidget" name="StartTab">
      <attribute name="title">
        <string>Refresh list</string>
       </property>
      </widget>
-     <widget class="QListView" name="listViewTopList">
-      <property name="geometry">
-       <rect>
-        <x>360</x>
-        <y>10</y>
-        <width>411</width>
-        <height>311</height>
-       </rect>
-      </property>
-      <property name="font">
-       <font>
-        <family>Bitstream Charter</family>
-        <pointsize>10</pointsize>
-       </font>
-      </property>
-      <property name="flow">
-       <enum>QListView::LeftToRight</enum>
-      </property>
-     </widget>
-     <widget class="QWidget" name="">
+     <widget class="QWidget" name="layoutWidget">
       <property name="geometry">
        <rect>
         <x>10</x>
        </item>
       </layout>
      </widget>
+     <widget class="QLabel" name="labelTopList">
+      <property name="geometry">
+       <rect>
+        <x>380</x>
+        <y>10</y>
+        <width>371</width>
+        <height>311</height>
+       </rect>
+      </property>
+      <property name="font">
+       <font>
+        <pointsize>9</pointsize>
+       </font>
+      </property>
+      <property name="text">
+       <string>TopList</string>
+      </property>
+      <property name="alignment">
+       <set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter</set>
+      </property>
+     </widget>
     </widget>
     <widget class="QWidget" name="settingsTab">
      <attribute name="title">
diff --git a/Client/readxmlfile.xml b/Client/readxmlfile.xml
new file mode 100644 (file)
index 0000000..3b30379
--- /dev/null
@@ -0,0 +1,33 @@
+<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE xml>
+<xml version="1.0">
+<top10acceleration rank="1" nick="toyota" acceleration="9" unit="m/s2"/>
+<top10acceleration rank="2" nick="toyota2" acceleration="8" unit="m/s2"/>
+<top10acceleration rank="3" nick="012345678912" acceleration="7" unit="m/s2"/>
+<top10acceleration rank="4" nick="toyota4" acceleration="6" unit="m/s2"/>
+<top10acceleration rank="5" nick="toyota5" acceleration="5" unit="m/s2"/>
+<top10acceleration rank="6" nick="toyota6" acceleration="4" unit="m/s2"/>
+<top10acceleration rank="7" nick="toyota7" acceleration="3" unit="m/s2"/>
+<top10acceleration rank="8" nick="toyota8" acceleration="2" unit="m/s2"/>
+<top10acceleration rank="9" nick="toyota9" acceleration="1" unit="m/s2"/>
+<top10acceleration rank="10" nick="toyota10" acceleration="1" unit="m/s2"/>
+<top10speed rank="1" nick="toyota" speed="100" unit="km/h"/>
+<top10speed rank="2" nick="toyota2" speed="80" unit="km/h"/>
+<top10speed rank="3" nick="toyota3" speed="70" unit="km/h"/>
+<top10speed rank="4" nick="toyota4" speed="60" unit="km/h"/>
+<top10speed rank="5" nick="toyota5" speed="50" unit="km/h"/>
+<top10speed rank="6" nick="toyota6" speed="40" unit="km/h"/>
+<top10speed rank="7" nick="toyota7" speed="30" unit="km/h"/>
+<top10speed rank="8" nick="toyota8" speed="20" unit="km/h"/>
+<top10speed rank="9" nick="toyota9" speed="10" unit="km/h"/>
+<top10speed rank="10" nick="toyota10" speed="1" unit="km/h"/>
+<top10gforce rank="1" nick="toyota" gforce="1.00" unit="g"/>
+<top10gforce rank="2" nick="toyota2" gforce="0.80" unit="g"/>
+<top10gforce rank="3" nick="toyota3" gforce="0.70" unit="g"/>
+<top10gforce rank="4" nick="toyota4" gforce="0.60" unit="g"/>
+<top10gforce rank="5" nick="toyota5" gforce="0.50" unit="g"/>
+<top10gforce rank="6" nick="toyota6" gforce="0.40" unit="g"/>
+<top10gforce rank="7" nick="toyota7" gforce="0.30" unit="g"/>
+<top10gforce rank="8" nick="toyota8" gforce="0.20" unit="g"/>
+<top10gforce rank="9" nick="toyota9" gforce="0.10" unit="g"/>
+<top10gforce rank="10" nick="toyota10" gforce="0.09" unit="g"/>
+</xml>
diff --git a/Client/results.xml b/Client/results.xml
new file mode 100755 (executable)
index 0000000..deb1966
--- /dev/null
@@ -0,0 +1,13 @@
+<?xml version="1.0" encoding="utf-8"?>
+<results category="acceleration-0-100" unit="seconds" description="Acceleration from 0 to 100 km/h">
+       <result position="1" user="test928" date="12/1/2010" value="13" />
+       <result position="2" user="test922" date="15/1/2010" value="12" />
+       <result position="3" user="test92a" date="11/1/2010" value="11" />
+       <result position="4" user="test92s" date="15/2/2010" value="10" />
+       <result position="5" user="test92d" date="1/1/2010" value="9" />
+       <result position="6" user="test92f" date="31/1/2010" value="8" />
+       <result position="7" user="test92f" date="1/1/2010" value="7" />
+       <result position="8" user="test92g" date="2/1/2010" value="6" />
+       <result position="9" user="test92w" date="3/1/2010" value="5" />
+       <result position="10" user="test92a" date="17/1/2010" value="4" />
+</results>
index cf86ef1..3e26629 100644 (file)
@@ -1,59 +1,37 @@
-#include <QtGui>
-#include <QNetworkRequest>
-#include <QNetworkReply>
-#include <QIODevice>
 #include <QFile>
-#include <QMessageBox>
 #include <QDebug>
-#include <QDateTime>
-#include <QDate>
-#include <QTime>
-#include <QApplication>
+//#include <QApplication>
 #include "xmlreader.h"
 
-
-/*! @brief Constructor, connects object to GUI
- */
-XmlReader::XmlReader(Ui_MainWindow* myMainWindow)
+/**
+  *Constructor of this class.
+  */
+XmlReader::XmlReader()
 {
-    ui = myMainWindow;
+    xmlShow();
 }
 
-/*! @brief Destructor
+/**
+  *Destructor of this class. Should be used to release all allocated resources.
   */
 XmlReader::~XmlReader()
 {
-
-}
-
-QString XmlReader::errorString() const
-{
-    return QObject::tr("%1\nLine %2, column %3")
-            .arg(xmlreader.errorString())
-            .arg(xmlreader.lineNumber())
-            .arg(xmlreader.columnNumber());
+    category = "";
+    unit = "";
+    description = "";
+    position = "";
+    user = "";
+    value = "";
 }
 
-
-/*! @brief parsing xml file that is sent by server over network.
-  * Shows received information on screen and parsed information on command line.
-  * @note Partly commented for git.
-  * @todo (Development: Seems to omit element tags and some values when reading from QFile. Why? This used
-  * earlier with QNetworkReply printed all xml contents.)
-  * @todo Remove command line output and use final GUI widget names.
-  * @todo Replace using of QFile to QNetworkReply.
+/**
+  *This function is used to parsing xml file.
   */
 void XmlReader::xmlRead(QIODevice *device)
-//void XmlReader::xmlRead(QNetworkReply *device)
 {
     qDebug() << "_xmlRead";
 
     xmlreader.addData(device->readAll());
-    //Or: View webpage contents on textEdit by adding all data to xml stream reader,
-    //since readAll() empties the buffer
-    /*QByteArray readAllArr = device->readAll();
-    ui->textEditXml->append(readAllArr);
-    xmlreader.addData(readAllArr);*/
 
     //Go trough the xml document
     while(!xmlreader.atEnd())
@@ -63,75 +41,112 @@ void XmlReader::xmlRead(QIODevice *device)
         //Check if this element is starting element
         if(xmlreader.isStartElement())
         {
-            if(xmlreader.name() == "place")
+            if(xmlreader.name() == "results")
             {
                 qDebug() << xmlreader.name();
+                attr = xmlreader.attributes();
+
+                category = attr.value("category").toString();
+                unit = attr.value("unit").toString();
+                description = attr.value("description").toString();
+
+                top10List << category;
+                qDebug() << top10List << unit << description;
             }
-            if(xmlreader.name() == "date")
-            {
-                 qDebug() << xmlreader.name();
-            }
-            if(xmlreader.name() == "time")
-            {
-                 qDebug() << xmlreader.name();
-            }
+
             if(xmlreader.name() == "result")
             {
-                //Two consequent start elements
-                if(xmlreader.readNextStartElement())
+                qDebug() << "result";
+                attr = xmlreader.attributes();
+
+                position = attr.value("position").toString();
+                user = attr.value("user").toString();
+                value = attr.value("value").toString();
+
+                if (category == "acceleration-0-100")
                 {
-                    if(xmlreader.name() == "speed")
-                    {
-                    QString speed;
-                    QXmlStreamAttributes attr = xmlreader.attributes();
-                    speed = (attr.value("value").toString() + " " + attr.value("unit").toString());
-                    //ui->lineEditSpeed->setText(speed);
-                    qDebug() << xmlreader.name() << speed;
-                    }
+                    top10AccelerationList.append(position + "\t" +
+                                                user + "\t" +
+                                                value +
+                                                unit + "\t" +
+                                                description + "\n");
                 }
-            }
-            if(xmlreader.name() == "distance")
-            {
-                QString distance;
-                QXmlStreamAttributes attr = xmlreader.attributes();
-                distance = (attr.value("value").toString() + " " + attr.value("unit").toString());
-                //ui->lineEditDist->setText(distance);
-                qDebug() << xmlreader.name() << distance;
-            }
-            if(xmlreader.name() == "acceleration")
-            {
-                QString acceleration;
-                QXmlStreamAttributes attr = xmlreader.attributes();
-                acceleration = (attr.value("value").toString()+ " " + attr.value("unit").toString());
-                //ui->lineEditAccel->setText(acceleration);
-                qDebug() << xmlreader.name() << acceleration;
+
+                if(category == "top10speed")
+                {
+                    top10SpeedList.append(position + "\t" +
+                                          user + "\t" +
+                                          value +
+                                          unit + "\t" +
+                                          description + "\n");
+                }
+
+                if(category == "top10gforce")
+                {
+                    top10GforceList.append(position + "\t" +
+                                           user + "\t" +
+                                           value +
+                                           unit + "\t" +
+                                           description + "\n");
+                }
+                qDebug() << position << user << value << unit;
             }
         }
     }
 }
 
-/*! @brief A function used tmp in development.
-  * @note Partly harcoded and commented for git.
+/**
+  *This function is used to read example xml file (results.xml).
+  *@todo Read real xml.
   */
 void XmlReader::xmlShow()
 {
-    //QString filename = ui->lineEditFile->text();
-    QString filename = "xmlfile.xml";
+    QString filename = "results.xml";
     QFile file(filename);
 
     if (!file.open(QFile::ReadOnly))
     {
-        /*QMessageBox::warning(this->ui->centralWidget,
-                                tr("QXmlStream Bookmarks"),
-                                tr("Cannot write file %1:\n%2.")
-                                .arg(file.fileName())
-                                .arg(file.errorString()));*/
         qDebug() << "_xmlShow fail";
         return;
     }
 
     xmlRead(&file);
-
     file.close();
 }
 
+/**
+  *This is return function.
+  *@todo Read real top 10 category list
+  *@return QStringList top10List
+  */
+QStringList XmlReader::getTop10List()
+{
+    return top10List;
+}
+
+/**
+  *This is return function.
+  *@return QString top10AccelerationList
+  */
+QString XmlReader::getTop10AccelerationList()
+{
+    return top10AccelerationList;
+}
+
+/**
+  *This is return function.
+  *@return QString top10SpeedList
+  */
+QString XmlReader::getTop10SpeedList()
+{
+    return top10SpeedList;
+}
+
+/**
+  *This is return function.
+  *@return QString top10GforceList
+  */
+QString XmlReader::getTop10GforceList()
+{
+    return top10GforceList;
+}
index 4b19bfa..fa69133 100644 (file)
@@ -1,25 +1,37 @@
 #ifndef XMLREADER_H
 #define XMLREADER_H
 
-#include "ui_mainwindow.h"
-
+#include <QXmlStreamReader>
+#include <QStringList>
 
 class XmlReader : public QObject
 {
 public:
-    XmlReader(Ui_MainWindow* myMainWindow);
+    XmlReader();
     ~XmlReader();
-    QString errorString() const;
+    QStringList getTop10List();
+    QString getTop10AccelerationList();
+    QString getTop10SpeedList();
+    QString getTop10GforceList(); 
 
 private:
     QXmlStreamReader xmlreader;
-    Ui_MainWindow* ui;
+    QStringList top10List;
+    QString top10AccelerationList;
+    QString top10SpeedList;
+    QString top10GforceList;
+
+    QXmlStreamAttributes attr;
+    QString category;
+    QString unit;
+    QString description;
+    QString position;
+    QString user;
+    QString value;
 
 public slots:
-    //void xmlRead(QNetworkReply *device);
     void xmlRead(QIODevice* device);
     void xmlShow();
-
 };
 
 #endif // XMLREADER_H