From f1bb5bdcbdfe2665d44b33afa97f55f1f61bcf0d Mon Sep 17 00:00:00 2001 From: birkeh Date: Mon, 27 Feb 2017 10:26:00 +0100 Subject: [PATCH] Progress --- Kooky/cdbinterface.h | 1 + Kooky/cimportingredientdialog.cpp | 7 +- Kooky/cimportingredientdialog.h | 3 +- Kooky/cingredient.cpp | 15 +++- Kooky/cingredient.h | 6 ++ Kooky/cingredientwindow.cpp | 127 ++++++++++++++++++++++++++++-- Kooky/cingredientwindow.h | 16 ++-- Kooky/cmainwindow.cpp | 105 +++++++++++++++++++++++- Kooky/cmainwindow.h | 2 + dMySQL/cmysqlplugin.cpp | 22 ++++++ dMySQL/cmysqlplugin.h | 1 + dSQLite/csqliteplugin.cpp | 22 ++++++ dSQLite/csqliteplugin.h | 1 + 13 files changed, 312 insertions(+), 16 deletions(-) diff --git a/Kooky/cdbinterface.h b/Kooky/cdbinterface.h index b6b6cf5..b15ebd3 100644 --- a/Kooky/cdbinterface.h +++ b/Kooky/cdbinterface.h @@ -81,6 +81,7 @@ public: virtual qint32 existsGroup(const QString& szGroup) = 0; virtual qint32 existsIngredient(qint32 id, qint16 ingredientNumber) = 0; virtual qint32 create(const QString& szName, const QString& szGroup) = 0; + virtual bool deleteIngredient(qint32 id) = 0; virtual qint32 createGroup(const QString& szGroup) = 0; virtual bool set(qint32 id, qint16 ingredientNumber, qreal value) = 0; virtual bool setName(qint32 id, const QString& szName) = 0; diff --git a/Kooky/cimportingredientdialog.cpp b/Kooky/cimportingredientdialog.cpp index dc78379..5b35171 100644 --- a/Kooky/cimportingredientdialog.cpp +++ b/Kooky/cimportingredientdialog.cpp @@ -73,7 +73,7 @@ void cImportIngredientDialog::setPluginList(const QList pluginList, co } } -void cImportIngredientDialog::setIngredientGroupList(const QStringList ingredientGroupList) +void cImportIngredientDialog::setIngredientGroupList(const QStringList &ingredientGroupList) { ui->m_lpIngredientGroup->addItems(ingredientGroupList); @@ -83,6 +83,11 @@ void cImportIngredientDialog::setIngredientGroupList(const QStringList ingredien ui->m_lpIngredientGroup->setCurrentIndex(index); } +void cImportIngredientDialog::setIngredientGroup(const QString& szIngredientGroup) +{ + ui->m_lpIngredientGroup->setCurrentText(szIngredientGroup); +} + QString cImportIngredientDialog::pluginSelected() { return(ui->m_lpSource->currentText()); diff --git a/Kooky/cimportingredientdialog.h b/Kooky/cimportingredientdialog.h index de1c560..4733a7d 100644 --- a/Kooky/cimportingredientdialog.h +++ b/Kooky/cimportingredientdialog.h @@ -64,7 +64,8 @@ public: QString pluginSelected(); - void setIngredientGroupList(const QStringList ingredientGroupList); + void setIngredientGroupList(const QStringList& ingredientGroupList); + void setIngredientGroup(const QString& szIngredientGroup); private: Ui::cImportIngredientDialog* ui; /*!< TODO: describe */ QStandardItemModel* m_lpIngredientListModel; /*!< TODO: describe */ diff --git a/Kooky/cingredient.cpp b/Kooky/cingredient.cpp index 7aa20af..9945dc1 100644 --- a/Kooky/cingredient.cpp +++ b/Kooky/cingredient.cpp @@ -188,7 +188,7 @@ cIngredient::cIngredient() : cIngredient::cIngredient(const QString &szIngredientName, const QString& szIngredientGroup) : m_szIngredientName(szIngredientName), m_szIngredientGroup(szIngredientGroup), - m_iID(0) + m_iID(-1) { for(int z = 0;z < iIngredientMax;z++) m_dValue[z] = -1; @@ -221,6 +221,9 @@ bool cIngredient::save(cPlugin* lpDB) if(!lpDB) return(false); + if(m_szIngredientName.isEmpty()) + return(false); + cDBInterface* lpInterface = lpDB->dbInterface(); lpInterface->beginTransaction(); @@ -262,11 +265,21 @@ bool cIngredient::save(cPlugin* lpDB) return(true); } +void cIngredient::setIngredientName(const QString& szIngredientName) +{ + m_szIngredientName = szIngredientName; +} + QString cIngredient::ingredientName() { return(m_szIngredientName); } +void cIngredient::setIngredientGroup(const QString& szIngredientGroup) +{ + m_szIngredientGroup = szIngredientGroup; +} + QString cIngredient::ingredientGroup() { return(m_szIngredientGroup); diff --git a/Kooky/cingredient.h b/Kooky/cingredient.h index 8cdb4b0..4812d4d 100644 --- a/Kooky/cingredient.h +++ b/Kooky/cingredient.h @@ -238,7 +238,9 @@ public: */ bool save(cPlugin* lpDB); + void setIngredientName(const QString& szIngredientName); QString ingredientName(); + void setIngredientGroup(const QString& szIngredientGroup); QString ingredientGroup(); QStringList groups(); /*! @@ -259,6 +261,7 @@ public: void operator=(const cIngredient& D) { m_szIngredientName = D.m_szIngredientName; + m_szIngredientGroup = D.m_szIngredientGroup; m_iID = D.m_iID; for(int z = 0;z < iIngredientMax;z++) m_dValue[z] = D.m_dValue[z]; @@ -269,6 +272,9 @@ public: if(m_szIngredientName != D.m_szIngredientName) return(false); + if(m_szIngredientGroup != D.m_szIngredientGroup) + return(false); + if(m_iID != D.m_iID) return(false); diff --git a/Kooky/cingredientwindow.cpp b/Kooky/cingredientwindow.cpp index 0ff8602..48c2182 100644 --- a/Kooky/cingredientwindow.cpp +++ b/Kooky/cingredientwindow.cpp @@ -4,11 +4,14 @@ #include "cingredientvalue.h" #include +#include +#include cIngredientWindow::cIngredientWindow(QWidget *parent) : QWidget(parent), - ui(new Ui::cIngredientWindow) + ui(new Ui::cIngredientWindow), + m_bIsInitialized(false) { ui->setupUi(this); ui->m_lpIngredientValues->setMinimumWidth(1); @@ -20,6 +23,12 @@ cIngredientWindow::cIngredientWindow(QWidget *parent) : ui->m_lpRevert->setEnabled(false); connect(ui->m_lpIngredientValues, &QTreeView::doubleClicked, this, &cIngredientWindow::ingredientEdit); + connect(ui->m_lpIngredientName, &QLineEdit::textChanged, this, &cIngredientWindow::nameChanged); + connect(ui->m_lpIngredientGroup, &QComboBox::currentTextChanged, this, &cIngredientWindow::groupChanged); + + connect(ui->m_lpSave, &QPushButton::clicked, this, &cIngredientWindow::saveClicked); + connect(ui->m_lpRevert, &QPushButton::clicked, this, &cIngredientWindow::revertClicked); + connect(ui->m_lpClose, &QPushButton::clicked, this, &cIngredientWindow::closeClicked); } cIngredientWindow::~cIngredientWindow() @@ -75,6 +84,8 @@ void cIngredientWindow::setIngredient(QStandardItem *lpItem, cPlugin* lpPlugin) m_lpItem->setText(QString("%1").arg(m_ingredientSaved.ingredientName())); ui->m_lpSave->setEnabled(false); ui->m_lpRevert->setEnabled(false); + + m_bIsInitialized = true; } qint32 cIngredientWindow::ingredientID(void) @@ -82,6 +93,29 @@ qint32 cIngredientWindow::ingredientID(void) return(m_iIngredient); } +void cIngredientWindow::setIngredientGroup(const QString& szIngredientGroup) +{ + ui->m_lpIngredientGroup->setCurrentText(szIngredientGroup); +} + +void cIngredientWindow::nameChanged(const QString& text) +{ + if(!m_bIsInitialized) + return; + + m_ingredient.setIngredientName(text); + updateTitle(); +} + +void cIngredientWindow::groupChanged(const QString & text) +{ + if(!m_bIsInitialized) + return; + + m_ingredient.setIngredientGroup(text); + updateTitle(); +} + void cIngredientWindow::ingredientEdit(const QModelIndex &modelIndex) { QStandardItem* lpCurrentItem = m_lpIngredientValuesModel->itemFromIndex(modelIndex); @@ -112,45 +146,101 @@ void cIngredientWindow::ingredientEdit(const QModelIndex &modelIndex) bool cIngredientWindow::isChanged() { + if(!m_bIsInitialized) + return(false); + if(m_ingredient != m_ingredientSaved) return(true); return(false); } -void cIngredientWindow::on_m_lpSave_clicked() +void cIngredientWindow::forceClose() { + m_ingredient = m_ingredientSaved; + closeClicked(); +} + +bool cIngredientWindow::save() +{ + if(m_ingredient.ingredientName().isEmpty()) + { + QMessageBox::critical(this, "Error", "Ingredient name is empty."); + return(false); + } + + if(m_ingredient.ingredientGroup().isEmpty()) + { + QMessageBox::critical(this, "Error", "Ingredient group is empty."); + return(false); + } + m_ingredient.save(m_lpDBPlugin); m_ingredientSaved = m_ingredient; + return(true); +} + +void cIngredientWindow::saveClicked() +{ + if(!save()) + return; updateTitle(); } -void cIngredientWindow::on_m_lpRevert_clicked() +void cIngredientWindow::revertClicked() { setIngredient(); updateTitle(); } -void cIngredientWindow::on_m_lpClose_clicked() +void cIngredientWindow::closeClicked() { - + if(this->parent()) + { + QMdiSubWindow* mdiSubWindow = (QMdiSubWindow*)this->parent(); + mdiSubWindow->close(); + } } void cIngredientWindow::closeEvent(QCloseEvent *event) { if(isChanged()) - event->ignore(); + { + int ret = QMessageBox::question(this, "Changed", tr("Ingredient has been changed.\nDo you want to save?"), QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel); + + switch(ret) + { + case QMessageBox::Yes: + m_ingredient.save(m_lpDBPlugin); + m_ingredient = m_ingredientSaved; + updateTitle(); + event->accept(); + break; + case QMessageBox::No: + m_ingredient = m_ingredientSaved; + updateTitle(); + event->accept(); + break; + default: + event->ignore(); + break; + } + } else event->accept(); } void cIngredientWindow::updateTitle() { + if(!m_bIsInitialized) + return; + if(isChanged()) { setWindowTitle(QString(" %1*").arg(m_ingredientSaved.ingredientName())); m_lpItem->setText(QString("%1*").arg(m_ingredientSaved.ingredientName())); + checkGroupChanged(); ui->m_lpSave->setEnabled(true); ui->m_lpRevert->setEnabled(true); } @@ -158,7 +248,32 @@ void cIngredientWindow::updateTitle() { setWindowTitle(QString(" %1").arg(m_ingredientSaved.ingredientName())); m_lpItem->setText(QString("%1").arg(m_ingredientSaved.ingredientName())); + checkGroupChanged(); ui->m_lpSave->setEnabled(false); ui->m_lpRevert->setEnabled(false); } } + +void cIngredientWindow::checkGroupChanged() +{ + if(!m_bIsInitialized) + return; + + if(m_ingredient.ingredientGroup() == m_ingredientSaved.ingredientGroup()) + return; + + QString szGroup = m_ingredient.ingredientGroup(); + QStandardItem* lpOldGroup = m_lpItem->parent(); + QStandardItemModel* lpModel = m_lpItem->model(); + QList lpItems = lpOldGroup->takeRow(lpModel->indexFromItem(m_lpItem).row()); + + for(int z = 0;z < lpModel->rowCount();z++) + { + QStandardItem* lpGroup = lpModel->item(z, 0); + if(lpGroup->text() == m_ingredient.ingredientGroup()) + { + lpGroup->insertRow(0, lpItems); + break; + } + } +} diff --git a/Kooky/cingredientwindow.h b/Kooky/cingredientwindow.h index f86938e..3457b9f 100644 --- a/Kooky/cingredientwindow.h +++ b/Kooky/cingredientwindow.h @@ -25,15 +25,16 @@ public: void setIngredient(QStandardItem* lpItem = 0, cPlugin* lpPlugin = 0); qint32 ingredientID(void); + void setIngredientGroup(const QString& szIngredientGroup); bool isChanged(); + void forceClose(); private slots: + void nameChanged(const QString& text); + void groupChanged(const QString & text); void ingredientEdit(const QModelIndex& modelIndex); - - void on_m_lpSave_clicked(); - - void on_m_lpRevert_clicked(); - - void on_m_lpClose_clicked(); + void saveClicked(); + void revertClicked(); + void closeClicked(); private: Ui::cIngredientWindow* ui; @@ -44,9 +45,12 @@ private: cIngredient m_ingredientSaved; QStandardItemModel* m_lpIngredientValuesModel; QStandardItem* m_lpItem; + bool m_bIsInitialized; + bool save(); void updateTitle(); protected: + void checkGroupChanged(); void closeEvent(QCloseEvent *event); }; diff --git a/Kooky/cmainwindow.cpp b/Kooky/cmainwindow.cpp index 49b11fe..bdd9665 100644 --- a/Kooky/cmainwindow.cpp +++ b/Kooky/cmainwindow.cpp @@ -441,6 +441,36 @@ void cMainWindow::ingredientsListCustomContextMenu(const QPoint& point) void cMainWindow::ingredientsListNewTriggered() { +/* + 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; + + QList windowsList = ui->m_lpMDIArea->subWindowList(); + for(int z = 0;z < windowsList.count();z++) + { + cIngredientWindow* lpWidget = (cIngredientWindow*)windowsList.at(z)->widget(); + if(lpWidget->ingredientID() == lpItem->data().toInt()) + { + ui->m_lpMDIArea->setActiveSubWindow(windowsList.at(z)); + return; + } + } + + cIngredientWindow* lpIngredientWindow = new cIngredientWindow(this); + lpIngredientWindow->setIngredient(ingredientItemFromID(lpItem->data().toInt()), m_lpDB); + QMdiSubWindow* lpSubWindow = ui->m_lpMDIArea->addSubWindow(lpIngredientWindow); + lpSubWindow->setAttribute(Qt::WA_DeleteOnClose); + lpIngredientWindow->show(); + } +*/ } void cMainWindow::ingredientsListImportTriggered() @@ -451,6 +481,23 @@ void cMainWindow::ingredientsListImportTriggered() if(lpInterface) importIngredientDialog.setIngredientGroupList(lpInterface->groups()); + QModelIndex index = ui->m_lpIngredientsList->currentIndex(); + if(index.isValid()) + { + QString szGroup; + QStandardItem* lpItem = m_lpIngredientsListModel->itemFromIndex(index); + if(lpItem) + { + QStandardItem* lpParent = lpItem->parent(); + if(lpParent) + szGroup = lpParent->text(); + else + szGroup = lpItem->text(); + } + if(!szGroup.isEmpty()) + importIngredientDialog.setIngredientGroup(szGroup); + } + if(importIngredientDialog.exec() == QDialog::Rejected) return; @@ -470,6 +517,62 @@ void cMainWindow::ingredientsListImportTriggered() void cMainWindow::ingredientsListDeleteTriggered() { + 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; + + lpItem = ingredientItemFromID(lpItem->data().toInt()); + + if(QMessageBox::question(this, "Delete", QString("You are about to delete \"%1\".\nAre you sure?").arg(lpItem->text()), QMessageBox::Yes | QMessageBox::No) == QMessageBox::No) + return; + + if(!m_lpDB->dbInterface()->deleteIngredient(lpItem->data().toInt())) + { + QMessageBox::critical(this, "Error", QString("Error deleting \"%1\"!").arg(lpItem->text())); + return; + } + + m_lpIngredientsListModel->removeRow(index.row()); + + QList windowsList = ui->m_lpMDIArea->subWindowList(); + for(int z = 0;z < windowsList.count();z++) + { + cIngredientWindow* lpWidget = (cIngredientWindow*)windowsList.at(z)->widget(); + if(lpWidget->ingredientID() == lpItem->data().toInt()) + { + lpWidget->forceClose(); + return; + } + } + } +} + +QStandardItem* cMainWindow::ingredientItemFromID(qint32 id) +{ + for(int z = 0;z < m_lpIngredientsListModel->rowCount();z++) + { + QStandardItem* lpItem = m_lpIngredientsListModel->item(z); + if(lpItem->data().toInt() == id) + return(lpItem); + + if(lpItem->hasChildren()) + { + for(int y = 0;y < lpItem->rowCount();y++) + { + QStandardItem* lpItem1 = lpItem->child(y); + if(lpItem1->data().toInt() == id) + return(lpItem1); + } + } + } + return(0); } void cMainWindow::ingredientsListEditTriggered() @@ -497,7 +600,7 @@ void cMainWindow::ingredientsListEditTriggered() } cIngredientWindow* lpIngredientWindow = new cIngredientWindow(this); - lpIngredientWindow->setIngredient(lpItem, m_lpDB); + lpIngredientWindow->setIngredient(ingredientItemFromID(lpItem->data().toInt()), m_lpDB); QMdiSubWindow* lpSubWindow = ui->m_lpMDIArea->addSubWindow(lpIngredientWindow); lpSubWindow->setAttribute(Qt::WA_DeleteOnClose); lpIngredientWindow->show(); diff --git a/Kooky/cmainwindow.h b/Kooky/cmainwindow.h index efe1c44..4a760ca 100644 --- a/Kooky/cmainwindow.h +++ b/Kooky/cmainwindow.h @@ -167,6 +167,8 @@ private: */ cPlugin* plugin(QAction* lpAction); QMdiSubWindow* activeMdiChild() const; + + QStandardItem* ingredientItemFromID(qint32 id); protected: }; diff --git a/dMySQL/cmysqlplugin.cpp b/dMySQL/cmysqlplugin.cpp index 1847c99..c9ce40d 100644 --- a/dMySQL/cmysqlplugin.cpp +++ b/dMySQL/cmysqlplugin.cpp @@ -252,6 +252,28 @@ qint32 cMySQLPlugin::createGroup(const QString& szGroup) return(existsGroup(szGroup)); } +bool cMySQLPlugin::deleteIngredient(qint32 id) +{ + if(!m_bConnected) + return(false); + + QSqlQuery query(m_db); + + if(!query.exec(QString("DELETE FROM ingredient_values WHERE ingredient_id=%1").arg(id))) + { + m_szLastError = query.lastError().text(); + return(false); + } + + if(!query.exec(QString("DELETE FROM ingredient WHERE id=%1").arg(id))) + { + m_szLastError = query.lastError().text(); + return(false); + } + + return(true); +} + bool cMySQLPlugin::set(qint32 id, qint16 ingredientNumber, qreal value) { if(!m_bConnected) diff --git a/dMySQL/cmysqlplugin.h b/dMySQL/cmysqlplugin.h index 824350d..4524a6f 100644 --- a/dMySQL/cmysqlplugin.h +++ b/dMySQL/cmysqlplugin.h @@ -116,6 +116,7 @@ public: qint32 existsIngredient(qint32 id, qint16 ingredientNumber); qint32 create(const QString& szName, const QString& szGroup); qint32 createGroup(const QString& szGroup); + bool deleteIngredient(qint32 id); bool set(qint32 id, qint16 ingredientNumber, qreal value); bool setName(qint32 id, const QString& szName); bool setGroup(qint32 id, const QString& szGroup); diff --git a/dSQLite/csqliteplugin.cpp b/dSQLite/csqliteplugin.cpp index 20843d2..3588196 100644 --- a/dSQLite/csqliteplugin.cpp +++ b/dSQLite/csqliteplugin.cpp @@ -238,6 +238,28 @@ qint32 cSQLitePlugin::createGroup(const QString& szGroup) return(existsGroup(szGroup)); } +bool cSQLitePlugin::deleteIngredient(qint32 id) +{ + if(!m_bConnected) + return(false); + + QSqlQuery query(m_db); + + if(!query.exec(QString("DELETE FROM ingredient_values WHERE ingredient_id=%1").arg(id))) + { + m_szLastError = query.lastError().text(); + return(false); + } + + if(!query.exec(QString("DELETE FROM ingredient WHERE id=%1").arg(id))) + { + m_szLastError = query.lastError().text(); + return(false); + } + + return(true); +} + bool cSQLitePlugin::set(qint32 id, qint16 ingredientNumber, qreal value) { if(!m_bConnected) diff --git a/dSQLite/csqliteplugin.h b/dSQLite/csqliteplugin.h index 8834ffc..18b0789 100644 --- a/dSQLite/csqliteplugin.h +++ b/dSQLite/csqliteplugin.h @@ -77,6 +77,7 @@ public: qint32 existsIngredient(qint32 id, qint16 ingredientNumber); qint32 create(const QString& szName, const QString &szGroup); qint32 createGroup(const QString& szGroup); + bool deleteIngredient(qint32 id); bool set(qint32 id, qint16 ingredientNumber, qreal value); bool setName(qint32 id, const QString& szName); bool setGroup(qint32 id, const QString& szGroup);