Skip to content

Instantly share code, notes, and snippets.

@anaselli
Created April 10, 2014 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anaselli/10385839 to your computer and use it in GitHub Desktop.
Save anaselli/10385839 to your computer and use it in GitHub Desktop.
diff --git a/examples/ManyWidgets.cc b/examples/ManyWidgets.cc
index 252cd3a..3069b0c 100644
--- a/examples/ManyWidgets.cc
+++ b/examples/ManyWidgets.cc
@@ -392,7 +392,6 @@ int main( int argc, char **argv )
}
}
- std::string TEST = "Test string";
frame = YUI::widgetFactory()->createCheckBoxFrame( hbox, "Tree", false );
{
frame->setWeight( YD_HORIZ, 1 );
@@ -424,10 +423,12 @@ int main( int argc, char **argv )
new YTreeItem( t, "Item 2" );
new YTreeItem( t, "Item 3" );
t = new YTreeItem( t, "Item 4" );
+ t->setSelected(true); // selected item
new YTreeItem( t, "Item 1" );
auto t2 = new YTreeItem( t, "Item 2" );
- t2->setSelected(true);
- new YTreeItem( t, "Item 3" );
+ t2->setSelected(true); // this selection is discarded as it is a children of a selected item
+ t2 = new YTreeItem( t, "Item 3" );
+ t2->setSelected(true); // this selection is discarded as it is a children of a selected item
items.push_back( (t = new YTreeItem( "Item 123456" )) );
new YTreeItem( t, "Item 1" );
diff --git a/src/YSelectionWidget.cc b/src/YSelectionWidget.cc
index 4d5c5f6..dfd0179 100644
--- a/src/YSelectionWidget.cc
+++ b/src/YSelectionWidget.cc
@@ -188,32 +188,26 @@ void YSelectionWidget::addItem( YItem * item )
if ( priv->enforceSingleSelection )
{
- YItem * selectedChild = findSelectedItem( item->childrenBegin(),
- item->childrenEnd() );
+ YItem* newItemSelected = NULL;
+ if ( item->selected() )
+ {
+ newItemSelected = item;
+ }
+ else
+ {
+ newItemSelected = findSelectedItem( item->childrenBegin(),
+ item->childrenEnd() );
+ }
- if ( item->selected() || selectedChild )
+ if ( newItemSelected )
{
- YItem * oldSelectedItem = selectedItem();
-
- // This looks expensive, but it is not: Even though selectedItem()
- // searches the complete item list until it finds a selected item,
- // this happens only if a new item is to be inserted that has the
- // "selected" flag on. In the normal case, this will only be one
- // item.
- //
- // Only if the calling application does this systematically wrong
- // and sets the "selected" flag for ALL items it inserts this will
- // be more expensive. But then, this is a bug in that application
- // that needs to be fixed.
-
- if ( oldSelectedItem)
- {
- if ((selectedChild && oldSelectedItem != selectedChild) ||
- (item->selected() && oldSelectedItem != item ) )
- {
- oldSelectedItem->setSelected( false );
- }
- }
+ // This looks expensive, but it is not: Even though deselectAllItems()
+ // searches the complete item list and de select all.
+ //
+ // This prevents the calling application does this systematically wrong
+ // and sets the "selected" flag for more items or children
+ deselectAllItems();
+ newItemSelected->setSelected( true );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment