Git commit 47d6678a7b16993a5b9ca911b596677b2b9776e7 by Jos=C3=A9 Manuel San= tamar=C3=ADa Lema. Committed on 31/03/2016 at 20:55. Pushed by joselema into branch 'master'. Update IngredientsEditor when prep methods change in the database. M +140 -4 src/dialogs/recipeinput/ingredientseditor.cpp M +4 -0 src/dialogs/recipeinput/ingredientseditor.h http://commits.kde.org/krecipes/47d6678a7b16993a5b9ca911b596677b2b9776e7 diff --git a/src/dialogs/recipeinput/ingredientseditor.cpp b/src/dialogs/re= cipeinput/ingredientseditor.cpp index 279956b..be55253 100644 --- a/src/dialogs/recipeinput/ingredientseditor.cpp +++ b/src/dialogs/recipeinput/ingredientseditor.cpp @@ -130,23 +130,31 @@ void IngredientsEditor::setDatabase( RecipeDB * datab= ase ) m_nutrientInfoDetailsDialog->setDatabase( m_database ); = //Connect signals from new database + + //Ingredients connect( m_database, SIGNAL(ingredientCreated(const Element &)), this, SLOT(ingredientCreatedDBSlot(const Element &)) ); - connect( m_database, SIGNAL(ingredientModified(const Ingredient &)), this, SLOT(ingredientModifiedDBSlot(const Ingredient &)) ); - connect( m_database, SIGNAL(ingredientRemoved(int)), this, SLOT(ingredientRemovedDBSlot(int)) ); = + //Units connect( m_database, SIGNAL(unitCreated(const Unit &)), this, SLOT(unitCreatedDBSlot(const Unit &)) ); - connect( m_database, SIGNAL(unitModified(const Unit &)), this, SLOT(unitModifiedDBSlot(const Unit &)) ); - connect( m_database, SIGNAL(unitRemoved(int)), this, SLOT(unitRemovedDBSlot(int)) ); + + kDebug() << "connecting methods"; + //Preparation methods + connect( m_database, SIGNAL(prepMethodCreated(const Element &)), + this, SLOT(prepMethodCreatedDBSlot(const Element &)) ); + connect( m_database, SIGNAL(prepMethodModified(const Element &)), + this, SLOT(prepMethodModifiedDBSlot(const Element &)) ); + connect( m_database, SIGNAL(prepMethodRemoved(int)), + this, SLOT(prepMethodRemovedDBSlot(int)) ); } = void IngredientsEditor::setRecipeTitle( const QString & title ) @@ -792,6 +800,134 @@ void IngredientsEditor::unitRemovedDBSlot( int unitRe= movedId ) = } = +void IngredientsEditor::prepMethodCreatedDBSlot( const Element & newPrepMe= thod ) +{ + //Disconnect the changed signal temporarily + disconnect( m_sourceModel, SIGNAL(itemChanged(QStandardItem*)), + this, SIGNAL(changed()) ); + + QModelIndex index; + int rowCount =3D m_sourceModel->rowCount(); + QString modelIngredientName; + bool prepMethodFound; + //Check in the model if there is a preparation method with the same name = and the Id + //set as RecipeDB::InvalidId (that means the preparation method is going = to be created + //when saving the recipe). If there is any, update its Id to the Id of th= e preparation + //method which was just created in the database so we won't have nonsense= duplicates. + for ( int i =3D 0; i < rowCount; ++i ) { + index =3D m_sourceModel->index( i, prepmethodsColumn() ); + + QList prepMethodsIds =3D m_sourceModel->data( index, IdRole ).= toList(); + QString prepMethodsString =3D m_sourceModel->data( index, Qt::EditRole )= .toString(); + QStringList prepStringList =3D prepMethodsString.split(", ", QString::Sk= ipEmptyParts); + QList::iterator prep_ids_it =3D prepMethodsIds.begin(); + QStringList::const_iterator prep_str_it =3D prepStringList.constBegin(); + Element element; + prepMethodFound =3D false; + while ( prep_str_it !=3D prepStringList.constEnd() ) { + if ( (prep_ids_it->toInt() =3D=3D RecipeDB::InvalidId) + && (*prep_str_it =3D=3D newPrepMethod.name) ) { + *prep_ids_it =3D newPrepMethod.id; + prepMethodFound =3D true; + } + ++prep_ids_it; + ++prep_str_it; + } + + if ( prepMethodFound ) { + m_sourceModel->setData( index, prepMethodsIds, IdRole ); + } + } + + //Re-connect the changed signal + connect( m_sourceModel, SIGNAL(itemChanged(QStandardItem*)), + this, SIGNAL(changed()) ); +} + +void IngredientsEditor::prepMethodModifiedDBSlot( const Element & newPrepM= ethod ) +{ + //Disconnect the changed signal temporarily + disconnect( m_sourceModel, SIGNAL(itemChanged(QStandardItem*)), + this, SIGNAL(changed()) ); + + QModelIndex index; + int rowCount =3D m_sourceModel->rowCount(); + QString modelIngredientName; + bool prepMethodFound; + //Find the modified prep method in the model and update its name + for ( int i =3D 0; i < rowCount; ++i ) { + index =3D m_sourceModel->index( i, prepmethodsColumn() ); + + QList prepMethodsIds =3D m_sourceModel->data( index, IdRole ).= toList(); + QString prepMethodsString =3D m_sourceModel->data( index, Qt::EditRole )= .toString(); + QStringList prepStringList =3D prepMethodsString.split(", ", QString::Sk= ipEmptyParts); + QList::const_iterator prep_ids_it =3D prepMethodsIds.constBegi= n(); + QStringList::iterator prep_str_it =3D prepStringList.begin(); + Element element; + prepMethodFound =3D false; + while ( prep_str_it !=3D prepStringList.end() ) { + if ( prep_ids_it->toInt() =3D=3D newPrepMethod.id ) { + *prep_str_it =3D newPrepMethod.name; + prepMethodFound =3D true; + } + ++prep_ids_it; + ++prep_str_it; + } + + if ( prepMethodFound ) { + prepMethodsString =3D prepStringList.join(", "); + m_sourceModel->setData( index, prepMethodsString, Qt::DisplayRole ); + } + } + + //Re-connect the changed signal + connect( m_sourceModel, SIGNAL(itemChanged(QStandardItem*)), + this, SIGNAL(changed()) ); +} + +void IngredientsEditor::prepMethodRemovedDBSlot( int prepMethodRemovedId ) +{ + + //Disconnect the changed signal temporarily + disconnect( m_sourceModel, SIGNAL(itemChanged(QStandardItem*)), + this, SIGNAL(changed()) ); + + QModelIndex index; + int rowCount =3D m_sourceModel->rowCount(); + QString modelIngredientName; + bool prepMethodFound; + //Find the preparation method in the model and, if found, set its Id as R= ecipeDB::InvalidId + for ( int i =3D 0; i < rowCount; ++i ) { + index =3D m_sourceModel->index( i, prepmethodsColumn() ); + + QList prepMethodsIds =3D m_sourceModel->data( index, IdRole ).= toList(); + QString prepMethodsString =3D m_sourceModel->data( index, Qt::EditRole )= .toString(); + QStringList prepStringList =3D prepMethodsString.split(", ", QString::Sk= ipEmptyParts); + QList::iterator prep_ids_it =3D prepMethodsIds.begin(); + QStringList::const_iterator prep_str_it =3D prepStringList.constBegin(); + Element element; + prepMethodFound =3D false; + while ( prep_str_it !=3D prepStringList.constEnd() ) { + if ( prep_ids_it->toInt() =3D=3D prepMethodRemovedId ) { + *prep_ids_it =3D RecipeDB::InvalidId; + kDebug() << "found"; + prepMethodFound =3D true; + } + ++prep_ids_it; + ++prep_str_it; + } + + if ( prepMethodFound ) { + m_sourceModel->setData( index, prepMethodsIds, IdRole ); + } + } + + + //Re-connect the changed signal + connect( m_sourceModel, SIGNAL(itemChanged(QStandardItem*)), + this, SIGNAL(changed()) ); +} + Ingredient IngredientsEditor::readIngredientFromRow( int row ) { Ingredient ingredient; diff --git a/src/dialogs/recipeinput/ingredientseditor.h b/src/dialogs/reci= peinput/ingredientseditor.h index 7429718..a435929 100644 --- a/src/dialogs/recipeinput/ingredientseditor.h +++ b/src/dialogs/recipeinput/ingredientseditor.h @@ -89,6 +89,10 @@ private slots: void unitModifiedDBSlot( const Unit & newUnit ); void unitRemovedDBSlot( int unitRemovedId ); = + void prepMethodCreatedDBSlot( const Element & newPrepMethod ); + void prepMethodModifiedDBSlot( const Element & newPrepMethod ); + void prepMethodRemovedDBSlot( int prepMethodRemovedId ); + private: void setRowData( int row, const Ingredient & ingredient ); void setRowData( int row, const Element & header );