[prev in list] [next in list] [prev in thread] [next in thread]
List: kde-commits
Subject: [krecipes] src/models: Update ingredients models with changes from the database.
From: José_Manuel_Santamaría_Lema <panfaust () gmail ! com>
Date: 2016-07-11 17:59:35
Message-ID: E1bMfUV-000753-BE () code ! kde ! org
[Download RAW message or body]
Git commit 1a2a74dc602d3fa673db4ebb07a5815e8a7c0b90 by José Manuel Santamaría Lema.
Committed on 11/07/2016 at 14:57.
Pushed by joselema into branch 'master'.
Update ingredients models with changes from the database.
M +112 -0 src/models/kreallingredientsmodels.cpp
M +6 -0 src/models/kreallingredientsmodels.h
http://commits.kde.org/krecipes/1a2a74dc602d3fa673db4ebb07a5815e8a7c0b90
diff --git a/src/models/kreallingredientsmodels.cpp b/src/models/kreallingredientsmodels.cpp
index d8425ab..fe0aba1 100644
--- a/src/models/kreallingredientsmodels.cpp
+++ b/src/models/kreallingredientsmodels.cpp
@@ -10,6 +10,7 @@
#include "kreallingredientsmodels.h"
#include "kresinglecolumnproxymodel.h"
+#include "backends/recipedb.h"
#include <KCompletion>
#include <QStandardItemModel>
@@ -18,11 +19,21 @@
KreAllIngredientsModels::KreAllIngredientsModels( RecipeDB * database ):
m_database( database )
{
+
+ //Create models
m_sourceModel = new QStandardItemModel( this );
m_ingredientNameModel = new KreSingleColumnProxyModel( 1, this );
m_ingredientNameModel->setSortCaseSensitivity( Qt::CaseInsensitive );
m_ingredientNameModel->setSourceModel( m_sourceModel );
m_ingredientNameCompletion = new KCompletion;
+
+ //Connect signals and slots for model updating
+ connect( database, SIGNAL(ingredientCreated(const KreIngredient &)),
+ this, SLOT(ingredientCreatedDBSlot(const KreIngredient &)) );
+ connect( database, SIGNAL(ingredientModified(const KreIngredient &)),
+ this, SLOT(ingredientModifiedDBSlot(const KreIngredient &)) );
+ connect( database, SIGNAL(ingredientRemoved(const QVariant &)),
+ this, SLOT(ingredientRemovedDBSlot(const QVariant &)) );
}
KreAllIngredientsModels::~KreAllIngredientsModels()
@@ -45,3 +56,104 @@ KCompletion * KreAllIngredientsModels::ingredientNameCompletion()
return m_ingredientNameCompletion;
}
+void KreAllIngredientsModels::ingredientCreatedDBSlot( const KreIngredient & ingredient )
+{
+ //Prepare the id item
+ QStandardItem * itemId = new QStandardItem;
+ itemId->setData( ingredient.id(), Qt::EditRole );
+ itemId->setEditable( false );
+
+ //Prepare the name item
+ QStandardItem *itemName = new QStandardItem( ingredient.name() );
+ itemName->setData( ingredient.name(), Qt::EditRole );
+ itemName->setEditable( true );
+
+ //Add the new row to the source model
+ QList<QStandardItem*> itemList;
+ itemList << itemId << itemName;
+ m_sourceModel->appendRow( itemList );
+
+ //Add the item to the completion object
+ m_ingredientNameCompletion->addItem( ingredient.name() );
+}
+
+void KreAllIngredientsModels::ingredientModifiedDBSlot( const KreIngredient & newIngredient )
+{
+ int i;
+ int rowCount;
+ QModelIndex index;
+
+ //Rename the ingredient in the source model
+ QVariant id;
+ QString oldName;
+ rowCount = m_sourceModel->rowCount();
+ for ( i = 0; i < rowCount; ++i ) {
+ index = m_sourceModel->index( i, 0 );
+ id = m_sourceModel->data( index );
+ if ( id == newIngredient.id() ) {
+ index = m_sourceModel->index( i, 1 );
+ oldName = m_sourceModel->data( index, Qt::EditRole ).toString();
+ m_sourceModel->setData( index, newIngredient.name(), Qt::EditRole );
+ break;
+ }
+ }
+
+ //Delete the ingredient name from the KCompletion if it was unique
+ QString name;
+ int oldNameMatches = 0;
+ for ( i = 0; i < rowCount; ++i ) {
+ index = m_sourceModel->index( i, 1 );
+ name = m_sourceModel->data( index ).toString();
+ if ( name == oldName ) {
+ ++oldNameMatches;
+ }
+ }
+
+ //If the ingredient name was unique, delete it from completion
+ if ( oldNameMatches == 0 ) {
+ m_ingredientNameCompletion->removeItem( oldName );
+ }
+
+ //Add the new ingredient name to completion
+ m_ingredientNameCompletion->addItem( newIngredient.name() );
+
+}
+
+void KreAllIngredientsModels::ingredientRemovedDBSlot( const QVariant & id )
+{
+ int i;
+ int rowCount;
+ QModelIndex index;
+
+ //Delete the ingredient in the source model
+ QVariant currentId;
+ QString oldName;
+ rowCount = m_sourceModel->rowCount();
+ for ( i = 0; i < rowCount; ++i ) {
+ index = m_sourceModel->index( i, 0 );
+ currentId = m_sourceModel->data( index );
+ if ( currentId == id ) {
+ index = m_sourceModel->index( i, 1 );
+ oldName = m_sourceModel->data( index, Qt::EditRole ).toString();
+ m_sourceModel->takeRow( i );
+ break;
+ }
+ }
+
+ //Delete the ingredient name from the KCompletion if it was unique
+ QString name;
+ int oldNameMatches = 0;
+ for ( i = 0; i < rowCount; ++i ) {
+ index = m_sourceModel->index( i, 1 );
+ name = m_sourceModel->data( index ).toString();
+ if ( name == oldName ) {
+ ++oldNameMatches;
+ }
+ }
+
+ //If the ingredient name was unique, delete it from completion
+ if ( oldNameMatches == 0 ) {
+ m_ingredientNameCompletion->removeItem( oldName );
+ }
+
+}
diff --git a/src/models/kreallingredientsmodels.h b/src/models/kreallingredientsmodels.h
index dfab8c1..1c45528 100644
--- a/src/models/kreallingredientsmodels.h
+++ b/src/models/kreallingredientsmodels.h
@@ -14,6 +14,7 @@
class RecipeDB;
class KreSingleColumnProxyModel;
+class KreIngredient;
class KCompletion;
class QStandardItemModel;
@@ -28,6 +29,11 @@ public:
KreSingleColumnProxyModel * ingredientNameModel();
KCompletion * ingredientNameCompletion();
+private slots:
+ void ingredientCreatedDBSlot( const KreIngredient & );
+ void ingredientModifiedDBSlot( const KreIngredient & );
+ void ingredientRemovedDBSlot( const QVariant & );
+
private:
RecipeDB * m_database;
QStandardItemModel * m_sourceModel;
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic