# HG changeset patch
# User insilmaril
# Date 1233306852 0
# Node ID 716a777c1c98ecae396771910dc8054e2dfbc5d6
# Parent  53e51e8d47e70294af71cedf677cb0b7fa154bb0
fixes for selecting branches

diff -r 53e51e8d47e7 -r 716a777c1c98 branchobj.cpp
--- a/branchobj.cpp	Thu Jan 22 15:40:08 2009 +0000
+++ b/branchobj.cpp	Fri Jan 30 09:14:12 2009 +0000
@@ -92,8 +92,6 @@
 		absPos+=parObj->getChildPos();
 	}
 
-    lastSelectedBranch=0;
-
     setChildObj(this);
 
 	scrolled=false;
@@ -1165,24 +1163,6 @@
 	requestReposition();
 }
 
-void BranchObj::setLastSelectedBranch (BranchObj* bo)
-{
-	cout << "BO::setLastSelectedBranch for "<<getHeading().toStdString()<<endl;
-    lastSelectedBranch=branch.indexOf(bo);
-}
-
-BranchObj* BranchObj::getLastSelectedBranch ()
-{
-    if (lastSelectedBranch>=0)
-	{
-		if ( branch.size()>lastSelectedBranch) 
-			return branch.at(lastSelectedBranch);
-		if (branch.size()>0)
-			return branch.last();
-	}	
-    return NULL;
-}
-
 BranchObj* BranchObj::getFirstBranch ()
 {
 	if (branch.size()>0)
diff -r 53e51e8d47e7 -r 716a777c1c98 branchobj.h
--- a/branchobj.h	Thu Jan 22 15:40:08 2009 +0000
+++ b/branchobj.h	Fri Jan 30 09:14:12 2009 +0000
@@ -105,8 +105,6 @@
     virtual void removeChildren();  
     virtual void removeBranch(BranchObj*);  
     virtual void removeBranchPtr (BranchObj*);  
-    virtual void setLastSelectedBranch(BranchObj*);
-    virtual BranchObj* getLastSelectedBranch();
     virtual BranchObj* getFirstBranch();
     virtual BranchObj* getLastBranch();
 	virtual BranchObj* getBranchNum(int);
@@ -143,7 +141,6 @@
 public:	
 	float angle;					// used in mainbranch to reorder mainbranches
 protected:	
-    int lastSelectedBranch;			// for going deeper into tree
 	bool scrolled;					// true if all children are scrolled and thus invisible
 	bool tmpUnscrolled;				// can only be true (temporary) for a scrolled subtree
 	bool includeImagesVer;			// include floatimages in bbox vertically
diff -r 53e51e8d47e7 -r 716a777c1c98 mapeditor.cpp
--- a/mapeditor.cpp	Thu Jan 22 15:40:08 2009 +0000
+++ b/mapeditor.cpp	Fri Jan 30 09:14:12 2009 +0000
@@ -537,19 +537,22 @@
 
     if (lmo) 
 	{	
+	/*
 		cout << "ME::mouse pressed\n";
 		cout << "  lmo="<<lmo<<endl;
 		cout << "  h="<<((BranchObj*)lmo)->getHeading().toStdString()<<endl;
+	*/
 		// Select the clicked object
 
 		// FIXME VM better let "find" return an index instead of lmo...
 		// Get index of clicked LMO
 		TreeItem *ti=lmo->getTreeItem();
+		/*
 		cout << "  lmo="<<lmo<<"    lmo(ti)="<<ti->getLMO()<<endl;
 		cout << "  ti ("<<ti->row()<<","<<ti->column()<<") = "<<ti<<endl;
+		*/
 		//QModelIndex ix=model->index( ti->row(), ti->column(), model->index (0,0,QModelIndex()) );
-		QModelIndex ix=model->index(ti);
-		model->getSelectionModel()->select (ix,QItemSelectionModel::ClearAndSelect  );
+		model->select (ti);
 
 		// Left Button	    Move Branches
 		if (e->button() == Qt::LeftButton )
diff -r 53e51e8d47e7 -r 716a777c1c98 treeitem.cpp
--- a/treeitem.cpp	Thu Jan 22 15:40:08 2009 +0000
+++ b/treeitem.cpp	Fri Jan 30 09:14:12 2009 +0000
@@ -7,6 +7,10 @@
     parentItem = parent;
     itemData = data;
 	lmo=NULL;
+
+	branchOffset=0;
+	branchCount=0;
+	lastSelectedBranchNum=-1;
 }
 
 TreeItem::~TreeItem()
@@ -17,6 +21,12 @@
 void TreeItem::appendChild(TreeItem *item)
 {
     childItems.append(item);
+	if (item->type == Branch)
+	{
+		if (branchCount==0)
+			branchOffset=childItems.count()-1;
+		branchCount++;
+	}
 }
 
 void TreeItem::removeChild(int row)
@@ -99,6 +109,50 @@
 	}
 }
 
