treemodel.cpp
changeset 741 1b4d1ea6ea8c
parent 740 6dc0a20031f7
child 745 2d4cc445a86a
     1.1 --- a/treemodel.cpp	Wed Feb 04 16:33:16 2009 +0000
     1.2 +++ b/treemodel.cpp	Fri Mar 06 15:02:58 2009 +0000
     1.3 @@ -1,11 +1,11 @@
     1.4  #include <QtGui>
     1.5  
     1.6 +#include <iostream>
     1.7 +using namespace std;
     1.8 +
     1.9  #include "treeitem.h"
    1.10  #include "treemodel.h"
    1.11  
    1.12 -QModelIndex TreeModel::ixCur=QModelIndex();
    1.13 -TreeItem* TreeModel::itStartParent=NULL;
    1.14 -
    1.15  TreeModel::TreeModel(QObject *parent)
    1.16      : QAbstractItemModel(parent)
    1.17  {
    1.18 @@ -19,14 +19,6 @@
    1.19      delete rootItem;
    1.20  }
    1.21  
    1.22 -int TreeModel::columnCount(const QModelIndex &parent) const
    1.23 -{
    1.24 -    if (parent.isValid())
    1.25 -        return static_cast<TreeItem*>(parent.internalPointer())->columnCount();
    1.26 -    else
    1.27 -        return rootItem->columnCount();
    1.28 -}
    1.29 -
    1.30  QVariant TreeModel::data(const QModelIndex &index, int role) const
    1.31  {
    1.32      if (!index.isValid())
    1.33 @@ -100,30 +92,72 @@
    1.34      return parentItem->childCount();
    1.35  }
    1.36  
    1.37 -TreeItem* TreeModel::first()
    1.38 +int TreeModel::columnCount(const QModelIndex &parent) const
    1.39  {
    1.40 -	if (rootItem->childCount()  ==0)
    1.41 -	{
    1.42 -		ixCur=QModelIndex();
    1.43 -		return NULL;
    1.44 -	} 
    1.45 -	ixCur=index (0,0);
    1.46 -	return 	static_cast<TreeItem*>(ixCur.internalPointer());
    1.47 -}
    1.48 -	
    1.49 -TreeItem* TreeModel::next()
    1.50 -{
    1.51 -	if (!ixCur.isValid() )
    1.52 -		return NULL;
    1.53 -
    1.54 -	ixCur=index (ixCur.row()+1,0);
    1.55 -	if (ixCur.isValid())
    1.56 -		return 	static_cast<TreeItem*>(ixCur.internalPointer());
    1.57 -	return NULL;	
    1.58 -
    1.59 +    if (parent.isValid())
    1.60 +        return static_cast<TreeItem*>(parent.internalPointer())->columnCount();
    1.61 +    else
    1.62 +        return rootItem->columnCount();
    1.63  }
    1.64  
    1.65 -#include <iostream>
    1.66 +TreeItem* TreeModel::next(TreeItem* &current, TreeItem* &previous, int &d0)
    1.67 +{
    1.68 +	// Walk through map beginning at current with previous==0
    1.69 +	// Start at root, if current==NULL
    1.70 +	if (!current) current=rootItem;
    1.71 +
    1.72 +	// Are we just beginning to walk the map?
    1.73 +	if (!previous)
    1.74 +	{
    1.75 +		previous=current;
    1.76 +		d0=current->depth();
    1.77 +		current=current->getFirstBranch();
    1.78 +		return current;
    1.79 +	}
    1.80 +
    1.81 +	//std::cout << " cur="<<current->getHeading().toStdString();
    1.82 +	//std::cout << " prev="<<previous->getHeading().toStdString()<<std::endl;
    1.83 +
    1.84 +	// Going up or down (deeper)?
    1.85 +	if (current->depth() > previous->depth() )
    1.86 +	{	
    1.87 +		// Coming from above
    1.88 +		// Trying  to go down deeper
    1.89 +		if (current->branchCount() >0 )
    1.90 +		{
    1.91 +			previous=current;
    1.92 +			current=current->getFirstBranch();
    1.93 +			return current;
    1.94 +		}	
    1.95 +		// turn around and go up again
    1.96 +	}	
    1.97 +
    1.98 +	// Coming from below,
    1.99 +	// Trying to go down again to siblings
   1.100 +
   1.101 +	TreeItem *sibling=current->getBranchNum (previous->num()+1);
   1.102 +
   1.103 +	if (sibling)
   1.104 +	{	
   1.105 +		// Found sibling of previous, go there
   1.106 +		previous=current;
   1.107 +		current=sibling;
   1.108 +		return current;
   1.109 +	} 
   1.110 +
   1.111 +	// Go up and try to find siblings of current
   1.112 +	previous=current;
   1.113 +	current=current->parent();
   1.114 +
   1.115 +	// Check if we still can go somewhere
   1.116 +	if (!current) return current;
   1.117 +	
   1.118 +	while (current && current->depth() < previous->depth() )
   1.119 +		next (current,previous,d0);
   1.120 +		
   1.121 +	return current;
   1.122 +}
   1.123 +
   1.124  bool TreeModel::removeRows ( int row, int count, const QModelIndex & parent)
   1.125  {
   1.126  	int last=row+count-1;