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

List:       kde-commits
Subject:    [krecipes] src/dialogs/recipeinput: Update IngredientsEditor when units change in database.
From:       José_Manuel_Santamaría_Lema <panfaust () gmail ! com>
Date:       2016-03-31 15:54:47
Message-ID: E1alevn-0006l9-8U () scm ! kde ! org
[Download RAW message or body]

Git commit 58cb2aee3283803da901bfa7ceb354c03c7ec106 by José Manuel Santamaría Lema.
Committed on 31/03/2016 at 15:53.
Pushed by joselema into branch 'master'.

Update IngredientsEditor when units change in database.

M  +73   -0    src/dialogs/recipeinput/ingredientseditor.cpp
M  +4    -0    src/dialogs/recipeinput/ingredientseditor.h

http://commits.kde.org/krecipes/58cb2aee3283803da901bfa7ceb354c03c7ec106

diff --git a/src/dialogs/recipeinput/ingredientseditor.cpp \
b/src/dialogs/recipeinput/ingredientseditor.cpp index 1472d94..279956b 100644
--- a/src/dialogs/recipeinput/ingredientseditor.cpp
+++ b/src/dialogs/recipeinput/ingredientseditor.cpp
@@ -138,6 +138,15 @@ void IngredientsEditor::setDatabase( RecipeDB * database )
 
 	connect( m_database, SIGNAL(ingredientRemoved(int)),
 		this, SLOT(ingredientRemovedDBSlot(int)) );
+
+	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)) );
 }
 
 void IngredientsEditor::setRecipeTitle( const QString & title )
@@ -719,6 +728,70 @@ void IngredientsEditor::ingredientRemovedDBSlot( \
RecipeDB::IdType ingredientRemo  this, SIGNAL(changed()) );
 }
 
+void IngredientsEditor::unitCreatedDBSlot( const Unit & newUnit )
+{
+	Q_UNUSED(newUnit)
+	//We don't need to add code here for now because right now new units
+	//are created in the database when the user types a new unit
+}
+
+void IngredientsEditor::unitModifiedDBSlot( const Unit & newUnit )
+{
+	//Disconnect the changed signal temporarily
+	disconnect( m_sourceModel, SIGNAL(itemChanged(QStandardItem*)),
+		this, SIGNAL(changed()) );
+
+	QModelIndex index;
+	int rowCount = m_sourceModel->rowCount();
+	RecipeDB::IdType modelUnitId;
+	QString newUnitName;
+	Ingredient modelIngredient;
+
+	//Find the modified ingredient in the model and update its name
+	for ( int i = 0; i < rowCount; ++i ) {
+		index = m_sourceModel->index( i, unitColumn() );
+		modelUnitId = m_sourceModel->data( index, IdRole ).toInt();
+		if ( newUnit.id() == modelUnitId ) {
+			modelIngredient = readIngredientFromRow( i );
+			newUnitName = modelIngredient.amountUnitString();
+			m_sourceModel->setData( index, newUnitName, Qt::DisplayRole );
+		}
+
+	}
+
+	//Re-connect the changed signal
+	connect( m_sourceModel, SIGNAL(itemChanged(QStandardItem*)),
+		this, SIGNAL(changed()) );
+
+}
+
+void IngredientsEditor::unitRemovedDBSlot( int unitRemovedId )
+{
+	//Disconnect the changed signal temporarily
+	disconnect( m_sourceModel, SIGNAL(itemChanged(QStandardItem*)),
+		this, SIGNAL(changed()) );
+
+	QModelIndex index;
+	RecipeDB::IdType modelUnitId;
+	int rowCount = m_sourceModel->rowCount();
+	//If the ingredient was removed in the database set the ID to RecipeDB::InvalidId
+	//in the model, this means the ingredient will be created again when saving the
+	//recipe.
+	for ( int i = 0; i < rowCount; ++i ) {
+		index = m_sourceModel->index( i, unitColumn() );
+		modelUnitId = m_sourceModel->data( index, IdRole ).toInt();
+		if ( unitRemovedId == modelUnitId ) {
+			m_sourceModel->setData( index, RecipeDB::InvalidId, IdRole );
+			m_sourceModel->setData( index, QString(""), Qt::DisplayRole );
+		}
+	}
+
+	//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 8a3e927..7429718 100644
--- a/src/dialogs/recipeinput/ingredientseditor.h
+++ b/src/dialogs/recipeinput/ingredientseditor.h
@@ -85,6 +85,10 @@ private slots:
 	void ingredientModifiedDBSlot( const Ingredient & newIngredient );
 	void ingredientRemovedDBSlot( int ingredientRemovedId );
 
+	void unitCreatedDBSlot( const Unit & newUnit );
+	void unitModifiedDBSlot( const Unit & newUnit );
+	void unitRemovedDBSlot( int unitRemovedId );
+
 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