# HG changeset patch
# User insilmaril
# Date 1233765196 0
# Node ID 6dc0a20031f74cd4106e4263ac7cefdb97a47c67
# Parent  3d43b46a856406902c5db2c6606b7a4f19690b33
started to rewrite first(), next()

diff -r 3d43b46a8564 -r 6dc0a20031f7 exports.cpp
--- a/exports.cpp	Wed Feb 04 11:52:52 2009 +0000
+++ b/exports.cpp	Wed Feb 04 16:33:16 2009 +0000
@@ -143,51 +143,57 @@
 	QString s;
 	QString curIndent;
 	int i;
+	TreeItem *ti;
+	ti=model->first();
 	BranchObj *bo;
-	bo=model->first();
-	while (bo) 
+	while (ti) 
 	{
-		// Make indentstring
-		curIndent="";
-		for (i=0;i<bo->getDepth()-1;i++) curIndent+= indentPerDepth;
+		if (ti->getType()==TreeItem::Branch || ti->getType()==TreeItem::MapCenter)
+		{
+			bo=(BranchObj*)(ti->getLMO());
 
-		if (!bo->hasHiddenExportParent() )
-		{
-			switch (bo->getDepth())
+			// Make indentstring
+			curIndent="";
+			for (i=0;i<ti->depth()-1;i++) curIndent+= indentPerDepth;
+
+			if (!bo->hasHiddenExportParent() )
 			{
-				case 0:
-					ts << underline (bo->getHeading(),QString("="));
-					ts << "\n";
-					break;
-				case 1:
-					ts << "\n";
-					ts << (underline (getSectionString(bo) + bo->getHeading(), QString("-") ) );
-					ts << "\n";
-					break;
-				case 2:
-					ts << "\n";
-					ts << (curIndent + "* " + bo->getHeading());
-					ts << "\n";
-					break;
-				case 3:
-					ts << (curIndent + "- " + bo->getHeading());
-					ts << "\n";
-					break;
-				default:
-					ts << (curIndent + "- " + bo->getHeading());
-					ts << "\n";
-					break;
-			}
+				switch (ti->depth())
+				{
+					case 0:
+						ts << underline (ti->getHeading(),QString("="));
+						ts << "\n";
+						break;
+					case 1:
+						ts << "\n";
+						ts << (underline (getSectionString(bo) + ti->getHeading(), QString("-") ) );
+						ts << "\n";
+						break;
+					case 2:
+						ts << "\n";
+						ts << (curIndent + "* " + ti->getHeading());
+						ts << "\n";
+						break;
+					case 3:
+						ts << (curIndent + "- " + ti->getHeading());
+						ts << "\n";
+						break;
+					default:
+						ts << (curIndent + "- " + ti->getHeading());
+						ts << "\n";
+						break;
+				}
 
-			// If necessary, write note
-			if (!bo->getNote().isEmpty())
-			{
-				curIndent +="  | ";
-				s=bo->getNoteASCII( curIndent, 80);
-				ts << s;
+				// If necessary, write note
+				if (!bo->getNote().isEmpty())
+				{
+					curIndent +="  | ";
+					s=bo->getNoteASCII( curIndent, 80);
+					ts << s;
+				}
 			}
 		}
-		bo=model->next(bo);
+		ti=model->next();
 	}
 	file.close();
 }
@@ -219,9 +225,11 @@
 	QString curIndent("");
 	int i;
 	BranchObj *bo;
-	bo=model->first();
-	while (bo) 
+	TreeItem *ti=model->first();
+	while (ti) 
 	{
+		bo=(BranchObj*)(ti->getLMO());
+
 		if (!bo->hasHiddenExportParent() )
 		{
 			// If necessary, write note
@@ -234,13 +242,13 @@
 				ts <<"\"\",";
 
 			// Make indentstring
-			for (i=0;i<bo->getDepth();i++) curIndent+= "\"\",";
+			for (i=0;i<ti->depth();i++) curIndent+= "\"\",";
 
 			// Write heading
-			ts << curIndent << "\"" << bo->getHeading()<<"\""<<endl;
+			ts << curIndent << "\"" << ti->getHeading()<<"\""<<endl;
 		}
 		
-		bo=model->next(bo);
+		ti=model->next();
 		curIndent="";
 	}
 	file.close();
@@ -344,34 +352,39 @@
   // QString curIndent("");
   // int i;
   BranchObj *bo;
-  bo=model->first();
-  while (bo) {
+  TreeItem *ti=model->first();
+  while (ti) 
+  {
+	bo=(BranchObj*)(ti->getLMO());
+
 	if (!bo->hasHiddenExportParent() )
 	{
-		if (bo->getDepth()==0);
-		else if (bo->getDepth()==1) {
-		  ts << ("\\chapter{" + bo->getHeading()+ "}\n");
+		switch (ti->depth() ) 
+		{
+			case 0: break;
+			case 1: 
+			  ts << ("\\chapter{" + bo->getHeading()+ "}\n");
+			  break;
+			case 2: 
+			  ts << ("\\section{" + bo->getHeading()+ "}\n");
+			  break;
+			case 3: 
+			  ts << ("\\subsection{" + bo->getHeading()+ "}\n");
+			  break;
+			case 4: 
+			  ts << ("\\subsubsection{" + bo->getHeading()+ "}\n");
+			  break;
+			default:
+			  ts << ("\\paragraph*{" + bo->getHeading()+ "}\n");
+			
 		}
-		else if (bo->getDepth()==2) {
-		  ts << ("\\section{" + bo->getHeading()+ "}\n");
-		}
-		else if (bo->getDepth()==3) {
-		  ts << ("\\subsection{" + bo->getHeading()+ "}\n");
-		}
-		else if (bo->getDepth()==4) {
-		  ts << ("\\subsubsection{" + bo->getHeading()+ "}\n");
-		}
-		else {
-		  ts << ("\\paragraph*{" + bo->getHeading()+ "}\n");
-		}
-		
 		// If necessary, write note
 		if (!bo->getNote().isEmpty()) {
 		  ts << (bo->getNoteASCII());
 		  ts << ("\n");
 		}
 	}
-    bo=model->next(bo);
+    ti=model->next();
    }
   file.close();
 }
diff -r 3d43b46a8564 -r 6dc0a20031f7 mapeditor.cpp
--- a/mapeditor.cpp	Wed Feb 04 11:52:52 2009 +0000
+++ b/mapeditor.cpp	Wed Feb 04 16:33:16 2009 +0000
@@ -291,17 +291,19 @@
 	QPointF p;
 	QPointF q;
 	BranchObj *bo;
-	bo=model->first();
-	while (bo) 
+	TreeItem *ti=model->first();
+	while (ti) 
 	{
-		if (bo->getDepth() >0 && !bo->hasScrolledParent(bo) )
+		bo=(BranchObj*)(ti->getLMO());
+
+		if (ti->depth() >0 && !bo->hasScrolledParent(bo) )
 		{
 			p=QPointF (qrand() %600-300, qrand () %600-300);
 			bo->setRelPos();
 			q=bo->getRelPos();
 			model->startAnimation (bo,p, q);
 		}
-		bo=model->next(bo);
+		ti=model->next();
 	}
 
 
diff -r 3d43b46a8564 -r 6dc0a20031f7 treeitem.cpp
--- a/treeitem.cpp	Wed Feb 04 11:52:52 2009 +0000
+++ b/treeitem.cpp	Wed Feb 04 16:33:16 2009 +0000
@@ -66,6 +66,18 @@
     return 0;
 }
 
+int TreeItem::depth() 
+{
+	int d=-1;
+	TreeItem *ti=this;
+	while (ti!=NULL)
+	{
+		ti=ti->parent();
+		d++;
+	}
+	return d;
+}
+
 TreeItem *TreeItem::parent()
 {
     return parentItem;
diff -r 3d43b46a8564 -r 6dc0a20031f7 treeitem.h
--- a/treeitem.h	Wed Feb 04 11:52:52 2009 +0000
+++ b/treeitem.h	Wed Feb 04 16:33:16 2009 +0000
@@ -23,6 +23,7 @@
 
     int row() const;
 	int column() const;
+	int depth() ;
     TreeItem *parent();
 
 	// Accessing data
diff -r 3d43b46a8564 -r 6dc0a20031f7 treemodel.cpp
--- a/treemodel.cpp	Wed Feb 04 11:52:52 2009 +0000
+++ b/treemodel.cpp	Wed Feb 04 16:33:16 2009 +0000
@@ -3,8 +3,8 @@
 #include "treeitem.h"
 #include "treemodel.h"
 
-TreeItem* TreeModel::itFirst=NULL;
-TreeItem* TreeModel::itCur=NULL;
+QModelIndex TreeModel::ixCur=QModelIndex();
+TreeItem* TreeModel::itStartParent=NULL;
 
 TreeModel::TreeModel(QObject *parent)
     : QAbstractItemModel(parent)
@@ -102,143 +102,25 @@
 
 TreeItem* TreeModel::first()
 {
-	itCur=NULL;	
-	itFirst=rootItem;
-	return rootItem; 
+	if (rootItem->childCount()  ==0)
+	{
+		ixCur=QModelIndex();
+		return NULL;
+	} 
+	ixCur=index (0,0);
+	return 	static_cast<TreeItem*>(ixCur.internalPointer());
 }
 	
 TreeItem* TreeModel::next()
 {
-/*
-	BranchObj *bo;
-	BranchObj *lmo;
-	BranchObj *po=(BranchObj*)parObj;
+	if (!ixCur.isValid() )
+		return NULL;
 
-	if (branch.isEmpty())
-		bo=NULL;
-	else
-		bo=branch.first();
+	ixCur=index (ixCur.row()+1,0);
+	if (ixCur.isValid())
+		return 	static_cast<TreeItem*>(ixCur.internalPointer());
+	return NULL;	
 
-	if (!itCur)
-	{
-		// no itCur, we are just beginning
-		if (bo) 
-		{
-			// we have children, return first one
-			itCur=this;
-			return bo;
-		}	
-		else
-		{
-			// No children, so there is no next
-			itCur=this;
-			return NULL;
-		}	
-	}
-
-	// We have an itCur
-	if (itCur==po)
-	{	// We come from parent
-		if (bo)
-		{
-			// there are children, go there
-			itCur=this;
-			return bo;
-		}	
-		else
-		{	// no children, try to go up again
-			if (po)
-			{
-				// go back to parent and try to find next there
-				itCur=this;
-				lmo=po->next();
-				itCur=this;
-				return lmo;
-
-			}	
-			else
-			{
-				// can't go up, I am mapCenter, no next
-				itCur=NULL;
-				return NULL;
-			}	
-		}
-	}
-
-	// We don't come from parent, but from brother or children
-
-	// Try to find last child, where we came from, in my own children
-	bool searching=true;
-	int i=0;
-	while (i<branch.size())
-	{
-		// Try to find itCur in my own children
-		if (itCur==branch.at(i))
-		{
-			// ok, we come from my own children
-			if (i<branch.size()-1)
-				bo=branch.at(i+1);
-			 else
-				bo=NULL;
-			searching=false;
-			i=branch.size();
-		} 	
-		++i;	
-	}
-	if (!searching)
-	{	// found itCur in my children
-		if (bo)
-		{
-			// found a brother of lastLMO 
-			itCur=this;
-			return bo;
-		}	
-		else
-		{
-			if (po)
-			{
-				if (this==itFirst) return NULL;	// Stop at starting point
-				// go up
-				itCur=this;
-				lmo=po->next();
-				itCur=this;
-				return lmo;
-			}
-			else
-			{
-				// can't go up, I am mapCenter
-				itCur=NULL;
-				return NULL;
-			}	
-		}
-	}
-
-	// couldn't find last child, it must be a nephew of mine
-	if (branch.size()>0)
-	{
-		// proceed with my first child
-		itCur=this;	
-		return branch.first();
-	}	
-	else
-	{
-		// or go back to my parents
-		if (po)
-		{
-			// go up
-			itCur=this;
-			lmo=po->next();
-			itCur=this;
-			return lmo;
-		}	
-		else
-		{
-			// can't go up, I am mapCenter
-			itCur=NULL;
-			return NULL;
-		}	
-	}	
-	*/
 }
 
 #include <iostream>
@@ -248,7 +130,6 @@
 	beginRemoveRows (parent,row,last);
 
     TreeItem *pi= static_cast<TreeItem*>(parent.internalPointer());
-    TreeItem *ti;
 	for (int i=row; i<=last; i++)
 	{
 		std::cout << "TreeModel::removeRows removing i="<<i<<std::endl;
diff -r 3d43b46a8564 -r 6dc0a20031f7 treemodel.h
--- a/treemodel.h	Wed Feb 04 11:52:52 2009 +0000
+++ b/treemodel.h	Wed Feb 04 16:33:16 2009 +0000
@@ -37,8 +37,8 @@
 
 protected:
     TreeItem *rootItem;
-	static TreeItem* itFirst;
-	static TreeItem* itCur;
+	static QModelIndex ixCur;
+	static TreeItem *itStartParent;
 };
 
 #endif
diff -r 3d43b46a8564 -r 6dc0a20031f7 vymmodel.cpp
--- a/vymmodel.cpp	Wed Feb 04 11:52:52 2009 +0000
+++ b/vymmodel.cpp	Wed Feb 04 16:33:16 2009 +0000
@@ -703,7 +703,7 @@
 		return fio;
 	}
 	return NULL;
