[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    [krecipes] src/dialogs/recipeinput: Update IngredientsEditor when prep methods change in the databas
From:       José_Manuel_Santamaría_Lema <panfaust () gmail ! com>
Date:       2016-03-31 21:15:46
Message-ID: E1aljwQ-0001Db-WD () scm ! kde ! org
[Download RAW message or body]

Git commit 47d6678a7b16993a5b9ca911b596677b2b9776e7 by José Manuel Santamaría 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/recipeinput/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 * database )
 	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 unitRemovedId )
 
 }
 
+void IngredientsEditor::prepMethodCreatedDBSlot( const Element & newPrepMethod )
+{
+	//Disconnect the changed signal temporarily
+	disconnect( m_sourceModel, SIGNAL(itemChanged(QStandardItem*)),
+		this, SIGNAL(changed()) );
+
+	QModelIndex index;
+	int rowCount = 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 the \
preparation +	//method which was just created in the database so we won't have \
nonsense duplicates. +	for ( int i = 0; i < rowCount; ++i ) {
+		index = m_sourceModel->index( i, prepmethodsColumn() );
+
+		QList<QVariant> prepMethodsIds = m_sourceModel->data( index, IdRole ).toList();
+		QString prepMethodsString = m_sourceModel->data( index, Qt::EditRole ).toString();
+		QStringList prepStringList = prepMethodsString.split(", ", \
QString::SkipEmptyParts); +		QList<QVariant>::iterator prep_ids_it = \
prepMethodsIds.begin(); +		QStringList::const_iterator prep_str_it = \
prepStringList.constBegin(); +		Element element;
+		prepMethodFound = false;
+		while ( prep_str_it != prepStringList.constEnd() ) {
+			if ( (prep_ids_it->toInt() == RecipeDB::InvalidId)
+			&& (*prep_str_it == newPrepMethod.name) ) {
+				*prep_ids_it = newPrepMethod.id;
+				prepMethodFound = 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 & newPrepMethod )
+{
+	//Disconnect the changed signal temporarily
+	disconnect( m_sourceModel, SIGNAL(itemChanged(QStandardItem*)),
+		this, SIGNAL(changed()) );
+
+	QModelIndex index;
+	int rowCount = m_sourceModel->rowCount();
+	QString modelIngredientName;
+	bool prepMethodFound;
+	//Find the modified prep method in the model and update its name
+	for ( int i = 0; i < rowCount; ++i ) {
+		index = m_sourceModel->index( i, prepmethodsColumn() );
+
+		QList<QVariant> prepMethodsIds = m_sourceModel->data( index, IdRole ).toList();
+		QString prepMethodsString = m_sourceModel->data( index, Qt::EditRole ).toString();
+		QStringList prepStringList = prepMethodsString.split(", ", \
QString::SkipEmptyParts); +		QList<QVariant>::const_iterator prep_ids_it = \
prepMethodsIds.constBegin(); +		QStringList::iterator prep_str_it = \
prepStringList.begin(); +		Element element;
+		prepMethodFound = false;
+		while ( prep_str_it != prepStringList.end() ) {
+			if ( prep_ids_it->toInt() == newPrepMethod.id ) {
+				*prep_str_it = newPrepMethod.name;
+				prepMethodFound = true;
+			}
+			++prep_ids_it;
+			++prep_str_it;
+		}
+
+		if ( prepMethodFound ) {
+			prepMethodsString = 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 = m_sourceModel->rowCount();
+	QString modelIngredientName;
+	bool prepMethodFound;
+	//Find the preparation method in the model and, if found, set its Id as \
RecipeDB::InvalidId +	for ( int i = 0; i < rowCount; ++i ) {
+		index = m_sourceModel->index( i, prepmethodsColumn() );
+
+		QList<QVariant> prepMethodsIds = m_sourceModel->data( index, IdRole ).toList();
+		QString prepMethodsString = m_sourceModel->data( index, Qt::EditRole ).toString();
+		QStringList prepStringList = prepMethodsString.split(", ", \
QString::SkipEmptyParts); +		QList<QVariant>::iterator prep_ids_it = \
prepMethodsIds.begin(); +		QStringList::const_iterator prep_str_it = \
prepStringList.constBegin(); +		Element element;
+		prepMethodFound = false;
+		while ( prep_str_it != prepStringList.constEnd() ) {
+			if ( prep_ids_it->toInt() == prepMethodRemovedId ) {
+				*prep_ids_it = RecipeDB::InvalidId;
+				kDebug() << "found";
+				prepMethodFound = 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/recipeinput/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 );


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic