Bug 143748

Summary: when a function depends on another, propose to delete both instead of refusing to delete the latter
Product: kmplot Reporter: koxinga <koxinga>
Component: generalAssignee: Klaus-Dieter M <kd.moeller>
Status: RESOLVED FIXED    
Severity: wishlist    
Priority: NOR    
Version: unspecified   
Target Milestone: ---   
Platform: unspecified   
OS: Linux   
Latest Commit: Version Fixed In:
Attachments: simple patch

Description koxinga 2007-04-02 18:53:23 UTC
Version:            (using KDE Devel)
Compiler:          gcc 4.1.2 
OS:                Linux

If I create a new file with functions f(x)=x and g(x)=f(x), I can't delete f because g depends on it. IMHO, it would be better to have a dialog box offering to delete both.

I made a simple patch to present the kind of behaviour I would like. It is rather crude, especially if there are several dependances, there will be several dialogs.

(if you don't use my patch, the label of the box should be changed from "This function is depending on another function" to "another function depends on this function". It seems more logical.)
Comment 1 koxinga 2007-04-02 18:54:58 UTC
Created attachment 20156 [details]
simple patch
Comment 2 David Saxton 2007-04-03 00:08:47 UTC
SVN commit 649628 by saxton:

If the user trys to delete a function, and other functions depend on this function, then give the option of deleting these other functions in addition.
Thanks for the good bug report, koxinga.
BUG: 143748


 M  +42 -10    parser.cpp  


--- trunk/KDE/kdeedu/kmplot/kmplot/parser.cpp #649627:649628
@@ -604,23 +604,55 @@
 
 bool Parser::removeFunction( Function * item )
 {
-	foreach ( Function * it, m_ufkt )
+	// Build up a list of functions that need to be removed is this function is removed
+	QList<Function *> toRemove;
+	QStringList otherRemoveNames;
+	QList<Function *> newFunctions; // Added since the last iteration
+	
+	toRemove << item;
+	newFunctions << item;
+	
+	while ( ! newFunctions.isEmpty() )
 	{
-		if ( it == item )
-			continue;
+		QList<Function *> currentFunctions = newFunctions;
+		newFunctions.clear();
 		
-		if ( it->dependsOn( item ) )
+		foreach ( Function *f, currentFunctions )
 		{
-			KMessageBox::sorry(0,i18n("This function is depending on an other function"));
-			return false;
+			foreach ( Function *other, m_ufkt )
+			{
+				if ( (other==f) || toRemove.contains(other) )
+					continue;
+				
+				if ( other->dependsOn( f ) )
+				{
+					toRemove << other;
+					otherRemoveNames << other->name();
+					newFunctions << other;
+				}
+			}
 		}
 	}
 	
-	uint const id = item->id();
-	m_ufkt.remove(id);
-	delete item;
+	if ( toRemove.size() > 1 )
+	{
+		KGuiItem buttonContinue = KStandardGuiItem::cont();
+		buttonContinue.setText( i18n("Remove all") );
+		
+		int answer = KMessageBox::warningContinueCancel( 0, i18n( "The function %1 is depended upon by the following functions: %2. These must be removed in addition.", item->name(), otherRemoveNames.join(", ") ), QString(), buttonContinue );
+		
+		if ( answer == KMessageBox::Cancel )
+			return false;
+	}
 	
-	emit functionRemoved( id );
+	foreach ( Function *f, toRemove )
+	{
+		uint id = f->id();
+		m_ufkt.remove( id );
+		delete f;
+		emit functionRemoved( id );
+	}
+	
 	return true;
 }