-	}	
+}	
 
 void VymModel::loadFloatImage ()
 {
@@ -1353,36 +1353,6 @@
 	return mapScene;
 }
 
-BranchObj* VymModel::first()
-{
-	if (mapCenters.count()>0) 
-		return mapCenters.first();
-	else	
-		return NULL;
-}
-	
-BranchObj* VymModel::next(BranchObj *bo_start)
-{
-	BranchObj *rbo;
-	BranchObj *bo=bo_start;
-	if (bo)
-	{
-		// Try to find next branch in current MapCenter
-		rbo=bo->next();
-		if (rbo) return rbo;
-
-		// Try to find MapCenter of bo
-		while (bo->getDepth()>0) bo=(BranchObj*)bo->getParObj();
-
-		// Try to find next MapCenter
-		int i=mapCenters.indexOf ((MapCenterObj*)bo);
-		if (i+2 > mapCenters.count() || i<0) return NULL;
-		if (mapCenters.at(i+1)!=bo_start)
-			return mapCenters.at(i+1);
-	} 
-	return NULL;
-}
-
 LinkableMapObj* VymModel::findMapObj(QPointF p, LinkableMapObj *excludeLMO)
 {
 	LinkableMapObj *lmo;
@@ -1520,8 +1490,9 @@
 	}
 }
 
-BranchObj* VymModel::findText (QString s, bool cs)
+BranchObj* VymModel::findText (QString s, bool cs)   // FIXME needs to converted to first() next() next (ti)
 {
+	/*
 	QTextDocument::FindFlags flags=0;
 	if (cs) flags=QTextDocument::FindCaseSensitively;
 
@@ -1571,6 +1542,7 @@
 		return getSelectedBranch();
 	else
 		return NULL;
+		*/
 }
 
 void VymModel::findReset()
@@ -3925,13 +3897,13 @@
 	else
 		linkstyle=LinkableMapObj::UndefinedStyle;
 
+	TreeItem *ti=first();
 	BranchObj *bo;
-	bo=first();
-	bo=next(bo);
-	while (bo) 
+	while (ti) 
 	{
+		bo=(BranchObj*)(ti->getLMO() );
 		bo->setLinkStyle(bo->getDefLinkStyle());
-		bo=next(bo);
+		ti=next();
 	}
 	reposition();
 }
@@ -3951,12 +3923,14 @@
 	);
 
 	defLinkColor=col;
+	TreeItem *ti;
 	BranchObj *bo;
-	bo=first();
-	while (bo) 
+	ti=first();
+	while (ti) 
 	{
+		bo=(BranchObj*)(ti->getLMO() );
 		bo->setLinkColor();
-		bo=next(bo);
+		ti=next();
 	}
 	updateActions();
 }
