From 583b6ab4a1a3b75e190549609dad24cc480afcb7 Mon Sep 17 00:00:00 2001 From: birkeh Date: Mon, 6 Feb 2017 16:04:25 +0100 Subject: [PATCH] init --- Kooky/Kooky.pro | 9 ++-- Kooky/cdbinterface.h | 3 ++ Kooky/cimportingredientdialog.cpp | 32 ++++++++++--- Kooky/cimportingredientdialog.h | 5 +- Kooky/cimportingredientdialog.ui | 34 +++++++++---- Kooky/cingredient.cpp | 39 ++++++++++++++- Kooky/cingredient.h | 12 ++++- Kooky/cingredientwindow.cpp | 28 +++++++++++ Kooky/cingredientwindow.h | 31 ++++++++++++ Kooky/cingredientwindow.ui | 19 ++++++++ Kooky/cmainwindow.cpp | 80 +++++++++++++++++++++---------- Kooky/cmainwindow.h | 2 +- Kooky/cmainwindow.ui | 25 +++++----- Kooky/typedef.h | 3 ++ dMySQL/cmysqlplugin.cpp | 57 ++++++++++++++++++++-- dMySQL/cmysqlplugin.h | 3 ++ dSQLite/csqliteplugin.cpp | 56 ++++++++++++++++++++-- dSQLite/csqliteplugin.h | 3 ++ iBleibFit/cbleibfitplugin.cpp | 3 ++ 19 files changed, 378 insertions(+), 66 deletions(-) create mode 100644 Kooky/cingredientwindow.cpp create mode 100644 Kooky/cingredientwindow.h create mode 100644 Kooky/cingredientwindow.ui diff --git a/Kooky/Kooky.pro b/Kooky/Kooky.pro index 6c29286..58b072d 100644 --- a/Kooky/Kooky.pro +++ b/Kooky/Kooky.pro @@ -24,7 +24,8 @@ SOURCES += main.cpp\ cimportingredientdialog.cpp \ cmessageanimatedialog.cpp \ cplugininfo.cpp \ - csplashscreen.cpp + csplashscreen.cpp \ + cingredientwindow.cpp HEADERS += cmainwindow.h \ cimportinterface.h \ @@ -39,14 +40,16 @@ HEADERS += cmainwindow.h \ cmessageanimatedialog.h \ cplugininfo.h \ typedef.h \ - csplashscreen.h + csplashscreen.h \ + cingredientwindow.h FORMS += cmainwindow.ui \ coptions.ui \ coptionsplugins.ui \ cimportingredientdialog.ui \ cmessageanimatedialog.ui \ - cplugininfo.ui + cplugininfo.ui \ + cingredientwindow.ui DESTDIR = ../bin diff --git a/Kooky/cdbinterface.h b/Kooky/cdbinterface.h index eb7522a..6f57df8 100644 --- a/Kooky/cdbinterface.h +++ b/Kooky/cdbinterface.h @@ -83,6 +83,9 @@ public: virtual qint32 create(const QString& szName, const QString& szGroup) = 0; virtual qint32 createGroup(const QString& szGroup) = 0; virtual bool set(qint32 id, qint16 ingredientNumber, qreal value) = 0; + virtual QString name(qint32 id) = 0; + virtual QString group(qint32 id) = 0; + virtual qreal get(qint32 id, qint16 ingredientNumber) = 0; virtual INGREDIENT_LIST ingredients() = 0; virtual QStringList groups() = 0; virtual bool beginTransaction() = 0; diff --git a/Kooky/cimportingredientdialog.cpp b/Kooky/cimportingredientdialog.cpp index 42ca84e..dae4d02 100644 --- a/Kooky/cimportingredientdialog.cpp +++ b/Kooky/cimportingredientdialog.cpp @@ -51,7 +51,7 @@ void cImportIngredientDialog::initIngredientDetails() m_lpIngredientDetailsModel->setHorizontalHeaderLabels(headerLabels); } -void cImportIngredientDialog::setPluginList(const QList pluginList) +void cImportIngredientDialog::setPluginList(const QList pluginList, const QString& szPlugin) { m_pluginList = pluginList; @@ -65,7 +65,27 @@ void cImportIngredientDialog::setPluginList(const QList pluginList) ui->m_lpSource->addItem(lpPlugin->pluginName(), QVariant::fromValue(lpPlugin)); } } - ui->m_lpSource->setCurrentIndex(0); + if(ui->m_lpSource->count()) + { + int index = ui->m_lpSource->findText(szPlugin); + if(index != -1) + ui->m_lpSource->setCurrentIndex(index); + } +} + +void cImportIngredientDialog::setIngredientGroupList(const QStringList ingredientGroupList) +{ + ui->m_lpIngredientGroup->addItems(ingredientGroupList); + + int index = ui->m_lpIngredientGroup->findText("default"); + if(index == -1) + index = 0; + ui->m_lpIngredientGroup->setCurrentIndex(index); +} + +QString cImportIngredientDialog::pluginSelected() +{ + return(ui->m_lpSource->currentText()); } void cImportIngredientDialog::on_m_lpSearchString_textChanged(const QString &/*arg1*/) @@ -189,17 +209,17 @@ cIngredient cImportIngredientDialog::toIngredient() cPlugin* lpPlugin = ui->m_lpSource->currentData().value(); if(!lpPlugin) - return(cIngredient("")); + return(cIngredient("", "")); cImportInterface* lpImport = lpPlugin->importInterface(); if(!lpImport) - return(cIngredient("")); + return(cIngredient("", "")); if(!lpImport->loaded()) - return(cIngredient("")); + return(cIngredient("", "")); - cIngredient ingredient(lpImport->ingredientName()); + cIngredient ingredient(lpImport->ingredientName(), ui->m_lpIngredientGroup->currentText()); for(int z = 0;z < cIngredient::iIngredientMax;z++) { if(lpImport->value((cIngredient::iIngredient)z) != -1) diff --git a/Kooky/cimportingredientdialog.h b/Kooky/cimportingredientdialog.h index 74a3717..de1c560 100644 --- a/Kooky/cimportingredientdialog.h +++ b/Kooky/cimportingredientdialog.h @@ -59,9 +59,12 @@ public: * * \param pluginList */ - void setPluginList(const QList pluginList); + void setPluginList(const QList pluginList, const QString& szPlugin = QString("")); cIngredient toIngredient(); + QString pluginSelected(); + + void setIngredientGroupList(const QStringList ingredientGroupList); private: Ui::cImportIngredientDialog* ui; /*!< TODO: describe */ QStandardItemModel* m_lpIngredientListModel; /*!< TODO: describe */ diff --git a/Kooky/cimportingredientdialog.ui b/Kooky/cimportingredientdialog.ui index 0e40669..aeedde8 100644 --- a/Kooky/cimportingredientdialog.ui +++ b/Kooky/cimportingredientdialog.ui @@ -29,16 +29,6 @@ - - - - Qt::Horizontal - - - QDialogButtonBox::Cancel|QDialogButtonBox::Ok - - - @@ -60,6 +50,16 @@ + + + + Qt::Horizontal + + + QDialogButtonBox::Cancel|QDialogButtonBox::Ok + + + @@ -77,6 +77,20 @@ + + + + + + Ingredient Group: + + + + + + + + diff --git a/Kooky/cingredient.cpp b/Kooky/cingredient.cpp index a8f36be..faf0acc 100644 --- a/Kooky/cingredient.cpp +++ b/Kooky/cingredient.cpp @@ -180,14 +180,39 @@ static GROUPNAME g_groupname[] = { QString::fromUtf8("average consumption"), QString::fromUtf8("Others"), cIngredient::iIngredientaverageconsumption }, }; -cIngredient::cIngredient(const QString &szIngredientName) : +cIngredient::cIngredient() : + m_szIngredientName(""), + m_szIngredientGroup(""), + m_iID(-1) +{ +} + +cIngredient::cIngredient(const QString &szIngredientName, const QString& szIngredientGroup) : m_szIngredientName(szIngredientName), + m_szIngredientGroup(szIngredientGroup), m_iID(0) { for(int z = 0;z < iIngredientMax;z++) m_dValue[z] = -1; } +bool cIngredient::load(qint32 iIngredient, cPlugin* lpDB) +{ + if(!lpDB) + return(false); + + cDBInterface* lpInterface = lpDB->dbInterface(); + + m_iID = iIngredient; + m_szIngredientName = lpInterface->name(iIngredient); + m_szIngredientGroup = lpInterface->group(iIngredient); + + for(int z = 0;z < cIngredient::iIngredientMax;z++) + m_dValue[z] = lpInterface->get(iIngredient, z); + + return(true); +} + bool cIngredient::reload(cPlugin* /*lpDB*/) { return(true); @@ -202,7 +227,7 @@ bool cIngredient::save(cPlugin* lpDB) lpInterface->beginTransaction(); - qint32 id = lpInterface->create(m_szIngredientName, "default"); + qint32 id = lpInterface->create(m_szIngredientName, m_szIngredientGroup); if(id == -1) return(false); @@ -220,6 +245,16 @@ bool cIngredient::save(cPlugin* lpDB) return(true); } +QString cIngredient::ingredientName() +{ + return(m_szIngredientName); +} + +QString cIngredient::ingredientGroup() +{ + return(m_szIngredientGroup); +} + QString cIngredient::group(cIngredient::iIngredient i) { for(int z = 0;z < (int)sizeof(g_groupname)/(int)sizeof(GROUPNAME);z++) diff --git a/Kooky/cingredient.h b/Kooky/cingredient.h index 4d22d54..dd4fea7 100644 --- a/Kooky/cingredient.h +++ b/Kooky/cingredient.h @@ -201,7 +201,8 @@ public: iIngredientMax, }; - cIngredient(const QString& szIngredientName); + cIngredient(); + cIngredient(const QString& szIngredientName, const QString& szIngredientGroup); void setValue(cIngredient::iIngredient i, qreal dValue); /*! @@ -209,6 +210,12 @@ public: \return bool */ + bool load(qint32 iIngredient, cPlugin* lpDB); + /*! + \brief + + \return bool + */ bool reload(cPlugin* lpDB); /*! \brief @@ -217,6 +224,8 @@ public: */ bool save(cPlugin* lpDB); + QString ingredientName(); + QString ingredientGroup(); /*! \brief @@ -242,6 +251,7 @@ public: private: QString m_szIngredientName; + QString m_szIngredientGroup; qint32 m_iID; qreal m_dValue[iIngredientMax]; }; diff --git a/Kooky/cingredientwindow.cpp b/Kooky/cingredientwindow.cpp new file mode 100644 index 0000000..966a448 --- /dev/null +++ b/Kooky/cingredientwindow.cpp @@ -0,0 +1,28 @@ +#include "cingredientwindow.h" +#include "ui_cingredientwindow.h" + +#include "cingredient.h" + + +cIngredientWindow::cIngredientWindow(QWidget *parent) : + QWidget(parent), + ui(new Ui::cIngredientWindow) +{ + ui->setupUi(this); +} + +cIngredientWindow::~cIngredientWindow() +{ + delete ui; +} + +void cIngredientWindow::setIngredient(qint32 iIngredient, cPlugin* lpPlugin) +{ + m_iIngredient = iIngredient; + m_lpDBPlugin = lpPlugin; + cIngredient ingredient; + if(!ingredient.load(m_iIngredient, m_lpDBPlugin)) + return; + + setWindowTitle(ingredient.ingredientName()); +} diff --git a/Kooky/cingredientwindow.h b/Kooky/cingredientwindow.h new file mode 100644 index 0000000..b352d27 --- /dev/null +++ b/Kooky/cingredientwindow.h @@ -0,0 +1,31 @@ +#ifndef CINGREDIENTWINDOW_H +#define CINGREDIENTWINDOW_H + + +#include "cplugin.h" + +#include + + +namespace Ui { +class cIngredientWindow; +} + + +class cIngredientWindow : public QWidget +{ + Q_OBJECT + +public: + explicit cIngredientWindow(QWidget *parent = 0); + ~cIngredientWindow(); + + void setIngredient(qint32 iIngredient, cPlugin* lpPlugin); +private: + Ui::cIngredientWindow *ui; + + cPlugin* m_lpDBPlugin; + qint32 m_iIngredient; +}; + +#endif // CINGREDIENTWINDOW_H diff --git a/Kooky/cingredientwindow.ui b/Kooky/cingredientwindow.ui new file mode 100644 index 0000000..d50f21e --- /dev/null +++ b/Kooky/cingredientwindow.ui @@ -0,0 +1,19 @@ + + + cIngredientWindow + + + + 0 + 0 + 791 + 599 + + + + Ingredient + + + + + diff --git a/Kooky/cmainwindow.cpp b/Kooky/cmainwindow.cpp index a2c7a90..1234b8f 100644 --- a/Kooky/cmainwindow.cpp +++ b/Kooky/cmainwindow.cpp @@ -3,6 +3,8 @@ #include "cimportinterface.h" #include "cingredient.h" +#include "cingredientwindow.h" + #include "typedef.h" #include "coptions.h" @@ -19,6 +21,7 @@ #include #include #include +#include cMainWindow::cMainWindow(QWidget *parent) : @@ -30,7 +33,8 @@ cMainWindow::cMainWindow(QWidget *parent) : m_lpIngredientsListDelete(0), m_lpIngredientsListEdit(0), m_lpIngredientsListMenu(0), - m_lpDB(0) + m_lpDB(0), + m_szLastImportPlugin("") { QThread::msleep(2000); init(); @@ -56,11 +60,21 @@ void cMainWindow::init() ui->setupUi(this); + ui->m_lpMDIArea->setViewMode(QMdiArea::TabbedView); + ui->m_lpMDIArea->setTabsClosable(true); + ui->m_lpMDIArea->setTabsMovable(true); + // loadPlugins(QString("%1%2%3").arg(QDir::currentPath()).arg(QDir::separator()).arg("plugins")); loadPlugins(QString("%1%2%3").arg(qApp->applicationDirPath()).arg(QDir::separator()).arg("plugins")); - m_lpIngredientsListModel = new QStandardItemModel(0, 1); + m_lpIngredientsListModel = new QStandardItemModel(0, 3); + QStringList header; + ui->m_lpIngredientsList->setModel(m_lpIngredientsListModel); + header << tr("Ingredient") << tr("Calories") << tr("Carboh."); + m_lpIngredientsListModel->setHorizontalHeaderLabels(header); + ui->m_lpIngredientsList->header()->setStretchLastSection(false); + ui->m_lpIngredientsList->header()->setSectionResizeMode(0, QHeaderView::Stretch); connect(ui->m_lpMenu_File_Exit, SIGNAL(triggered(bool)), this, SLOT(menuFileExitTriggered())); connect(ui->m_lpMenu_Tools_Options, SIGNAL(triggered(bool)), this, SLOT(menuToolsOptionsTriggered())); @@ -256,22 +270,26 @@ void cMainWindow::loadIngredients() lpGroupItem = new QStandardItem(szOldGroup); m_lpIngredientsListModel->appendRow(lpGroupItem); } - QStandardItem* lpNew = new QStandardItem(ingredients.at(z).szIngredient); - lpGroupItem->appendRow(lpNew); + QStandardItem* lpNew = new QStandardItem(ingredients.at(z).szIngredient); + lpNew->setData(ingredients.at(z).iIngredient); + QStandardItem* lpCalories = new QStandardItem(QString("%1 kCal").arg(ingredients.at(z).dCalories)); + QStandardItem* lpCarbos = new QStandardItem(QString("%1 g").arg(ingredients.at(z).dCarbohydrates, 0, 'f', 1)); + lpCalories->setTextAlignment(Qt::AlignRight); + lpCarbos->setTextAlignment(Qt::AlignRight); + lpGroupItem->appendRow(QList() << lpNew << lpCalories << lpCarbos); } } } + ui->m_lpIngredientsList->expandAll(); + + ui->m_lpIngredientsList->resizeColumnToContents(0); + ui->m_lpIngredientsList->resizeColumnToContents(1); + ui->m_lpIngredientsList->resizeColumnToContents(2); } void cMainWindow::pluginImportTriggered() { -/* - QSettings settings; - cIngredient ingredient(QString("%1/000001.ingredient").arg(settings.value("program/ingredientDir").toString())); - ingredient.save(); -*/ - QAction* lpAction = qobject_cast(sender()); if(lpAction) { @@ -316,20 +334,9 @@ void cMainWindow::pluginDBTriggered() cPlugin* lpPlugin = plugin(lpAction); if(lpPlugin) { - if(!lpPlugin->pluginName().compare("MySQL DB")) - { - cDBInterface* lpDBInterface = lpPlugin->dbInterface(); - QString str = lpDBInterface->pluginName(); - if(lpDBInterface) - lpDBInterface->init(); - } - else if(!lpPlugin->pluginName().compare("SQLite DB")) - { - cDBInterface* lpDBInterface = lpPlugin->dbInterface(); - QString str = lpDBInterface->pluginName(); - if(lpDBInterface) - lpDBInterface->init(); - } + cDBInterface* lpDBInterface = lpPlugin->dbInterface(); + if(lpDBInterface->config()) + loadIngredients(); } } } @@ -391,10 +398,16 @@ void cMainWindow::ingredientsListNewTriggered() void cMainWindow::ingredientsListImportTriggered() { cImportIngredientDialog importIngredientDialog(this); - importIngredientDialog.setPluginList(m_pluginList); + importIngredientDialog.setPluginList(m_pluginList, m_szLastImportPlugin); + cDBInterface* lpInterface = m_lpDB->dbInterface(); + if(lpInterface) + importIngredientDialog.setIngredientGroupList(lpInterface->groups()); + if(importIngredientDialog.exec() == QDialog::Rejected) return; + m_szLastImportPlugin = importIngredientDialog.pluginSelected(); + cMessageAnimateDialog* lpDialog = new cMessageAnimateDialog(this); lpDialog->setTitle("Import Ingredient"); lpDialog->setMessage("Saving"); @@ -413,4 +426,21 @@ void cMainWindow::ingredientsListDeleteTriggered() void cMainWindow::ingredientsListEditTriggered() { + QModelIndex index = ui->m_lpIngredientsList->currentIndex(); + if(index.isValid()) + { + QStandardItem* lpItem = m_lpIngredientsListModel->itemFromIndex(index); + if(!lpItem) + return; + + QStandardItem* lpParent = lpItem->parent(); + if(!lpParent) + return; + + cIngredientWindow* lpIngredientWindow = new cIngredientWindow(this); + QMdiSubWindow* lpSubWindow = ui->m_lpMDIArea->addSubWindow(lpIngredientWindow); + lpSubWindow->setAttribute(Qt::WA_DeleteOnClose); + lpIngredientWindow->setIngredient(lpItem->data().toInt(), m_lpDB); + lpIngredientWindow->show(); + } } diff --git a/Kooky/cmainwindow.h b/Kooky/cmainwindow.h index 3b412c9..90d35aa 100644 --- a/Kooky/cmainwindow.h +++ b/Kooky/cmainwindow.h @@ -129,7 +129,7 @@ private: QMenu* m_lpIngredientsListMenu; /**< Context menu for ingredients list */ cPlugin* m_lpDB; - + QString m_szLastImportPlugin; /** * \brief Initialized main windows. * diff --git a/Kooky/cmainwindow.ui b/Kooky/cmainwindow.ui index e8cc48d..53a72b6 100644 --- a/Kooky/cmainwindow.ui +++ b/Kooky/cmainwindow.ui @@ -6,7 +6,7 @@ 0 0 - 1124 + 776 624 @@ -37,8 +37,11 @@ QAbstractItemView::NoEditTriggers - + false + + + true @@ -50,14 +53,14 @@ - - - QFrame::StyledPanel - - - QFrame::Raised - - + + + QFrame::StyledPanel + + + QFrame::Raised + + @@ -67,7 +70,7 @@ 0 0 - 1124 + 776 21 diff --git a/Kooky/typedef.h b/Kooky/typedef.h index 138e614..28bd8f0 100644 --- a/Kooky/typedef.h +++ b/Kooky/typedef.h @@ -8,8 +8,11 @@ typedef struct tagINGREDIENT { + qint32 iIngredient; QString szGroup; QString szIngredient; + double dCalories; + double dCarbohydrates; } INGREDIENT, *LPINGREDIENT; typedef QList INGREDIENT_LIST; diff --git a/dMySQL/cmysqlplugin.cpp b/dMySQL/cmysqlplugin.cpp index 56231bb..3d6f446 100644 --- a/dMySQL/cmysqlplugin.cpp +++ b/dMySQL/cmysqlplugin.cpp @@ -1,6 +1,8 @@ #include "cmysqlplugin.h" #include "cconfigdialog.h" +#include "cingredient.h" + #include #include #include @@ -267,13 +269,59 @@ bool cMySQLPlugin::set(qint32 id, qint16 ingredientNumber, qreal value) return(existsIngredient(id, ingredientNumber)); } +QString cMySQLPlugin::name(qint32 id) +{ + if(!m_bConnected) + return(""); + + QSqlQuery query(m_db); + if(!query.exec(QString("SELECT name FROM ingredient WHERE id=%1").arg(id).arg(id))) + { + m_szLastError = query.lastError().text(); + return(""); + } + query.first(); + return(query.value("name").toString()); +} + +QString cMySQLPlugin::group(qint32 id) +{ + if(!m_bConnected) + return(""); + + QSqlQuery query(m_db); + if(!query.exec(QString("SELECT name FROM ingredient, ingredient_group WHERE ingredient.ingredient_group_id=ingredient_group.id AND ingredient.id=%1").arg(id).arg(id))) + { + m_szLastError = query.lastError().text(); + return(""); + } + query.first(); + return(query.value("name").toString()); +} + +qreal cMySQLPlugin::get(qint32 id, qint16 ingredientNumber) +{ + if(!m_bConnected) + return(-1); + + QSqlQuery query(m_db); + if(!query.exec(QString("SELECT value FROM ingredient_values WHERE ingredient_id=%1 AND ingredient_number=%2").arg(id).arg(ingredientNumber))) + { + m_szLastError = query.lastError().text(); + return(-1); + } + query.first(); + return(query.value("value").toReal()); +} + INGREDIENT_LIST cMySQLPlugin::ingredients() { if(!m_bConnected) return(INGREDIENT_LIST()); QSqlQuery query(m_db); - if(!query.exec(QString("SELECT ingredient.name, ingredient_group.name FROM ingredient, ingredient_group WHERE ingredient.ingredient_group_id = ingredient_group.id ORDER BY ingredient_group.name, ingredient.name"))) +// if(!query.exec(QString("SELECT ingredient.name, ingredient_group.name FROM ingredient, ingredient_group WHERE ingredient.ingredient_group_id = ingredient_group.id ORDER BY ingredient_group.name, ingredient.name"))) + if(!query.exec(QString(QString("SELECT ingredient.id, ingredient.name, ingredient_group.name, calories.value, carbohydrates.value FROM ingredient, ingredient_group, ingredient_values AS calories, ingredient_values AS carbohydrates WHERE ingredient.ingredient_group_id = ingredient_group.id AND calories.ingredient_id = ingredient.id AND calories.ingredient_number = %1 AND carbohydrates.ingredient_id = ingredient.id AND carbohydrates.ingredient_number = %2 ORDER BY ingredient_group.name, ingredient.name").arg(cIngredient::iIngredientCalories).arg(cIngredient::iIngredientCarbohydrates)))) { m_szLastError = query.lastError().text(); return(INGREDIENT_LIST()); @@ -283,8 +331,11 @@ INGREDIENT_LIST cMySQLPlugin::ingredients() while(query.next()) { INGREDIENT i; - i.szGroup = query.value(1).toString(); - i.szIngredient = query.value(0).toString(); + i.iIngredient = query.value(0).toInt(); + i.szIngredient = query.value(1).toString(); + i.szGroup = query.value(2).toString(); + i.dCalories = query.value(3).toDouble(); + i.dCarbohydrates = query.value(4).toDouble(); list.append(i); } diff --git a/dMySQL/cmysqlplugin.h b/dMySQL/cmysqlplugin.h index 152a8d9..d59ccae 100644 --- a/dMySQL/cmysqlplugin.h +++ b/dMySQL/cmysqlplugin.h @@ -117,6 +117,9 @@ public: qint32 create(const QString& szName, const QString& szGroup); qint32 createGroup(const QString& szGroup); bool set(qint32 id, qint16 ingredientNumber, qreal value); + QString name(qint32 id); + QString group(qint32 id); + qreal get(qint32 id, qint16 ingredientNumber); INGREDIENT_LIST ingredients(); QStringList groups(); bool beginTransaction(); diff --git a/dSQLite/csqliteplugin.cpp b/dSQLite/csqliteplugin.cpp index bc8c6b1..bc8d9ae 100644 --- a/dSQLite/csqliteplugin.cpp +++ b/dSQLite/csqliteplugin.cpp @@ -1,6 +1,8 @@ #include "csqliteplugin.h" #include "cconfigdialog.h" +#include "cingredient.h" + #include #include #include @@ -253,13 +255,58 @@ bool cSQLitePlugin::set(qint32 id, qint16 ingredientNumber, qreal value) return(existsIngredient(id, ingredientNumber)); } +QString cSQLitePlugin::name(qint32 id) +{ + if(!m_bConnected) + return(""); + + QSqlQuery query(m_db); + if(!query.exec(QString("SELECT name FROM ingredient WHERE id=%1").arg(id))) + { + m_szLastError = query.lastError().text(); + return(""); + } + query.first(); + return(query.value("name").toString()); +} + +QString cSQLitePlugin::group(qint32 id) +{ + if(!m_bConnected) + return(""); + + QSqlQuery query(m_db); + if(!query.exec(QString("SELECT ingredient_group.name FROM ingredient, ingredient_group WHERE ingredient.ingredient_group_id=ingredient_group.id AND ingredient.id=%1").arg(id).arg(id))) + { + m_szLastError = query.lastError().text(); + return(""); + } + query.first(); + return(query.value("name").toString()); +} + +qreal cSQLitePlugin::get(qint32 id, qint16 ingredientNumber) +{ + if(!m_bConnected) + return(-1); + + QSqlQuery query(m_db); + if(!query.exec(QString("SELECT value FROM ingredient_values WHERE ingredient_id=%1 AND ingredient_number=%2").arg(id).arg(ingredientNumber))) + { + m_szLastError = query.lastError().text(); + return(-1); + } + query.first(); + return(query.value("value").toReal()); +} + INGREDIENT_LIST cSQLitePlugin::ingredients() { if(!m_bConnected) return(INGREDIENT_LIST()); QSqlQuery query(m_db); - if(!query.exec(QString("SELECT ingredient.name, ingredient_group.name FROM ingredient, ingredient_group WHERE ingredient.ingredient_group_id = ingredient_group.id ORDER BY ingredient_group.name, ingredient.name"))) + if(!query.exec(QString(QString("SELECT ingredient.id, ingredient.name, ingredient_group.name, calories.value, carbohydrates.value FROM ingredient, ingredient_group, ingredient_values AS calories, ingredient_values AS carbohydrates WHERE ingredient.ingredient_group_id = ingredient_group.id AND calories.ingredient_id = ingredient.id AND calories.ingredient_number = %1 AND carbohydrates.ingredient_id = ingredient.id AND carbohydrates.ingredient_number = %2 ORDER BY ingredient_group.name, ingredient.name").arg(cIngredient::iIngredientCalories).arg(cIngredient::iIngredientCarbohydrates)))) { m_szLastError = query.lastError().text(); return(INGREDIENT_LIST()); @@ -269,8 +316,11 @@ INGREDIENT_LIST cSQLitePlugin::ingredients() while(query.next()) { INGREDIENT i; - i.szGroup = query.value(1).toString(); - i.szIngredient = query.value(0).toString(); + i.iIngredient = query.value(0).toInt(); + i.szIngredient = query.value(1).toString(); + i.szGroup = query.value(2).toString(); + i.dCalories = query.value(3).toDouble(); + i.dCarbohydrates = query.value(4).toDouble(); list.append(i); } diff --git a/dSQLite/csqliteplugin.h b/dSQLite/csqliteplugin.h index b48d4d6..9cf13df 100644 --- a/dSQLite/csqliteplugin.h +++ b/dSQLite/csqliteplugin.h @@ -78,6 +78,9 @@ public: qint32 create(const QString& szName, const QString &szGroup); qint32 createGroup(const QString& szGroup); bool set(qint32 id, qint16 ingredientNumber, qreal value); + QString name(qint32 id); + QString group(qint32 id); + qreal get(qint32 id, qint16 ingredientNumber); INGREDIENT_LIST ingredients(); QStringList groups(); bool beginTransaction(); diff --git a/iBleibFit/cbleibfitplugin.cpp b/iBleibFit/cbleibfitplugin.cpp index c4058ce..6f152cf 100644 --- a/iBleibFit/cbleibfitplugin.cpp +++ b/iBleibFit/cbleibfitplugin.cpp @@ -167,6 +167,9 @@ bool cBleibFitPlugin::load(qint16 iIndex) if(iIndex > m_szUrls.count()) return(false); + for(int i = 0;i < cIngredient::iIngredientMax;i++) + m_rValues[i] = -1; + QNetworkAccessManager networkManager; QNetworkRequest request(QUrl(m_szUrls.at(iIndex))); request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");