This patch was sent to the list a few days back. I'm reposting it before
committing, in case anyone wants to comment.

The previous code for setting a "default" reference was irremediably
broken. State has to be maintained in QRef itself, not in the list
widget, which gets cleared every time through redoRefs()---and we go
through several times each update. Hence the new last_reference_
variable.

By the way, running several of these dialogs through the debugger, I've
noticed that this is generally true: if you go through update_contents()
once, you'll go through it two or three times. This does not seem like a
good thing, but I don't know enough about how this code works to have
any sense why it happens.

Richard

-- 
==================================================================
Richard G Heck, Jr
Professor of Philosophy
Brown University
http://frege.brown.edu/heck/
==================================================================
Get my public key from http://sks.keyserver.penguin.de
Hash: 0x1DE91F1E66FFBDEC
Learn how to sign your email using Thunderbird and GnuPG at:
http://dudu.dyn.2-h.org/nist/gpg-enigmail-howto


Index: QRef.C
===================================================================
--- QRef.C	(revision 17918)
+++ QRef.C	(working copy)
@@ -110,9 +110,11 @@
 void QRef::apply()
 {
 	InsetCommandParams & params = controller().params();
+	
+	last_reference_ = dialog_->referenceED->text();
 
 	params.setCmdName(InsetRef::getName(dialog_->typeCO->currentIndex()));
-	params["reference"] = qstring_to_ucs4(dialog_->referenceED->text());
+	params["reference"] = qstring_to_ucs4(last_reference_);
 	params["name"] = qstring_to_ucs4(dialog_->nameED->text());
 
 	restored_buffer_ = dialog_->bufferCO->currentIndex();
@@ -173,15 +175,13 @@
 	// we modify their state.
 	dialog_->refsLW->blockSignals(true);
 	dialog_->referenceED->blockSignals(true);
-
-	int lastref = dialog_->refsLW->currentRow();
-
 	dialog_->refsLW->setUpdatesEnabled(false);
+	
 	dialog_->refsLW->clear();
 
 	// need this because Qt will send a highlight() here for
 	// the first item inserted
-	QString const tmp(dialog_->referenceED->text());
+	QString const oldSelection(dialog_->referenceED->text());
 
 	for (std::vector<docstring>::const_iterator iter = refs_.begin();
 		iter != refs_.end(); ++iter) {
@@ -191,21 +191,28 @@
 	if (sort_)
 		dialog_->refsLW->sortItems();
 
-	dialog_->referenceED->setText(tmp);
+	dialog_->referenceED->setText(oldSelection);
 
-	// restore the last selection for new insets
-	if (tmp.isEmpty() && lastref != -1
-		&& lastref < int(dialog_->refsLW->count())) {
-		dialog_->refsLW->setCurrentRow(lastref);
-		dialog_->refsLW->clearSelection();
-	} else
-		for (int i = 0; i < dialog_->refsLW->count(); ++i) {
+	// restore the last selection or, for new insets, highlight
+	// the previous selection
+	if (!oldSelection.isEmpty() || !last_reference_.isEmpty()) {
+		bool const newInset = oldSelection.isEmpty();
+		QString textToFind = newInset ? last_reference_ : oldSelection;
+		bool foundItem = false;
+		for (int i = 0; !foundItem && i < dialog_->refsLW->count(); ++i) {
 			QListWidgetItem * item = dialog_->refsLW->item(i);
-			if (tmp == item->text()) {
-				dialog_->refsLW->setItemSelected(item, true);
+			if (textToFind == item->text()) {
+				dialog_->refsLW->setCurrentItem(item);
+				dialog_->refsLW->setItemSelected(item, !newInset);
+				//Make sure selected item is visible
+				dialog_->refsLW->scrollToItem(item);
+				foundItem = true;
 			}
 		}
-
+		if (foundItem)
+			last_reference_ = textToFind;
+		else last_reference_ = "";
+	}
 	dialog_->refsLW->setUpdatesEnabled(true);
 	dialog_->refsLW->update();
 
Index: QRef.h
===================================================================
--- QRef.h	(revision 17918)
+++ QRef.h	(working copy)
@@ -66,8 +66,11 @@
 	/// sort or not persistent state
 	bool sort_;
 
-	/// at a reference ?
+	/// went to a reference ?
 	bool at_ref_;
+	
+	/// the last reference entered or examined
+	QString last_reference_;
 
 	/// store the buffer settings
 	int restored_buffer_;

Reply via email to