Changeset 12400


Ignore:
Timestamp:
08/30/10 05:47:05 (18 months ago)
Author:
adamk
Message:

Save and restore the directory tree state as we move back and forth between projects

Location:
trunk/Padre/lib/Padre/Wx
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/Padre/lib/Padre/Wx/Directory.pm

    r12399 r12400  
    100100    $self->{tree}->SetPlData( 
    101101        $self->{tree}->GetRootItem, 
    102         Wx::TreeItemData->new( 
    103             Padre::Wx::Directory::Path->directory, 
    104         ), 
     102        Padre::Wx::Directory::Path->directory, 
    105103    ); 
    106104    Wx::Event::EVT_TREE_ITEM_EXPANDED( 
     
    258256    my $self   = shift; 
    259257    my $tree   = $self->{tree}; 
    260     my $files  = delete $self->{files}; 
     258    my $root   = $tree->GetRootItem; 
     259    my $files  = delete $self->{files}  or return; 
    261260    my $expand = delete $self->{expand} or return; 
    262     my @stack  = $tree->GetRootItem; 
    263     foreach my $i ( 0 .. $#$files ) { 
     261    my @stack  = (); 
     262    my $lock   = $self->main->lock('UPDATE'); 
     263    foreach my $i ( 1 .. $#$files ) { 
    264264        my $path  = $files->[$i]; 
    265265        my $image = $path->type ? 'folder' : 'package'; 
    266         unless ( $tree->GetPlData($stack[-1])->GetData->is_parent($path) ) { 
    267             if ( $files->[$i - 1]->is_parent($path) ) { 
    268                 # Descending 
    269                 push @stack, $tree->GetLastChild($stack[-1]); 
    270             } else { 
    271                 # Ascending 
    272                 while ( @stack ) { 
    273                     pop @stack; 
    274                     if ( $tree->GetPlData($stack[-1])->GetData->is_parent($path) ) { 
    275                         last; 
    276                     } 
    277                 } 
    278             }                    
    279         } 
    280         $tree->AppendItem( 
    281             $stack[-1],                   # Parent node 
     266        while (@stack) { 
     267 
     268            # If we are not the child of the deepest element in 
     269            # the stack, move up a level and try again 
     270            last if $tree->GetPlData( $stack[-1] )->is_parent($path); 
     271 
     272            # We have finished filling the directory. 
     273            # Now it (maybe) has children, we can expand it. 
     274            my $complete = pop @stack; 
     275            if ( $expand->{ $tree->GetPlData($complete)->unix } ) { 
     276                $tree->Expand($complete); 
     277            } 
     278        } 
     279 
     280        # If there is anything left on the stack it is our parent 
     281        my $parent = $stack[-1] || $root; 
     282 
     283        # Add the next item to that parent 
     284        my $item = $tree->AppendItem( 
     285            $parent,                      # Parent node 
    282286            $path->name,                  # Label 
    283287            $tree->{images}->{$image},    # Icon 
     
    285289            Wx::TreeItemData->new($path), # Embedded data 
    286290        ); 
    287     } 
    288 } 
    289          
     291 
     292        # If it is a folder, it goes onto the stack 
     293        if ( $path->type == 1 ) { 
     294            push @stack, $item; 
     295        } 
     296    } 
     297 
     298    # Apply the same Expand logic above to any remaining stack elements 
     299    while (@stack) { 
     300        my $complete = pop @stack; 
     301        if ( $expand->{ $tree->GetPlData($complete)->unix } ) { 
     302            $tree->Expand($complete); 
     303        } 
     304    } 
     305 
     306    # If we moved during the fill, move back 
     307    my $first = ($tree->GetFirstChild($root))[0]; 
     308    $tree->ScrollTo($first) if $first->IsOk; 
     309 
     310    return 1; 
     311} 
     312 
    290313 
    291314 
     
    335358                    root   => $self->{root}, 
    336359                    files  => $self->tree->GetChildrenPlData, 
    337                     expand => $self->tree->GetExpandedPlData, 
     360                    expand => $self->tree->expanded, 
    338361                ); 
    339362            } 
     
    354377            if ( $stash->{root} ) { 
    355378                # We have a cached state 
     379                $self->{files}  = $stash->{files}; 
    356380                $self->{expand} = $stash->{expand}; 
     381                $self->refill; 
    357382            } 
    358383        } 
    359384 
    360         # Flush the search box and start the rerender 
    361         if ( $self->{expand} ) { 
    362             $self->browse( @{$self->{expand}} ); 
    363         } else { 
    364             $self->browse; 
    365         } 
     385        # Flush the search box and restart 
     386        $self->browse; 
    366387    } 
    367388 
  • trunk/Padre/lib/Padre/Wx/Directory/TreeCtrl.pm

    r12378 r12400  
    185185# Returns a reference to a HASH of ->unix path strings. 
    186186sub expanded { 
    187     my $self   = shift; 
    188     my @queue  = $self->GetRootItem; 
    189     my %expand = (); 
    190     while (@queue) { 
    191         my $parent = shift @queue; 
    192         my ( $child, $cookie ) = $self->GetFirstChild($parent); 
    193         while ($child) { 
    194             if ( $self->IsExpanded($child) ) { 
    195                 $expand{ $self->GetPlData($child)->unix } = 1; 
    196                 push @queue, $child; 
    197             } 
    198             ( $child, $cookie ) = $self->GetNextChild( $parent, $cookie ); 
    199         } 
    200     } 
    201     return \%expand; 
     187    my $self  = shift; 
     188    my $items = $self->GetExpandedPlData; 
     189    my %hash  = map { $_->unix => 1 } @$items; 
     190    return \%hash; 
    202191} 
    203192 
  • trunk/Padre/lib/Padre/Wx/TreeCtrl.pm

    r12399 r12400  
    4343    while ( @queue ) { 
    4444        my $item = shift @queue; 
    45         push @data, $self->GetPlData($item)->GetData; 
    46  
    47         # Continue if we have no child nodes 
    48         next unless $self->GetChildrenCount( $item, 0 ); 
     45        push @data, $self->GetPlData($item); 
    4946 
    5047        # Processing children last to first and unshifting onto the 
    5148        # queue, lets us achieve depth-first top-down within the need 
    5249        # for intermediate storage or grepping. 
    53         my ( $child, $cookie ) = $self->GetLastChild($item); 
    54         while ( $cookie ) { 
     50        my $child = $self->GetLastChild($item); 
     51        while ( $child->IsOk ) { 
    5552            unshift @queue, $child; 
    56             ( $child, $cookie ) = $self->GetPreviousChild( $item, $cookie ); 
     53            $child = $self->GetPrevSibling($child); 
    5754        } 
    5855    } 
     
    6966    while ( @queue ) { 
    7067        my $item = shift @queue; 
    71         push @data, $self->GetPlData($item)->GetData; 
     68        push @data, $self->GetPlData($item); 
    7269 
    7370        # Processing children last to first and unshifting onto the 
    7471        # queue, lets us achieve depth-first top-down within the need 
    7572        # for intermediate storage or grepping. 
    76         my ( $child, $cookie ) = $self->GetLastChild($item); 
    77         while ( $cookie ) { 
    78             if ( $self->IsExpanded ) { 
     73        my $child = $self->GetLastChild($item); 
     74        while ( $child->IsOk ) { 
     75            if ( $self->IsExpanded($child) ) { 
    7976                unshift @queue, $child; 
    8077            } 
    81             ( $child, $cookie ) = $self->GetPreviousChild( $item, $cookie ); 
     78            $child = $self->GetPrevSibling($child); 
    8279        } 
    8380    } 
Note: See TracChangeset for help on using the changeset viewer.