treemodel.cpp
changeset 804 14f2b1b15242
parent 801 16a8ef1d82b2
child 825 1ad892c1a709
     1.1 --- a/treemodel.cpp	Fri Oct 02 14:31:03 2009 +0000
     1.2 +++ b/treemodel.cpp	Fri Nov 13 08:32:03 2009 +0000
     1.3 @@ -146,14 +146,8 @@
     1.4  	return c;
     1.5  }
     1.6  
     1.7 -BranchItem* TreeModel::next(BranchItem* &current, BranchItem* &previous, BranchItem* start)	// FIXME-3 change this to nextBranch and use "next" for all TIs
     1.8 +BranchItem* TreeModel::nextBranch (BranchItem* &current, BranchItem* &previous, bool deepLevelsFirst, BranchItem *start)	
     1.9  {
    1.10 -/*FIXME-3	cout << "TM::next \n"; 
    1.11 -	std::string ch="()"; if (current) ch=current->getHeadingStd();
    1.12 -	std::string ph="()"; if (previous) ph=previous->getHeadingStd();
    1.13 -	cout << "  cur="<<ch << " prev="<<ph<<endl;
    1.14 -*/
    1.15 -
    1.16  	// Walk through map beginning at current with previous==0
    1.17  	// Start at root, if current==NULL
    1.18  	if (!current) current=(BranchItem*)rootItem;
    1.19 @@ -166,63 +160,98 @@
    1.20  		current=current->getFirstBranch();
    1.21  		return current;
    1.22  	}
    1.23 +	if (deepLevelsFirst)
    1.24 +	{
    1.25 +		// Going up or down (deeper)?
    1.26 +		if (current->depth() > previous->depth() )
    1.27 +		{	
    1.28 +			// Coming from above
    1.29 +			// Trying  to go down deeper
    1.30 +			if (current->branchCount() >0 )
    1.31 +			{
    1.32 +				previous=current;
    1.33 +				current=current->getFirstBranch();
    1.34 +				return current;
    1.35 +			}	
    1.36 +			// turn around and go up again
    1.37 +			BranchItem *bi=current;
    1.38 +			current=previous;
    1.39 +			previous=bi;
    1.40 +		}	
    1.41  
    1.42 -	// Going up or down (deeper)?
    1.43 -	if (current->depth() > previous->depth() )
    1.44 -	{	
    1.45 -		// Coming from above
    1.46 -		// Trying  to go down deeper
    1.47 -//		cout << "  trying to go deeper\n";
    1.48 -		if (current->branchCount() >0 )
    1.49 -		{
    1.50 -//			cout << "  yes, going deeper\n";
    1.51 +		// Coming from below
    1.52 +		// Trying to go down again to siblings
    1.53 +
    1.54 +		BranchItem *sibling=current->getBranchNum (previous->num()+1);
    1.55 +
    1.56 +		if (sibling)
    1.57 +		{	
    1.58 +			// Found sibling of previous, go there
    1.59  			previous=current;
    1.60 -			current=current->getFirstBranch();
    1.61 +			current=sibling;
    1.62  			return current;
    1.63 -		}	
    1.64 -		// turn around and go up again
    1.65 -//		cout << "  sorry, turn around\n";
    1.66 -		BranchItem *bi=current;
    1.67 -		current=previous;
    1.68 -		previous=bi;
    1.69 -	}	
    1.70 +		} 
    1.71  
    1.72 -/*
    1.73 -	cout << "  coming from below\n";
    1.74 -	ch="()"; if (current) ch=current->getHeadingStd();
    1.75 -	ph="()"; if (previous) ph=previous->getHeadingStd();
    1.76 -	cout << "  cur="<<ch << " prev="<<ph<<endl;
    1.77 -*/	
    1.78 -	// Coming from below
    1.79 -	// Trying to go down again to siblings
    1.80 +		// If we only needed to go through subtree, we are done now
    1.81 +		if (start==current) return NULL;
    1.82  
    1.83 -	BranchItem *sibling=current->getBranchNum (previous->num()+1);
    1.84 -//	cout <<"    prev->num()="<<previous->num()<<endl;
    1.85 +		// Go up and try to find siblings of current
    1.86 +		previous=current;
    1.87 +		current=(BranchItem*)current->parent();
    1.88  
    1.89 -	if (sibling)
    1.90 -	{	
    1.91 -		// Found sibling of previous, go there
    1.92 -//		cout << "  sib=cur="<<sibling->getHeadingStd()<<endl;
    1.93 -		previous=current;
    1.94 -		current=sibling;
    1.95 +		// Check if we still can go somewhere
    1.96 +		if (!current) return current;
    1.97 +		
    1.98 +		while (current && current->depth() < previous->depth() )
    1.99 +			current=nextBranch (current,previous,true,start);
   1.100 +			
   1.101  		return current;
   1.102 -	} 
   1.103  
   1.104 -	// If we only needed to go through subtree, we are done now
   1.105 -	if (start==current) return NULL;
   1.106 +	} else
   1.107 +	{
   1.108 +/*FIXME-3
   1.109 +		cout << "TM::nextBranch shallow\n"; 
   1.110 +		std::string ch="()"; if (current) ch=current->getHeadingStd();
   1.111 +		std::string ph="()"; if (previous) ph=previous->getHeadingStd();
   1.112 +		cout << "  cur="<<ch << " prev="<<ph<<endl;
   1.113 +*/
   1.114  
   1.115 -	// Go up and try to find siblings of current
   1.116 -//	cout <<"  going up again...\n";
   1.117 -	previous=current;
   1.118 -	current=(BranchItem*)current->parent();
   1.119 +		// Try to find sibling with same depth
   1.120 +		BranchItem *sibling=current->parent()->getBranchNum (current->num()+1);
   1.121 +		if (sibling)
   1.122 +		{	
   1.123 +			// Found sibling of previous, go there
   1.124 +			previous=current;
   1.125 +			current=sibling;
   1.126 +			return current;
   1.127 +		}  else
   1.128 +		{	
   1.129 +			// Try to find next branch with same depth or greater
   1.130 +			
   1.131  
   1.132 -	// Check if we still can go somewhere
   1.133 -	if (!current) return current;
   1.134 -	
   1.135 -	while (current && current->depth() < previous->depth() )
   1.136 -		current=next (current,previous,start);
   1.137 -		
   1.138 -	return current;
   1.139 +			current=NULL;
   1.140 +			return current;
   1.141 +		}
   1.142 +
   1.143 +
   1.144 +		/*
   1.145 +	while (ix.isValid())
   1.146 +	{
   1.147 +		TreeItem *ti=model->getItem (ix);
   1.148 +		cout << "  level="<<level<<"  ix=";
   1.149 +		if (ti) cout << ti->getHeadingStd();
   1.150 +		row=ix.row();
   1.151 +		col=ix.column();
   1.152 +		if (! treeEditor->isExpanded(ix))
   1.153 +			cout <<"  expand!";
   1.154 +		else	
   1.155 +			cout <<"  is expanded.";
   1.156 +		cout <<endl;
   1.157 +		ix=ix.sibling(row+1,col);
   1.158 +	}
   1.159 +	*/
   1.160 +
   1.161 +	}
   1.162  }
   1.163  
   1.164  bool TreeModel::removeRows ( int row, int count, const QModelIndex & parent)