+TreeItem* TreeItem::getChildNum(const int &n)
+{
+	if (n>=0 && n<childItems.count() )
+		return childItems.at(n);
+	else
+		return NULL;
+}
+
+TreeItem* TreeItem::getFirstBranch()
+{
+	if (branchCount>0)
+		return getBranchNum (branchOffset);
+	else
+		return NULL;
+}
+
+TreeItem* TreeItem::getLastBranch()
+{
+	if (branchCount>0)
+		return getBranchNum (branchOffset + branchCount-1);
+	else
+		return NULL;
+}
+
+
+TreeItem* TreeItem::getBranchNum(const int &n)
+{
+	if (branchCount>0)
+		return getChildNum (branchOffset + n);
+	else
+		return NULL;
+}
+
+void TreeItem::setLastSelectedBranch()
+{
+	if (parentItem)
+		parentItem->lastSelectedBranchNum=parentItem->childItems.indexOf(this);
+}
+
+TreeItem* TreeItem::getLastSelectedBranch()
+{
+	return getBranchNum (lastSelectedBranchNum);
+}
+
 LinkableMapObj* TreeItem::getLMO()	// FIXME VM should be unnecessary in the end
 {
 	return lmo;
diff -r 53e51e8d47e7 -r 716a777c1c98 treeitem.h
--- a/treeitem.h	Thu Jan 22 15:40:08 2009 +0000
+++ b/treeitem.h	Fri Jan 30 09:14:12 2009 +0000
@@ -32,6 +32,15 @@
 	void setType (const Type t);
 	Type getType ();
 	QString getTypeName ();
+
+	// Navigation and selection
+	TreeItem* getChildNum(const int &n);
+	TreeItem* getFirstBranch();
+	TreeItem* getLastBranch();
+	TreeItem* getBranchNum(const int &n);
+	void setLastSelectedBranch();
+	TreeItem* getLastSelectedBranch();
+
 	
 	// Relation to map objects in graphicsscene
 	LinkableMapObj* getLMO();
@@ -44,6 +53,10 @@
  
 	Type type;
 	LinkableMapObj *lmo;
+
+	int branchOffset;
+	int branchCount;
+	int lastSelectedBranchNum;
 };
 
 #endif
diff -r 53e51e8d47e7 -r 716a777c1c98 version.h
--- a/version.h	Thu Jan 22 15:40:08 2009 +0000
+++ b/version.h	Fri Jan 30 09:14:12 2009 +0000
@@ -7,7 +7,7 @@
 #define __VYM_VERSION "1.13.0"
 //#define __VYM_CODENAME "Codename: RC-1"
 #define __VYM_CODENAME "Codename: development version"
-#define __VYM_BUILD_DATE "2009-01-22"
+#define __VYM_BUILD_DATE "2009-01-28"
 
 
 bool checkVersion(const QString &);
diff -r 53e51e8d47e7 -r 716a777c1c98 vymmodel.cpp
--- a/vymmodel.cpp	Thu Jan 22 15:40:08 2009 +0000
+++ b/vymmodel.cpp	Fri Jan 30 09:14:12 2009 +0000
@@ -1500,7 +1500,7 @@
 		sel->setHeading(s );
 		/* FIXME testing only
 		*/
-		TreeItem *ti=getSelectedTreeItem();
+		TreeItem *ti=getSelectedItem();
 		if (ti)
 		{
 			ti->setHeading (s);
@@ -3975,7 +3975,7 @@
 	}
 }
 
-void VymModel::selectMapBackgroundImage ()
+void VymModel::selectMapBackgroundImage ()	// FIXME move to ME
 {
 	Q3FileDialog *fd=new Q3FileDialog( NULL);
 	fd->setMode (Q3FileDialog::ExistingFile);
@@ -4012,7 +4012,7 @@
 	mapScene->setBackgroundBrush(brush);
 }
 
-void VymModel::selectMapBackgroundColor()
+void VymModel::selectMapBackgroundColor()	// FIXME move to ME
 {
 	QColor col = QColorDialog::getColor( mapScene->backgroundBrush().color(), NULL);
 	if ( !col.isValid() ) return;
@@ -4020,7 +4020,7 @@
 }
 
 
