implement eco details with webview
authorUser@User-PC.domain_not_set.invalid
Fri, 26 Mar 2010 15:22:34 -0700
changeset 10 79eeacfd15ff
parent 9 23f6727b5587
child 11 f3dbeee07821
implement eco details with webview
Seafood/Fishes.cpp
Seafood/Fishes.h
Seafood/mainwindow.cpp
Seafood/mainwindow.h
--- a/Seafood/Fishes.cpp	Fri Mar 26 15:19:41 2010 -0700
+++ b/Seafood/Fishes.cpp	Fri Mar 26 15:22:34 2010 -0700
@@ -12,9 +12,15 @@
     // Find QSLite driver
     db = QSqlDatabase::addDatabase("QSQLITE");
 
+#ifdef Q_OS_SYMBIAN
     QString dbFile = QDesktopServices::storageLocation(QDesktopServices::DataLocation)
                      + '/'  // Qt Universal file separator
                      + "seafood.db";
+#else
+    // Windows assumed.
+    // unfortunately, "C:\Documents and Settings" is corrupted on my home PC. hard coding until I fix it. -jk
+    QString dbFile = "C:/workspace/QtExamples/Seafood/populateDB/seafood.db";
+#endif
     QFile f(dbFile);
     std::string errString(dbFile.toStdString());
     if (!f.exists()) {
@@ -54,6 +60,33 @@
     }
 }
 
+/* given the name of a fish, return a list of eco details in html format.
+ */
+QString Fishes::getEcoDetails(QString name)
+{
+    QString detailsInHtml;
+    QSqlQuery query;
+
+    query.prepare("select details from ecoDetails "
+                  "where fid in (select fid from fish where name = :name )");
+    query.bindValue(":name",name);
+
+    if (!query.exec())
+    {
+        QString errCode =  "failed to get eco details " + query.lastError().text();
+        qWarning(errCode.toStdString().c_str());
+    }
+
+    detailsInHtml.append("<html> <title>name</title> <body> <h2>Eco Details</h2> <ul> ");
+    while (query.next()){
+        detailsInHtml.append( " <li>");
+        detailsInHtml.append( query.value(0).toString());
+        detailsInHtml.append( "</li> " );
+    }
+    detailsInHtml.append("</ul> </body> </html> ");
+    return  detailsInHtml;
+}
+
 void Fishes::populate(TCATEGORIES cat)
 {
     QSqlQuery query;
--- a/Seafood/Fishes.h	Fri Mar 26 15:19:41 2010 -0700
+++ b/Seafood/Fishes.h	Fri Mar 26 15:22:34 2010 -0700
@@ -15,12 +15,13 @@
     const QStringList GetOK();
     const QStringList GetWorst();
 
+    QString getEcoDetails(QString name);
+
 private:
     void populate(TCATEGORIES cat);
     QStringList bestFish;
     QStringList okFish;
     QStringList worstFish;
-    // QSqlDatabase db;
 };
 
 #endif // FISHES_H
