Git commit 1bd488aa1dc84c9a9f26d733db8ecf0f6c5743e6 by Jos=C3=A9 Manuel San= tamar=C3=ADa Lema. Committed on 12/07/2016 at 08:47. Pushed by joselema into branch 'master'. Update ingredient headers models with changes from the database. M +106 -108 src/models/kreallingheadersmodels.cpp M +4 -4 src/models/kreallingheadersmodels.h http://commits.kde.org/krecipes/1bd488aa1dc84c9a9f26d733db8ecf0f6c5743e6 diff --git a/src/models/kreallingheadersmodels.cpp b/src/models/kreallinghe= adersmodels.cpp index 27d7663..61f9c0e 100644 --- a/src/models/kreallingheadersmodels.cpp +++ b/src/models/kreallingheadersmodels.cpp @@ -27,13 +27,13 @@ KreAllIngHeadersModels::KreAllIngHeadersModels( RecipeD= B * database ): m_ingHeaderNameModel->setSourceModel( m_sourceModel ); m_ingHeaderNameCompletion =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 &)) ); + //Connect signals and slots for model updating + connect( database, SIGNAL(ingHeaderCreated(const KreIngHeader &)), + this, SLOT(ingHeaderCreatedDBSlot(const KreIngHeader &)) ); + connect( database, SIGNAL(ingHeaderModified(const KreIngHeader &)), + this, SLOT(ingHeaderModifiedDBSlot(const KreIngHeader &)) ); + connect( database, SIGNAL(ingHeaderRemoved(const QVariant &)), + this, SLOT(ingHeaderRemovedDBSlot(const QVariant &)) ); } = KreAllIngHeadersModels::~KreAllIngHeadersModels() @@ -56,104 +56,102 @@ KCompletion * KreAllIngHeadersModels::ingHeaderNameCo= mpletion() return m_ingHeaderNameCompletion; } = -//void KreAllIngredientsModels::ingredientCreatedDBSlot( const KreIngredie= nt & 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 KreIngredi= ent & 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 ); -// } -// -//} +void KreAllIngHeadersModels::ingHeaderCreatedDBSlot( const KreIngHeader & = ingHeader ) +{ + //Prepare the id item + QStandardItem * itemId =3D new QStandardItem; + itemId->setData( ingHeader.id(), Qt::EditRole ); + itemId->setEditable( false ); + + //Prepare the name item + QStandardItem *itemName =3D new QStandardItem( ingHeader.name() ); + itemName->setData( ingHeader.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_ingHeaderNameCompletion->addItem( ingHeader.name() ); +} + +void KreAllIngHeadersModels::ingHeaderModifiedDBSlot( const KreIngHeader &= newIngHeader ) +{ + int i; + int rowCount; + QModelIndex index; + + //Rename the ingredient header 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 newIngHeader.id() ) { + index =3D m_sourceModel->index( i, 1 ); + oldName =3D m_sourceModel->data( index, Qt::EditRole ).toString(); + m_sourceModel->setData( index, newIngHeader.name(), Qt::EditRole ); + break; + } + } + + //Delete the ingredient header 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 header name was unique, delete it from completion + if ( oldNameMatches =3D=3D 0 ) { + m_ingHeaderNameCompletion->removeItem( oldName ); + } + + //Add the new ingredient header name to completion + m_ingHeaderNameCompletion->addItem( newIngHeader.name() ); +} + +void KreAllIngHeadersModels::ingHeaderRemovedDBSlot( const QVariant & id ) +{ + int i; + int rowCount; + QModelIndex index; + + //Delete the ingredient header 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 header 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 header name was unique, delete it from completion + if ( oldNameMatches =3D=3D 0 ) { + m_ingHeaderNameCompletion->removeItem( oldName ); + } +} diff --git a/src/models/kreallingheadersmodels.h b/src/models/kreallinghead= ersmodels.h index 1beb749..237dfcb 100644 --- a/src/models/kreallingheadersmodels.h +++ b/src/models/kreallingheadersmodels.h @@ -29,10 +29,10 @@ public: KreSingleColumnProxyModel * ingHeaderNameModel(); KCompletion * ingHeaderNameCompletion(); = -//private slots: -// void ingHeaderCreatedDBSlot( const KreIngHeader & ); -// void ingHeaderModifiedDBSlot( const KreIngHeader & ); -// void ingHeaderRemovedDBSlot( const QVariant & ); +private slots: + void ingHeaderCreatedDBSlot( const KreIngHeader & ); + void ingHeaderModifiedDBSlot( const KreIngHeader & ); + void ingHeaderRemovedDBSlot( const QVariant & ); = private: RecipeDB * m_database;