get nutrition information from db
authorJohn Kern <johnk@symbian.org>
Tue, 06 Apr 2010 08:02:52 -0700
changeset 14 a16afe3df8c9
parent 13 92f7e918c7b5
child 15 0f80a0f39475
get nutrition information from db
Seafood/Fishes.cpp
Seafood/Fishes.h
Seafood/SeafoodTabbed.pro.user
Seafood/mainwindow.cpp
Seafood/mainwindow.h
--- a/Seafood/Fishes.cpp	Thu Apr 01 14:57:00 2010 -0700
+++ b/Seafood/Fishes.cpp	Tue Apr 06 08:02:52 2010 -0700
@@ -8,7 +8,6 @@
 Fishes::Fishes(QObject *parent) : QObject(parent)
 {
     QSqlDatabase db;
-    this->dbErrString = "noErr";
 
     // Find QSLite driver
     db = QSqlDatabase::addDatabase("QSQLITE");
@@ -24,7 +23,6 @@
     QString dbFile = "C:/workspace/QtExamples/Seafood/populateDB/seafood.db";
 #endif
     QFile f(dbFile);
-    std::string errString(dbFile.toStdString());
     if (!f.exists()) {
         qWarning("db not found ");
     } else {
@@ -36,8 +34,6 @@
     // Open databasee
     if(!db.open())
     {
-        this->dbErrString =  db.lastError().databaseText();
-
         qWarning("DB: failed to open.");
 
         this->bestFish << "Abalone (farmed)" << "Anchovy, European" << "Barramundi U.S."
@@ -68,7 +64,6 @@
 {
     QString detailsInHtml;
     QSqlQuery query;
-    this->dbErrString = "noErr";
 
     query.prepare("select details from ecoDetails "
                   "where fid in (select fid from fish where name = :name )");
@@ -78,7 +73,6 @@
     {
         QString errCode =  "failed to get eco details " + query.lastError().text();
         qWarning(errCode.toStdString().c_str());
-        this->dbErrString = name + " " + query.lastError().text();
     }
 
     detailsInHtml.append("<html> <title>name</title> <body> <h2>Eco Details</h2> <ul> ");
@@ -91,6 +85,36 @@
     return  detailsInHtml;
 }
 
+void Fishes::getNutrition(QString name)
+{
+    QSqlQuery query;
+    QMap<QString, QString> nutrition;
+
+    query.prepare("select calories,fat,protein,omega3,cholesterol,sodium from fish where name = :name ");
+    query.bindValue(":name",name);
+
+    if (!query.exec())
+    {
+        QString errCode =  "failed to get nutrition information " + query.lastError().text();
+        qWarning(errCode.toStdString().c_str());
+    }
+
+    query.next();
+    nutrition["Calories"] = query.value(0).toString();
+    nutrition["Total Fat"] = query.value(1).toString();
+    nutrition["Total Protein"] = query.value(2).toString();
+    nutrition["Omega-3"] = query.value(3).toString();
+    nutrition["Cholesterol"] = query.value(4).toString();
+    nutrition["Sodium"] = query.value(5).toString();
+
+    QMapIterator<QString, QString> i(nutrition);
+    while (i.hasNext())
+    {
+         i.next();
+         qDebug() << i.key() << ": " << i.value() << endl;
+    }
+}
+
 void Fishes::populate(TCATEGORIES cat)
 {
     QSqlQuery query;
@@ -126,18 +150,18 @@
 }
 
 
-const QStringList Fishes::GetBest()
+const QStringList Fishes::getBest()
 {
 
     return bestFish;
 }
 
-const QStringList Fishes::GetOK()
+const QStringList Fishes::getOK()
 {
     return okFish;
 }
 
-const QStringList Fishes::GetWorst()
+const QStringList Fishes::getWorst()
 {
     return worstFish;
 }
--- a/Seafood/Fishes.h	Thu Apr 01 14:57:00 2010 -0700
+++ b/Seafood/Fishes.h	Tue Apr 06 08:02:52 2010 -0700
@@ -11,19 +11,18 @@
     Fishes(QObject *parent=0);
     enum TCATEGORIES {EPresentBest=0, EPresentOK, EPresentWorst};
 
-    const QStringList GetBest();
-    const QStringList GetOK();
-    const QStringList GetWorst();
-    const QString GetDbErr() { return dbErrString; };
+    const QStringList getBest();
+    const QStringList getOK();
+    const QStringList getWorst();
 
     QString getEcoDetails(QString name);
+    void getNutrition(QString name);
 
 private:
     void populate(TCATEGORIES cat);
     QStringList bestFish;
     QStringList okFish;
     QStringList worstFish;
-    QString dbErrString;
 };
 
 #endif // FISHES_H
--- a/Seafood/SeafoodTabbed.pro.user	Thu Apr 01 14:57:00 2010 -0700
+++ b/Seafood/SeafoodTabbed.pro.user	Tue Apr 06 08:02:52 2010 -0700
@@ -2,7 +2,7 @@
 <qtcreator>
  <data>
   <variable>ProjectExplorer.Project.ActiveTarget</variable>
-  <value type="int">1</value>
+  <value type="int">0</value>
  </data>
  <data>
   <variable>ProjectExplorer.Project.EditorSettings</variable>
@@ -103,97 +103,8 @@
   </valuemap>
  </data>
  <data>
-  <variable>ProjectExplorer.Project.Target.1</variable>
-  <valuemap type="QVariantMap">
-   <value key="ProjectExplorer.ProjectConfiguration.DisplayName" type="QString">Symbian Device</value>
-   <value key="ProjectExplorer.ProjectConfiguration.Id" type="QString">Qt4ProjectManager.Target.S60DeviceTarget</value>
-   <value key="ProjectExplorer.Target.ActiveBuildConfiguration" type="int">1</value>
-   <value key="ProjectExplorer.Target.ActiveRunConfiguration" type="int">0</value>
-   <valuemap key="ProjectExplorer.Target.BuildConfiguration.0" type="QVariantMap">
-    <valuemap key="ProjectExplorer.BuildConfiguration.BuildStep.0" type="QVariantMap">
-     <value key="ProjectExplorer.ProjectConfiguration.DisplayName" type="QString">qmake</value>
-     <value key="ProjectExplorer.ProjectConfiguration.Id" type="QString">QtProjectManager.QMakeBuildStep</value>
-     <valuelist key="QtProjectManager.QMakeBuildStep.QMakeArguments" type="QVariantList"/>
-    </valuemap>
-    <valuemap key="ProjectExplorer.BuildConfiguration.BuildStep.1" type="QVariantMap">
-     <value key="ProjectExplorer.ProjectConfiguration.DisplayName" type="QString">Make</value>
-     <value key="ProjectExplorer.ProjectConfiguration.Id" type="QString">Qt4ProjectManager.MakeStep</value>
-     <value key="Qt4ProjectManager.MakeStep.Clean" type="bool">false</value>
-     <valuelist key="Qt4ProjectManager.MakeStep.MakeArguments" type="QVariantList"/>
-     <value key="Qt4ProjectManager.MakeStep.MakeCommand" type="QString"></value>
-    </valuemap>
-    <value key="ProjectExplorer.BuildConfiguration.BuildStepsCount" type="int">2</value>
-    <valuemap key="ProjectExplorer.BuildConfiguration.CleanStep.0" type="QVariantMap">
-     <value key="ProjectExplorer.ProjectConfiguration.DisplayName" type="QString">Make</value>
-     <value key="ProjectExplorer.ProjectConfiguration.Id" type="QString">Qt4ProjectManager.MakeStep</value>
-     <value key="Qt4ProjectManager.MakeStep.Clean" type="bool">true</value>
-     <valuelist key="Qt4ProjectManager.MakeStep.MakeArguments" type="QVariantList">
-      <value type="QString">clean</value>
-     </valuelist>
-     <value key="Qt4ProjectManager.MakeStep.MakeCommand" type="QString"></value>
-    </valuemap>
-    <value key="ProjectExplorer.BuildConfiguration.CleanStepsCount" type="int">1</value>
-    <value key="ProjectExplorer.ProjectConfiguration.DisplayName" type="QString">Debug</value>
-    <value key="ProjectExplorer.ProjectConfiguration.Id" type="QString">Qt4ProjectManager.Qt4BuildConfiguration</value>
-    <value key="Qt4ProjectManager.Qt4BuildConfiguration.BuildConfiguration" type="int">2</value>
-    <value key="Qt4ProjectManager.Qt4BuildConfiguration.BuildDirectory" type="QString">C:\workspace\QtExamples\Seafood</value>
-    <value key="Qt4ProjectManager.Qt4BuildConfiguration.ClearSystemEnvironment" type="bool">false</value>
-    <value key="Qt4ProjectManager.Qt4BuildConfiguration.QtVersionId" type="int">5</value>
-    <value key="Qt4ProjectManager.Qt4BuildConfiguration.ToolChain" type="int">6</value>
-    <value key="Qt4ProjectManager.Qt4BuildConfiguration.UseShadowBuild" type="bool">false</value>
-    <valuelist key="Qt4ProjectManager.Qt4BuildConfiguration.UserEnvironmentChanges" type="QVariantList"/>
-   </valuemap>
-   <valuemap key="ProjectExplorer.Target.BuildConfiguration.1" type="QVariantMap">
-    <valuemap key="ProjectExplorer.BuildConfiguration.BuildStep.0" type="QVariantMap">
-     <value key="ProjectExplorer.ProjectConfiguration.DisplayName" type="QString">qmake</value>
-     <value key="ProjectExplorer.ProjectConfiguration.Id" type="QString">QtProjectManager.QMakeBuildStep</value>
-     <valuelist key="QtProjectManager.QMakeBuildStep.QMakeArguments" type="QVariantList"/>
-    </valuemap>
-    <valuemap key="ProjectExplorer.BuildConfiguration.BuildStep.1" type="QVariantMap">
-     <value key="ProjectExplorer.ProjectConfiguration.DisplayName" type="QString">Make</value>
-     <value key="ProjectExplorer.ProjectConfiguration.Id" type="QString">Qt4ProjectManager.MakeStep</value>
-     <value key="Qt4ProjectManager.MakeStep.Clean" type="bool">false</value>
-     <valuelist key="Qt4ProjectManager.MakeStep.MakeArguments" type="QVariantList"/>
-     <value key="Qt4ProjectManager.MakeStep.MakeCommand" type="QString"></value>
-    </valuemap>
-    <value key="ProjectExplorer.BuildConfiguration.BuildStepsCount" type="int">2</value>
-    <valuemap key="ProjectExplorer.BuildConfiguration.CleanStep.0" type="QVariantMap">
-     <value key="ProjectExplorer.ProjectConfiguration.DisplayName" type="QString">Make</value>
-     <value key="ProjectExplorer.ProjectConfiguration.Id" type="QString">Qt4ProjectManager.MakeStep</value>
-     <value key="Qt4ProjectManager.MakeStep.Clean" type="bool">true</value>
-     <valuelist key="Qt4ProjectManager.MakeStep.MakeArguments" type="QVariantList">
-      <value type="QString">clean</value>
-     </valuelist>
-     <value key="Qt4ProjectManager.MakeStep.MakeCommand" type="QString"></value>
-    </valuemap>
-    <value key="ProjectExplorer.BuildConfiguration.CleanStepsCount" type="int">1</value>
-    <value key="ProjectExplorer.ProjectConfiguration.DisplayName" type="QString">Release</value>
-    <value key="ProjectExplorer.ProjectConfiguration.Id" type="QString">Qt4ProjectManager.Qt4BuildConfiguration</value>
-    <value key="Qt4ProjectManager.Qt4BuildConfiguration.BuildConfiguration" type="int">0</value>
-    <value key="Qt4ProjectManager.Qt4BuildConfiguration.BuildDirectory" type="QString">C:\workspace\QtExamples\Seafood</value>
-    <value key="Qt4ProjectManager.Qt4BuildConfiguration.ClearSystemEnvironment" type="bool">false</value>
-    <value key="Qt4ProjectManager.Qt4BuildConfiguration.QtVersionId" type="int">5</value>
-    <value key="Qt4ProjectManager.Qt4BuildConfiguration.ToolChain" type="int">6</value>
-    <value key="Qt4ProjectManager.Qt4BuildConfiguration.UseShadowBuild" type="bool">false</value>
-    <valuelist key="Qt4ProjectManager.Qt4BuildConfiguration.UserEnvironmentChanges" type="QVariantList"/>
-   </valuemap>
-   <value key="ProjectExplorer.Target.BuildConfigurationCount" type="int">2</value>
-   <valuemap key="ProjectExplorer.Target.RunConfiguration.0" type="QVariantMap">
-    <value key="ProjectExplorer.ProjectConfiguration.DisplayName" type="QString">SeafoodTabbed on Symbian Device</value>
-    <value key="ProjectExplorer.ProjectConfiguration.Id" type="QString">Qt4ProjectManager.S60DeviceRunConfiguration</value>
-    <valuelist key="Qt4ProjectManager.S60DeviceRunConfiguration.CommandLineArguments" type="QVariantList"/>
-    <value key="Qt4ProjectManager.S60DeviceRunConfiguration.CustomKeyPath" type="QString"></value>
-    <value key="Qt4ProjectManager.S60DeviceRunConfiguration.CustomSignaturePath" type="QString"></value>
-    <value key="Qt4ProjectManager.S60DeviceRunConfiguration.ProFile" type="QString">SeafoodTabbed.pro</value>
-    <value key="Qt4ProjectManager.S60DeviceRunConfiguration.SerialPortName" type="QString"></value>
-    <value key="Qt4ProjectManager.S60DeviceRunConfiguration.SigningMode" type="int">0</value>
-   </valuemap>
-   <value key="ProjectExplorer.Target.RunConfigurationCount" type="int">1</value>
-  </valuemap>
- </data>
- <data>
   <variable>ProjectExplorer.Project.TargetCount</variable>
-  <value type="int">2</value>
+  <value type="int">1</value>
  </data>
  <data>
   <variable>ProjectExplorer.Project.Updater.FileVersion</variable>
--- a/Seafood/mainwindow.cpp	Thu Apr 01 14:57:00 2010 -0700
+++ b/Seafood/mainwindow.cpp	Tue Apr 06 08:02:52 2010 -0700
@@ -26,15 +26,15 @@
     this->tabWidget = new QTabWidget;
 
     this->bestList = new QListWidget;
-    this->bestList->addItems(this->fishDb->GetBest());
+    this->bestList->addItems(this->fishDb->getBest());
     this->tabWidget->addTab(this->bestList, "best");
 
     this->okList = new QListWidget;
-    this->okList->addItems(this->fishDb->GetOK());
+    this->okList->addItems(this->fishDb->getOK());
     this->tabWidget->addTab(this->okList, "ok");
 
     this->worstList = new QListWidget;
-    this->worstList->addItems(this->fishDb->GetWorst());
+    this->worstList->addItems(this->fishDb->getWorst());
     this->tabWidget->addTab(this->worstList, "worst");
 
     this->index = EBestList;
@@ -84,7 +84,7 @@
 
         verAction = new QAction(tr("&Version"),this);
         menuBar()->addAction(verAction);
-        connect(verAction, SIGNAL(triggered()),this, SLOT(displayPath()));
+        connect(verAction, SIGNAL(triggered()),this, SLOT(displayVersion()));
 
         exitAction = new QAction(tr("&Exit"),this);
         menuBar()->addAction(exitAction);
@@ -96,44 +96,42 @@
     QMessageBox::information(this,"Qt Version", qVersion());
 }
 
-void MainWindow::displayNutrition()
-{
-    this->stackedWidget->setCurrentIndex(MainWindow::ENutritionPage);
-}
+ QString MainWindow::currentlySelectedItem()
+ {
+     QString selectedName;
+     QListWidgetItem *item=NULL;
+
+     switch (this->index)
+     {
+     case EBestList:
+         item = this->bestList->currentItem();
+         if (item)
+         {
+             selectedName = item->text();
+         }
+
+         break;
+     case EOkList:
+         item = this->okList->currentItem();
+         if (item) {
+             selectedName = item->text();
+         }
+         break;
+     case EWorstList:
+         item = this->worstList->currentItem();
+         if (item) {
+             selectedName = item->text();
+         }
+         break;
+     }
+     return selectedName;
+ }
 
 void MainWindow::displayEcoDetails()
 {
-
-    QString selectedName;
-    QListWidgetItem *item=NULL;
-
-
-    switch (this->index)
-    {
-
-    case EBestList:
-        item = this->bestList->currentItem();
-        if (item)
-        {
-            selectedName = item->text();
-        }
+    QString selectedName = this->currentlySelectedItem();
 
-        break;
-    case EOkList:
-        item = this->okList->currentItem();
-        if (item) {
-            selectedName = item->text();
-        }
-        break;
-    case EWorstList:
-        item = this->worstList->currentItem();
-        if (item) {
-            selectedName = item->text();
-        }
-        break;
-    }
-
-    if (!item) {
+    if (selectedName.isEmpty()) {
         QMessageBox::information(this,"warning","select an item from list." );
     } else {
         this->ecoDetails->setHtml( this->fishDb->getEcoDetails(selectedName));
@@ -141,16 +139,24 @@
     }
 }
 
+void MainWindow::displayNutrition()
+{
+    QString selectedName = this->currentlySelectedItem();
+
+    if (selectedName.isEmpty()) {
+        QMessageBox::information(this,"warning","select an item from list." );
+    } else {
+      // this->ecoDetails->setHtml( this->fishDb->getNutrition(selectedName));
+        this->fishDb->getNutrition(selectedName);
+        this->stackedWidget->setCurrentIndex(MainWindow::ENutritionPage);
+    }
+}
+
 void MainWindow::displayList()
 {
     this->stackedWidget->setCurrentIndex(MainWindow::EListPage);
 }
 
-void MainWindow::displayPath()
-{
-    QMessageBox::information(this, "db Error",this->fishDb->GetDbErr());
-}
-
 void MainWindow::setCurrentList(int l)
 {
     switch (l)
--- a/Seafood/mainwindow.h	Thu Apr 01 14:57:00 2010 -0700
+++ b/Seafood/mainwindow.h	Tue Apr 06 08:02:52 2010 -0700
@@ -28,8 +28,8 @@
     void createMenus();
 
 public slots:
+    QString currentlySelectedItem();
     void displayVersion();
-    void displayPath();
     void displayEcoDetails();
     void displayList();
     void displayNutrition();