@@ -3964,12 +3938,14 @@
 void VymModel::setMapLinkColorHintInt()
 {
 	// called from setMapLinkColorHint(lch) or at end of parse
+	TreeItem *ti;
 	BranchObj *bo;
-	bo=first();
-	while (bo) 
+	ti=first();
+	while (ti) 
 	{
+		bo=(BranchObj*)(ti->getLMO() );
 		bo->setLinkColor();
-		bo=next(bo);
+		ti=next();
 	}
 }
 
@@ -3985,12 +3961,14 @@
 		linkcolorhint=LinkableMapObj::DefaultColor;
 	else	
 		linkcolorhint=LinkableMapObj::HeadingColor;
+	TreeItem *ti;
 	BranchObj *bo;
-	bo=first();
+	ti=first();
 	while (bo) 
 	{
+		bo=(BranchObj*)(ti->getLMO() );
 		bo->setLinkColor();
-		bo=next(bo);
+		ti=next();
 	}
 }
 
diff -r 3d43b46a8564 -r 6dc0a20031f7 vymmodel.h
--- a/vymmodel.h	Wed Feb 04 11:52:52 2009 +0000
+++ b/vymmodel.h	Wed Feb 04 16:33:16 2009 +0000
@@ -212,9 +212,6 @@
 	void setScene(QGraphicsScene *s);
 	QGraphicsScene *getScene();
 
-	BranchObj* first();					
-	BranchObj* next(BranchObj *bo);		
-
     LinkableMapObj* findMapObj(QPointF,LinkableMapObj*);	// find MapObj 
     LinkableMapObj* findObjBySelect (const QString &s);		// find MapObj by select string
     LinkableMapObj* findID (const QString &s);				// find MapObj by previously set ID
diff -r 3d43b46a8564 -r 6dc0a20031f7 xml-freemind.cpp
--- a/xml-freemind.cpp	Wed Feb 04 11:52:52 2009 +0000
+++ b/xml-freemind.cpp	Wed Feb 04 16:33:16 2009 +0000
@@ -67,16 +67,23 @@
 				   "saved using freemind " +atts.value("version")+". "
 				   "The version of this vym can parse freemind " + v +"."); 
 		}
+		TreeItem *ti=model->first();	// FIXME this will be NULL !!!
+		BranchObj *bo;
+		if (ti->getType()==TreeItem::MapCenter)
+			bo=(BranchObj*)(ti->getLMO());
+		else
+			qWarning ("parseFreeMindHandler::startElement  no mapCenter!!");
+
 		cout <<"model="<<model<<"   first="<<model->first()<<endl;
-		model->first()->addBranch();
-		lastBranch=model->first()->getLastBranch();
+
+		lastBranch=bo->addBranch();
+
 		lastBranch->move2RelPos (200,0);
 		lastBranch->setHeading ("  ");
-		model->first()->addBranch();
-		lastBranch=model->first()->getLastBranch();
+		lastBranch=bo->addBranch();
 		lastBranch->move2RelPos (-200,0);
 		lastBranch->setHeading ("  ");
-		lastBranch=model->first();
+		lastBranch=bo;
 
 	} else if ( eName == "node" &&  (state == StateMap || state == StateNode )) 
 	{
diff -r 3d43b46a8564 -r 6dc0a20031f7 xml-vym.cpp
--- a/xml-vym.cpp	Wed Feb 04 11:52:52 2009 +0000
+++ b/xml-vym.cpp	Wed Feb 04 16:33:16 2009 +0000
@@ -51,7 +51,7 @@
 		{
 			// Create mapCenter
 			model->clear();
-			lastBranch=model->first();	// avoid empty pointer
+			lastBranch=NULL;
 
 			if (!atts.value( "author").isEmpty() )
 				model->setAuthor(atts.value( "author" ) );
@@ -182,7 +182,7 @@
 			// selection==lmo==NULL
 			// Treat it like ImportAdd then...
 			loadMode=ImportAdd;
-			lmo=model->first();		
+			lmo=model->first()->getLMO();		
 		}	
 		if (lmo && ( (typeid(*lmo) == typeid(BranchObj) ) 
 				|| (typeid(*lmo) == typeid(MapCenterObj) ) ) )