Git commit 1a2a74dc602d3fa673db4ebb07a5815e8a7c0b90 by Jos=C3=A9 Manuel San= tamar=C3=ADa 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/kreallingr= edientsmodels.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 #include @@ -18,11 +19,21 @@ KreAllIngredientsModels::KreAllIngredientsModels( RecipeDB * database ): m_database( database ) { + + //Create models m_sourceModel =3D new QStandardItemModel( this ); m_ingredientNameModel =3D new KreSingleColumnProxyModel( 1, this ); m_ingredientNameModel->setSortCaseSensitivity( Qt::CaseInsensitive ); m_ingredientNameModel->setSourceModel( m_sourceModel ); m_ingredientNameCompletion =3D 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::ingredientNameCo= mpletion() return m_ingredientNameCompletion; } = +void KreAllIngredientsModels::ingredientCreatedDBSlot( const KreIngredient= & ingredient ) +{ + //Prepare the id item + QStandardItem * itemId =3D new QStandardItem; + itemId->setData( ingredient.id(), Qt::EditRole ); + itemId->setEditable( false ); + + //Prepare the name item + QStandardItem *itemName =3D new QStandardItem( ingredient.name() ); + itemName->setData( ingredient.name(), Qt::EditRole ); + itemName->setEditable( true ); + + //Add the new row to the source model + QList itemList; + itemList << itemId << itemName; + m_sourceModel->appendRow( itemList ); + + //Add the item to the completion object + m_ingredientNameCompletion->addItem( ingredient.name() ); +} + +void KreAllIngredientsModels::ingredientModifiedDBSlot( const KreIngredien= t & newIngredient ) +{ + int i; + int rowCount; + QModelIndex index; + + //Rename the ingredient in the source model + QVariant id; + QString oldName; + rowCount =3D m_sourceModel->rowCount(); + for ( i =3D 0; i < rowCount; ++i ) { + index =3D m_sourceModel->index( i, 0 ); + id =3D m_sourceModel->data( index ); + if ( id =3D=3D newIngredient.id() ) { + index =3D m_sourceModel->index( i, 1 ); + oldName =3D 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 =3D 0; + for ( i =3D 0; i < rowCount; ++i ) { + index =3D m_sourceModel->index( i, 1 ); + name =3D m_sourceModel->data( index ).toString(); + if ( name =3D=3D oldName ) { + ++oldNameMatches; + } + } + + //If the ingredient name was unique, delete it from completion + if ( oldNameMatches =3D=3D 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 =3D m_sourceModel->rowCount(); + for ( i =3D 0; i < rowCount; ++i ) { + index =3D m_sourceModel->index( i, 0 ); + currentId =3D m_sourceModel->data( index ); + if ( currentId =3D=3D id ) { + index =3D m_sourceModel->index( i, 1 ); + oldName =3D 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 =3D 0; + for ( i =3D 0; i < rowCount; ++i ) { + index =3D m_sourceModel->index( i, 1 ); + name =3D m_sourceModel->data( index ).toString(); + if ( name =3D=3D oldName ) { + ++oldNameMatches; + } + } + + //If the ingredient name was unique, delete it from completion + if ( oldNameMatches =3D=3D 0 ) { + m_ingredientNameCompletion->removeItem( oldName ); + } + +} diff --git a/src/models/kreallingredientsmodels.h b/src/models/kreallingred= ientsmodels.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;