-void VymModel::setMapBackgroundColor(QColor col)
+void VymModel::setMapBackgroundColor(QColor col)	// FIXME move to ME
 {
 	QColor oldcol=mapScene->backgroundBrush().color();
 	saveState(
@@ -4030,38 +4030,38 @@
 	mapScene->setBackgroundBrush(col);
 }
 
-QColor VymModel::getMapBackgroundColor()
+QColor VymModel::getMapBackgroundColor()	// FIXME move to ME
 {
     return mapScene->backgroundBrush().color();
 }
 
 
-LinkableMapObj::ColorHint VymModel::getMapLinkColorHint()
+LinkableMapObj::ColorHint VymModel::getMapLinkColorHint()	// FIXME move to ME
 {
 	return linkcolorhint;
 }
 
-QColor VymModel::getMapDefLinkColor()
+QColor VymModel::getMapDefLinkColor()	// FIXME move to ME
 {
 	return defLinkColor;
 }
 
-void VymModel::setMapDefXLinkColor(QColor col)
+void VymModel::setMapDefXLinkColor(QColor col)	// FIXME move to ME
 {
 	defXLinkColor=col;
 }
 
-QColor VymModel::getMapDefXLinkColor()
+QColor VymModel::getMapDefXLinkColor()	// FIXME move to ME
 {
 	return defXLinkColor;
 }
 
-void VymModel::setMapDefXLinkWidth (int w)
+void VymModel::setMapDefXLinkWidth (int w)	// FIXME move to ME
 {
 	defXLinkWidth=w;
 }
 
-int VymModel::getMapDefXLinkWidth()
+int VymModel::getMapDefXLinkWidth()	// FIXME move to ME
 {
 	return defXLinkWidth;
 }
@@ -4326,7 +4326,6 @@
 
 void VymModel::updateSelection()
 {
-	cout << "VM::updateSelection ()\n";
 	QItemSelection newsel=selModel->selection();
 	updateSelection (newsel);
 }
@@ -4334,6 +4333,7 @@
 void VymModel::updateSelection(const QItemSelection &oldsel)
 {
 	QItemSelection newsel=selModel->selection();
+	/*
 	cout << "VM::updateSelection   new=";
 	if (!newsel.indexes().isEmpty() )
 		cout << newsel.indexes().first().row()<<"," << newsel.indexes().first().column();
@@ -4341,7 +4341,8 @@
 	if (!oldsel.indexes().isEmpty() )
 		cout << oldsel.indexes().first().row()<<"," << oldsel.indexes().first().column();
 	cout <<endl;
-	//emit (selectionChanged(newsel,oldsel));
+	*/
+	//emit (selectionChanged(newsel,oldsel));	// FIXME needed?
 	ensureSelectionVisible();
 	sendSelection();
 }
@@ -4435,10 +4436,18 @@
 	QItemSelection oldsel=selModel->selection();
 
 	if (lmo)
+		return select (lmo->getTreeItem() );
+	else	
+		return false;
+}
+
+bool VymModel::select (TreeItem *ti)
+{
+	if (ti)
 	{
-		TreeItem *ti=lmo->getTreeItem();
 		QModelIndex ix=index(ti);
 		selModel->select (ix,QItemSelectionModel::ClearAndSelect  );
+		ti->setLastSelectedBranch();
 		//updateSelection(oldsel);	//FIXME needed?
 		return true;
 	}
@@ -4556,10 +4565,9 @@
 void VymModel::selectPrevBranchInt()
 {
 	// Decrease number of branch
-	BranchObj *bo=getSelectedBranch();
-	if (bo)
+	if (selectionType()==TreeItem::Branch)
 	{
-		QString s=selection.getSelectString();
+		QString s=getSelectString();
 		QString part;
 		QString typ;
 		QString num;
@@ -4577,6 +4585,9 @@
 		num=QString ("%1").arg(n);
 		s=s+num;
 		
+		cout <<"SP::  s0="<<s.toStdString()<<endl;
+
+
 		// Try to select this one
 		if (n>=0 && select (s)) return;
 
@@ -4612,6 +4623,7 @@
 				s=typ+num;
 			}	
 
+		cout <<"SP::  si="<<s.toStdString()<<endl;
 			if (select(s))
 				// pad to oldDepth, select the last branch for each depth
 				for (i=d;i<oldDepth;i++)
@@ -4753,16 +4765,14 @@
 
 void VymModel::selectFirstBranch()
 {
-	BranchObj *bo1=getSelectedBranch();
-	BranchObj *bo2;
-	BranchObj* par;
-	if (bo1)
+	TreeItem *ti=getSelectedBranchItem();
+	if (ti)
 	{
-		par=(BranchObj*)(bo1->getParObj());
+		TreeItem *par=ti->parent();
 		if (!par) return;
-		bo2=par->getFirstBranch();
-		if (bo2) {
-			selection.select(bo2);
+		TreeItem *ti2=par->getFirstBranch();
+		if (ti2) {
+			select(ti2);
 			selection.update();
 			ensureSelectionVisible();
 			sendSelection();
@@ -4772,17 +4782,14 @@
 
 void VymModel::selectLastBranch()
 {
-	BranchObj *bo1=getSelectedBranch();
-	BranchObj *bo2;
-	BranchObj* par;
-	if (bo1)
+	TreeItem *ti=getSelectedBranchItem();
+	if (ti)
 	{
-		par=(BranchObj*)(bo1->getParObj());
+		TreeItem *par=ti->parent();
 		if (!par) return;
-		bo2=par->getLastBranch();
-		if (bo2) 
-		{
-			selection.select(bo2);
+		TreeItem *ti2=par->getLastBranch();
+		if (ti2) {
+			select(ti2);
 			selection.update();
 			ensureSelectionVisible();
 			sendSelection();
@@ -4792,29 +4799,23 @@
 
 void VymModel::selectLastSelectedBranch()
 {
-	QItemSelection oldsel=selModel->selection();
-
+	TreeItem *ti=getSelectedBranchItem();
 	BranchObj *bo1=getSelectedBranch();
-	BranchObj *bo2;
-	if (bo1)
+	if (ti)
 	{
-		cout << "bo1="<<bo1->getHeading().toStdString()<<endl;
-		bo2=bo1->getLastSelectedBranch();
-		if (bo2) 
-		{
-			cout << "bo2="<<bo2->getHeading().toStdString()<<endl;
-			select(bo2);
-		}
+		ti=ti->getLastSelectedBranch();
+		if (ti) select (ti);
 	}		
 }
 
 void VymModel::selectParent()
 {
+	TreeItem *ti=getSelectedItem();
 	LinkableMapObj *lmo=getSelectedLMO();
-	BranchObj* par;
-	if (lmo)
+	TreeItem *par;
+	if (ti)
 	{
-		par=(BranchObj*)(lmo->getParObj());
+		par=ti->parent();
 		if (!par) return;
 		select(par);
 		selection.update();
@@ -4849,21 +4850,27 @@
 
 BranchObj* VymModel::getSelectedBranch()
 {
+	TreeItem *ti = getSelectedBranchItem();
+	if (ti)
+		return (BranchObj*)ti->getLMO();
+	else	
+		return NULL;
+}
+
+TreeItem* VymModel::getSelectedBranchItem()
+{
 	QModelIndexList list=selModel->selectedIndexes();
 	if (!list.isEmpty() )
 	{
 		TreeItem *ti = static_cast<TreeItem*>(list.first().internalPointer());
 		TreeItem::Type type=ti->getType();
 		if (type ==TreeItem::Branch || type==TreeItem::MapCenter)
-		{
-			return (BranchObj*)ti->getLMO();
-		}	
+			return ti;
 	}
 	return NULL;
-
 }
 
-TreeItem* VymModel::getSelectedTreeItem()
+TreeItem* VymModel::getSelectedItem()
 {
 	// FIXME this may not only be branch, but also float etc...
 	BranchObj* bo=getSelectedBranch();
@@ -4887,7 +4894,11 @@
 
 QString VymModel::getSelectString ()
 {
-	return selection.getSelectString();
+	LinkableMapObj *lmo=getSelectedLMO();
+	if (lmo) 
+		return getSelectString(lmo);
+	else
+		return QString();
 }
 
 QString VymModel::getSelectString (LinkableMapObj *lmo)	// FIXME VM needs to use TreeModel
diff -r 53e51e8d47e7 -r 716a777c1c98 vymmodel.h
--- a/vymmodel.h	Thu Jan 22 15:40:08 2009 +0000
+++ b/vymmodel.h	Fri Jan 30 09:14:12 2009 +0000
@@ -534,7 +534,8 @@
 
 	bool select ();							// select by using common QItemSlectionModel
 	bool select (const QString &);			// Select by string
-	bool select (LinkableMapObj *lmo);		// Select by pointer
+	bool select (LinkableMapObj *lmo);		// Select by pointer to LMO
+	bool select (TreeItem *ti );			// Select by point to TreeItem
 	void unselect();
 	void reselect();
 
@@ -559,7 +560,8 @@
 	TreeItem::Type selectionType();
 	LinkableMapObj* getSelectedLMO();
 	BranchObj* getSelectedBranch();
-	TreeItem* getSelectedTreeItem();
+	TreeItem* getSelectedBranchItem();
+	TreeItem* getSelectedItem();
 	QModelIndex getSelectedIndex();
 	FloatImageObj* getSelectedFloatImage();
 	QString getSelectString ();