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

List:       kde-commits
Subject:    [krecipes] src/dialogs/recipeinput: Update IngredientsEditor when headers change in database.
From:       José_Manuel_Santamaría_Lema <panfaust () gmail ! com>
Date:       2016-04-01 9:56:56
Message-ID: E1alvp2-0008L1-4C () scm ! kde ! org
[Download RAW message or body]

Git commit 59e1e2ce38659b0dd4395a1789f89bffcd1a014c by José Manuel Santamaría Lema.
Committed on 01/04/2016 at 09:30.
Pushed by joselema into branch 'master'.

Update IngredientsEditor when headers change in database.

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

http://commits.kde.org/krecipes/59e1e2ce38659b0dd4395a1789f89bffcd1a014c

diff --git a/src/dialogs/recipeinput/ingredientseditor.cpp \
b/src/dialogs/recipeinput/ingredientseditor.cpp index be55253..900c6eb 100644
--- a/src/dialogs/recipeinput/ingredientseditor.cpp
+++ b/src/dialogs/recipeinput/ingredientseditor.cpp
@@ -147,7 +147,6 @@ void IngredientsEditor::setDatabase( RecipeDB * database )
 	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 &)) );
@@ -155,6 +154,14 @@ void IngredientsEditor::setDatabase( RecipeDB * database )
 		this, SLOT(prepMethodModifiedDBSlot(const Element &)) );
 	connect( m_database, SIGNAL(prepMethodRemoved(int)),
 		this, SLOT(prepMethodRemovedDBSlot(int)) );
+
+	//Headers
+	connect( m_database, SIGNAL(ingGroupCreated(const Element &)),
+		this, SLOT(headerCreatedDBSlot(const Element &)) );
+	connect( m_database, SIGNAL(ingGroupModified(const Element &)),
+		this, SLOT(headerModifiedDBSlot(const Element &)) );
+	connect( m_database, SIGNAL(ingGroupRemoved(int)),
+		this, SLOT(headerRemovedDBSlot(int)) );
 }
 
 void IngredientsEditor::setRecipeTitle( const QString & title )
@@ -928,6 +935,113 @@ void IngredientsEditor::prepMethodRemovedDBSlot( int \
prepMethodRemovedId )  this, SIGNAL(changed()) );
 }
 
+void IngredientsEditor::headerCreatedDBSlot( const Element & newHeader )
+{
+	//Disconnect the changed signal temporarily
+	disconnect( m_sourceModel, SIGNAL(itemChanged(QStandardItem*)),
+		this, SIGNAL(changed()) );
+
+	QModelIndex index;
+	int rowCount = m_sourceModel->rowCount();
+	RecipeDB::IdType modelHeaderId;
+	QString modelHeaderName;
+	bool isHeader;
+	//Check in the model if there is a header with the same name and the Id set
+	//as RecipeDB::InvalidId (that means the header is going to be created when
+	//saving the recipe). If there is any, update its Id to the Id of the header
+	//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, headerColumn() );
+		isHeader = m_sourceModel->data( index, IsHeaderRole ).toBool();
+		if ( !isHeader ) {
+			continue;
+		}
+		modelHeaderId = m_sourceModel->data( index, IdRole ).toInt();
+		modelHeaderName = m_sourceModel->data( index, Qt::DisplayRole ).toString();
+		if ( (modelHeaderId == RecipeDB::InvalidId)
+		&& (newHeader.name == modelHeaderName) ) {
+			m_sourceModel->setData( index, newHeader.id, IdRole );
+			index = m_sourceModel->index( i, ingredientIdColumn() ); //FIXME: create \
headerIdColumn +			m_sourceModel->setData( index, newHeader.id, Qt::DisplayRole );
+		}
+
+	}
+
+	//Re-connect the changed signal
+	connect( m_sourceModel, SIGNAL(itemChanged(QStandardItem*)),
+		this, SIGNAL(changed()) );
+}
+
+void IngredientsEditor::headerModifiedDBSlot( const Element & newHeader )
+{
+	//Disconnect the changed signal temporarily
+	disconnect( m_sourceModel, SIGNAL(itemChanged(QStandardItem*)),
+		this, SIGNAL(changed()) );
+
+	QModelIndex index;
+	int rowCount = m_sourceModel->rowCount();
+	RecipeDB::IdType modelHeaderId;
+	QString modelHeaderName;
+	bool isHeader;
+	//Check in the model if there is a header with the same name and the Id set
+	//as RecipeDB::InvalidId (that means the header is going to be created when
+	//saving the recipe). If there is any, update its Id to the Id of the header
+	//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, headerColumn() );
+		isHeader = m_sourceModel->data( index, IsHeaderRole ).toBool();
+		if ( !isHeader ) {
+			continue;
+		}
+		modelHeaderId = m_sourceModel->data( index, IdRole ).toInt();
+		modelHeaderName = m_sourceModel->data( index, Qt::DisplayRole ).toString();
+		if ( modelHeaderId == newHeader.id ) {
+			m_sourceModel->setData( index, newHeader.name, Qt::DisplayRole );
+		}
+
+	}
+
+	//Re-connect the changed signal
+	connect( m_sourceModel, SIGNAL(itemChanged(QStandardItem*)),
+		this, SIGNAL(changed()) );
+
+}
+
+void IngredientsEditor::headerRemovedDBSlot( int headerRemovedId )
+{
+	//Disconnect the changed signal temporarily
+	disconnect( m_sourceModel, SIGNAL(itemChanged(QStandardItem*)),
+		this, SIGNAL(changed()) );
+
+	QModelIndex index;
+	int rowCount = m_sourceModel->rowCount();
+	RecipeDB::IdType modelHeaderId;
+	QString modelHeaderName;
+	bool isHeader;
+	//Check in the model if there is a header with the same name and the Id set
+	//as RecipeDB::InvalidId (that means the header is going to be created when
+	//saving the recipe). If there is any, update its Id to the Id of the header
+	//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, headerColumn() );
+		isHeader = m_sourceModel->data( index, IsHeaderRole ).toBool();
+		if ( !isHeader ) {
+			continue;
+		}
+		modelHeaderId = m_sourceModel->data( index, IdRole ).toInt();
+		modelHeaderName = m_sourceModel->data( index, Qt::DisplayRole ).toString();
+		if ( modelHeaderId == headerRemovedId ) {
+			m_sourceModel->setData( index, RecipeDB::InvalidId, IdRole );
+			index = m_sourceModel->index( i, ingredientIdColumn() ); //FIXME: create \
headerIdColumn +			m_sourceModel->setData( index, RecipeDB::InvalidId, \
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 a435929..51084bd 100644
--- a/src/dialogs/recipeinput/ingredientseditor.h
+++ b/src/dialogs/recipeinput/ingredientseditor.h
@@ -93,6 +93,10 @@ private slots:
 	void prepMethodModifiedDBSlot( const Element & newPrepMethod );
 	void prepMethodRemovedDBSlot( int prepMethodRemovedId );
 
+	void headerCreatedDBSlot( const Element & newHeader );
+	void headerModifiedDBSlot( const Element & newHeader );
+	void headerRemovedDBSlot( int removedHeaderId );
+
 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