--- a/Seafood/mainwindow.cpp	Fri Mar 26 15:19:41 2010 -0700
+++ b/Seafood/mainwindow.cpp	Fri Mar 26 15:22:34 2010 -0700
@@ -18,36 +18,48 @@
 {
     ui->setupUi(this);
     setContextMenuPolicy(Qt::NoContextMenu);
-    Fishes *f = new Fishes();
+    this->fishDb = new Fishes(this);
 
     this->stackedWidget = new QStackedWidget;
 
+    // lists of fish
     this->tabWidget = new QTabWidget;
-    // QIcon icon = new QIcon(":/database/icons/weather-sunny.svg");
-    // this->tabWidget->setTabIcon(1, icon);
 
     this->bestList = new QListWidget;
-    this->bestList->addItems(f->GetBest());
+    this->bestList->addItems(this->fishDb->GetBest());
     this->tabWidget->addTab(this->bestList, "best");
 
     this->okList = new QListWidget;
-    this->okList->addItems(f->GetOK());
+    this->okList->addItems(this->fishDb->GetOK());
     this->tabWidget->addTab(this->okList, "ok");
 
     this->worstList = new QListWidget;
-    this->worstList->addItems(f->GetWorst());
+    this->worstList->addItems(this->fishDb->GetWorst());
     this->tabWidget->addTab(this->worstList, "worst");
 
+    this->index = EBestList;
+
+    connect(this->tabWidget, SIGNAL(currentChanged(int)),
+            this, SLOT(setCurrentList(int)));
+
     this->stackedWidget->addWidget(tabWidget); // associated with EListPage
 
+    // Eco details
+
     this->ecoDetails = new QWebView;
     this->ecoDetails->setHtml("<html><title>Eco Details</title><ul><li>not yet implemented</li></ul></html>");
 
     this->stackedWidget->addWidget(this->ecoDetails); // associated with EEcoPage
 
+    // Nutrition page
+    // this->model = new QSqlQueryModel;
+    // this->model->
     this->stackedWidget->addWidget(ui->centralWidget); // associated with ENutritionPage
 
+
     setCentralWidget(stackedWidget);
+
+
 }
 
 MainWindow::~MainWindow()
@@ -81,8 +93,7 @@
 
 void MainWindow::displayVersion()
 {
-//    QMessageBox::information(this,"Qt Version", qVersion());
-     QMessageBox::information(this,"selected item", "NYI");
+    QMessageBox::information(this,"Qt Version", qVersion());
 }
 
 void MainWindow::displayNutrition()
@@ -92,6 +103,23 @@
 
 void MainWindow::displayEcoDetails()
 {
+
+    QString selectedName;
+    switch (this->index)
+    {
+    case EBestList:
+        selectedName = this->bestList->currentItem()->text();
+        break;
+    case EOkList:
+        selectedName = this->okList->currentItem()->text();
+        break;
+    case EWorstList:
+        selectedName = this->worstList->currentItem()->text();
+        break;
+    }
+
+    this->ecoDetails->setHtml( this->fishDb->getEcoDetails(selectedName));
+
     this->stackedWidget->setCurrentIndex(MainWindow::EEcoPage);
 }
 
@@ -114,6 +142,24 @@
 
 }
 
+void MainWindow::setCurrentList(int l)
+{
+    switch (l)
+    {
+    case 0:
+        this->index = EBestList;
+        break;
+    case 1:
+        this->index = EOkList;
+        break;
+    case 2:
+        this->index = EWorstList;
+        break;
+    }
+
+
+}
+
 void MainWindow::changeEvent(QEvent *e)
 {
     QMainWindow::changeEvent(e);
--- a/Seafood/mainwindow.h	Fri Mar 26 15:19:41 2010 -0700
+++ b/Seafood/mainwindow.h	Fri Mar 26 15:22:34 2010 -0700
@@ -3,10 +3,12 @@
 
 #include <QMainWindow>
 
+class Fishes;
 class QStackedWidget;
 class QTabWidget;
 class QListWidget;
 class QWebView;
+class QSqlQueryModel;
 
 namespace Ui {
     class MainWindow;
@@ -21,6 +23,7 @@
     enum {EListPage = 0,
           EEcoPage,
           ENutritionPage};
+    enum ListIndex {EBestList =0, EOkList, EWorstList};
 
     void createMenus();
 
@@ -30,6 +33,7 @@
     void displayEcoDetails();
     void displayList();
     void displayNutrition();
+    void setCurrentList(int l);
 
 protected:
     void changeEvent(QEvent *e);
@@ -44,6 +48,10 @@
 
     QWebView *ecoDetails;
 
+    QSqlQueryModel *model;
+
+    ListIndex index; // which list is currently displayed in tab widget
+    Fishes *fishDb;
     QAction *ecoAction;
     QAction *fishesAction;
     QAction *nuAction;